radius_server.c 53 KB

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