radius_server.c revision b36ed7cd946148d829f311de8fe53ea3ffaaffe3
1/* 2 * RADIUS authentication server 3 * Copyright (c) 2005-2009, 2011-2014, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "includes.h" 10#include <net/if.h> 11#ifdef CONFIG_SQLITE 12#include <sqlite3.h> 13#endif /* CONFIG_SQLITE */ 14 15#include "common.h" 16#include "radius.h" 17#include "eloop.h" 18#include "eap_server/eap.h" 19#include "ap/ap_config.h" 20#include "crypto/tls.h" 21#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; 57 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; 64}; 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; 76 char *username; /* from User-Name attribute */ 77 char *nas_ip; 78 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]; 87 88 unsigned int remediation:1; 89 90 struct hostapd_radius_attr *accept_attr; 91}; 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 /** 120 * acct_sock - Socket for RADIUS accounting messages 121 */ 122 int acct_sock; 123 124 /** 125 * 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 /** 249 * server_id - Server identity 250 */ 251 const char *server_id; 252 253 /** 254 * 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 */ 270 struct os_reltime start_time; 271 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; 317 318#ifdef CONFIG_RADIUS_TEST 319 char *dump_msk_file; 320#endif /* CONFIG_RADIUS_TEST */ 321 322 char *subscr_remediation_url; 323 u8 subscr_remediation_method; 324 325#ifdef CONFIG_SQLITE 326 sqlite3 *db; 327#endif /* CONFIG_SQLITE */ 328}; 329 330 331#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 345void 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 391 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); 456 os_free(sess->username); 457 os_free(sess->nas_ip); 458 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 537#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 case 4: 558 srv_log(sess, "TLS test - RSA-DHE using a short 511-bit prime"); 559 eap_conf->tls_test_flags = TLS_DHE_PRIME_511B; 560 break; 561 case 5: 562 srv_log(sess, "TLS test - RSA-DHE using a short 767-bit prime"); 563 eap_conf->tls_test_flags = TLS_DHE_PRIME_767B; 564 break; 565 case 6: 566 srv_log(sess, "TLS test - RSA-DHE using a bogus 15 \"prime\""); 567 eap_conf->tls_test_flags = TLS_DHE_PRIME_15; 568 break; 569 case 7: 570 srv_log(sess, "TLS test - RSA-DHE using a short 58-bit prime in long container"); 571 eap_conf->tls_test_flags = TLS_DHE_PRIME_58B; 572 break; 573 case 8: 574 srv_log(sess, "TLS test - RSA-DHE using a non-prime"); 575 eap_conf->tls_test_flags = TLS_DHE_NON_PRIME; 576 break; 577 default: 578 srv_log(sess, "Unrecognized TLS test"); 579 break; 580 } 581} 582#endif /* CONFIG_TESTING_OPTIONS */ 583 584static void radius_server_testing_options(struct radius_session *sess, 585 struct eap_config *eap_conf) 586{ 587#ifdef CONFIG_TESTING_OPTIONS 588 const char *pos; 589 590 pos = os_strstr(sess->username, "@test-"); 591 if (pos == NULL) 592 return; 593 pos += 6; 594 if (os_strncmp(pos, "tls-", 4) == 0) 595 radius_server_testing_options_tls(sess, pos + 4, eap_conf); 596 else 597 srv_log(sess, "Unrecognized test: %s", pos); 598#endif /* CONFIG_TESTING_OPTIONS */ 599} 600 601 602static struct radius_session * 603radius_server_get_new_session(struct radius_server_data *data, 604 struct radius_client *client, 605 struct radius_msg *msg, const char *from_addr) 606{ 607 u8 *user; 608 size_t user_len; 609 int res; 610 struct radius_session *sess; 611 struct eap_config eap_conf; 612 struct eap_user tmp; 613 614 RADIUS_DEBUG("Creating a new session"); 615 616 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &user, 617 &user_len, NULL) < 0) { 618 RADIUS_DEBUG("Could not get User-Name"); 619 return NULL; 620 } 621 RADIUS_DUMP_ASCII("User-Name", user, user_len); 622 623 os_memset(&tmp, 0, sizeof(tmp)); 624 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, &tmp); 625 os_free(tmp.password); 626 627 if (res != 0) { 628 RADIUS_DEBUG("User-Name not found from user database"); 629 return NULL; 630 } 631 632 RADIUS_DEBUG("Matching user entry found"); 633 sess = radius_server_new_session(data, client); 634 if (sess == NULL) { 635 RADIUS_DEBUG("Failed to create a new session"); 636 return NULL; 637 } 638 sess->accept_attr = tmp.accept_attr; 639 640 sess->username = os_malloc(user_len * 2 + 1); 641 if (sess->username == NULL) { 642 radius_server_session_free(data, sess); 643 return NULL; 644 } 645 printf_encode(sess->username, user_len * 2 + 1, user, user_len); 646 647 sess->nas_ip = os_strdup(from_addr); 648 if (sess->nas_ip == NULL) { 649 radius_server_session_free(data, sess); 650 return NULL; 651 } 652 653 srv_log(sess, "New session created"); 654 655 os_memset(&eap_conf, 0, sizeof(eap_conf)); 656 eap_conf.ssl_ctx = data->ssl_ctx; 657 eap_conf.msg_ctx = data->msg_ctx; 658 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv; 659 eap_conf.backend_auth = TRUE; 660 eap_conf.eap_server = 1; 661 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key; 662 eap_conf.eap_fast_a_id = data->eap_fast_a_id; 663 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len; 664 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info; 665 eap_conf.eap_fast_prov = data->eap_fast_prov; 666 eap_conf.pac_key_lifetime = data->pac_key_lifetime; 667 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time; 668 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind; 669 eap_conf.tnc = data->tnc; 670 eap_conf.wps = data->wps; 671 eap_conf.pwd_group = data->pwd_group; 672 eap_conf.server_id = (const u8 *) data->server_id; 673 eap_conf.server_id_len = os_strlen(data->server_id); 674 radius_server_testing_options(sess, &eap_conf); 675 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb, 676 &eap_conf); 677 if (sess->eap == NULL) { 678 RADIUS_DEBUG("Failed to initialize EAP state machine for the " 679 "new session"); 680 radius_server_session_free(data, sess); 681 return NULL; 682 } 683 sess->eap_if = eap_get_interface(sess->eap); 684 sess->eap_if->eapRestart = TRUE; 685 sess->eap_if->portEnabled = TRUE; 686 687 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id); 688 689 return sess; 690} 691 692 693static struct radius_msg * 694radius_server_encapsulate_eap(struct radius_server_data *data, 695 struct radius_client *client, 696 struct radius_session *sess, 697 struct radius_msg *request) 698{ 699 struct radius_msg *msg; 700 int code; 701 unsigned int sess_id; 702 struct radius_hdr *hdr = radius_msg_get_hdr(request); 703 704 if (sess->eap_if->eapFail) { 705 sess->eap_if->eapFail = FALSE; 706 code = RADIUS_CODE_ACCESS_REJECT; 707 } else if (sess->eap_if->eapSuccess) { 708 sess->eap_if->eapSuccess = FALSE; 709 code = RADIUS_CODE_ACCESS_ACCEPT; 710 } else { 711 sess->eap_if->eapReq = FALSE; 712 code = RADIUS_CODE_ACCESS_CHALLENGE; 713 } 714 715 msg = radius_msg_new(code, hdr->identifier); 716 if (msg == NULL) { 717 RADIUS_DEBUG("Failed to allocate reply message"); 718 return NULL; 719 } 720 721 sess_id = htonl(sess->sess_id); 722 if (code == RADIUS_CODE_ACCESS_CHALLENGE && 723 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE, 724 (u8 *) &sess_id, sizeof(sess_id))) { 725 RADIUS_DEBUG("Failed to add State attribute"); 726 } 727 728 if (sess->eap_if->eapReqData && 729 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData), 730 wpabuf_len(sess->eap_if->eapReqData))) { 731 RADIUS_DEBUG("Failed to add EAP-Message attribute"); 732 } 733 734 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) { 735 int len; 736#ifdef CONFIG_RADIUS_TEST 737 if (data->dump_msk_file) { 738 FILE *f; 739 char buf[2 * 64 + 1]; 740 f = fopen(data->dump_msk_file, "a"); 741 if (f) { 742 len = sess->eap_if->eapKeyDataLen; 743 if (len > 64) 744 len = 64; 745 len = wpa_snprintf_hex( 746 buf, sizeof(buf), 747 sess->eap_if->eapKeyData, len); 748 buf[len] = '\0'; 749 fprintf(f, "%s\n", buf); 750 fclose(f); 751 } 752 } 753#endif /* CONFIG_RADIUS_TEST */ 754 if (sess->eap_if->eapKeyDataLen > 64) { 755 len = 32; 756 } else { 757 len = sess->eap_if->eapKeyDataLen / 2; 758 } 759 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator, 760 (u8 *) client->shared_secret, 761 client->shared_secret_len, 762 sess->eap_if->eapKeyData + len, 763 len, sess->eap_if->eapKeyData, 764 len)) { 765 RADIUS_DEBUG("Failed to add MPPE key attributes"); 766 } 767 } 768 769#ifdef CONFIG_HS20 770 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation && 771 data->subscr_remediation_url) { 772 u8 *buf; 773 size_t url_len = os_strlen(data->subscr_remediation_url); 774 buf = os_malloc(1 + url_len); 775 if (buf == NULL) { 776 radius_msg_free(msg); 777 return NULL; 778 } 779 buf[0] = data->subscr_remediation_method; 780 os_memcpy(&buf[1], data->subscr_remediation_url, url_len); 781 if (!radius_msg_add_wfa( 782 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION, 783 buf, 1 + url_len)) { 784 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem"); 785 } 786 os_free(buf); 787 } else if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation) { 788 u8 buf[1]; 789 if (!radius_msg_add_wfa( 790 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION, 791 buf, 0)) { 792 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem"); 793 } 794 } 795#endif /* CONFIG_HS20 */ 796 797 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) { 798 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)"); 799 radius_msg_free(msg); 800 return NULL; 801 } 802 803 if (code == RADIUS_CODE_ACCESS_ACCEPT) { 804 struct hostapd_radius_attr *attr; 805 for (attr = sess->accept_attr; attr; attr = attr->next) { 806 if (!radius_msg_add_attr(msg, attr->type, 807 wpabuf_head(attr->val), 808 wpabuf_len(attr->val))) { 809 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute"); 810 radius_msg_free(msg); 811 return NULL; 812 } 813 } 814 } 815 816 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret, 817 client->shared_secret_len, 818 hdr->authenticator) < 0) { 819 RADIUS_DEBUG("Failed to add Message-Authenticator attribute"); 820 } 821 822 return msg; 823} 824 825 826static int radius_server_reject(struct radius_server_data *data, 827 struct radius_client *client, 828 struct radius_msg *request, 829 struct sockaddr *from, socklen_t fromlen, 830 const char *from_addr, int from_port) 831{ 832 struct radius_msg *msg; 833 int ret = 0; 834 struct eap_hdr eapfail; 835 struct wpabuf *buf; 836 struct radius_hdr *hdr = radius_msg_get_hdr(request); 837 838 RADIUS_DEBUG("Reject invalid request from %s:%d", 839 from_addr, from_port); 840 841 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier); 842 if (msg == NULL) { 843 return -1; 844 } 845 846 os_memset(&eapfail, 0, sizeof(eapfail)); 847 eapfail.code = EAP_CODE_FAILURE; 848 eapfail.identifier = 0; 849 eapfail.length = host_to_be16(sizeof(eapfail)); 850 851 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) { 852 RADIUS_DEBUG("Failed to add EAP-Message attribute"); 853 } 854 855 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) { 856 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)"); 857 radius_msg_free(msg); 858 return -1; 859 } 860 861 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret, 862 client->shared_secret_len, 863 hdr->authenticator) < 864 0) { 865 RADIUS_DEBUG("Failed to add Message-Authenticator attribute"); 866 } 867 868 if (wpa_debug_level <= MSG_MSGDUMP) { 869 radius_msg_dump(msg); 870 } 871 872 data->counters.access_rejects++; 873 client->counters.access_rejects++; 874 buf = radius_msg_get_buf(msg); 875 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0, 876 (struct sockaddr *) from, sizeof(*from)) < 0) { 877 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno)); 878 ret = -1; 879 } 880 881 radius_msg_free(msg); 882 883 return ret; 884} 885 886 887static int radius_server_request(struct radius_server_data *data, 888 struct radius_msg *msg, 889 struct sockaddr *from, socklen_t fromlen, 890 struct radius_client *client, 891 const char *from_addr, int from_port, 892 struct radius_session *force_sess) 893{ 894 struct wpabuf *eap = NULL; 895 int res, state_included = 0; 896 u8 statebuf[4]; 897 unsigned int state; 898 struct radius_session *sess; 899 struct radius_msg *reply; 900 int is_complete = 0; 901 902 if (force_sess) 903 sess = force_sess; 904 else { 905 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf, 906 sizeof(statebuf)); 907 state_included = res >= 0; 908 if (res == sizeof(statebuf)) { 909 state = WPA_GET_BE32(statebuf); 910 sess = radius_server_get_session(client, state); 911 } else { 912 sess = NULL; 913 } 914 } 915 916 if (sess) { 917 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id); 918 } else if (state_included) { 919 RADIUS_DEBUG("State attribute included but no session found"); 920 radius_server_reject(data, client, msg, from, fromlen, 921 from_addr, from_port); 922 return -1; 923 } else { 924 sess = radius_server_get_new_session(data, client, msg, 925 from_addr); 926 if (sess == NULL) { 927 RADIUS_DEBUG("Could not create a new session"); 928 radius_server_reject(data, client, msg, from, fromlen, 929 from_addr, from_port); 930 return -1; 931 } 932 } 933 934 if (sess->last_from_port == from_port && 935 sess->last_identifier == radius_msg_get_hdr(msg)->identifier && 936 os_memcmp(sess->last_authenticator, 937 radius_msg_get_hdr(msg)->authenticator, 16) == 0) { 938 RADIUS_DEBUG("Duplicate message from %s", from_addr); 939 data->counters.dup_access_requests++; 940 client->counters.dup_access_requests++; 941 942 if (sess->last_reply) { 943 struct wpabuf *buf; 944 buf = radius_msg_get_buf(sess->last_reply); 945 res = sendto(data->auth_sock, wpabuf_head(buf), 946 wpabuf_len(buf), 0, 947 (struct sockaddr *) from, fromlen); 948 if (res < 0) { 949 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", 950 strerror(errno)); 951 } 952 return 0; 953 } 954 955 RADIUS_DEBUG("No previous reply available for duplicate " 956 "message"); 957 return -1; 958 } 959 960 eap = radius_msg_get_eap(msg); 961 if (eap == NULL) { 962 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s", 963 from_addr); 964 data->counters.packets_dropped++; 965 client->counters.packets_dropped++; 966 return -1; 967 } 968 969 RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap)); 970 971 /* FIX: if Code is Request, Success, or Failure, send Access-Reject; 972 * RFC3579 Sect. 2.6.2. 973 * Include EAP-Response/Nak with no preferred method if 974 * code == request. 975 * If code is not 1-4, discard the packet silently. 976 * Or is this already done by the EAP state machine? */ 977 978 wpabuf_free(sess->eap_if->eapRespData); 979 sess->eap_if->eapRespData = eap; 980 sess->eap_if->eapResp = TRUE; 981 eap_server_sm_step(sess->eap); 982 983 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess || 984 sess->eap_if->eapFail) && sess->eap_if->eapReqData) { 985 RADIUS_DUMP("EAP data from the state machine", 986 wpabuf_head(sess->eap_if->eapReqData), 987 wpabuf_len(sess->eap_if->eapReqData)); 988 } else if (sess->eap_if->eapFail) { 989 RADIUS_DEBUG("No EAP data from the state machine, but eapFail " 990 "set"); 991 } else if (eap_sm_method_pending(sess->eap)) { 992 radius_msg_free(sess->last_msg); 993 sess->last_msg = msg; 994 sess->last_from_port = from_port; 995 os_free(sess->last_from_addr); 996 sess->last_from_addr = os_strdup(from_addr); 997 sess->last_fromlen = fromlen; 998 os_memcpy(&sess->last_from, from, fromlen); 999 return -2; 1000 } else { 1001 RADIUS_DEBUG("No EAP data from the state machine - ignore this" 1002 " Access-Request silently (assuming it was a " 1003 "duplicate)"); 1004 data->counters.packets_dropped++; 1005 client->counters.packets_dropped++; 1006 return -1; 1007 } 1008 1009 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail) 1010 is_complete = 1; 1011 if (sess->eap_if->eapFail) 1012 srv_log(sess, "EAP authentication failed"); 1013 else if (sess->eap_if->eapSuccess) 1014 srv_log(sess, "EAP authentication succeeded"); 1015 1016 reply = radius_server_encapsulate_eap(data, client, sess, msg); 1017 1018 if (reply) { 1019 struct wpabuf *buf; 1020 struct radius_hdr *hdr; 1021 1022 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port); 1023 if (wpa_debug_level <= MSG_MSGDUMP) { 1024 radius_msg_dump(reply); 1025 } 1026 1027 switch (radius_msg_get_hdr(reply)->code) { 1028 case RADIUS_CODE_ACCESS_ACCEPT: 1029 srv_log(sess, "Sending Access-Accept"); 1030 data->counters.access_accepts++; 1031 client->counters.access_accepts++; 1032 break; 1033 case RADIUS_CODE_ACCESS_REJECT: 1034 srv_log(sess, "Sending Access-Reject"); 1035 data->counters.access_rejects++; 1036 client->counters.access_rejects++; 1037 break; 1038 case RADIUS_CODE_ACCESS_CHALLENGE: 1039 data->counters.access_challenges++; 1040 client->counters.access_challenges++; 1041 break; 1042 } 1043 buf = radius_msg_get_buf(reply); 1044 res = sendto(data->auth_sock, wpabuf_head(buf), 1045 wpabuf_len(buf), 0, 1046 (struct sockaddr *) from, fromlen); 1047 if (res < 0) { 1048 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", 1049 strerror(errno)); 1050 } 1051 radius_msg_free(sess->last_reply); 1052 sess->last_reply = reply; 1053 sess->last_from_port = from_port; 1054 hdr = radius_msg_get_hdr(msg); 1055 sess->last_identifier = hdr->identifier; 1056 os_memcpy(sess->last_authenticator, hdr->authenticator, 16); 1057 } else { 1058 data->counters.packets_dropped++; 1059 client->counters.packets_dropped++; 1060 } 1061 1062 if (is_complete) { 1063 RADIUS_DEBUG("Removing completed session 0x%x after timeout", 1064 sess->sess_id); 1065 eloop_cancel_timeout(radius_server_session_remove_timeout, 1066 data, sess); 1067 eloop_register_timeout(10, 0, 1068 radius_server_session_remove_timeout, 1069 data, sess); 1070 } 1071 1072 return 0; 1073} 1074 1075 1076static void radius_server_receive_auth(int sock, void *eloop_ctx, 1077 void *sock_ctx) 1078{ 1079 struct radius_server_data *data = eloop_ctx; 1080 u8 *buf = NULL; 1081 union { 1082 struct sockaddr_storage ss; 1083 struct sockaddr_in sin; 1084#ifdef CONFIG_IPV6 1085 struct sockaddr_in6 sin6; 1086#endif /* CONFIG_IPV6 */ 1087 } from; 1088 socklen_t fromlen; 1089 int len; 1090 struct radius_client *client = NULL; 1091 struct radius_msg *msg = NULL; 1092 char abuf[50]; 1093 int from_port = 0; 1094 1095 buf = os_malloc(RADIUS_MAX_MSG_LEN); 1096 if (buf == NULL) { 1097 goto fail; 1098 } 1099 1100 fromlen = sizeof(from); 1101 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0, 1102 (struct sockaddr *) &from.ss, &fromlen); 1103 if (len < 0) { 1104 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s", 1105 strerror(errno)); 1106 goto fail; 1107 } 1108 1109#ifdef CONFIG_IPV6 1110 if (data->ipv6) { 1111 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf, 1112 sizeof(abuf)) == NULL) 1113 abuf[0] = '\0'; 1114 from_port = ntohs(from.sin6.sin6_port); 1115 RADIUS_DEBUG("Received %d bytes from %s:%d", 1116 len, abuf, from_port); 1117 1118 client = radius_server_get_client(data, 1119 (struct in_addr *) 1120 &from.sin6.sin6_addr, 1); 1121 } 1122#endif /* CONFIG_IPV6 */ 1123 1124 if (!data->ipv6) { 1125 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf)); 1126 from_port = ntohs(from.sin.sin_port); 1127 RADIUS_DEBUG("Received %d bytes from %s:%d", 1128 len, abuf, from_port); 1129 1130 client = radius_server_get_client(data, &from.sin.sin_addr, 0); 1131 } 1132 1133 RADIUS_DUMP("Received data", buf, len); 1134 1135 if (client == NULL) { 1136 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf); 1137 data->counters.invalid_requests++; 1138 goto fail; 1139 } 1140 1141 msg = radius_msg_parse(buf, len); 1142 if (msg == NULL) { 1143 RADIUS_DEBUG("Parsing incoming RADIUS frame failed"); 1144 data->counters.malformed_access_requests++; 1145 client->counters.malformed_access_requests++; 1146 goto fail; 1147 } 1148 1149 os_free(buf); 1150 buf = NULL; 1151 1152 if (wpa_debug_level <= MSG_MSGDUMP) { 1153 radius_msg_dump(msg); 1154 } 1155 1156 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) { 1157 RADIUS_DEBUG("Unexpected RADIUS code %d", 1158 radius_msg_get_hdr(msg)->code); 1159 data->counters.unknown_types++; 1160 client->counters.unknown_types++; 1161 goto fail; 1162 } 1163 1164 data->counters.access_requests++; 1165 client->counters.access_requests++; 1166 1167 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret, 1168 client->shared_secret_len, NULL)) { 1169 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf); 1170 data->counters.bad_authenticators++; 1171 client->counters.bad_authenticators++; 1172 goto fail; 1173 } 1174 1175 if (radius_server_request(data, msg, (struct sockaddr *) &from, 1176 fromlen, client, abuf, from_port, NULL) == 1177 -2) 1178 return; /* msg was stored with the session */ 1179 1180fail: 1181 radius_msg_free(msg); 1182 os_free(buf); 1183} 1184 1185 1186static void radius_server_receive_acct(int sock, void *eloop_ctx, 1187 void *sock_ctx) 1188{ 1189 struct radius_server_data *data = eloop_ctx; 1190 u8 *buf = NULL; 1191 union { 1192 struct sockaddr_storage ss; 1193 struct sockaddr_in sin; 1194#ifdef CONFIG_IPV6 1195 struct sockaddr_in6 sin6; 1196#endif /* CONFIG_IPV6 */ 1197 } from; 1198 socklen_t fromlen; 1199 int len, res; 1200 struct radius_client *client = NULL; 1201 struct radius_msg *msg = NULL, *resp = NULL; 1202 char abuf[50]; 1203 int from_port = 0; 1204 struct radius_hdr *hdr; 1205 struct wpabuf *rbuf; 1206 1207 buf = os_malloc(RADIUS_MAX_MSG_LEN); 1208 if (buf == NULL) { 1209 goto fail; 1210 } 1211 1212 fromlen = sizeof(from); 1213 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0, 1214 (struct sockaddr *) &from.ss, &fromlen); 1215 if (len < 0) { 1216 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s", 1217 strerror(errno)); 1218 goto fail; 1219 } 1220 1221#ifdef CONFIG_IPV6 1222 if (data->ipv6) { 1223 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf, 1224 sizeof(abuf)) == NULL) 1225 abuf[0] = '\0'; 1226 from_port = ntohs(from.sin6.sin6_port); 1227 RADIUS_DEBUG("Received %d bytes from %s:%d", 1228 len, abuf, from_port); 1229 1230 client = radius_server_get_client(data, 1231 (struct in_addr *) 1232 &from.sin6.sin6_addr, 1); 1233 } 1234#endif /* CONFIG_IPV6 */ 1235 1236 if (!data->ipv6) { 1237 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf)); 1238 from_port = ntohs(from.sin.sin_port); 1239 RADIUS_DEBUG("Received %d bytes from %s:%d", 1240 len, abuf, from_port); 1241 1242 client = radius_server_get_client(data, &from.sin.sin_addr, 0); 1243 } 1244 1245 RADIUS_DUMP("Received data", buf, len); 1246 1247 if (client == NULL) { 1248 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf); 1249 data->counters.invalid_acct_requests++; 1250 goto fail; 1251 } 1252 1253 msg = radius_msg_parse(buf, len); 1254 if (msg == NULL) { 1255 RADIUS_DEBUG("Parsing incoming RADIUS frame failed"); 1256 data->counters.malformed_acct_requests++; 1257 client->counters.malformed_acct_requests++; 1258 goto fail; 1259 } 1260 1261 os_free(buf); 1262 buf = NULL; 1263 1264 if (wpa_debug_level <= MSG_MSGDUMP) { 1265 radius_msg_dump(msg); 1266 } 1267 1268 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_REQUEST) { 1269 RADIUS_DEBUG("Unexpected RADIUS code %d", 1270 radius_msg_get_hdr(msg)->code); 1271 data->counters.unknown_acct_types++; 1272 client->counters.unknown_acct_types++; 1273 goto fail; 1274 } 1275 1276 data->counters.acct_requests++; 1277 client->counters.acct_requests++; 1278 1279 if (radius_msg_verify_acct_req(msg, (u8 *) client->shared_secret, 1280 client->shared_secret_len)) { 1281 RADIUS_DEBUG("Invalid Authenticator from %s", abuf); 1282 data->counters.acct_bad_authenticators++; 1283 client->counters.acct_bad_authenticators++; 1284 goto fail; 1285 } 1286 1287 /* TODO: Write accounting information to a file or database */ 1288 1289 hdr = radius_msg_get_hdr(msg); 1290 1291 resp = radius_msg_new(RADIUS_CODE_ACCOUNTING_RESPONSE, hdr->identifier); 1292 if (resp == NULL) 1293 goto fail; 1294 1295 radius_msg_finish_acct_resp(resp, (u8 *) client->shared_secret, 1296 client->shared_secret_len, 1297 hdr->authenticator); 1298 1299 RADIUS_DEBUG("Reply to %s:%d", abuf, from_port); 1300 if (wpa_debug_level <= MSG_MSGDUMP) { 1301 radius_msg_dump(resp); 1302 } 1303 rbuf = radius_msg_get_buf(resp); 1304 data->counters.acct_responses++; 1305 client->counters.acct_responses++; 1306 res = sendto(data->acct_sock, wpabuf_head(rbuf), wpabuf_len(rbuf), 0, 1307 (struct sockaddr *) &from.ss, fromlen); 1308 if (res < 0) { 1309 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", 1310 strerror(errno)); 1311 } 1312 1313fail: 1314 radius_msg_free(resp); 1315 radius_msg_free(msg); 1316 os_free(buf); 1317} 1318 1319 1320static int radius_server_disable_pmtu_discovery(int s) 1321{ 1322 int r = -1; 1323#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) 1324 /* Turn off Path MTU discovery on IPv4/UDP sockets. */ 1325 int action = IP_PMTUDISC_DONT; 1326 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action, 1327 sizeof(action)); 1328 if (r == -1) 1329 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: " 1330 "%s", strerror(errno)); 1331#endif 1332 return r; 1333} 1334 1335 1336static int radius_server_open_socket(int port) 1337{ 1338 int s; 1339 struct sockaddr_in addr; 1340 1341 s = socket(PF_INET, SOCK_DGRAM, 0); 1342 if (s < 0) { 1343 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno)); 1344 return -1; 1345 } 1346 1347 radius_server_disable_pmtu_discovery(s); 1348 1349 os_memset(&addr, 0, sizeof(addr)); 1350 addr.sin_family = AF_INET; 1351 addr.sin_port = htons(port); 1352 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 1353 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno)); 1354 close(s); 1355 return -1; 1356 } 1357 1358 return s; 1359} 1360 1361 1362#ifdef CONFIG_IPV6 1363static int radius_server_open_socket6(int port) 1364{ 1365 int s; 1366 struct sockaddr_in6 addr; 1367 1368 s = socket(PF_INET6, SOCK_DGRAM, 0); 1369 if (s < 0) { 1370 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s", 1371 strerror(errno)); 1372 return -1; 1373 } 1374 1375 os_memset(&addr, 0, sizeof(addr)); 1376 addr.sin6_family = AF_INET6; 1377 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any)); 1378 addr.sin6_port = htons(port); 1379 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 1380 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno)); 1381 close(s); 1382 return -1; 1383 } 1384 1385 return s; 1386} 1387#endif /* CONFIG_IPV6 */ 1388 1389 1390static void radius_server_free_sessions(struct radius_server_data *data, 1391 struct radius_session *sessions) 1392{ 1393 struct radius_session *session, *prev; 1394 1395 session = sessions; 1396 while (session) { 1397 prev = session; 1398 session = session->next; 1399 radius_server_session_free(data, prev); 1400 } 1401} 1402 1403 1404static void radius_server_free_clients(struct radius_server_data *data, 1405 struct radius_client *clients) 1406{ 1407 struct radius_client *client, *prev; 1408 1409 client = clients; 1410 while (client) { 1411 prev = client; 1412 client = client->next; 1413 1414 radius_server_free_sessions(data, prev->sessions); 1415 os_free(prev->shared_secret); 1416 os_free(prev); 1417 } 1418} 1419 1420 1421static struct radius_client * 1422radius_server_read_clients(const char *client_file, int ipv6) 1423{ 1424 FILE *f; 1425 const int buf_size = 1024; 1426 char *buf, *pos; 1427 struct radius_client *clients, *tail, *entry; 1428 int line = 0, mask, failed = 0, i; 1429 struct in_addr addr; 1430#ifdef CONFIG_IPV6 1431 struct in6_addr addr6; 1432#endif /* CONFIG_IPV6 */ 1433 unsigned int val; 1434 1435 f = fopen(client_file, "r"); 1436 if (f == NULL) { 1437 RADIUS_ERROR("Could not open client file '%s'", client_file); 1438 return NULL; 1439 } 1440 1441 buf = os_malloc(buf_size); 1442 if (buf == NULL) { 1443 fclose(f); 1444 return NULL; 1445 } 1446 1447 clients = tail = NULL; 1448 while (fgets(buf, buf_size, f)) { 1449 /* Configuration file format: 1450 * 192.168.1.0/24 secret 1451 * 192.168.1.2 secret 1452 * fe80::211:22ff:fe33:4455/64 secretipv6 1453 */ 1454 line++; 1455 buf[buf_size - 1] = '\0'; 1456 pos = buf; 1457 while (*pos != '\0' && *pos != '\n') 1458 pos++; 1459 if (*pos == '\n') 1460 *pos = '\0'; 1461 if (*buf == '\0' || *buf == '#') 1462 continue; 1463 1464 pos = buf; 1465 while ((*pos >= '0' && *pos <= '9') || *pos == '.' || 1466 (*pos >= 'a' && *pos <= 'f') || *pos == ':' || 1467 (*pos >= 'A' && *pos <= 'F')) { 1468 pos++; 1469 } 1470 1471 if (*pos == '\0') { 1472 failed = 1; 1473 break; 1474 } 1475 1476 if (*pos == '/') { 1477 char *end; 1478 *pos++ = '\0'; 1479 mask = strtol(pos, &end, 10); 1480 if ((pos == end) || 1481 (mask < 0 || mask > (ipv6 ? 128 : 32))) { 1482 failed = 1; 1483 break; 1484 } 1485 pos = end; 1486 } else { 1487 mask = ipv6 ? 128 : 32; 1488 *pos++ = '\0'; 1489 } 1490 1491 if (!ipv6 && inet_aton(buf, &addr) == 0) { 1492 failed = 1; 1493 break; 1494 } 1495#ifdef CONFIG_IPV6 1496 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) { 1497 if (inet_pton(AF_INET, buf, &addr) <= 0) { 1498 failed = 1; 1499 break; 1500 } 1501 /* Convert IPv4 address to IPv6 */ 1502 if (mask <= 32) 1503 mask += (128 - 32); 1504 os_memset(addr6.s6_addr, 0, 10); 1505 addr6.s6_addr[10] = 0xff; 1506 addr6.s6_addr[11] = 0xff; 1507 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr, 1508 4); 1509 } 1510#endif /* CONFIG_IPV6 */ 1511 1512 while (*pos == ' ' || *pos == '\t') { 1513 pos++; 1514 } 1515 1516 if (*pos == '\0') { 1517 failed = 1; 1518 break; 1519 } 1520 1521 entry = os_zalloc(sizeof(*entry)); 1522 if (entry == NULL) { 1523 failed = 1; 1524 break; 1525 } 1526 entry->shared_secret = os_strdup(pos); 1527 if (entry->shared_secret == NULL) { 1528 failed = 1; 1529 os_free(entry); 1530 break; 1531 } 1532 entry->shared_secret_len = os_strlen(entry->shared_secret); 1533 if (!ipv6) { 1534 entry->addr.s_addr = addr.s_addr; 1535 val = 0; 1536 for (i = 0; i < mask; i++) 1537 val |= 1 << (31 - i); 1538 entry->mask.s_addr = htonl(val); 1539 } 1540#ifdef CONFIG_IPV6 1541 if (ipv6) { 1542 int offset = mask / 8; 1543 1544 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16); 1545 os_memset(entry->mask6.s6_addr, 0xff, offset); 1546 val = 0; 1547 for (i = 0; i < (mask % 8); i++) 1548 val |= 1 << (7 - i); 1549 if (offset < 16) 1550 entry->mask6.s6_addr[offset] = val; 1551 } 1552#endif /* CONFIG_IPV6 */ 1553 1554 if (tail == NULL) { 1555 clients = tail = entry; 1556 } else { 1557 tail->next = entry; 1558 tail = entry; 1559 } 1560 } 1561 1562 if (failed) { 1563 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file); 1564 radius_server_free_clients(NULL, clients); 1565 clients = NULL; 1566 } 1567 1568 os_free(buf); 1569 fclose(f); 1570 1571 return clients; 1572} 1573 1574 1575/** 1576 * radius_server_init - Initialize RADIUS server 1577 * @conf: Configuration for the RADIUS server 1578 * Returns: Pointer to private RADIUS server context or %NULL on failure 1579 * 1580 * This initializes a RADIUS server instance and returns a context pointer that 1581 * will be used in other calls to the RADIUS server module. The server can be 1582 * deinitialize by calling radius_server_deinit(). 1583 */ 1584struct radius_server_data * 1585radius_server_init(struct radius_server_conf *conf) 1586{ 1587 struct radius_server_data *data; 1588 1589#ifndef CONFIG_IPV6 1590 if (conf->ipv6) { 1591 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support"); 1592 return NULL; 1593 } 1594#endif /* CONFIG_IPV6 */ 1595 1596 data = os_zalloc(sizeof(*data)); 1597 if (data == NULL) 1598 return NULL; 1599 1600 os_get_reltime(&data->start_time); 1601 data->conf_ctx = conf->conf_ctx; 1602 data->eap_sim_db_priv = conf->eap_sim_db_priv; 1603 data->ssl_ctx = conf->ssl_ctx; 1604 data->msg_ctx = conf->msg_ctx; 1605 data->ipv6 = conf->ipv6; 1606 if (conf->pac_opaque_encr_key) { 1607 data->pac_opaque_encr_key = os_malloc(16); 1608 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key, 1609 16); 1610 } 1611 if (conf->eap_fast_a_id) { 1612 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len); 1613 if (data->eap_fast_a_id) { 1614 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id, 1615 conf->eap_fast_a_id_len); 1616 data->eap_fast_a_id_len = conf->eap_fast_a_id_len; 1617 } 1618 } 1619 if (conf->eap_fast_a_id_info) 1620 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info); 1621 data->eap_fast_prov = conf->eap_fast_prov; 1622 data->pac_key_lifetime = conf->pac_key_lifetime; 1623 data->pac_key_refresh_time = conf->pac_key_refresh_time; 1624 data->get_eap_user = conf->get_eap_user; 1625 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind; 1626 data->tnc = conf->tnc; 1627 data->wps = conf->wps; 1628 data->pwd_group = conf->pwd_group; 1629 data->server_id = conf->server_id; 1630 if (conf->eap_req_id_text) { 1631 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len); 1632 if (data->eap_req_id_text) { 1633 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text, 1634 conf->eap_req_id_text_len); 1635 data->eap_req_id_text_len = conf->eap_req_id_text_len; 1636 } 1637 } 1638 1639 if (conf->subscr_remediation_url) { 1640 data->subscr_remediation_url = 1641 os_strdup(conf->subscr_remediation_url); 1642 } 1643 1644#ifdef CONFIG_SQLITE 1645 if (conf->sqlite_file) { 1646 if (sqlite3_open(conf->sqlite_file, &data->db)) { 1647 RADIUS_ERROR("Could not open SQLite file '%s'", 1648 conf->sqlite_file); 1649 radius_server_deinit(data); 1650 return NULL; 1651 } 1652 } 1653#endif /* CONFIG_SQLITE */ 1654 1655#ifdef CONFIG_RADIUS_TEST 1656 if (conf->dump_msk_file) 1657 data->dump_msk_file = os_strdup(conf->dump_msk_file); 1658#endif /* CONFIG_RADIUS_TEST */ 1659 1660 data->clients = radius_server_read_clients(conf->client_file, 1661 conf->ipv6); 1662 if (data->clients == NULL) { 1663 wpa_printf(MSG_ERROR, "No RADIUS clients configured"); 1664 radius_server_deinit(data); 1665 return NULL; 1666 } 1667 1668#ifdef CONFIG_IPV6 1669 if (conf->ipv6) 1670 data->auth_sock = radius_server_open_socket6(conf->auth_port); 1671 else 1672#endif /* CONFIG_IPV6 */ 1673 data->auth_sock = radius_server_open_socket(conf->auth_port); 1674 if (data->auth_sock < 0) { 1675 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server"); 1676 radius_server_deinit(data); 1677 return NULL; 1678 } 1679 if (eloop_register_read_sock(data->auth_sock, 1680 radius_server_receive_auth, 1681 data, NULL)) { 1682 radius_server_deinit(data); 1683 return NULL; 1684 } 1685 1686 if (conf->acct_port) { 1687#ifdef CONFIG_IPV6 1688 if (conf->ipv6) 1689 data->acct_sock = radius_server_open_socket6( 1690 conf->acct_port); 1691 else 1692#endif /* CONFIG_IPV6 */ 1693 data->acct_sock = radius_server_open_socket(conf->acct_port); 1694 if (data->acct_sock < 0) { 1695 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS accounting server"); 1696 radius_server_deinit(data); 1697 return NULL; 1698 } 1699 if (eloop_register_read_sock(data->acct_sock, 1700 radius_server_receive_acct, 1701 data, NULL)) { 1702 radius_server_deinit(data); 1703 return NULL; 1704 } 1705 } else { 1706 data->acct_sock = -1; 1707 } 1708 1709 return data; 1710} 1711 1712 1713/** 1714 * radius_server_deinit - Deinitialize RADIUS server 1715 * @data: RADIUS server context from radius_server_init() 1716 */ 1717void radius_server_deinit(struct radius_server_data *data) 1718{ 1719 if (data == NULL) 1720 return; 1721 1722 if (data->auth_sock >= 0) { 1723 eloop_unregister_read_sock(data->auth_sock); 1724 close(data->auth_sock); 1725 } 1726 1727 if (data->acct_sock >= 0) { 1728 eloop_unregister_read_sock(data->acct_sock); 1729 close(data->acct_sock); 1730 } 1731 1732 radius_server_free_clients(data, data->clients); 1733 1734 os_free(data->pac_opaque_encr_key); 1735 os_free(data->eap_fast_a_id); 1736 os_free(data->eap_fast_a_id_info); 1737 os_free(data->eap_req_id_text); 1738#ifdef CONFIG_RADIUS_TEST 1739 os_free(data->dump_msk_file); 1740#endif /* CONFIG_RADIUS_TEST */ 1741 os_free(data->subscr_remediation_url); 1742 1743#ifdef CONFIG_SQLITE 1744 if (data->db) 1745 sqlite3_close(data->db); 1746#endif /* CONFIG_SQLITE */ 1747 1748 os_free(data); 1749} 1750 1751 1752/** 1753 * radius_server_get_mib - Get RADIUS server MIB information 1754 * @data: RADIUS server context from radius_server_init() 1755 * @buf: Buffer for returning the MIB data in text format 1756 * @buflen: buf length in octets 1757 * Returns: Number of octets written into buf 1758 */ 1759int radius_server_get_mib(struct radius_server_data *data, char *buf, 1760 size_t buflen) 1761{ 1762 int ret, uptime; 1763 unsigned int idx; 1764 char *end, *pos; 1765 struct os_reltime now; 1766 struct radius_client *cli; 1767 1768 /* RFC 2619 - RADIUS Authentication Server MIB */ 1769 1770 if (data == NULL || buflen == 0) 1771 return 0; 1772 1773 pos = buf; 1774 end = buf + buflen; 1775 1776 os_get_reltime(&now); 1777 uptime = (now.sec - data->start_time.sec) * 100 + 1778 ((now.usec - data->start_time.usec) / 10000) % 100; 1779 ret = os_snprintf(pos, end - pos, 1780 "RADIUS-AUTH-SERVER-MIB\n" 1781 "radiusAuthServIdent=hostapd\n" 1782 "radiusAuthServUpTime=%d\n" 1783 "radiusAuthServResetTime=0\n" 1784 "radiusAuthServConfigReset=4\n", 1785 uptime); 1786 if (ret < 0 || ret >= end - pos) { 1787 *pos = '\0'; 1788 return pos - buf; 1789 } 1790 pos += ret; 1791 1792 ret = os_snprintf(pos, end - pos, 1793 "radiusAuthServTotalAccessRequests=%u\n" 1794 "radiusAuthServTotalInvalidRequests=%u\n" 1795 "radiusAuthServTotalDupAccessRequests=%u\n" 1796 "radiusAuthServTotalAccessAccepts=%u\n" 1797 "radiusAuthServTotalAccessRejects=%u\n" 1798 "radiusAuthServTotalAccessChallenges=%u\n" 1799 "radiusAuthServTotalMalformedAccessRequests=%u\n" 1800 "radiusAuthServTotalBadAuthenticators=%u\n" 1801 "radiusAuthServTotalPacketsDropped=%u\n" 1802 "radiusAuthServTotalUnknownTypes=%u\n" 1803 "radiusAccServTotalRequests=%u\n" 1804 "radiusAccServTotalInvalidRequests=%u\n" 1805 "radiusAccServTotalResponses=%u\n" 1806 "radiusAccServTotalMalformedRequests=%u\n" 1807 "radiusAccServTotalBadAuthenticators=%u\n" 1808 "radiusAccServTotalUnknownTypes=%u\n", 1809 data->counters.access_requests, 1810 data->counters.invalid_requests, 1811 data->counters.dup_access_requests, 1812 data->counters.access_accepts, 1813 data->counters.access_rejects, 1814 data->counters.access_challenges, 1815 data->counters.malformed_access_requests, 1816 data->counters.bad_authenticators, 1817 data->counters.packets_dropped, 1818 data->counters.unknown_types, 1819 data->counters.acct_requests, 1820 data->counters.invalid_acct_requests, 1821 data->counters.acct_responses, 1822 data->counters.malformed_acct_requests, 1823 data->counters.acct_bad_authenticators, 1824 data->counters.unknown_acct_types); 1825 if (ret < 0 || ret >= end - pos) { 1826 *pos = '\0'; 1827 return pos - buf; 1828 } 1829 pos += ret; 1830 1831 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) { 1832 char abuf[50], mbuf[50]; 1833#ifdef CONFIG_IPV6 1834 if (data->ipv6) { 1835 if (inet_ntop(AF_INET6, &cli->addr6, abuf, 1836 sizeof(abuf)) == NULL) 1837 abuf[0] = '\0'; 1838 if (inet_ntop(AF_INET6, &cli->mask6, abuf, 1839 sizeof(mbuf)) == NULL) 1840 mbuf[0] = '\0'; 1841 } 1842#endif /* CONFIG_IPV6 */ 1843 if (!data->ipv6) { 1844 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf)); 1845 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf)); 1846 } 1847 1848 ret = os_snprintf(pos, end - pos, 1849 "radiusAuthClientIndex=%u\n" 1850 "radiusAuthClientAddress=%s/%s\n" 1851 "radiusAuthServAccessRequests=%u\n" 1852 "radiusAuthServDupAccessRequests=%u\n" 1853 "radiusAuthServAccessAccepts=%u\n" 1854 "radiusAuthServAccessRejects=%u\n" 1855 "radiusAuthServAccessChallenges=%u\n" 1856 "radiusAuthServMalformedAccessRequests=%u\n" 1857 "radiusAuthServBadAuthenticators=%u\n" 1858 "radiusAuthServPacketsDropped=%u\n" 1859 "radiusAuthServUnknownTypes=%u\n" 1860 "radiusAccServTotalRequests=%u\n" 1861 "radiusAccServTotalInvalidRequests=%u\n" 1862 "radiusAccServTotalResponses=%u\n" 1863 "radiusAccServTotalMalformedRequests=%u\n" 1864 "radiusAccServTotalBadAuthenticators=%u\n" 1865 "radiusAccServTotalUnknownTypes=%u\n", 1866 idx, 1867 abuf, mbuf, 1868 cli->counters.access_requests, 1869 cli->counters.dup_access_requests, 1870 cli->counters.access_accepts, 1871 cli->counters.access_rejects, 1872 cli->counters.access_challenges, 1873 cli->counters.malformed_access_requests, 1874 cli->counters.bad_authenticators, 1875 cli->counters.packets_dropped, 1876 cli->counters.unknown_types, 1877 cli->counters.acct_requests, 1878 cli->counters.invalid_acct_requests, 1879 cli->counters.acct_responses, 1880 cli->counters.malformed_acct_requests, 1881 cli->counters.acct_bad_authenticators, 1882 cli->counters.unknown_acct_types); 1883 if (ret < 0 || ret >= end - pos) { 1884 *pos = '\0'; 1885 return pos - buf; 1886 } 1887 pos += ret; 1888 } 1889 1890 return pos - buf; 1891} 1892 1893 1894static int radius_server_get_eap_user(void *ctx, const u8 *identity, 1895 size_t identity_len, int phase2, 1896 struct eap_user *user) 1897{ 1898 struct radius_session *sess = ctx; 1899 struct radius_server_data *data = sess->server; 1900 int ret; 1901 1902 ret = data->get_eap_user(data->conf_ctx, identity, identity_len, 1903 phase2, user); 1904 if (ret == 0 && user) { 1905 sess->accept_attr = user->accept_attr; 1906 sess->remediation = user->remediation; 1907 } 1908 return ret; 1909} 1910 1911 1912static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len) 1913{ 1914 struct radius_session *sess = ctx; 1915 struct radius_server_data *data = sess->server; 1916 *len = data->eap_req_id_text_len; 1917 return data->eap_req_id_text; 1918} 1919 1920 1921static void radius_server_log_msg(void *ctx, const char *msg) 1922{ 1923 struct radius_session *sess = ctx; 1924 srv_log(sess, "EAP: %s", msg); 1925} 1926 1927 1928static struct eapol_callbacks radius_server_eapol_cb = 1929{ 1930 .get_eap_user = radius_server_get_eap_user, 1931 .get_eap_req_id_text = radius_server_get_eap_req_id_text, 1932 .log_msg = radius_server_log_msg, 1933}; 1934 1935 1936/** 1937 * radius_server_eap_pending_cb - Pending EAP data notification 1938 * @data: RADIUS server context from radius_server_init() 1939 * @ctx: Pending EAP context pointer 1940 * 1941 * This function is used to notify EAP server module that a pending operation 1942 * has been completed and processing of the EAP session can proceed. 1943 */ 1944void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx) 1945{ 1946 struct radius_client *cli; 1947 struct radius_session *s, *sess = NULL; 1948 struct radius_msg *msg; 1949 1950 if (data == NULL) 1951 return; 1952 1953 for (cli = data->clients; cli; cli = cli->next) { 1954 for (s = cli->sessions; s; s = s->next) { 1955 if (s->eap == ctx && s->last_msg) { 1956 sess = s; 1957 break; 1958 } 1959 if (sess) 1960 break; 1961 } 1962 if (sess) 1963 break; 1964 } 1965 1966 if (sess == NULL) { 1967 RADIUS_DEBUG("No session matched callback ctx"); 1968 return; 1969 } 1970 1971 msg = sess->last_msg; 1972 sess->last_msg = NULL; 1973 eap_sm_pending_cb(sess->eap); 1974 if (radius_server_request(data, msg, 1975 (struct sockaddr *) &sess->last_from, 1976 sess->last_fromlen, cli, 1977 sess->last_from_addr, 1978 sess->last_from_port, sess) == -2) 1979 return; /* msg was stored with the session */ 1980 1981 radius_msg_free(msg); 1982} 1983