blob: c929fe1831ad98c055d1b3aa89c1183dae4e53ed [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RADIUS authentication server
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003 * Copyright (c) 2005-2009, 2011-2014, 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>
Dmitry Shmidt818ea482014-03-10 13:15:21 -070011#ifdef CONFIG_SQLITE
12#include <sqlite3.h>
13#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014
15#include "common.h"
16#include "radius.h"
17#include "eloop.h"
18#include "eap_server/eap.h"
Dmitry Shmidt818ea482014-03-10 13:15:21 -070019#include "ap/ap_config.h"
20#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "radius_server.h"
22
23/**
24 * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
25 */
26#define RADIUS_SESSION_TIMEOUT 60
27
28/**
29 * RADIUS_MAX_SESSION - Maximum number of active sessions
30 */
31#define RADIUS_MAX_SESSION 100
32
33/**
34 * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
35 */
36#define RADIUS_MAX_MSG_LEN 3000
37
38static struct eapol_callbacks radius_server_eapol_cb;
39
40struct radius_client;
41struct radius_server_data;
42
43/**
44 * struct radius_server_counters - RADIUS server statistics counters
45 */
46struct radius_server_counters {
47 u32 access_requests;
48 u32 invalid_requests;
49 u32 dup_access_requests;
50 u32 access_accepts;
51 u32 access_rejects;
52 u32 access_challenges;
53 u32 malformed_access_requests;
54 u32 bad_authenticators;
55 u32 packets_dropped;
56 u32 unknown_types;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -080057
58 u32 acct_requests;
59 u32 invalid_acct_requests;
60 u32 acct_responses;
61 u32 malformed_acct_requests;
62 u32 acct_bad_authenticators;
63 u32 unknown_acct_types;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064};
65
66/**
67 * struct radius_session - Internal RADIUS server data for a session
68 */
69struct radius_session {
70 struct radius_session *next;
71 struct radius_client *client;
72 struct radius_server_data *server;
73 unsigned int sess_id;
74 struct eap_sm *eap;
75 struct eap_eapol_interface *eap_if;
Dmitry Shmidt818ea482014-03-10 13:15:21 -070076 char *username; /* from User-Name attribute */
77 char *nas_ip;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078
79 struct radius_msg *last_msg;
80 char *last_from_addr;
81 int last_from_port;
82 struct sockaddr_storage last_from;
83 socklen_t last_fromlen;
84 u8 last_identifier;
85 struct radius_msg *last_reply;
86 u8 last_authenticator[16];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080087
88 unsigned int remediation:1;
Dmitry Shmidt818ea482014-03-10 13:15:21 -070089
90 struct hostapd_radius_attr *accept_attr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091};
92
93/**
94 * struct radius_client - Internal RADIUS server data for a client
95 */
96struct radius_client {
97 struct radius_client *next;
98 struct in_addr addr;
99 struct in_addr mask;
100#ifdef CONFIG_IPV6
101 struct in6_addr addr6;
102 struct in6_addr mask6;
103#endif /* CONFIG_IPV6 */
104 char *shared_secret;
105 int shared_secret_len;
106 struct radius_session *sessions;
107 struct radius_server_counters counters;
108};
109
110/**
111 * struct radius_server_data - Internal RADIUS server data
112 */
113struct radius_server_data {
114 /**
115 * auth_sock - Socket for RADIUS authentication messages
116 */
117 int auth_sock;
118
119 /**
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800120 * acct_sock - Socket for RADIUS accounting messages
121 */
122 int acct_sock;
123
124 /**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 * clients - List of authorized RADIUS clients
126 */
127 struct radius_client *clients;
128
129 /**
130 * next_sess_id - Next session identifier
131 */
132 unsigned int next_sess_id;
133
134 /**
135 * conf_ctx - Context pointer for callbacks
136 *
137 * This is used as the ctx argument in get_eap_user() calls.
138 */
139 void *conf_ctx;
140
141 /**
142 * num_sess - Number of active sessions
143 */
144 int num_sess;
145
146 /**
147 * eap_sim_db_priv - EAP-SIM/AKA database context
148 *
149 * This is passed to the EAP-SIM/AKA server implementation as a
150 * callback context.
151 */
152 void *eap_sim_db_priv;
153
154 /**
155 * ssl_ctx - TLS context
156 *
157 * This is passed to the EAP server implementation as a callback
158 * context for TLS operations.
159 */
160 void *ssl_ctx;
161
162 /**
163 * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
164 *
165 * This parameter is used to set a key for EAP-FAST to encrypt the
166 * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
167 * set, must point to a 16-octet key.
168 */
169 u8 *pac_opaque_encr_key;
170
171 /**
172 * eap_fast_a_id - EAP-FAST authority identity (A-ID)
173 *
174 * If EAP-FAST is not used, this can be set to %NULL. In theory, this
175 * is a variable length field, but due to some existing implementations
176 * requiring A-ID to be 16 octets in length, it is recommended to use
177 * that length for the field to provide interoperability with deployed
178 * peer implementations.
179 */
180 u8 *eap_fast_a_id;
181
182 /**
183 * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
184 */
185 size_t eap_fast_a_id_len;
186
187 /**
188 * eap_fast_a_id_info - EAP-FAST authority identifier information
189 *
190 * This A-ID-Info contains a user-friendly name for the A-ID. For
191 * example, this could be the enterprise and server names in
192 * human-readable format. This field is encoded as UTF-8. If EAP-FAST
193 * is not used, this can be set to %NULL.
194 */
195 char *eap_fast_a_id_info;
196
197 /**
198 * eap_fast_prov - EAP-FAST provisioning modes
199 *
200 * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
201 * 2 = only authenticated provisioning allowed, 3 = both provisioning
202 * modes allowed.
203 */
204 int eap_fast_prov;
205
206 /**
207 * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
208 *
209 * This is the hard limit on how long a provisioned PAC-Key can be
210 * used.
211 */
212 int pac_key_lifetime;
213
214 /**
215 * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
216 *
217 * This is a soft limit on the PAC-Key. The server will automatically
218 * generate a new PAC-Key when this number of seconds (or fewer) of the
219 * lifetime remains.
220 */
221 int pac_key_refresh_time;
222
223 /**
224 * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
225 *
226 * This controls whether the protected success/failure indication
227 * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
228 */
229 int eap_sim_aka_result_ind;
230
231 /**
232 * tnc - Trusted Network Connect (TNC)
233 *
234 * This controls whether TNC is enabled and will be required before the
235 * peer is allowed to connect. Note: This is only used with EAP-TTLS
236 * and EAP-FAST. If any other EAP method is enabled, the peer will be
237 * allowed to connect without TNC.
238 */
239 int tnc;
240
241 /**
242 * pwd_group - The D-H group assigned for EAP-pwd
243 *
244 * If EAP-pwd is not used it can be set to zero.
245 */
246 u16 pwd_group;
247
248 /**
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700249 * server_id - Server identity
250 */
251 const char *server_id;
252
253 /**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 * wps - Wi-Fi Protected Setup context
255 *
256 * If WPS is used with an external RADIUS server (which is quite
257 * unlikely configuration), this is used to provide a pointer to WPS
258 * context data. Normally, this can be set to %NULL.
259 */
260 struct wps_context *wps;
261
262 /**
263 * ipv6 - Whether to enable IPv6 support in the RADIUS server
264 */
265 int ipv6;
266
267 /**
268 * start_time - Timestamp of server start
269 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 struct os_reltime start_time;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271
272 /**
273 * counters - Statistics counters for server operations
274 *
275 * These counters are the sum over all clients.
276 */
277 struct radius_server_counters counters;
278
279 /**
280 * get_eap_user - Callback for fetching EAP user information
281 * @ctx: Context data from conf_ctx
282 * @identity: User identity
283 * @identity_len: identity buffer length in octets
284 * @phase2: Whether this is for Phase 2 identity
285 * @user: Data structure for filling in the user information
286 * Returns: 0 on success, -1 on failure
287 *
288 * This is used to fetch information from user database. The callback
289 * will fill in information about allowed EAP methods and the user
290 * password. The password field will be an allocated copy of the
291 * password data and RADIUS server will free it after use.
292 */
293 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
294 int phase2, struct eap_user *user);
295
296 /**
297 * eap_req_id_text - Optional data for EAP-Request/Identity
298 *
299 * This can be used to configure an optional, displayable message that
300 * will be sent in EAP-Request/Identity. This string can contain an
301 * ASCII-0 character (nul) to separate network infromation per RFC
302 * 4284. The actual string length is explicit provided in
303 * eap_req_id_text_len since nul character will not be used as a string
304 * terminator.
305 */
306 char *eap_req_id_text;
307
308 /**
309 * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
310 */
311 size_t eap_req_id_text_len;
312
313 /*
314 * msg_ctx - Context data for wpa_msg() calls
315 */
316 void *msg_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317
318#ifdef CONFIG_RADIUS_TEST
319 char *dump_msk_file;
320#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800321
322 char *subscr_remediation_url;
323 u8 subscr_remediation_method;
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700324
325#ifdef CONFIG_SQLITE
326 sqlite3 *db;
327#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328};
329
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331#define RADIUS_DEBUG(args...) \
332wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
333#define RADIUS_ERROR(args...) \
334wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
335#define RADIUS_DUMP(args...) \
336wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
337#define RADIUS_DUMP_ASCII(args...) \
338wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
339
340
341static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
342static void radius_server_session_remove_timeout(void *eloop_ctx,
343 void *timeout_ctx);
344
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700345void srv_log(struct radius_session *sess, const char *fmt, ...)
346PRINTF_FORMAT(2, 3);
347
348void srv_log(struct radius_session *sess, const char *fmt, ...)
349{
350 va_list ap;
351 char *buf;
352 int buflen;
353
354 va_start(ap, fmt);
355 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
356 va_end(ap);
357
358 buf = os_malloc(buflen);
359 if (buf == NULL)
360 return;
361 va_start(ap, fmt);
362 vsnprintf(buf, buflen, fmt, ap);
363 va_end(ap);
364
365 RADIUS_DEBUG("[0x%x %s] %s", sess->sess_id, sess->nas_ip, buf);
366
367#ifdef CONFIG_SQLITE
368 if (sess->server->db) {
369 char *sql;
370 sql = sqlite3_mprintf("INSERT INTO authlog"
371 "(timestamp,session,nas_ip,username,note)"
372 " VALUES ("
373 "strftime('%%Y-%%m-%%d %%H:%%M:%%f',"
374 "'now'),%u,%Q,%Q,%Q)",
375 sess->sess_id, sess->nas_ip,
376 sess->username, buf);
377 if (sql) {
378 if (sqlite3_exec(sess->server->db, sql, NULL, NULL,
379 NULL) != SQLITE_OK) {
380 RADIUS_ERROR("Failed to add authlog entry into sqlite database: %s",
381 sqlite3_errmsg(sess->server->db));
382 }
383 sqlite3_free(sql);
384 }
385 }
386#endif /* CONFIG_SQLITE */
387
388 os_free(buf);
389}
390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
392static struct radius_client *
393radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
394 int ipv6)
395{
396 struct radius_client *client = data->clients;
397
398 while (client) {
399#ifdef CONFIG_IPV6
400 if (ipv6) {
401 struct in6_addr *addr6;
402 int i;
403
404 addr6 = (struct in6_addr *) addr;
405 for (i = 0; i < 16; i++) {
406 if ((addr6->s6_addr[i] &
407 client->mask6.s6_addr[i]) !=
408 (client->addr6.s6_addr[i] &
409 client->mask6.s6_addr[i])) {
410 i = 17;
411 break;
412 }
413 }
414 if (i == 16) {
415 break;
416 }
417 }
418#endif /* CONFIG_IPV6 */
419 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
420 (addr->s_addr & client->mask.s_addr)) {
421 break;
422 }
423
424 client = client->next;
425 }
426
427 return client;
428}
429
430
431static struct radius_session *
432radius_server_get_session(struct radius_client *client, unsigned int sess_id)
433{
434 struct radius_session *sess = client->sessions;
435
436 while (sess) {
437 if (sess->sess_id == sess_id) {
438 break;
439 }
440 sess = sess->next;
441 }
442
443 return sess;
444}
445
446
447static void radius_server_session_free(struct radius_server_data *data,
448 struct radius_session *sess)
449{
450 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
451 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
452 eap_server_sm_deinit(sess->eap);
453 radius_msg_free(sess->last_msg);
454 os_free(sess->last_from_addr);
455 radius_msg_free(sess->last_reply);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700456 os_free(sess->username);
457 os_free(sess->nas_ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 os_free(sess);
459 data->num_sess--;
460}
461
462
463static void radius_server_session_remove(struct radius_server_data *data,
464 struct radius_session *sess)
465{
466 struct radius_client *client = sess->client;
467 struct radius_session *session, *prev;
468
469 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
470
471 prev = NULL;
472 session = client->sessions;
473 while (session) {
474 if (session == sess) {
475 if (prev == NULL) {
476 client->sessions = sess->next;
477 } else {
478 prev->next = sess->next;
479 }
480 radius_server_session_free(data, sess);
481 break;
482 }
483 prev = session;
484 session = session->next;
485 }
486}
487
488
489static void radius_server_session_remove_timeout(void *eloop_ctx,
490 void *timeout_ctx)
491{
492 struct radius_server_data *data = eloop_ctx;
493 struct radius_session *sess = timeout_ctx;
494 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
495 radius_server_session_remove(data, sess);
496}
497
498
499static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
500{
501 struct radius_server_data *data = eloop_ctx;
502 struct radius_session *sess = timeout_ctx;
503
504 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
505 radius_server_session_remove(data, sess);
506}
507
508
509static struct radius_session *
510radius_server_new_session(struct radius_server_data *data,
511 struct radius_client *client)
512{
513 struct radius_session *sess;
514
515 if (data->num_sess >= RADIUS_MAX_SESSION) {
516 RADIUS_DEBUG("Maximum number of existing session - no room "
517 "for a new session");
518 return NULL;
519 }
520
521 sess = os_zalloc(sizeof(*sess));
522 if (sess == NULL)
523 return NULL;
524
525 sess->server = data;
526 sess->client = client;
527 sess->sess_id = data->next_sess_id++;
528 sess->next = client->sessions;
529 client->sessions = sess;
530 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
531 radius_server_session_timeout, data, sess);
532 data->num_sess++;
533 return sess;
534}
535
536
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700537#ifdef CONFIG_TESTING_OPTIONS
538static void radius_server_testing_options_tls(struct radius_session *sess,
539 const char *tls,
540 struct eap_config *eap_conf)
541{
542 int test = atoi(tls);
543
544 switch (test) {
545 case 1:
546 srv_log(sess, "TLS test - break VerifyData");
547 eap_conf->tls_test_flags = TLS_BREAK_VERIFY_DATA;
548 break;
549 case 2:
550 srv_log(sess, "TLS test - break ServerKeyExchange ServerParams hash");
551 eap_conf->tls_test_flags = TLS_BREAK_SRV_KEY_X_HASH;
552 break;
553 case 3:
554 srv_log(sess, "TLS test - break ServerKeyExchange ServerParams Signature");
555 eap_conf->tls_test_flags = TLS_BREAK_SRV_KEY_X_SIGNATURE;
556 break;
557 default:
558 srv_log(sess, "Unrecognized TLS test");
559 break;
560 }
561}
562#endif /* CONFIG_TESTING_OPTIONS */
563
564static void radius_server_testing_options(struct radius_session *sess,
565 struct eap_config *eap_conf)
566{
567#ifdef CONFIG_TESTING_OPTIONS
568 const char *pos;
569
570 pos = os_strstr(sess->username, "@test-");
571 if (pos == NULL)
572 return;
573 pos += 6;
574 if (os_strncmp(pos, "tls-", 4) == 0)
575 radius_server_testing_options_tls(sess, pos + 4, eap_conf);
576 else
577 srv_log(sess, "Unrecognized test: %s", pos);
578#endif /* CONFIG_TESTING_OPTIONS */
579}
580
581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582static struct radius_session *
583radius_server_get_new_session(struct radius_server_data *data,
584 struct radius_client *client,
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700585 struct radius_msg *msg, const char *from_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586{
587 u8 *user;
588 size_t user_len;
589 int res;
590 struct radius_session *sess;
591 struct eap_config eap_conf;
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700592 struct eap_user tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593
594 RADIUS_DEBUG("Creating a new session");
595
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700596 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &user,
597 &user_len, NULL) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 RADIUS_DEBUG("Could not get User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 return NULL;
600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 RADIUS_DUMP_ASCII("User-Name", user, user_len);
602
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700603 os_memset(&tmp, 0, sizeof(tmp));
604 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, &tmp);
605 os_free(tmp.password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700607 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 RADIUS_DEBUG("User-Name not found from user database");
609 return NULL;
610 }
611
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700612 RADIUS_DEBUG("Matching user entry found");
613 sess = radius_server_new_session(data, client);
614 if (sess == NULL) {
615 RADIUS_DEBUG("Failed to create a new session");
616 return NULL;
617 }
618 sess->accept_attr = tmp.accept_attr;
619
620 sess->username = os_malloc(user_len * 2 + 1);
621 if (sess->username == NULL) {
622 radius_server_session_free(data, sess);
623 return NULL;
624 }
625 printf_encode(sess->username, user_len * 2 + 1, user, user_len);
626
627 sess->nas_ip = os_strdup(from_addr);
628 if (sess->nas_ip == NULL) {
629 radius_server_session_free(data, sess);
630 return NULL;
631 }
632
633 srv_log(sess, "New session created");
634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 os_memset(&eap_conf, 0, sizeof(eap_conf));
636 eap_conf.ssl_ctx = data->ssl_ctx;
637 eap_conf.msg_ctx = data->msg_ctx;
638 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
639 eap_conf.backend_auth = TRUE;
640 eap_conf.eap_server = 1;
641 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
642 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
643 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
644 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
645 eap_conf.eap_fast_prov = data->eap_fast_prov;
646 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
647 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
648 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
649 eap_conf.tnc = data->tnc;
650 eap_conf.wps = data->wps;
651 eap_conf.pwd_group = data->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700652 eap_conf.server_id = (const u8 *) data->server_id;
653 eap_conf.server_id_len = os_strlen(data->server_id);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700654 radius_server_testing_options(sess, &eap_conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
656 &eap_conf);
657 if (sess->eap == NULL) {
658 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
659 "new session");
660 radius_server_session_free(data, sess);
661 return NULL;
662 }
663 sess->eap_if = eap_get_interface(sess->eap);
664 sess->eap_if->eapRestart = TRUE;
665 sess->eap_if->portEnabled = TRUE;
666
667 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
668
669 return sess;
670}
671
672
673static struct radius_msg *
674radius_server_encapsulate_eap(struct radius_server_data *data,
675 struct radius_client *client,
676 struct radius_session *sess,
677 struct radius_msg *request)
678{
679 struct radius_msg *msg;
680 int code;
681 unsigned int sess_id;
682 struct radius_hdr *hdr = radius_msg_get_hdr(request);
683
684 if (sess->eap_if->eapFail) {
685 sess->eap_if->eapFail = FALSE;
686 code = RADIUS_CODE_ACCESS_REJECT;
687 } else if (sess->eap_if->eapSuccess) {
688 sess->eap_if->eapSuccess = FALSE;
689 code = RADIUS_CODE_ACCESS_ACCEPT;
690 } else {
691 sess->eap_if->eapReq = FALSE;
692 code = RADIUS_CODE_ACCESS_CHALLENGE;
693 }
694
695 msg = radius_msg_new(code, hdr->identifier);
696 if (msg == NULL) {
697 RADIUS_DEBUG("Failed to allocate reply message");
698 return NULL;
699 }
700
701 sess_id = htonl(sess->sess_id);
702 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
703 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
704 (u8 *) &sess_id, sizeof(sess_id))) {
705 RADIUS_DEBUG("Failed to add State attribute");
706 }
707
708 if (sess->eap_if->eapReqData &&
709 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
710 wpabuf_len(sess->eap_if->eapReqData))) {
711 RADIUS_DEBUG("Failed to add EAP-Message attribute");
712 }
713
714 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
715 int len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716#ifdef CONFIG_RADIUS_TEST
717 if (data->dump_msk_file) {
718 FILE *f;
719 char buf[2 * 64 + 1];
720 f = fopen(data->dump_msk_file, "a");
721 if (f) {
722 len = sess->eap_if->eapKeyDataLen;
723 if (len > 64)
724 len = 64;
725 len = wpa_snprintf_hex(
726 buf, sizeof(buf),
727 sess->eap_if->eapKeyData, len);
728 buf[len] = '\0';
729 fprintf(f, "%s\n", buf);
730 fclose(f);
731 }
732 }
733#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 if (sess->eap_if->eapKeyDataLen > 64) {
735 len = 32;
736 } else {
737 len = sess->eap_if->eapKeyDataLen / 2;
738 }
739 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
740 (u8 *) client->shared_secret,
741 client->shared_secret_len,
742 sess->eap_if->eapKeyData + len,
743 len, sess->eap_if->eapKeyData,
744 len)) {
745 RADIUS_DEBUG("Failed to add MPPE key attributes");
746 }
747 }
748
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800749#ifdef CONFIG_HS20
750 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation &&
751 data->subscr_remediation_url) {
752 u8 *buf;
753 size_t url_len = os_strlen(data->subscr_remediation_url);
754 buf = os_malloc(1 + url_len);
755 if (buf == NULL) {
756 radius_msg_free(msg);
757 return NULL;
758 }
759 buf[0] = data->subscr_remediation_method;
760 os_memcpy(&buf[1], data->subscr_remediation_url, url_len);
761 if (!radius_msg_add_wfa(
762 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
763 buf, 1 + url_len)) {
764 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
765 }
766 os_free(buf);
767 } else if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation) {
768 u8 buf[1];
769 if (!radius_msg_add_wfa(
770 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
771 buf, 0)) {
772 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
773 }
774 }
775#endif /* CONFIG_HS20 */
776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
778 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
779 radius_msg_free(msg);
780 return NULL;
781 }
782
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700783 if (code == RADIUS_CODE_ACCESS_ACCEPT) {
784 struct hostapd_radius_attr *attr;
785 for (attr = sess->accept_attr; attr; attr = attr->next) {
786 if (!radius_msg_add_attr(msg, attr->type,
787 wpabuf_head(attr->val),
788 wpabuf_len(attr->val))) {
789 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
790 radius_msg_free(msg);
791 return NULL;
792 }
793 }
794 }
795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
797 client->shared_secret_len,
798 hdr->authenticator) < 0) {
799 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
800 }
801
802 return msg;
803}
804
805
806static int radius_server_reject(struct radius_server_data *data,
807 struct radius_client *client,
808 struct radius_msg *request,
809 struct sockaddr *from, socklen_t fromlen,
810 const char *from_addr, int from_port)
811{
812 struct radius_msg *msg;
813 int ret = 0;
814 struct eap_hdr eapfail;
815 struct wpabuf *buf;
816 struct radius_hdr *hdr = radius_msg_get_hdr(request);
817
818 RADIUS_DEBUG("Reject invalid request from %s:%d",
819 from_addr, from_port);
820
821 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
822 if (msg == NULL) {
823 return -1;
824 }
825
826 os_memset(&eapfail, 0, sizeof(eapfail));
827 eapfail.code = EAP_CODE_FAILURE;
828 eapfail.identifier = 0;
829 eapfail.length = host_to_be16(sizeof(eapfail));
830
831 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
832 RADIUS_DEBUG("Failed to add EAP-Message attribute");
833 }
834
835 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
836 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
837 radius_msg_free(msg);
838 return -1;
839 }
840
841 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
842 client->shared_secret_len,
843 hdr->authenticator) <
844 0) {
845 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
846 }
847
848 if (wpa_debug_level <= MSG_MSGDUMP) {
849 radius_msg_dump(msg);
850 }
851
852 data->counters.access_rejects++;
853 client->counters.access_rejects++;
854 buf = radius_msg_get_buf(msg);
855 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
856 (struct sockaddr *) from, sizeof(*from)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800857 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 ret = -1;
859 }
860
861 radius_msg_free(msg);
862
863 return ret;
864}
865
866
867static int radius_server_request(struct radius_server_data *data,
868 struct radius_msg *msg,
869 struct sockaddr *from, socklen_t fromlen,
870 struct radius_client *client,
871 const char *from_addr, int from_port,
872 struct radius_session *force_sess)
873{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700874 struct wpabuf *eap = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 int res, state_included = 0;
876 u8 statebuf[4];
877 unsigned int state;
878 struct radius_session *sess;
879 struct radius_msg *reply;
880 int is_complete = 0;
881
882 if (force_sess)
883 sess = force_sess;
884 else {
885 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
886 sizeof(statebuf));
887 state_included = res >= 0;
888 if (res == sizeof(statebuf)) {
889 state = WPA_GET_BE32(statebuf);
890 sess = radius_server_get_session(client, state);
891 } else {
892 sess = NULL;
893 }
894 }
895
896 if (sess) {
897 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
898 } else if (state_included) {
899 RADIUS_DEBUG("State attribute included but no session found");
900 radius_server_reject(data, client, msg, from, fromlen,
901 from_addr, from_port);
902 return -1;
903 } else {
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700904 sess = radius_server_get_new_session(data, client, msg,
905 from_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 if (sess == NULL) {
907 RADIUS_DEBUG("Could not create a new session");
908 radius_server_reject(data, client, msg, from, fromlen,
909 from_addr, from_port);
910 return -1;
911 }
912 }
913
914 if (sess->last_from_port == from_port &&
915 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
916 os_memcmp(sess->last_authenticator,
917 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
918 RADIUS_DEBUG("Duplicate message from %s", from_addr);
919 data->counters.dup_access_requests++;
920 client->counters.dup_access_requests++;
921
922 if (sess->last_reply) {
923 struct wpabuf *buf;
924 buf = radius_msg_get_buf(sess->last_reply);
925 res = sendto(data->auth_sock, wpabuf_head(buf),
926 wpabuf_len(buf), 0,
927 (struct sockaddr *) from, fromlen);
928 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800929 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
930 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 }
932 return 0;
933 }
934
935 RADIUS_DEBUG("No previous reply available for duplicate "
936 "message");
937 return -1;
938 }
939
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 if (eap == NULL) {
942 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
943 from_addr);
944 data->counters.packets_dropped++;
945 client->counters.packets_dropped++;
946 return -1;
947 }
948
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700949 RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
951 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
952 * RFC3579 Sect. 2.6.2.
953 * Include EAP-Response/Nak with no preferred method if
954 * code == request.
955 * If code is not 1-4, discard the packet silently.
956 * Or is this already done by the EAP state machine? */
957
958 wpabuf_free(sess->eap_if->eapRespData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700959 sess->eap_if->eapRespData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 sess->eap_if->eapResp = TRUE;
961 eap_server_sm_step(sess->eap);
962
963 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
964 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
965 RADIUS_DUMP("EAP data from the state machine",
966 wpabuf_head(sess->eap_if->eapReqData),
967 wpabuf_len(sess->eap_if->eapReqData));
968 } else if (sess->eap_if->eapFail) {
969 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
970 "set");
971 } else if (eap_sm_method_pending(sess->eap)) {
972 radius_msg_free(sess->last_msg);
973 sess->last_msg = msg;
974 sess->last_from_port = from_port;
975 os_free(sess->last_from_addr);
976 sess->last_from_addr = os_strdup(from_addr);
977 sess->last_fromlen = fromlen;
978 os_memcpy(&sess->last_from, from, fromlen);
979 return -2;
980 } else {
981 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
982 " Access-Request silently (assuming it was a "
983 "duplicate)");
984 data->counters.packets_dropped++;
985 client->counters.packets_dropped++;
986 return -1;
987 }
988
989 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
990 is_complete = 1;
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700991 if (sess->eap_if->eapFail)
992 srv_log(sess, "EAP authentication failed");
993 else if (sess->eap_if->eapSuccess)
994 srv_log(sess, "EAP authentication succeeded");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995
996 reply = radius_server_encapsulate_eap(data, client, sess, msg);
997
998 if (reply) {
999 struct wpabuf *buf;
1000 struct radius_hdr *hdr;
1001
1002 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
1003 if (wpa_debug_level <= MSG_MSGDUMP) {
1004 radius_msg_dump(reply);
1005 }
1006
1007 switch (radius_msg_get_hdr(reply)->code) {
1008 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001009 srv_log(sess, "Sending Access-Accept");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 data->counters.access_accepts++;
1011 client->counters.access_accepts++;
1012 break;
1013 case RADIUS_CODE_ACCESS_REJECT:
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001014 srv_log(sess, "Sending Access-Reject");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 data->counters.access_rejects++;
1016 client->counters.access_rejects++;
1017 break;
1018 case RADIUS_CODE_ACCESS_CHALLENGE:
1019 data->counters.access_challenges++;
1020 client->counters.access_challenges++;
1021 break;
1022 }
1023 buf = radius_msg_get_buf(reply);
1024 res = sendto(data->auth_sock, wpabuf_head(buf),
1025 wpabuf_len(buf), 0,
1026 (struct sockaddr *) from, fromlen);
1027 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001028 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
1029 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 }
1031 radius_msg_free(sess->last_reply);
1032 sess->last_reply = reply;
1033 sess->last_from_port = from_port;
1034 hdr = radius_msg_get_hdr(msg);
1035 sess->last_identifier = hdr->identifier;
1036 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
1037 } else {
1038 data->counters.packets_dropped++;
1039 client->counters.packets_dropped++;
1040 }
1041
1042 if (is_complete) {
1043 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
1044 sess->sess_id);
1045 eloop_cancel_timeout(radius_server_session_remove_timeout,
1046 data, sess);
1047 eloop_register_timeout(10, 0,
1048 radius_server_session_remove_timeout,
1049 data, sess);
1050 }
1051
1052 return 0;
1053}
1054
1055
1056static void radius_server_receive_auth(int sock, void *eloop_ctx,
1057 void *sock_ctx)
1058{
1059 struct radius_server_data *data = eloop_ctx;
1060 u8 *buf = NULL;
1061 union {
1062 struct sockaddr_storage ss;
1063 struct sockaddr_in sin;
1064#ifdef CONFIG_IPV6
1065 struct sockaddr_in6 sin6;
1066#endif /* CONFIG_IPV6 */
1067 } from;
1068 socklen_t fromlen;
1069 int len;
1070 struct radius_client *client = NULL;
1071 struct radius_msg *msg = NULL;
1072 char abuf[50];
1073 int from_port = 0;
1074
1075 buf = os_malloc(RADIUS_MAX_MSG_LEN);
1076 if (buf == NULL) {
1077 goto fail;
1078 }
1079
1080 fromlen = sizeof(from);
1081 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
1082 (struct sockaddr *) &from.ss, &fromlen);
1083 if (len < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001084 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
1085 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 goto fail;
1087 }
1088
1089#ifdef CONFIG_IPV6
1090 if (data->ipv6) {
1091 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
1092 sizeof(abuf)) == NULL)
1093 abuf[0] = '\0';
1094 from_port = ntohs(from.sin6.sin6_port);
1095 RADIUS_DEBUG("Received %d bytes from %s:%d",
1096 len, abuf, from_port);
1097
1098 client = radius_server_get_client(data,
1099 (struct in_addr *)
1100 &from.sin6.sin6_addr, 1);
1101 }
1102#endif /* CONFIG_IPV6 */
1103
1104 if (!data->ipv6) {
1105 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
1106 from_port = ntohs(from.sin.sin_port);
1107 RADIUS_DEBUG("Received %d bytes from %s:%d",
1108 len, abuf, from_port);
1109
1110 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
1111 }
1112
1113 RADIUS_DUMP("Received data", buf, len);
1114
1115 if (client == NULL) {
1116 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
1117 data->counters.invalid_requests++;
1118 goto fail;
1119 }
1120
1121 msg = radius_msg_parse(buf, len);
1122 if (msg == NULL) {
1123 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
1124 data->counters.malformed_access_requests++;
1125 client->counters.malformed_access_requests++;
1126 goto fail;
1127 }
1128
1129 os_free(buf);
1130 buf = NULL;
1131
1132 if (wpa_debug_level <= MSG_MSGDUMP) {
1133 radius_msg_dump(msg);
1134 }
1135
1136 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
1137 RADIUS_DEBUG("Unexpected RADIUS code %d",
1138 radius_msg_get_hdr(msg)->code);
1139 data->counters.unknown_types++;
1140 client->counters.unknown_types++;
1141 goto fail;
1142 }
1143
1144 data->counters.access_requests++;
1145 client->counters.access_requests++;
1146
1147 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
1148 client->shared_secret_len, NULL)) {
1149 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
1150 data->counters.bad_authenticators++;
1151 client->counters.bad_authenticators++;
1152 goto fail;
1153 }
1154
1155 if (radius_server_request(data, msg, (struct sockaddr *) &from,
1156 fromlen, client, abuf, from_port, NULL) ==
1157 -2)
1158 return; /* msg was stored with the session */
1159
1160fail:
1161 radius_msg_free(msg);
1162 os_free(buf);
1163}
1164
1165
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001166static void radius_server_receive_acct(int sock, void *eloop_ctx,
1167 void *sock_ctx)
1168{
1169 struct radius_server_data *data = eloop_ctx;
1170 u8 *buf = NULL;
1171 union {
1172 struct sockaddr_storage ss;
1173 struct sockaddr_in sin;
1174#ifdef CONFIG_IPV6
1175 struct sockaddr_in6 sin6;
1176#endif /* CONFIG_IPV6 */
1177 } from;
1178 socklen_t fromlen;
1179 int len, res;
1180 struct radius_client *client = NULL;
1181 struct radius_msg *msg = NULL, *resp = NULL;
1182 char abuf[50];
1183 int from_port = 0;
1184 struct radius_hdr *hdr;
1185 struct wpabuf *rbuf;
1186
1187 buf = os_malloc(RADIUS_MAX_MSG_LEN);
1188 if (buf == NULL) {
1189 goto fail;
1190 }
1191
1192 fromlen = sizeof(from);
1193 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
1194 (struct sockaddr *) &from.ss, &fromlen);
1195 if (len < 0) {
1196 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
1197 strerror(errno));
1198 goto fail;
1199 }
1200
1201#ifdef CONFIG_IPV6
1202 if (data->ipv6) {
1203 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
1204 sizeof(abuf)) == NULL)
1205 abuf[0] = '\0';
1206 from_port = ntohs(from.sin6.sin6_port);
1207 RADIUS_DEBUG("Received %d bytes from %s:%d",
1208 len, abuf, from_port);
1209
1210 client = radius_server_get_client(data,
1211 (struct in_addr *)
1212 &from.sin6.sin6_addr, 1);
1213 }
1214#endif /* CONFIG_IPV6 */
1215
1216 if (!data->ipv6) {
1217 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
1218 from_port = ntohs(from.sin.sin_port);
1219 RADIUS_DEBUG("Received %d bytes from %s:%d",
1220 len, abuf, from_port);
1221
1222 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
1223 }
1224
1225 RADIUS_DUMP("Received data", buf, len);
1226
1227 if (client == NULL) {
1228 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
1229 data->counters.invalid_acct_requests++;
1230 goto fail;
1231 }
1232
1233 msg = radius_msg_parse(buf, len);
1234 if (msg == NULL) {
1235 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
1236 data->counters.malformed_acct_requests++;
1237 client->counters.malformed_acct_requests++;
1238 goto fail;
1239 }
1240
1241 os_free(buf);
1242 buf = NULL;
1243
1244 if (wpa_debug_level <= MSG_MSGDUMP) {
1245 radius_msg_dump(msg);
1246 }
1247
1248 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_REQUEST) {
1249 RADIUS_DEBUG("Unexpected RADIUS code %d",
1250 radius_msg_get_hdr(msg)->code);
1251 data->counters.unknown_acct_types++;
1252 client->counters.unknown_acct_types++;
1253 goto fail;
1254 }
1255
1256 data->counters.acct_requests++;
1257 client->counters.acct_requests++;
1258
1259 if (radius_msg_verify_acct_req(msg, (u8 *) client->shared_secret,
1260 client->shared_secret_len)) {
1261 RADIUS_DEBUG("Invalid Authenticator from %s", abuf);
1262 data->counters.acct_bad_authenticators++;
1263 client->counters.acct_bad_authenticators++;
1264 goto fail;
1265 }
1266
1267 /* TODO: Write accounting information to a file or database */
1268
1269 hdr = radius_msg_get_hdr(msg);
1270
1271 resp = radius_msg_new(RADIUS_CODE_ACCOUNTING_RESPONSE, hdr->identifier);
1272 if (resp == NULL)
1273 goto fail;
1274
1275 radius_msg_finish_acct_resp(resp, (u8 *) client->shared_secret,
1276 client->shared_secret_len,
1277 hdr->authenticator);
1278
1279 RADIUS_DEBUG("Reply to %s:%d", abuf, from_port);
1280 if (wpa_debug_level <= MSG_MSGDUMP) {
1281 radius_msg_dump(resp);
1282 }
1283 rbuf = radius_msg_get_buf(resp);
1284 data->counters.acct_responses++;
1285 client->counters.acct_responses++;
1286 res = sendto(data->acct_sock, wpabuf_head(rbuf), wpabuf_len(rbuf), 0,
1287 (struct sockaddr *) &from.ss, fromlen);
1288 if (res < 0) {
1289 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
1290 strerror(errno));
1291 }
1292
1293fail:
1294 radius_msg_free(resp);
1295 radius_msg_free(msg);
1296 os_free(buf);
1297}
1298
1299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300static int radius_server_disable_pmtu_discovery(int s)
1301{
1302 int r = -1;
1303#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1304 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
1305 int action = IP_PMTUDISC_DONT;
1306 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
1307 sizeof(action));
1308 if (r == -1)
1309 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
1310 "%s", strerror(errno));
1311#endif
1312 return r;
1313}
1314
1315
1316static int radius_server_open_socket(int port)
1317{
1318 int s;
1319 struct sockaddr_in addr;
1320
1321 s = socket(PF_INET, SOCK_DGRAM, 0);
1322 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001323 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 return -1;
1325 }
1326
1327 radius_server_disable_pmtu_discovery(s);
1328
1329 os_memset(&addr, 0, sizeof(addr));
1330 addr.sin_family = AF_INET;
1331 addr.sin_port = htons(port);
1332 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001333 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 close(s);
1335 return -1;
1336 }
1337
1338 return s;
1339}
1340
1341
1342#ifdef CONFIG_IPV6
1343static int radius_server_open_socket6(int port)
1344{
1345 int s;
1346 struct sockaddr_in6 addr;
1347
1348 s = socket(PF_INET6, SOCK_DGRAM, 0);
1349 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001350 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s",
1351 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 return -1;
1353 }
1354
1355 os_memset(&addr, 0, sizeof(addr));
1356 addr.sin6_family = AF_INET6;
1357 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1358 addr.sin6_port = htons(port);
1359 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001360 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 close(s);
1362 return -1;
1363 }
1364
1365 return s;
1366}
1367#endif /* CONFIG_IPV6 */
1368
1369
1370static void radius_server_free_sessions(struct radius_server_data *data,
1371 struct radius_session *sessions)
1372{
1373 struct radius_session *session, *prev;
1374
1375 session = sessions;
1376 while (session) {
1377 prev = session;
1378 session = session->next;
1379 radius_server_session_free(data, prev);
1380 }
1381}
1382
1383
1384static void radius_server_free_clients(struct radius_server_data *data,
1385 struct radius_client *clients)
1386{
1387 struct radius_client *client, *prev;
1388
1389 client = clients;
1390 while (client) {
1391 prev = client;
1392 client = client->next;
1393
1394 radius_server_free_sessions(data, prev->sessions);
1395 os_free(prev->shared_secret);
1396 os_free(prev);
1397 }
1398}
1399
1400
1401static struct radius_client *
1402radius_server_read_clients(const char *client_file, int ipv6)
1403{
1404 FILE *f;
1405 const int buf_size = 1024;
1406 char *buf, *pos;
1407 struct radius_client *clients, *tail, *entry;
1408 int line = 0, mask, failed = 0, i;
1409 struct in_addr addr;
1410#ifdef CONFIG_IPV6
1411 struct in6_addr addr6;
1412#endif /* CONFIG_IPV6 */
1413 unsigned int val;
1414
1415 f = fopen(client_file, "r");
1416 if (f == NULL) {
1417 RADIUS_ERROR("Could not open client file '%s'", client_file);
1418 return NULL;
1419 }
1420
1421 buf = os_malloc(buf_size);
1422 if (buf == NULL) {
1423 fclose(f);
1424 return NULL;
1425 }
1426
1427 clients = tail = NULL;
1428 while (fgets(buf, buf_size, f)) {
1429 /* Configuration file format:
1430 * 192.168.1.0/24 secret
1431 * 192.168.1.2 secret
1432 * fe80::211:22ff:fe33:4455/64 secretipv6
1433 */
1434 line++;
1435 buf[buf_size - 1] = '\0';
1436 pos = buf;
1437 while (*pos != '\0' && *pos != '\n')
1438 pos++;
1439 if (*pos == '\n')
1440 *pos = '\0';
1441 if (*buf == '\0' || *buf == '#')
1442 continue;
1443
1444 pos = buf;
1445 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1446 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1447 (*pos >= 'A' && *pos <= 'F')) {
1448 pos++;
1449 }
1450
1451 if (*pos == '\0') {
1452 failed = 1;
1453 break;
1454 }
1455
1456 if (*pos == '/') {
1457 char *end;
1458 *pos++ = '\0';
1459 mask = strtol(pos, &end, 10);
1460 if ((pos == end) ||
1461 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1462 failed = 1;
1463 break;
1464 }
1465 pos = end;
1466 } else {
1467 mask = ipv6 ? 128 : 32;
1468 *pos++ = '\0';
1469 }
1470
1471 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1472 failed = 1;
1473 break;
1474 }
1475#ifdef CONFIG_IPV6
1476 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1477 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1478 failed = 1;
1479 break;
1480 }
1481 /* Convert IPv4 address to IPv6 */
1482 if (mask <= 32)
1483 mask += (128 - 32);
1484 os_memset(addr6.s6_addr, 0, 10);
1485 addr6.s6_addr[10] = 0xff;
1486 addr6.s6_addr[11] = 0xff;
1487 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1488 4);
1489 }
1490#endif /* CONFIG_IPV6 */
1491
1492 while (*pos == ' ' || *pos == '\t') {
1493 pos++;
1494 }
1495
1496 if (*pos == '\0') {
1497 failed = 1;
1498 break;
1499 }
1500
1501 entry = os_zalloc(sizeof(*entry));
1502 if (entry == NULL) {
1503 failed = 1;
1504 break;
1505 }
1506 entry->shared_secret = os_strdup(pos);
1507 if (entry->shared_secret == NULL) {
1508 failed = 1;
1509 os_free(entry);
1510 break;
1511 }
1512 entry->shared_secret_len = os_strlen(entry->shared_secret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 if (!ipv6) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001514 entry->addr.s_addr = addr.s_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 val = 0;
1516 for (i = 0; i < mask; i++)
1517 val |= 1 << (31 - i);
1518 entry->mask.s_addr = htonl(val);
1519 }
1520#ifdef CONFIG_IPV6
1521 if (ipv6) {
1522 int offset = mask / 8;
1523
1524 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1525 os_memset(entry->mask6.s6_addr, 0xff, offset);
1526 val = 0;
1527 for (i = 0; i < (mask % 8); i++)
1528 val |= 1 << (7 - i);
1529 if (offset < 16)
1530 entry->mask6.s6_addr[offset] = val;
1531 }
1532#endif /* CONFIG_IPV6 */
1533
1534 if (tail == NULL) {
1535 clients = tail = entry;
1536 } else {
1537 tail->next = entry;
1538 tail = entry;
1539 }
1540 }
1541
1542 if (failed) {
1543 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1544 radius_server_free_clients(NULL, clients);
1545 clients = NULL;
1546 }
1547
1548 os_free(buf);
1549 fclose(f);
1550
1551 return clients;
1552}
1553
1554
1555/**
1556 * radius_server_init - Initialize RADIUS server
1557 * @conf: Configuration for the RADIUS server
1558 * Returns: Pointer to private RADIUS server context or %NULL on failure
1559 *
1560 * This initializes a RADIUS server instance and returns a context pointer that
1561 * will be used in other calls to the RADIUS server module. The server can be
1562 * deinitialize by calling radius_server_deinit().
1563 */
1564struct radius_server_data *
1565radius_server_init(struct radius_server_conf *conf)
1566{
1567 struct radius_server_data *data;
1568
1569#ifndef CONFIG_IPV6
1570 if (conf->ipv6) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001571 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 return NULL;
1573 }
1574#endif /* CONFIG_IPV6 */
1575
1576 data = os_zalloc(sizeof(*data));
1577 if (data == NULL)
1578 return NULL;
1579
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001580 os_get_reltime(&data->start_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 data->conf_ctx = conf->conf_ctx;
1582 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1583 data->ssl_ctx = conf->ssl_ctx;
1584 data->msg_ctx = conf->msg_ctx;
1585 data->ipv6 = conf->ipv6;
1586 if (conf->pac_opaque_encr_key) {
1587 data->pac_opaque_encr_key = os_malloc(16);
1588 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1589 16);
1590 }
1591 if (conf->eap_fast_a_id) {
1592 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1593 if (data->eap_fast_a_id) {
1594 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1595 conf->eap_fast_a_id_len);
1596 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1597 }
1598 }
1599 if (conf->eap_fast_a_id_info)
1600 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1601 data->eap_fast_prov = conf->eap_fast_prov;
1602 data->pac_key_lifetime = conf->pac_key_lifetime;
1603 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1604 data->get_eap_user = conf->get_eap_user;
1605 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1606 data->tnc = conf->tnc;
1607 data->wps = conf->wps;
1608 data->pwd_group = conf->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001609 data->server_id = conf->server_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 if (conf->eap_req_id_text) {
1611 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1612 if (data->eap_req_id_text) {
1613 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1614 conf->eap_req_id_text_len);
1615 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1616 }
1617 }
1618
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001619 if (conf->subscr_remediation_url) {
1620 data->subscr_remediation_url =
1621 os_strdup(conf->subscr_remediation_url);
1622 }
1623
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001624#ifdef CONFIG_SQLITE
1625 if (conf->sqlite_file) {
1626 if (sqlite3_open(conf->sqlite_file, &data->db)) {
1627 RADIUS_ERROR("Could not open SQLite file '%s'",
1628 conf->sqlite_file);
1629 radius_server_deinit(data);
1630 return NULL;
1631 }
1632 }
1633#endif /* CONFIG_SQLITE */
1634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001635#ifdef CONFIG_RADIUS_TEST
1636 if (conf->dump_msk_file)
1637 data->dump_msk_file = os_strdup(conf->dump_msk_file);
1638#endif /* CONFIG_RADIUS_TEST */
1639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 data->clients = radius_server_read_clients(conf->client_file,
1641 conf->ipv6);
1642 if (data->clients == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001643 wpa_printf(MSG_ERROR, "No RADIUS clients configured");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 radius_server_deinit(data);
1645 return NULL;
1646 }
1647
1648#ifdef CONFIG_IPV6
1649 if (conf->ipv6)
1650 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1651 else
1652#endif /* CONFIG_IPV6 */
1653 data->auth_sock = radius_server_open_socket(conf->auth_port);
1654 if (data->auth_sock < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001655 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 radius_server_deinit(data);
1657 return NULL;
1658 }
1659 if (eloop_register_read_sock(data->auth_sock,
1660 radius_server_receive_auth,
1661 data, NULL)) {
1662 radius_server_deinit(data);
1663 return NULL;
1664 }
1665
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001666 if (conf->acct_port) {
1667#ifdef CONFIG_IPV6
1668 if (conf->ipv6)
1669 data->acct_sock = radius_server_open_socket6(
1670 conf->acct_port);
1671 else
1672#endif /* CONFIG_IPV6 */
1673 data->acct_sock = radius_server_open_socket(conf->acct_port);
1674 if (data->acct_sock < 0) {
1675 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS accounting server");
1676 radius_server_deinit(data);
1677 return NULL;
1678 }
1679 if (eloop_register_read_sock(data->acct_sock,
1680 radius_server_receive_acct,
1681 data, NULL)) {
1682 radius_server_deinit(data);
1683 return NULL;
1684 }
1685 } else {
1686 data->acct_sock = -1;
1687 }
1688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 return data;
1690}
1691
1692
1693/**
1694 * radius_server_deinit - Deinitialize RADIUS server
1695 * @data: RADIUS server context from radius_server_init()
1696 */
1697void radius_server_deinit(struct radius_server_data *data)
1698{
1699 if (data == NULL)
1700 return;
1701
1702 if (data->auth_sock >= 0) {
1703 eloop_unregister_read_sock(data->auth_sock);
1704 close(data->auth_sock);
1705 }
1706
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001707 if (data->acct_sock >= 0) {
1708 eloop_unregister_read_sock(data->acct_sock);
1709 close(data->acct_sock);
1710 }
1711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 radius_server_free_clients(data, data->clients);
1713
1714 os_free(data->pac_opaque_encr_key);
1715 os_free(data->eap_fast_a_id);
1716 os_free(data->eap_fast_a_id_info);
1717 os_free(data->eap_req_id_text);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718#ifdef CONFIG_RADIUS_TEST
1719 os_free(data->dump_msk_file);
1720#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001721 os_free(data->subscr_remediation_url);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001722
1723#ifdef CONFIG_SQLITE
1724 if (data->db)
1725 sqlite3_close(data->db);
1726#endif /* CONFIG_SQLITE */
1727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 os_free(data);
1729}
1730
1731
1732/**
1733 * radius_server_get_mib - Get RADIUS server MIB information
1734 * @data: RADIUS server context from radius_server_init()
1735 * @buf: Buffer for returning the MIB data in text format
1736 * @buflen: buf length in octets
1737 * Returns: Number of octets written into buf
1738 */
1739int radius_server_get_mib(struct radius_server_data *data, char *buf,
1740 size_t buflen)
1741{
1742 int ret, uptime;
1743 unsigned int idx;
1744 char *end, *pos;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001745 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 struct radius_client *cli;
1747
1748 /* RFC 2619 - RADIUS Authentication Server MIB */
1749
1750 if (data == NULL || buflen == 0)
1751 return 0;
1752
1753 pos = buf;
1754 end = buf + buflen;
1755
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001756 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 uptime = (now.sec - data->start_time.sec) * 100 +
1758 ((now.usec - data->start_time.usec) / 10000) % 100;
1759 ret = os_snprintf(pos, end - pos,
1760 "RADIUS-AUTH-SERVER-MIB\n"
1761 "radiusAuthServIdent=hostapd\n"
1762 "radiusAuthServUpTime=%d\n"
1763 "radiusAuthServResetTime=0\n"
1764 "radiusAuthServConfigReset=4\n",
1765 uptime);
1766 if (ret < 0 || ret >= end - pos) {
1767 *pos = '\0';
1768 return pos - buf;
1769 }
1770 pos += ret;
1771
1772 ret = os_snprintf(pos, end - pos,
1773 "radiusAuthServTotalAccessRequests=%u\n"
1774 "radiusAuthServTotalInvalidRequests=%u\n"
1775 "radiusAuthServTotalDupAccessRequests=%u\n"
1776 "radiusAuthServTotalAccessAccepts=%u\n"
1777 "radiusAuthServTotalAccessRejects=%u\n"
1778 "radiusAuthServTotalAccessChallenges=%u\n"
1779 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1780 "radiusAuthServTotalBadAuthenticators=%u\n"
1781 "radiusAuthServTotalPacketsDropped=%u\n"
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001782 "radiusAuthServTotalUnknownTypes=%u\n"
1783 "radiusAccServTotalRequests=%u\n"
1784 "radiusAccServTotalInvalidRequests=%u\n"
1785 "radiusAccServTotalResponses=%u\n"
1786 "radiusAccServTotalMalformedRequests=%u\n"
1787 "radiusAccServTotalBadAuthenticators=%u\n"
1788 "radiusAccServTotalUnknownTypes=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 data->counters.access_requests,
1790 data->counters.invalid_requests,
1791 data->counters.dup_access_requests,
1792 data->counters.access_accepts,
1793 data->counters.access_rejects,
1794 data->counters.access_challenges,
1795 data->counters.malformed_access_requests,
1796 data->counters.bad_authenticators,
1797 data->counters.packets_dropped,
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001798 data->counters.unknown_types,
1799 data->counters.acct_requests,
1800 data->counters.invalid_acct_requests,
1801 data->counters.acct_responses,
1802 data->counters.malformed_acct_requests,
1803 data->counters.acct_bad_authenticators,
1804 data->counters.unknown_acct_types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 if (ret < 0 || ret >= end - pos) {
1806 *pos = '\0';
1807 return pos - buf;
1808 }
1809 pos += ret;
1810
1811 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1812 char abuf[50], mbuf[50];
1813#ifdef CONFIG_IPV6
1814 if (data->ipv6) {
1815 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1816 sizeof(abuf)) == NULL)
1817 abuf[0] = '\0';
1818 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1819 sizeof(mbuf)) == NULL)
1820 mbuf[0] = '\0';
1821 }
1822#endif /* CONFIG_IPV6 */
1823 if (!data->ipv6) {
1824 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1825 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1826 }
1827
1828 ret = os_snprintf(pos, end - pos,
1829 "radiusAuthClientIndex=%u\n"
1830 "radiusAuthClientAddress=%s/%s\n"
1831 "radiusAuthServAccessRequests=%u\n"
1832 "radiusAuthServDupAccessRequests=%u\n"
1833 "radiusAuthServAccessAccepts=%u\n"
1834 "radiusAuthServAccessRejects=%u\n"
1835 "radiusAuthServAccessChallenges=%u\n"
1836 "radiusAuthServMalformedAccessRequests=%u\n"
1837 "radiusAuthServBadAuthenticators=%u\n"
1838 "radiusAuthServPacketsDropped=%u\n"
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001839 "radiusAuthServUnknownTypes=%u\n"
1840 "radiusAccServTotalRequests=%u\n"
1841 "radiusAccServTotalInvalidRequests=%u\n"
1842 "radiusAccServTotalResponses=%u\n"
1843 "radiusAccServTotalMalformedRequests=%u\n"
1844 "radiusAccServTotalBadAuthenticators=%u\n"
1845 "radiusAccServTotalUnknownTypes=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 idx,
1847 abuf, mbuf,
1848 cli->counters.access_requests,
1849 cli->counters.dup_access_requests,
1850 cli->counters.access_accepts,
1851 cli->counters.access_rejects,
1852 cli->counters.access_challenges,
1853 cli->counters.malformed_access_requests,
1854 cli->counters.bad_authenticators,
1855 cli->counters.packets_dropped,
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001856 cli->counters.unknown_types,
1857 cli->counters.acct_requests,
1858 cli->counters.invalid_acct_requests,
1859 cli->counters.acct_responses,
1860 cli->counters.malformed_acct_requests,
1861 cli->counters.acct_bad_authenticators,
1862 cli->counters.unknown_acct_types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 if (ret < 0 || ret >= end - pos) {
1864 *pos = '\0';
1865 return pos - buf;
1866 }
1867 pos += ret;
1868 }
1869
1870 return pos - buf;
1871}
1872
1873
1874static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1875 size_t identity_len, int phase2,
1876 struct eap_user *user)
1877{
1878 struct radius_session *sess = ctx;
1879 struct radius_server_data *data = sess->server;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001880 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001882 ret = data->get_eap_user(data->conf_ctx, identity, identity_len,
1883 phase2, user);
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001884 if (ret == 0 && user) {
1885 sess->accept_attr = user->accept_attr;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001886 sess->remediation = user->remediation;
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001887 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001888 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889}
1890
1891
1892static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1893{
1894 struct radius_session *sess = ctx;
1895 struct radius_server_data *data = sess->server;
1896 *len = data->eap_req_id_text_len;
1897 return data->eap_req_id_text;
1898}
1899
1900
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001901static void radius_server_log_msg(void *ctx, const char *msg)
1902{
1903 struct radius_session *sess = ctx;
1904 srv_log(sess, "EAP: %s", msg);
1905}
1906
1907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908static struct eapol_callbacks radius_server_eapol_cb =
1909{
1910 .get_eap_user = radius_server_get_eap_user,
1911 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001912 .log_msg = radius_server_log_msg,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913};
1914
1915
1916/**
1917 * radius_server_eap_pending_cb - Pending EAP data notification
1918 * @data: RADIUS server context from radius_server_init()
1919 * @ctx: Pending EAP context pointer
1920 *
1921 * This function is used to notify EAP server module that a pending operation
1922 * has been completed and processing of the EAP session can proceed.
1923 */
1924void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1925{
1926 struct radius_client *cli;
1927 struct radius_session *s, *sess = NULL;
1928 struct radius_msg *msg;
1929
1930 if (data == NULL)
1931 return;
1932
1933 for (cli = data->clients; cli; cli = cli->next) {
1934 for (s = cli->sessions; s; s = s->next) {
1935 if (s->eap == ctx && s->last_msg) {
1936 sess = s;
1937 break;
1938 }
1939 if (sess)
1940 break;
1941 }
1942 if (sess)
1943 break;
1944 }
1945
1946 if (sess == NULL) {
1947 RADIUS_DEBUG("No session matched callback ctx");
1948 return;
1949 }
1950
1951 msg = sess->last_msg;
1952 sess->last_msg = NULL;
1953 eap_sm_pending_cb(sess->eap);
1954 if (radius_server_request(data, msg,
1955 (struct sockaddr *) &sess->last_from,
1956 sess->last_fromlen, cli,
1957 sess->last_from_addr,
1958 sess->last_from_port, sess) == -2)
1959 return; /* msg was stored with the session */
1960
1961 radius_msg_free(msg);
1962}