blob: 5074b6029d356e78da0b674976615effa65cd287 [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>
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;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -080052
53 u32 acct_requests;
54 u32 invalid_acct_requests;
55 u32 acct_responses;
56 u32 malformed_acct_requests;
57 u32 acct_bad_authenticators;
58 u32 unknown_acct_types;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059};
60
61/**
62 * struct radius_session - Internal RADIUS server data for a session
63 */
64struct radius_session {
65 struct radius_session *next;
66 struct radius_client *client;
67 struct radius_server_data *server;
68 unsigned int sess_id;
69 struct eap_sm *eap;
70 struct eap_eapol_interface *eap_if;
71
72 struct radius_msg *last_msg;
73 char *last_from_addr;
74 int last_from_port;
75 struct sockaddr_storage last_from;
76 socklen_t last_fromlen;
77 u8 last_identifier;
78 struct radius_msg *last_reply;
79 u8 last_authenticator[16];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080080
81 unsigned int remediation:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082};
83
84/**
85 * struct radius_client - Internal RADIUS server data for a client
86 */
87struct radius_client {
88 struct radius_client *next;
89 struct in_addr addr;
90 struct in_addr mask;
91#ifdef CONFIG_IPV6
92 struct in6_addr addr6;
93 struct in6_addr mask6;
94#endif /* CONFIG_IPV6 */
95 char *shared_secret;
96 int shared_secret_len;
97 struct radius_session *sessions;
98 struct radius_server_counters counters;
99};
100
101/**
102 * struct radius_server_data - Internal RADIUS server data
103 */
104struct radius_server_data {
105 /**
106 * auth_sock - Socket for RADIUS authentication messages
107 */
108 int auth_sock;
109
110 /**
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800111 * acct_sock - Socket for RADIUS accounting messages
112 */
113 int acct_sock;
114
115 /**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 * clients - List of authorized RADIUS clients
117 */
118 struct radius_client *clients;
119
120 /**
121 * next_sess_id - Next session identifier
122 */
123 unsigned int next_sess_id;
124
125 /**
126 * conf_ctx - Context pointer for callbacks
127 *
128 * This is used as the ctx argument in get_eap_user() calls.
129 */
130 void *conf_ctx;
131
132 /**
133 * num_sess - Number of active sessions
134 */
135 int num_sess;
136
137 /**
138 * eap_sim_db_priv - EAP-SIM/AKA database context
139 *
140 * This is passed to the EAP-SIM/AKA server implementation as a
141 * callback context.
142 */
143 void *eap_sim_db_priv;
144
145 /**
146 * ssl_ctx - TLS context
147 *
148 * This is passed to the EAP server implementation as a callback
149 * context for TLS operations.
150 */
151 void *ssl_ctx;
152
153 /**
154 * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
155 *
156 * This parameter is used to set a key for EAP-FAST to encrypt the
157 * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
158 * set, must point to a 16-octet key.
159 */
160 u8 *pac_opaque_encr_key;
161
162 /**
163 * eap_fast_a_id - EAP-FAST authority identity (A-ID)
164 *
165 * If EAP-FAST is not used, this can be set to %NULL. In theory, this
166 * is a variable length field, but due to some existing implementations
167 * requiring A-ID to be 16 octets in length, it is recommended to use
168 * that length for the field to provide interoperability with deployed
169 * peer implementations.
170 */
171 u8 *eap_fast_a_id;
172
173 /**
174 * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
175 */
176 size_t eap_fast_a_id_len;
177
178 /**
179 * eap_fast_a_id_info - EAP-FAST authority identifier information
180 *
181 * This A-ID-Info contains a user-friendly name for the A-ID. For
182 * example, this could be the enterprise and server names in
183 * human-readable format. This field is encoded as UTF-8. If EAP-FAST
184 * is not used, this can be set to %NULL.
185 */
186 char *eap_fast_a_id_info;
187
188 /**
189 * eap_fast_prov - EAP-FAST provisioning modes
190 *
191 * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
192 * 2 = only authenticated provisioning allowed, 3 = both provisioning
193 * modes allowed.
194 */
195 int eap_fast_prov;
196
197 /**
198 * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
199 *
200 * This is the hard limit on how long a provisioned PAC-Key can be
201 * used.
202 */
203 int pac_key_lifetime;
204
205 /**
206 * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
207 *
208 * This is a soft limit on the PAC-Key. The server will automatically
209 * generate a new PAC-Key when this number of seconds (or fewer) of the
210 * lifetime remains.
211 */
212 int pac_key_refresh_time;
213
214 /**
215 * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
216 *
217 * This controls whether the protected success/failure indication
218 * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
219 */
220 int eap_sim_aka_result_ind;
221
222 /**
223 * tnc - Trusted Network Connect (TNC)
224 *
225 * This controls whether TNC is enabled and will be required before the
226 * peer is allowed to connect. Note: This is only used with EAP-TTLS
227 * and EAP-FAST. If any other EAP method is enabled, the peer will be
228 * allowed to connect without TNC.
229 */
230 int tnc;
231
232 /**
233 * pwd_group - The D-H group assigned for EAP-pwd
234 *
235 * If EAP-pwd is not used it can be set to zero.
236 */
237 u16 pwd_group;
238
239 /**
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700240 * server_id - Server identity
241 */
242 const char *server_id;
243
244 /**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 * wps - Wi-Fi Protected Setup context
246 *
247 * If WPS is used with an external RADIUS server (which is quite
248 * unlikely configuration), this is used to provide a pointer to WPS
249 * context data. Normally, this can be set to %NULL.
250 */
251 struct wps_context *wps;
252
253 /**
254 * ipv6 - Whether to enable IPv6 support in the RADIUS server
255 */
256 int ipv6;
257
258 /**
259 * start_time - Timestamp of server start
260 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800261 struct os_reltime start_time;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262
263 /**
264 * counters - Statistics counters for server operations
265 *
266 * These counters are the sum over all clients.
267 */
268 struct radius_server_counters counters;
269
270 /**
271 * get_eap_user - Callback for fetching EAP user information
272 * @ctx: Context data from conf_ctx
273 * @identity: User identity
274 * @identity_len: identity buffer length in octets
275 * @phase2: Whether this is for Phase 2 identity
276 * @user: Data structure for filling in the user information
277 * Returns: 0 on success, -1 on failure
278 *
279 * This is used to fetch information from user database. The callback
280 * will fill in information about allowed EAP methods and the user
281 * password. The password field will be an allocated copy of the
282 * password data and RADIUS server will free it after use.
283 */
284 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
285 int phase2, struct eap_user *user);
286
287 /**
288 * eap_req_id_text - Optional data for EAP-Request/Identity
289 *
290 * This can be used to configure an optional, displayable message that
291 * will be sent in EAP-Request/Identity. This string can contain an
292 * ASCII-0 character (nul) to separate network infromation per RFC
293 * 4284. The actual string length is explicit provided in
294 * eap_req_id_text_len since nul character will not be used as a string
295 * terminator.
296 */
297 char *eap_req_id_text;
298
299 /**
300 * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
301 */
302 size_t eap_req_id_text_len;
303
304 /*
305 * msg_ctx - Context data for wpa_msg() calls
306 */
307 void *msg_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308
309#ifdef CONFIG_RADIUS_TEST
310 char *dump_msk_file;
311#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800312
313 char *subscr_remediation_url;
314 u8 subscr_remediation_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315};
316
317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318#define RADIUS_DEBUG(args...) \
319wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
320#define RADIUS_ERROR(args...) \
321wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
322#define RADIUS_DUMP(args...) \
323wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
324#define RADIUS_DUMP_ASCII(args...) \
325wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
326
327
328static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
329static void radius_server_session_remove_timeout(void *eloop_ctx,
330 void *timeout_ctx);
331
332
333static struct radius_client *
334radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
335 int ipv6)
336{
337 struct radius_client *client = data->clients;
338
339 while (client) {
340#ifdef CONFIG_IPV6
341 if (ipv6) {
342 struct in6_addr *addr6;
343 int i;
344
345 addr6 = (struct in6_addr *) addr;
346 for (i = 0; i < 16; i++) {
347 if ((addr6->s6_addr[i] &
348 client->mask6.s6_addr[i]) !=
349 (client->addr6.s6_addr[i] &
350 client->mask6.s6_addr[i])) {
351 i = 17;
352 break;
353 }
354 }
355 if (i == 16) {
356 break;
357 }
358 }
359#endif /* CONFIG_IPV6 */
360 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
361 (addr->s_addr & client->mask.s_addr)) {
362 break;
363 }
364
365 client = client->next;
366 }
367
368 return client;
369}
370
371
372static struct radius_session *
373radius_server_get_session(struct radius_client *client, unsigned int sess_id)
374{
375 struct radius_session *sess = client->sessions;
376
377 while (sess) {
378 if (sess->sess_id == sess_id) {
379 break;
380 }
381 sess = sess->next;
382 }
383
384 return sess;
385}
386
387
388static void radius_server_session_free(struct radius_server_data *data,
389 struct radius_session *sess)
390{
391 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
392 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
393 eap_server_sm_deinit(sess->eap);
394 radius_msg_free(sess->last_msg);
395 os_free(sess->last_from_addr);
396 radius_msg_free(sess->last_reply);
397 os_free(sess);
398 data->num_sess--;
399}
400
401
402static void radius_server_session_remove(struct radius_server_data *data,
403 struct radius_session *sess)
404{
405 struct radius_client *client = sess->client;
406 struct radius_session *session, *prev;
407
408 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
409
410 prev = NULL;
411 session = client->sessions;
412 while (session) {
413 if (session == sess) {
414 if (prev == NULL) {
415 client->sessions = sess->next;
416 } else {
417 prev->next = sess->next;
418 }
419 radius_server_session_free(data, sess);
420 break;
421 }
422 prev = session;
423 session = session->next;
424 }
425}
426
427
428static void radius_server_session_remove_timeout(void *eloop_ctx,
429 void *timeout_ctx)
430{
431 struct radius_server_data *data = eloop_ctx;
432 struct radius_session *sess = timeout_ctx;
433 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
434 radius_server_session_remove(data, sess);
435}
436
437
438static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
439{
440 struct radius_server_data *data = eloop_ctx;
441 struct radius_session *sess = timeout_ctx;
442
443 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
444 radius_server_session_remove(data, sess);
445}
446
447
448static struct radius_session *
449radius_server_new_session(struct radius_server_data *data,
450 struct radius_client *client)
451{
452 struct radius_session *sess;
453
454 if (data->num_sess >= RADIUS_MAX_SESSION) {
455 RADIUS_DEBUG("Maximum number of existing session - no room "
456 "for a new session");
457 return NULL;
458 }
459
460 sess = os_zalloc(sizeof(*sess));
461 if (sess == NULL)
462 return NULL;
463
464 sess->server = data;
465 sess->client = client;
466 sess->sess_id = data->next_sess_id++;
467 sess->next = client->sessions;
468 client->sessions = sess;
469 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
470 radius_server_session_timeout, data, sess);
471 data->num_sess++;
472 return sess;
473}
474
475
476static struct radius_session *
477radius_server_get_new_session(struct radius_server_data *data,
478 struct radius_client *client,
479 struct radius_msg *msg)
480{
481 u8 *user;
482 size_t user_len;
483 int res;
484 struct radius_session *sess;
485 struct eap_config eap_conf;
486
487 RADIUS_DEBUG("Creating a new session");
488
489 user = os_malloc(256);
490 if (user == NULL) {
491 return NULL;
492 }
493 res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
494 if (res < 0 || res > 256) {
495 RADIUS_DEBUG("Could not get User-Name");
496 os_free(user);
497 return NULL;
498 }
499 user_len = res;
500 RADIUS_DUMP_ASCII("User-Name", user, user_len);
501
502 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
503 os_free(user);
504
505 if (res == 0) {
506 RADIUS_DEBUG("Matching user entry found");
507 sess = radius_server_new_session(data, client);
508 if (sess == NULL) {
509 RADIUS_DEBUG("Failed to create a new session");
510 return NULL;
511 }
512 } else {
513 RADIUS_DEBUG("User-Name not found from user database");
514 return NULL;
515 }
516
517 os_memset(&eap_conf, 0, sizeof(eap_conf));
518 eap_conf.ssl_ctx = data->ssl_ctx;
519 eap_conf.msg_ctx = data->msg_ctx;
520 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
521 eap_conf.backend_auth = TRUE;
522 eap_conf.eap_server = 1;
523 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
524 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
525 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
526 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
527 eap_conf.eap_fast_prov = data->eap_fast_prov;
528 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
529 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
530 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
531 eap_conf.tnc = data->tnc;
532 eap_conf.wps = data->wps;
533 eap_conf.pwd_group = data->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700534 eap_conf.server_id = (const u8 *) data->server_id;
535 eap_conf.server_id_len = os_strlen(data->server_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
537 &eap_conf);
538 if (sess->eap == NULL) {
539 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
540 "new session");
541 radius_server_session_free(data, sess);
542 return NULL;
543 }
544 sess->eap_if = eap_get_interface(sess->eap);
545 sess->eap_if->eapRestart = TRUE;
546 sess->eap_if->portEnabled = TRUE;
547
548 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
549
550 return sess;
551}
552
553
554static struct radius_msg *
555radius_server_encapsulate_eap(struct radius_server_data *data,
556 struct radius_client *client,
557 struct radius_session *sess,
558 struct radius_msg *request)
559{
560 struct radius_msg *msg;
561 int code;
562 unsigned int sess_id;
563 struct radius_hdr *hdr = radius_msg_get_hdr(request);
564
565 if (sess->eap_if->eapFail) {
566 sess->eap_if->eapFail = FALSE;
567 code = RADIUS_CODE_ACCESS_REJECT;
568 } else if (sess->eap_if->eapSuccess) {
569 sess->eap_if->eapSuccess = FALSE;
570 code = RADIUS_CODE_ACCESS_ACCEPT;
571 } else {
572 sess->eap_if->eapReq = FALSE;
573 code = RADIUS_CODE_ACCESS_CHALLENGE;
574 }
575
576 msg = radius_msg_new(code, hdr->identifier);
577 if (msg == NULL) {
578 RADIUS_DEBUG("Failed to allocate reply message");
579 return NULL;
580 }
581
582 sess_id = htonl(sess->sess_id);
583 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
584 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
585 (u8 *) &sess_id, sizeof(sess_id))) {
586 RADIUS_DEBUG("Failed to add State attribute");
587 }
588
589 if (sess->eap_if->eapReqData &&
590 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
591 wpabuf_len(sess->eap_if->eapReqData))) {
592 RADIUS_DEBUG("Failed to add EAP-Message attribute");
593 }
594
595 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
596 int len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800597#ifdef CONFIG_RADIUS_TEST
598 if (data->dump_msk_file) {
599 FILE *f;
600 char buf[2 * 64 + 1];
601 f = fopen(data->dump_msk_file, "a");
602 if (f) {
603 len = sess->eap_if->eapKeyDataLen;
604 if (len > 64)
605 len = 64;
606 len = wpa_snprintf_hex(
607 buf, sizeof(buf),
608 sess->eap_if->eapKeyData, len);
609 buf[len] = '\0';
610 fprintf(f, "%s\n", buf);
611 fclose(f);
612 }
613 }
614#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 if (sess->eap_if->eapKeyDataLen > 64) {
616 len = 32;
617 } else {
618 len = sess->eap_if->eapKeyDataLen / 2;
619 }
620 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
621 (u8 *) client->shared_secret,
622 client->shared_secret_len,
623 sess->eap_if->eapKeyData + len,
624 len, sess->eap_if->eapKeyData,
625 len)) {
626 RADIUS_DEBUG("Failed to add MPPE key attributes");
627 }
628 }
629
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800630#ifdef CONFIG_HS20
631 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation &&
632 data->subscr_remediation_url) {
633 u8 *buf;
634 size_t url_len = os_strlen(data->subscr_remediation_url);
635 buf = os_malloc(1 + url_len);
636 if (buf == NULL) {
637 radius_msg_free(msg);
638 return NULL;
639 }
640 buf[0] = data->subscr_remediation_method;
641 os_memcpy(&buf[1], data->subscr_remediation_url, url_len);
642 if (!radius_msg_add_wfa(
643 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
644 buf, 1 + url_len)) {
645 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
646 }
647 os_free(buf);
648 } else if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation) {
649 u8 buf[1];
650 if (!radius_msg_add_wfa(
651 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
652 buf, 0)) {
653 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
654 }
655 }
656#endif /* CONFIG_HS20 */
657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
659 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
660 radius_msg_free(msg);
661 return NULL;
662 }
663
664 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
665 client->shared_secret_len,
666 hdr->authenticator) < 0) {
667 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
668 }
669
670 return msg;
671}
672
673
674static int radius_server_reject(struct radius_server_data *data,
675 struct radius_client *client,
676 struct radius_msg *request,
677 struct sockaddr *from, socklen_t fromlen,
678 const char *from_addr, int from_port)
679{
680 struct radius_msg *msg;
681 int ret = 0;
682 struct eap_hdr eapfail;
683 struct wpabuf *buf;
684 struct radius_hdr *hdr = radius_msg_get_hdr(request);
685
686 RADIUS_DEBUG("Reject invalid request from %s:%d",
687 from_addr, from_port);
688
689 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
690 if (msg == NULL) {
691 return -1;
692 }
693
694 os_memset(&eapfail, 0, sizeof(eapfail));
695 eapfail.code = EAP_CODE_FAILURE;
696 eapfail.identifier = 0;
697 eapfail.length = host_to_be16(sizeof(eapfail));
698
699 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
700 RADIUS_DEBUG("Failed to add EAP-Message attribute");
701 }
702
703 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
704 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
705 radius_msg_free(msg);
706 return -1;
707 }
708
709 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
710 client->shared_secret_len,
711 hdr->authenticator) <
712 0) {
713 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
714 }
715
716 if (wpa_debug_level <= MSG_MSGDUMP) {
717 radius_msg_dump(msg);
718 }
719
720 data->counters.access_rejects++;
721 client->counters.access_rejects++;
722 buf = radius_msg_get_buf(msg);
723 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
724 (struct sockaddr *) from, sizeof(*from)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800725 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 ret = -1;
727 }
728
729 radius_msg_free(msg);
730
731 return ret;
732}
733
734
735static int radius_server_request(struct radius_server_data *data,
736 struct radius_msg *msg,
737 struct sockaddr *from, socklen_t fromlen,
738 struct radius_client *client,
739 const char *from_addr, int from_port,
740 struct radius_session *force_sess)
741{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700742 struct wpabuf *eap = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 int res, state_included = 0;
744 u8 statebuf[4];
745 unsigned int state;
746 struct radius_session *sess;
747 struct radius_msg *reply;
748 int is_complete = 0;
749
750 if (force_sess)
751 sess = force_sess;
752 else {
753 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
754 sizeof(statebuf));
755 state_included = res >= 0;
756 if (res == sizeof(statebuf)) {
757 state = WPA_GET_BE32(statebuf);
758 sess = radius_server_get_session(client, state);
759 } else {
760 sess = NULL;
761 }
762 }
763
764 if (sess) {
765 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
766 } else if (state_included) {
767 RADIUS_DEBUG("State attribute included but no session found");
768 radius_server_reject(data, client, msg, from, fromlen,
769 from_addr, from_port);
770 return -1;
771 } else {
772 sess = radius_server_get_new_session(data, client, msg);
773 if (sess == NULL) {
774 RADIUS_DEBUG("Could not create a new session");
775 radius_server_reject(data, client, msg, from, fromlen,
776 from_addr, from_port);
777 return -1;
778 }
779 }
780
781 if (sess->last_from_port == from_port &&
782 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
783 os_memcmp(sess->last_authenticator,
784 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
785 RADIUS_DEBUG("Duplicate message from %s", from_addr);
786 data->counters.dup_access_requests++;
787 client->counters.dup_access_requests++;
788
789 if (sess->last_reply) {
790 struct wpabuf *buf;
791 buf = radius_msg_get_buf(sess->last_reply);
792 res = sendto(data->auth_sock, wpabuf_head(buf),
793 wpabuf_len(buf), 0,
794 (struct sockaddr *) from, fromlen);
795 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800796 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
797 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 }
799 return 0;
800 }
801
802 RADIUS_DEBUG("No previous reply available for duplicate "
803 "message");
804 return -1;
805 }
806
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700807 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 if (eap == NULL) {
809 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
810 from_addr);
811 data->counters.packets_dropped++;
812 client->counters.packets_dropped++;
813 return -1;
814 }
815
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700816 RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817
818 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
819 * RFC3579 Sect. 2.6.2.
820 * Include EAP-Response/Nak with no preferred method if
821 * code == request.
822 * If code is not 1-4, discard the packet silently.
823 * Or is this already done by the EAP state machine? */
824
825 wpabuf_free(sess->eap_if->eapRespData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700826 sess->eap_if->eapRespData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 sess->eap_if->eapResp = TRUE;
828 eap_server_sm_step(sess->eap);
829
830 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
831 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
832 RADIUS_DUMP("EAP data from the state machine",
833 wpabuf_head(sess->eap_if->eapReqData),
834 wpabuf_len(sess->eap_if->eapReqData));
835 } else if (sess->eap_if->eapFail) {
836 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
837 "set");
838 } else if (eap_sm_method_pending(sess->eap)) {
839 radius_msg_free(sess->last_msg);
840 sess->last_msg = msg;
841 sess->last_from_port = from_port;
842 os_free(sess->last_from_addr);
843 sess->last_from_addr = os_strdup(from_addr);
844 sess->last_fromlen = fromlen;
845 os_memcpy(&sess->last_from, from, fromlen);
846 return -2;
847 } else {
848 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
849 " Access-Request silently (assuming it was a "
850 "duplicate)");
851 data->counters.packets_dropped++;
852 client->counters.packets_dropped++;
853 return -1;
854 }
855
856 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
857 is_complete = 1;
858
859 reply = radius_server_encapsulate_eap(data, client, sess, msg);
860
861 if (reply) {
862 struct wpabuf *buf;
863 struct radius_hdr *hdr;
864
865 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
866 if (wpa_debug_level <= MSG_MSGDUMP) {
867 radius_msg_dump(reply);
868 }
869
870 switch (radius_msg_get_hdr(reply)->code) {
871 case RADIUS_CODE_ACCESS_ACCEPT:
872 data->counters.access_accepts++;
873 client->counters.access_accepts++;
874 break;
875 case RADIUS_CODE_ACCESS_REJECT:
876 data->counters.access_rejects++;
877 client->counters.access_rejects++;
878 break;
879 case RADIUS_CODE_ACCESS_CHALLENGE:
880 data->counters.access_challenges++;
881 client->counters.access_challenges++;
882 break;
883 }
884 buf = radius_msg_get_buf(reply);
885 res = sendto(data->auth_sock, wpabuf_head(buf),
886 wpabuf_len(buf), 0,
887 (struct sockaddr *) from, fromlen);
888 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800889 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
890 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 }
892 radius_msg_free(sess->last_reply);
893 sess->last_reply = reply;
894 sess->last_from_port = from_port;
895 hdr = radius_msg_get_hdr(msg);
896 sess->last_identifier = hdr->identifier;
897 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
898 } else {
899 data->counters.packets_dropped++;
900 client->counters.packets_dropped++;
901 }
902
903 if (is_complete) {
904 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
905 sess->sess_id);
906 eloop_cancel_timeout(radius_server_session_remove_timeout,
907 data, sess);
908 eloop_register_timeout(10, 0,
909 radius_server_session_remove_timeout,
910 data, sess);
911 }
912
913 return 0;
914}
915
916
917static void radius_server_receive_auth(int sock, void *eloop_ctx,
918 void *sock_ctx)
919{
920 struct radius_server_data *data = eloop_ctx;
921 u8 *buf = NULL;
922 union {
923 struct sockaddr_storage ss;
924 struct sockaddr_in sin;
925#ifdef CONFIG_IPV6
926 struct sockaddr_in6 sin6;
927#endif /* CONFIG_IPV6 */
928 } from;
929 socklen_t fromlen;
930 int len;
931 struct radius_client *client = NULL;
932 struct radius_msg *msg = NULL;
933 char abuf[50];
934 int from_port = 0;
935
936 buf = os_malloc(RADIUS_MAX_MSG_LEN);
937 if (buf == NULL) {
938 goto fail;
939 }
940
941 fromlen = sizeof(from);
942 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
943 (struct sockaddr *) &from.ss, &fromlen);
944 if (len < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800945 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
946 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 goto fail;
948 }
949
950#ifdef CONFIG_IPV6
951 if (data->ipv6) {
952 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
953 sizeof(abuf)) == NULL)
954 abuf[0] = '\0';
955 from_port = ntohs(from.sin6.sin6_port);
956 RADIUS_DEBUG("Received %d bytes from %s:%d",
957 len, abuf, from_port);
958
959 client = radius_server_get_client(data,
960 (struct in_addr *)
961 &from.sin6.sin6_addr, 1);
962 }
963#endif /* CONFIG_IPV6 */
964
965 if (!data->ipv6) {
966 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
967 from_port = ntohs(from.sin.sin_port);
968 RADIUS_DEBUG("Received %d bytes from %s:%d",
969 len, abuf, from_port);
970
971 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
972 }
973
974 RADIUS_DUMP("Received data", buf, len);
975
976 if (client == NULL) {
977 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
978 data->counters.invalid_requests++;
979 goto fail;
980 }
981
982 msg = radius_msg_parse(buf, len);
983 if (msg == NULL) {
984 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
985 data->counters.malformed_access_requests++;
986 client->counters.malformed_access_requests++;
987 goto fail;
988 }
989
990 os_free(buf);
991 buf = NULL;
992
993 if (wpa_debug_level <= MSG_MSGDUMP) {
994 radius_msg_dump(msg);
995 }
996
997 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
998 RADIUS_DEBUG("Unexpected RADIUS code %d",
999 radius_msg_get_hdr(msg)->code);
1000 data->counters.unknown_types++;
1001 client->counters.unknown_types++;
1002 goto fail;
1003 }
1004
1005 data->counters.access_requests++;
1006 client->counters.access_requests++;
1007
1008 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
1009 client->shared_secret_len, NULL)) {
1010 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
1011 data->counters.bad_authenticators++;
1012 client->counters.bad_authenticators++;
1013 goto fail;
1014 }
1015
1016 if (radius_server_request(data, msg, (struct sockaddr *) &from,
1017 fromlen, client, abuf, from_port, NULL) ==
1018 -2)
1019 return; /* msg was stored with the session */
1020
1021fail:
1022 radius_msg_free(msg);
1023 os_free(buf);
1024}
1025
1026
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001027static void radius_server_receive_acct(int sock, void *eloop_ctx,
1028 void *sock_ctx)
1029{
1030 struct radius_server_data *data = eloop_ctx;
1031 u8 *buf = NULL;
1032 union {
1033 struct sockaddr_storage ss;
1034 struct sockaddr_in sin;
1035#ifdef CONFIG_IPV6
1036 struct sockaddr_in6 sin6;
1037#endif /* CONFIG_IPV6 */
1038 } from;
1039 socklen_t fromlen;
1040 int len, res;
1041 struct radius_client *client = NULL;
1042 struct radius_msg *msg = NULL, *resp = NULL;
1043 char abuf[50];
1044 int from_port = 0;
1045 struct radius_hdr *hdr;
1046 struct wpabuf *rbuf;
1047
1048 buf = os_malloc(RADIUS_MAX_MSG_LEN);
1049 if (buf == NULL) {
1050 goto fail;
1051 }
1052
1053 fromlen = sizeof(from);
1054 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
1055 (struct sockaddr *) &from.ss, &fromlen);
1056 if (len < 0) {
1057 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
1058 strerror(errno));
1059 goto fail;
1060 }
1061
1062#ifdef CONFIG_IPV6
1063 if (data->ipv6) {
1064 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
1065 sizeof(abuf)) == NULL)
1066 abuf[0] = '\0';
1067 from_port = ntohs(from.sin6.sin6_port);
1068 RADIUS_DEBUG("Received %d bytes from %s:%d",
1069 len, abuf, from_port);
1070
1071 client = radius_server_get_client(data,
1072 (struct in_addr *)
1073 &from.sin6.sin6_addr, 1);
1074 }
1075#endif /* CONFIG_IPV6 */
1076
1077 if (!data->ipv6) {
1078 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
1079 from_port = ntohs(from.sin.sin_port);
1080 RADIUS_DEBUG("Received %d bytes from %s:%d",
1081 len, abuf, from_port);
1082
1083 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
1084 }
1085
1086 RADIUS_DUMP("Received data", buf, len);
1087
1088 if (client == NULL) {
1089 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
1090 data->counters.invalid_acct_requests++;
1091 goto fail;
1092 }
1093
1094 msg = radius_msg_parse(buf, len);
1095 if (msg == NULL) {
1096 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
1097 data->counters.malformed_acct_requests++;
1098 client->counters.malformed_acct_requests++;
1099 goto fail;
1100 }
1101
1102 os_free(buf);
1103 buf = NULL;
1104
1105 if (wpa_debug_level <= MSG_MSGDUMP) {
1106 radius_msg_dump(msg);
1107 }
1108
1109 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_REQUEST) {
1110 RADIUS_DEBUG("Unexpected RADIUS code %d",
1111 radius_msg_get_hdr(msg)->code);
1112 data->counters.unknown_acct_types++;
1113 client->counters.unknown_acct_types++;
1114 goto fail;
1115 }
1116
1117 data->counters.acct_requests++;
1118 client->counters.acct_requests++;
1119
1120 if (radius_msg_verify_acct_req(msg, (u8 *) client->shared_secret,
1121 client->shared_secret_len)) {
1122 RADIUS_DEBUG("Invalid Authenticator from %s", abuf);
1123 data->counters.acct_bad_authenticators++;
1124 client->counters.acct_bad_authenticators++;
1125 goto fail;
1126 }
1127
1128 /* TODO: Write accounting information to a file or database */
1129
1130 hdr = radius_msg_get_hdr(msg);
1131
1132 resp = radius_msg_new(RADIUS_CODE_ACCOUNTING_RESPONSE, hdr->identifier);
1133 if (resp == NULL)
1134 goto fail;
1135
1136 radius_msg_finish_acct_resp(resp, (u8 *) client->shared_secret,
1137 client->shared_secret_len,
1138 hdr->authenticator);
1139
1140 RADIUS_DEBUG("Reply to %s:%d", abuf, from_port);
1141 if (wpa_debug_level <= MSG_MSGDUMP) {
1142 radius_msg_dump(resp);
1143 }
1144 rbuf = radius_msg_get_buf(resp);
1145 data->counters.acct_responses++;
1146 client->counters.acct_responses++;
1147 res = sendto(data->acct_sock, wpabuf_head(rbuf), wpabuf_len(rbuf), 0,
1148 (struct sockaddr *) &from.ss, fromlen);
1149 if (res < 0) {
1150 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
1151 strerror(errno));
1152 }
1153
1154fail:
1155 radius_msg_free(resp);
1156 radius_msg_free(msg);
1157 os_free(buf);
1158}
1159
1160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161static int radius_server_disable_pmtu_discovery(int s)
1162{
1163 int r = -1;
1164#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1165 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
1166 int action = IP_PMTUDISC_DONT;
1167 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
1168 sizeof(action));
1169 if (r == -1)
1170 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
1171 "%s", strerror(errno));
1172#endif
1173 return r;
1174}
1175
1176
1177static int radius_server_open_socket(int port)
1178{
1179 int s;
1180 struct sockaddr_in addr;
1181
1182 s = socket(PF_INET, SOCK_DGRAM, 0);
1183 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001184 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 return -1;
1186 }
1187
1188 radius_server_disable_pmtu_discovery(s);
1189
1190 os_memset(&addr, 0, sizeof(addr));
1191 addr.sin_family = AF_INET;
1192 addr.sin_port = htons(port);
1193 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001194 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 close(s);
1196 return -1;
1197 }
1198
1199 return s;
1200}
1201
1202
1203#ifdef CONFIG_IPV6
1204static int radius_server_open_socket6(int port)
1205{
1206 int s;
1207 struct sockaddr_in6 addr;
1208
1209 s = socket(PF_INET6, SOCK_DGRAM, 0);
1210 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001211 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s",
1212 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 return -1;
1214 }
1215
1216 os_memset(&addr, 0, sizeof(addr));
1217 addr.sin6_family = AF_INET6;
1218 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1219 addr.sin6_port = htons(port);
1220 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001221 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 close(s);
1223 return -1;
1224 }
1225
1226 return s;
1227}
1228#endif /* CONFIG_IPV6 */
1229
1230
1231static void radius_server_free_sessions(struct radius_server_data *data,
1232 struct radius_session *sessions)
1233{
1234 struct radius_session *session, *prev;
1235
1236 session = sessions;
1237 while (session) {
1238 prev = session;
1239 session = session->next;
1240 radius_server_session_free(data, prev);
1241 }
1242}
1243
1244
1245static void radius_server_free_clients(struct radius_server_data *data,
1246 struct radius_client *clients)
1247{
1248 struct radius_client *client, *prev;
1249
1250 client = clients;
1251 while (client) {
1252 prev = client;
1253 client = client->next;
1254
1255 radius_server_free_sessions(data, prev->sessions);
1256 os_free(prev->shared_secret);
1257 os_free(prev);
1258 }
1259}
1260
1261
1262static struct radius_client *
1263radius_server_read_clients(const char *client_file, int ipv6)
1264{
1265 FILE *f;
1266 const int buf_size = 1024;
1267 char *buf, *pos;
1268 struct radius_client *clients, *tail, *entry;
1269 int line = 0, mask, failed = 0, i;
1270 struct in_addr addr;
1271#ifdef CONFIG_IPV6
1272 struct in6_addr addr6;
1273#endif /* CONFIG_IPV6 */
1274 unsigned int val;
1275
1276 f = fopen(client_file, "r");
1277 if (f == NULL) {
1278 RADIUS_ERROR("Could not open client file '%s'", client_file);
1279 return NULL;
1280 }
1281
1282 buf = os_malloc(buf_size);
1283 if (buf == NULL) {
1284 fclose(f);
1285 return NULL;
1286 }
1287
1288 clients = tail = NULL;
1289 while (fgets(buf, buf_size, f)) {
1290 /* Configuration file format:
1291 * 192.168.1.0/24 secret
1292 * 192.168.1.2 secret
1293 * fe80::211:22ff:fe33:4455/64 secretipv6
1294 */
1295 line++;
1296 buf[buf_size - 1] = '\0';
1297 pos = buf;
1298 while (*pos != '\0' && *pos != '\n')
1299 pos++;
1300 if (*pos == '\n')
1301 *pos = '\0';
1302 if (*buf == '\0' || *buf == '#')
1303 continue;
1304
1305 pos = buf;
1306 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1307 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1308 (*pos >= 'A' && *pos <= 'F')) {
1309 pos++;
1310 }
1311
1312 if (*pos == '\0') {
1313 failed = 1;
1314 break;
1315 }
1316
1317 if (*pos == '/') {
1318 char *end;
1319 *pos++ = '\0';
1320 mask = strtol(pos, &end, 10);
1321 if ((pos == end) ||
1322 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1323 failed = 1;
1324 break;
1325 }
1326 pos = end;
1327 } else {
1328 mask = ipv6 ? 128 : 32;
1329 *pos++ = '\0';
1330 }
1331
1332 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1333 failed = 1;
1334 break;
1335 }
1336#ifdef CONFIG_IPV6
1337 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1338 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1339 failed = 1;
1340 break;
1341 }
1342 /* Convert IPv4 address to IPv6 */
1343 if (mask <= 32)
1344 mask += (128 - 32);
1345 os_memset(addr6.s6_addr, 0, 10);
1346 addr6.s6_addr[10] = 0xff;
1347 addr6.s6_addr[11] = 0xff;
1348 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1349 4);
1350 }
1351#endif /* CONFIG_IPV6 */
1352
1353 while (*pos == ' ' || *pos == '\t') {
1354 pos++;
1355 }
1356
1357 if (*pos == '\0') {
1358 failed = 1;
1359 break;
1360 }
1361
1362 entry = os_zalloc(sizeof(*entry));
1363 if (entry == NULL) {
1364 failed = 1;
1365 break;
1366 }
1367 entry->shared_secret = os_strdup(pos);
1368 if (entry->shared_secret == NULL) {
1369 failed = 1;
1370 os_free(entry);
1371 break;
1372 }
1373 entry->shared_secret_len = os_strlen(entry->shared_secret);
1374 entry->addr.s_addr = addr.s_addr;
1375 if (!ipv6) {
1376 val = 0;
1377 for (i = 0; i < mask; i++)
1378 val |= 1 << (31 - i);
1379 entry->mask.s_addr = htonl(val);
1380 }
1381#ifdef CONFIG_IPV6
1382 if (ipv6) {
1383 int offset = mask / 8;
1384
1385 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1386 os_memset(entry->mask6.s6_addr, 0xff, offset);
1387 val = 0;
1388 for (i = 0; i < (mask % 8); i++)
1389 val |= 1 << (7 - i);
1390 if (offset < 16)
1391 entry->mask6.s6_addr[offset] = val;
1392 }
1393#endif /* CONFIG_IPV6 */
1394
1395 if (tail == NULL) {
1396 clients = tail = entry;
1397 } else {
1398 tail->next = entry;
1399 tail = entry;
1400 }
1401 }
1402
1403 if (failed) {
1404 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1405 radius_server_free_clients(NULL, clients);
1406 clients = NULL;
1407 }
1408
1409 os_free(buf);
1410 fclose(f);
1411
1412 return clients;
1413}
1414
1415
1416/**
1417 * radius_server_init - Initialize RADIUS server
1418 * @conf: Configuration for the RADIUS server
1419 * Returns: Pointer to private RADIUS server context or %NULL on failure
1420 *
1421 * This initializes a RADIUS server instance and returns a context pointer that
1422 * will be used in other calls to the RADIUS server module. The server can be
1423 * deinitialize by calling radius_server_deinit().
1424 */
1425struct radius_server_data *
1426radius_server_init(struct radius_server_conf *conf)
1427{
1428 struct radius_server_data *data;
1429
1430#ifndef CONFIG_IPV6
1431 if (conf->ipv6) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001432 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 return NULL;
1434 }
1435#endif /* CONFIG_IPV6 */
1436
1437 data = os_zalloc(sizeof(*data));
1438 if (data == NULL)
1439 return NULL;
1440
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001441 os_get_reltime(&data->start_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 data->conf_ctx = conf->conf_ctx;
1443 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1444 data->ssl_ctx = conf->ssl_ctx;
1445 data->msg_ctx = conf->msg_ctx;
1446 data->ipv6 = conf->ipv6;
1447 if (conf->pac_opaque_encr_key) {
1448 data->pac_opaque_encr_key = os_malloc(16);
1449 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1450 16);
1451 }
1452 if (conf->eap_fast_a_id) {
1453 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1454 if (data->eap_fast_a_id) {
1455 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1456 conf->eap_fast_a_id_len);
1457 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1458 }
1459 }
1460 if (conf->eap_fast_a_id_info)
1461 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1462 data->eap_fast_prov = conf->eap_fast_prov;
1463 data->pac_key_lifetime = conf->pac_key_lifetime;
1464 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1465 data->get_eap_user = conf->get_eap_user;
1466 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1467 data->tnc = conf->tnc;
1468 data->wps = conf->wps;
1469 data->pwd_group = conf->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001470 data->server_id = conf->server_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 if (conf->eap_req_id_text) {
1472 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1473 if (data->eap_req_id_text) {
1474 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1475 conf->eap_req_id_text_len);
1476 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1477 }
1478 }
1479
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001480 if (conf->subscr_remediation_url) {
1481 data->subscr_remediation_url =
1482 os_strdup(conf->subscr_remediation_url);
1483 }
1484
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485#ifdef CONFIG_RADIUS_TEST
1486 if (conf->dump_msk_file)
1487 data->dump_msk_file = os_strdup(conf->dump_msk_file);
1488#endif /* CONFIG_RADIUS_TEST */
1489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 data->clients = radius_server_read_clients(conf->client_file,
1491 conf->ipv6);
1492 if (data->clients == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001493 wpa_printf(MSG_ERROR, "No RADIUS clients configured");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 radius_server_deinit(data);
1495 return NULL;
1496 }
1497
1498#ifdef CONFIG_IPV6
1499 if (conf->ipv6)
1500 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1501 else
1502#endif /* CONFIG_IPV6 */
1503 data->auth_sock = radius_server_open_socket(conf->auth_port);
1504 if (data->auth_sock < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001505 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506 radius_server_deinit(data);
1507 return NULL;
1508 }
1509 if (eloop_register_read_sock(data->auth_sock,
1510 radius_server_receive_auth,
1511 data, NULL)) {
1512 radius_server_deinit(data);
1513 return NULL;
1514 }
1515
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001516 if (conf->acct_port) {
1517#ifdef CONFIG_IPV6
1518 if (conf->ipv6)
1519 data->acct_sock = radius_server_open_socket6(
1520 conf->acct_port);
1521 else
1522#endif /* CONFIG_IPV6 */
1523 data->acct_sock = radius_server_open_socket(conf->acct_port);
1524 if (data->acct_sock < 0) {
1525 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS accounting server");
1526 radius_server_deinit(data);
1527 return NULL;
1528 }
1529 if (eloop_register_read_sock(data->acct_sock,
1530 radius_server_receive_acct,
1531 data, NULL)) {
1532 radius_server_deinit(data);
1533 return NULL;
1534 }
1535 } else {
1536 data->acct_sock = -1;
1537 }
1538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 return data;
1540}
1541
1542
1543/**
1544 * radius_server_deinit - Deinitialize RADIUS server
1545 * @data: RADIUS server context from radius_server_init()
1546 */
1547void radius_server_deinit(struct radius_server_data *data)
1548{
1549 if (data == NULL)
1550 return;
1551
1552 if (data->auth_sock >= 0) {
1553 eloop_unregister_read_sock(data->auth_sock);
1554 close(data->auth_sock);
1555 }
1556
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001557 if (data->acct_sock >= 0) {
1558 eloop_unregister_read_sock(data->acct_sock);
1559 close(data->acct_sock);
1560 }
1561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 radius_server_free_clients(data, data->clients);
1563
1564 os_free(data->pac_opaque_encr_key);
1565 os_free(data->eap_fast_a_id);
1566 os_free(data->eap_fast_a_id_info);
1567 os_free(data->eap_req_id_text);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568#ifdef CONFIG_RADIUS_TEST
1569 os_free(data->dump_msk_file);
1570#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001571 os_free(data->subscr_remediation_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 os_free(data);
1573}
1574
1575
1576/**
1577 * radius_server_get_mib - Get RADIUS server MIB information
1578 * @data: RADIUS server context from radius_server_init()
1579 * @buf: Buffer for returning the MIB data in text format
1580 * @buflen: buf length in octets
1581 * Returns: Number of octets written into buf
1582 */
1583int radius_server_get_mib(struct radius_server_data *data, char *buf,
1584 size_t buflen)
1585{
1586 int ret, uptime;
1587 unsigned int idx;
1588 char *end, *pos;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001589 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 struct radius_client *cli;
1591
1592 /* RFC 2619 - RADIUS Authentication Server MIB */
1593
1594 if (data == NULL || buflen == 0)
1595 return 0;
1596
1597 pos = buf;
1598 end = buf + buflen;
1599
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001600 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 uptime = (now.sec - data->start_time.sec) * 100 +
1602 ((now.usec - data->start_time.usec) / 10000) % 100;
1603 ret = os_snprintf(pos, end - pos,
1604 "RADIUS-AUTH-SERVER-MIB\n"
1605 "radiusAuthServIdent=hostapd\n"
1606 "radiusAuthServUpTime=%d\n"
1607 "radiusAuthServResetTime=0\n"
1608 "radiusAuthServConfigReset=4\n",
1609 uptime);
1610 if (ret < 0 || ret >= end - pos) {
1611 *pos = '\0';
1612 return pos - buf;
1613 }
1614 pos += ret;
1615
1616 ret = os_snprintf(pos, end - pos,
1617 "radiusAuthServTotalAccessRequests=%u\n"
1618 "radiusAuthServTotalInvalidRequests=%u\n"
1619 "radiusAuthServTotalDupAccessRequests=%u\n"
1620 "radiusAuthServTotalAccessAccepts=%u\n"
1621 "radiusAuthServTotalAccessRejects=%u\n"
1622 "radiusAuthServTotalAccessChallenges=%u\n"
1623 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1624 "radiusAuthServTotalBadAuthenticators=%u\n"
1625 "radiusAuthServTotalPacketsDropped=%u\n"
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001626 "radiusAuthServTotalUnknownTypes=%u\n"
1627 "radiusAccServTotalRequests=%u\n"
1628 "radiusAccServTotalInvalidRequests=%u\n"
1629 "radiusAccServTotalResponses=%u\n"
1630 "radiusAccServTotalMalformedRequests=%u\n"
1631 "radiusAccServTotalBadAuthenticators=%u\n"
1632 "radiusAccServTotalUnknownTypes=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 data->counters.access_requests,
1634 data->counters.invalid_requests,
1635 data->counters.dup_access_requests,
1636 data->counters.access_accepts,
1637 data->counters.access_rejects,
1638 data->counters.access_challenges,
1639 data->counters.malformed_access_requests,
1640 data->counters.bad_authenticators,
1641 data->counters.packets_dropped,
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001642 data->counters.unknown_types,
1643 data->counters.acct_requests,
1644 data->counters.invalid_acct_requests,
1645 data->counters.acct_responses,
1646 data->counters.malformed_acct_requests,
1647 data->counters.acct_bad_authenticators,
1648 data->counters.unknown_acct_types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 if (ret < 0 || ret >= end - pos) {
1650 *pos = '\0';
1651 return pos - buf;
1652 }
1653 pos += ret;
1654
1655 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1656 char abuf[50], mbuf[50];
1657#ifdef CONFIG_IPV6
1658 if (data->ipv6) {
1659 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1660 sizeof(abuf)) == NULL)
1661 abuf[0] = '\0';
1662 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1663 sizeof(mbuf)) == NULL)
1664 mbuf[0] = '\0';
1665 }
1666#endif /* CONFIG_IPV6 */
1667 if (!data->ipv6) {
1668 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1669 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1670 }
1671
1672 ret = os_snprintf(pos, end - pos,
1673 "radiusAuthClientIndex=%u\n"
1674 "radiusAuthClientAddress=%s/%s\n"
1675 "radiusAuthServAccessRequests=%u\n"
1676 "radiusAuthServDupAccessRequests=%u\n"
1677 "radiusAuthServAccessAccepts=%u\n"
1678 "radiusAuthServAccessRejects=%u\n"
1679 "radiusAuthServAccessChallenges=%u\n"
1680 "radiusAuthServMalformedAccessRequests=%u\n"
1681 "radiusAuthServBadAuthenticators=%u\n"
1682 "radiusAuthServPacketsDropped=%u\n"
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001683 "radiusAuthServUnknownTypes=%u\n"
1684 "radiusAccServTotalRequests=%u\n"
1685 "radiusAccServTotalInvalidRequests=%u\n"
1686 "radiusAccServTotalResponses=%u\n"
1687 "radiusAccServTotalMalformedRequests=%u\n"
1688 "radiusAccServTotalBadAuthenticators=%u\n"
1689 "radiusAccServTotalUnknownTypes=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 idx,
1691 abuf, mbuf,
1692 cli->counters.access_requests,
1693 cli->counters.dup_access_requests,
1694 cli->counters.access_accepts,
1695 cli->counters.access_rejects,
1696 cli->counters.access_challenges,
1697 cli->counters.malformed_access_requests,
1698 cli->counters.bad_authenticators,
1699 cli->counters.packets_dropped,
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001700 cli->counters.unknown_types,
1701 cli->counters.acct_requests,
1702 cli->counters.invalid_acct_requests,
1703 cli->counters.acct_responses,
1704 cli->counters.malformed_acct_requests,
1705 cli->counters.acct_bad_authenticators,
1706 cli->counters.unknown_acct_types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 if (ret < 0 || ret >= end - pos) {
1708 *pos = '\0';
1709 return pos - buf;
1710 }
1711 pos += ret;
1712 }
1713
1714 return pos - buf;
1715}
1716
1717
1718static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1719 size_t identity_len, int phase2,
1720 struct eap_user *user)
1721{
1722 struct radius_session *sess = ctx;
1723 struct radius_server_data *data = sess->server;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001724 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001726 ret = data->get_eap_user(data->conf_ctx, identity, identity_len,
1727 phase2, user);
1728 if (ret == 0 && user)
1729 sess->remediation = user->remediation;
1730 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731}
1732
1733
1734static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1735{
1736 struct radius_session *sess = ctx;
1737 struct radius_server_data *data = sess->server;
1738 *len = data->eap_req_id_text_len;
1739 return data->eap_req_id_text;
1740}
1741
1742
1743static struct eapol_callbacks radius_server_eapol_cb =
1744{
1745 .get_eap_user = radius_server_get_eap_user,
1746 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1747};
1748
1749
1750/**
1751 * radius_server_eap_pending_cb - Pending EAP data notification
1752 * @data: RADIUS server context from radius_server_init()
1753 * @ctx: Pending EAP context pointer
1754 *
1755 * This function is used to notify EAP server module that a pending operation
1756 * has been completed and processing of the EAP session can proceed.
1757 */
1758void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1759{
1760 struct radius_client *cli;
1761 struct radius_session *s, *sess = NULL;
1762 struct radius_msg *msg;
1763
1764 if (data == NULL)
1765 return;
1766
1767 for (cli = data->clients; cli; cli = cli->next) {
1768 for (s = cli->sessions; s; s = s->next) {
1769 if (s->eap == ctx && s->last_msg) {
1770 sess = s;
1771 break;
1772 }
1773 if (sess)
1774 break;
1775 }
1776 if (sess)
1777 break;
1778 }
1779
1780 if (sess == NULL) {
1781 RADIUS_DEBUG("No session matched callback ctx");
1782 return;
1783 }
1784
1785 msg = sess->last_msg;
1786 sess->last_msg = NULL;
1787 eap_sm_pending_cb(sess->eap);
1788 if (radius_server_request(data, msg,
1789 (struct sockaddr *) &sess->last_from,
1790 sess->last_fromlen, cli,
1791 sess->last_from_addr,
1792 sess->last_from_port, sess) == -2)
1793 return; /* msg was stored with the session */
1794
1795 radius_msg_free(msg);
1796}