hostapd.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <syslog.h>
  17. #endif /* CONFIG_NATIVE_WINDOWS */
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_1x.h"
  21. #include "beacon.h"
  22. #include "hw_features.h"
  23. #include "accounting.h"
  24. #include "eapol_sm.h"
  25. #include "iapp.h"
  26. #include "ap.h"
  27. #include "ieee802_11_auth.h"
  28. #include "ap_list.h"
  29. #include "sta_info.h"
  30. #include "driver_i.h"
  31. #include "radius/radius_client.h"
  32. #include "radius/radius_server.h"
  33. #include "wpa.h"
  34. #include "preauth.h"
  35. #include "vlan_init.h"
  36. #include "ctrl_iface.h"
  37. #include "tls.h"
  38. #include "eap_server/eap_sim_db.h"
  39. #include "eap_server/eap.h"
  40. #include "eap_server/tncs.h"
  41. #include "version.h"
  42. #include "l2_packet/l2_packet.h"
  43. #include "wps_hostapd.h"
  44. #include "tkip_countermeasures.h"
  45. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  46. size_t identity_len, int phase2,
  47. struct eap_user *user);
  48. static int hostapd_flush_old_stations(struct hostapd_data *hapd);
  49. static int hostapd_setup_wpa(struct hostapd_data *hapd);
  50. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  51. struct hapd_interfaces {
  52. size_t count;
  53. struct hostapd_iface **iface;
  54. };
  55. extern int wpa_debug_level;
  56. extern int wpa_debug_show_keys;
  57. extern int wpa_debug_timestamp;
  58. int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
  59. void *ctx), void *ctx)
  60. {
  61. struct hapd_interfaces *interfaces = eloop_get_user_data();
  62. size_t i;
  63. int ret;
  64. for (i = 0; i < interfaces->count; i++) {
  65. ret = cb(interfaces->iface[i], ctx);
  66. if (ret)
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  72. int level, const char *txt, size_t len)
  73. {
  74. struct hostapd_data *hapd = ctx;
  75. char *format, *module_str;
  76. int maxlen;
  77. int conf_syslog_level, conf_stdout_level;
  78. unsigned int conf_syslog, conf_stdout;
  79. maxlen = len + 100;
  80. format = os_malloc(maxlen);
  81. if (!format)
  82. return;
  83. if (hapd && hapd->conf) {
  84. conf_syslog_level = hapd->conf->logger_syslog_level;
  85. conf_stdout_level = hapd->conf->logger_stdout_level;
  86. conf_syslog = hapd->conf->logger_syslog;
  87. conf_stdout = hapd->conf->logger_stdout;
  88. } else {
  89. conf_syslog_level = conf_stdout_level = 0;
  90. conf_syslog = conf_stdout = (unsigned int) -1;
  91. }
  92. switch (module) {
  93. case HOSTAPD_MODULE_IEEE80211:
  94. module_str = "IEEE 802.11";
  95. break;
  96. case HOSTAPD_MODULE_IEEE8021X:
  97. module_str = "IEEE 802.1X";
  98. break;
  99. case HOSTAPD_MODULE_RADIUS:
  100. module_str = "RADIUS";
  101. break;
  102. case HOSTAPD_MODULE_WPA:
  103. module_str = "WPA";
  104. break;
  105. case HOSTAPD_MODULE_DRIVER:
  106. module_str = "DRIVER";
  107. break;
  108. case HOSTAPD_MODULE_IAPP:
  109. module_str = "IAPP";
  110. break;
  111. case HOSTAPD_MODULE_MLME:
  112. module_str = "MLME";
  113. break;
  114. default:
  115. module_str = NULL;
  116. break;
  117. }
  118. if (hapd && hapd->conf && addr)
  119. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  120. hapd->conf->iface, MAC2STR(addr),
  121. module_str ? " " : "", module_str, txt);
  122. else if (hapd && hapd->conf)
  123. os_snprintf(format, maxlen, "%s:%s%s %s",
  124. hapd->conf->iface, module_str ? " " : "",
  125. module_str, txt);
  126. else if (addr)
  127. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  128. MAC2STR(addr), module_str ? " " : "",
  129. module_str, txt);
  130. else
  131. os_snprintf(format, maxlen, "%s%s%s",
  132. module_str, module_str ? ": " : "", txt);
  133. if ((conf_stdout & module) && level >= conf_stdout_level) {
  134. wpa_debug_print_timestamp();
  135. printf("%s\n", format);
  136. }
  137. #ifndef CONFIG_NATIVE_WINDOWS
  138. if ((conf_syslog & module) && level >= conf_syslog_level) {
  139. int priority;
  140. switch (level) {
  141. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  142. case HOSTAPD_LEVEL_DEBUG:
  143. priority = LOG_DEBUG;
  144. break;
  145. case HOSTAPD_LEVEL_INFO:
  146. priority = LOG_INFO;
  147. break;
  148. case HOSTAPD_LEVEL_NOTICE:
  149. priority = LOG_NOTICE;
  150. break;
  151. case HOSTAPD_LEVEL_WARNING:
  152. priority = LOG_WARNING;
  153. break;
  154. default:
  155. priority = LOG_INFO;
  156. break;
  157. }
  158. syslog(priority, "%s", format);
  159. }
  160. #endif /* CONFIG_NATIVE_WINDOWS */
  161. os_free(format);
  162. }
  163. #ifdef EAP_SERVER
  164. static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
  165. struct sta_info *sta, void *ctx)
  166. {
  167. if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
  168. return 1;
  169. return 0;
  170. }
  171. static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
  172. {
  173. struct hostapd_data *hapd = ctx;
  174. if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
  175. radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
  176. }
  177. #endif /* EAP_SERVER */
  178. /**
  179. * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
  180. */
  181. static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
  182. {
  183. wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
  184. eloop_terminate();
  185. }
  186. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  187. struct wpa_auth_config *wconf)
  188. {
  189. wconf->wpa = conf->wpa;
  190. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  191. wconf->wpa_pairwise = conf->wpa_pairwise;
  192. wconf->wpa_group = conf->wpa_group;
  193. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  194. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  195. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  196. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  197. wconf->rsn_pairwise = conf->rsn_pairwise;
  198. wconf->rsn_preauth = conf->rsn_preauth;
  199. wconf->eapol_version = conf->eapol_version;
  200. wconf->peerkey = conf->peerkey;
  201. wconf->wme_enabled = conf->wme_enabled;
  202. wconf->okc = conf->okc;
  203. #ifdef CONFIG_IEEE80211W
  204. wconf->ieee80211w = conf->ieee80211w;
  205. #endif /* CONFIG_IEEE80211W */
  206. #ifdef CONFIG_IEEE80211R
  207. wconf->ssid_len = conf->ssid.ssid_len;
  208. if (wconf->ssid_len > SSID_LEN)
  209. wconf->ssid_len = SSID_LEN;
  210. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  211. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  212. MOBILITY_DOMAIN_ID_LEN);
  213. if (conf->nas_identifier &&
  214. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  215. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  216. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  217. wconf->r0_key_holder_len);
  218. }
  219. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  220. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  221. wconf->reassociation_deadline = conf->reassociation_deadline;
  222. wconf->r0kh_list = conf->r0kh_list;
  223. wconf->r1kh_list = conf->r1kh_list;
  224. wconf->pmk_r1_push = conf->pmk_r1_push;
  225. #endif /* CONFIG_IEEE80211R */
  226. }
  227. int hostapd_reload_config(struct hostapd_iface *iface)
  228. {
  229. struct hostapd_data *hapd = iface->bss[0];
  230. struct hostapd_config *newconf, *oldconf;
  231. struct wpa_auth_config wpa_auth_conf;
  232. newconf = hostapd_config_read(iface->config_fname);
  233. if (newconf == NULL)
  234. return -1;
  235. /*
  236. * Deauthenticate all stations since the new configuration may not
  237. * allow them to use the BSS anymore.
  238. */
  239. hostapd_flush_old_stations(hapd);
  240. /* TODO: update dynamic data based on changed configuration
  241. * items (e.g., open/close sockets, etc.) */
  242. radius_client_flush(hapd->radius, 0);
  243. oldconf = hapd->iconf;
  244. hapd->iconf = newconf;
  245. hapd->conf = &newconf->bss[0];
  246. iface->conf = newconf;
  247. if (hostapd_setup_wpa_psk(hapd->conf)) {
  248. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  249. "after reloading configuration");
  250. }
  251. if (hapd->conf->wpa && hapd->wpa_auth == NULL)
  252. hostapd_setup_wpa(hapd);
  253. else if (hapd->conf->wpa) {
  254. hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
  255. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  256. } else if (hapd->wpa_auth) {
  257. wpa_deinit(hapd->wpa_auth);
  258. hapd->wpa_auth = NULL;
  259. hostapd_set_privacy(hapd, 0);
  260. hostapd_setup_encryption(hapd->conf->iface, hapd);
  261. }
  262. ieee802_11_set_beacon(hapd);
  263. hostapd_config_free(oldconf);
  264. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  265. return 0;
  266. }
  267. #ifndef CONFIG_NATIVE_WINDOWS
  268. /**
  269. * handle_reload - SIGHUP handler to reload configuration
  270. */
  271. static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
  272. {
  273. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  274. size_t i;
  275. wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
  276. sig);
  277. for (i = 0; i < hapds->count; i++) {
  278. if (hostapd_reload_config(hapds->iface[i]) < 0) {
  279. wpa_printf(MSG_WARNING, "Failed to read new "
  280. "configuration file - continuing with "
  281. "old.");
  282. continue;
  283. }
  284. }
  285. }
  286. #ifdef HOSTAPD_DUMP_STATE
  287. /**
  288. * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
  289. */
  290. static void hostapd_dump_state(struct hostapd_data *hapd)
  291. {
  292. FILE *f;
  293. time_t now;
  294. struct sta_info *sta;
  295. int i;
  296. char *buf;
  297. if (!hapd->conf->dump_log_name) {
  298. wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
  299. "request");
  300. return;
  301. }
  302. wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
  303. hapd->conf->dump_log_name);
  304. f = fopen(hapd->conf->dump_log_name, "w");
  305. if (f == NULL) {
  306. wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
  307. "writing.", hapd->conf->dump_log_name);
  308. return;
  309. }
  310. time(&now);
  311. fprintf(f, "hostapd state dump - %s", ctime(&now));
  312. fprintf(f, "num_sta=%d num_sta_non_erp=%d "
  313. "num_sta_no_short_slot_time=%d\n"
  314. "num_sta_no_short_preamble=%d\n",
  315. hapd->num_sta, hapd->iface->num_sta_non_erp,
  316. hapd->iface->num_sta_no_short_slot_time,
  317. hapd->iface->num_sta_no_short_preamble);
  318. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  319. fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
  320. fprintf(f,
  321. " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
  322. " capability=0x%x listen_interval=%d\n",
  323. sta->aid,
  324. sta->flags,
  325. (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  326. (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  327. (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
  328. (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
  329. (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
  330. (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
  331. ""),
  332. (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  333. ""),
  334. (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
  335. "[SHORT_PREAMBLE]" : ""),
  336. (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  337. (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
  338. (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
  339. (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
  340. (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
  341. (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  342. sta->capability,
  343. sta->listen_interval);
  344. fprintf(f, " supported_rates=");
  345. for (i = 0; i < sta->supported_rates_len; i++)
  346. fprintf(f, "%02x ", sta->supported_rates[i]);
  347. fprintf(f, "\n");
  348. fprintf(f,
  349. " timeout_next=%s\n",
  350. (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
  351. (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
  352. "DEAUTH")));
  353. ieee802_1x_dump_state(f, " ", sta);
  354. }
  355. buf = os_malloc(4096);
  356. if (buf) {
  357. int count = radius_client_get_mib(hapd->radius, buf, 4096);
  358. if (count < 0)
  359. count = 0;
  360. else if (count > 4095)
  361. count = 4095;
  362. buf[count] = '\0';
  363. fprintf(f, "%s", buf);
  364. count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
  365. if (count < 0)
  366. count = 0;
  367. else if (count > 4095)
  368. count = 4095;
  369. buf[count] = '\0';
  370. fprintf(f, "%s", buf);
  371. os_free(buf);
  372. }
  373. fclose(f);
  374. }
  375. #endif /* HOSTAPD_DUMP_STATE */
  376. static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
  377. {
  378. #ifdef HOSTAPD_DUMP_STATE
  379. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  380. size_t i, j;
  381. for (i = 0; i < hapds->count; i++) {
  382. struct hostapd_iface *hapd_iface = hapds->iface[i];
  383. for (j = 0; j < hapd_iface->num_bss; j++)
  384. hostapd_dump_state(hapd_iface->bss[j]);
  385. }
  386. #endif /* HOSTAPD_DUMP_STATE */
  387. }
  388. #endif /* CONFIG_NATIVE_WINDOWS */
  389. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  390. char *ifname)
  391. {
  392. int i;
  393. for (i = 0; i < NUM_WEP_KEYS; i++) {
  394. if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
  395. 0, i == 0 ? 1 : 0)) {
  396. wpa_printf(MSG_DEBUG, "Failed to clear default "
  397. "encryption keys (ifname=%s keyidx=%d)",
  398. ifname, i);
  399. }
  400. }
  401. #ifdef CONFIG_IEEE80211W
  402. if (hapd->conf->ieee80211w) {
  403. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  404. if (hostapd_set_encryption(ifname, hapd, "none", NULL,
  405. i, NULL, 0,
  406. i == 0 ? 1 : 0)) {
  407. wpa_printf(MSG_DEBUG, "Failed to clear "
  408. "default mgmt encryption keys "
  409. "(ifname=%s keyidx=%d)", ifname, i);
  410. }
  411. }
  412. }
  413. #endif /* CONFIG_IEEE80211W */
  414. }
  415. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  416. {
  417. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  418. return 0;
  419. }
  420. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  421. {
  422. int errors = 0, idx;
  423. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  424. idx = ssid->wep.idx;
  425. if (ssid->wep.default_len &&
  426. hostapd_set_encryption(hapd->conf->iface,
  427. hapd, "WEP", NULL, idx,
  428. ssid->wep.key[idx],
  429. ssid->wep.len[idx],
  430. idx == ssid->wep.idx)) {
  431. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  432. errors++;
  433. }
  434. if (ssid->dyn_vlan_keys) {
  435. size_t i;
  436. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  437. const char *ifname;
  438. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  439. if (key == NULL)
  440. continue;
  441. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  442. i);
  443. if (ifname == NULL)
  444. continue;
  445. idx = key->idx;
  446. if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
  447. idx, key->key[idx],
  448. key->len[idx],
  449. idx == key->idx)) {
  450. wpa_printf(MSG_WARNING, "Could not set "
  451. "dynamic VLAN WEP encryption.");
  452. errors++;
  453. }
  454. }
  455. }
  456. return errors;
  457. }
  458. /**
  459. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  460. * @hapd: Pointer to BSS data
  461. *
  462. * This function is used to free all per-BSS data structures and resources.
  463. * This gets called in a loop for each BSS between calls to
  464. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  465. * is deinitialized. Most of the modules that are initialized in
  466. * hostapd_setup_bss() are deinitialized here.
  467. */
  468. static void hostapd_cleanup(struct hostapd_data *hapd)
  469. {
  470. hostapd_ctrl_iface_deinit(hapd);
  471. os_free(hapd->default_wep_key);
  472. hapd->default_wep_key = NULL;
  473. iapp_deinit(hapd->iapp);
  474. hapd->iapp = NULL;
  475. accounting_deinit(hapd);
  476. rsn_preauth_iface_deinit(hapd);
  477. if (hapd->wpa_auth) {
  478. wpa_deinit(hapd->wpa_auth);
  479. hapd->wpa_auth = NULL;
  480. if (hostapd_set_privacy(hapd, 0)) {
  481. wpa_printf(MSG_DEBUG, "Could not disable "
  482. "PrivacyInvoked for interface %s",
  483. hapd->conf->iface);
  484. }
  485. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  486. wpa_printf(MSG_DEBUG, "Could not remove generic "
  487. "information element from interface %s",
  488. hapd->conf->iface);
  489. }
  490. }
  491. ieee802_1x_deinit(hapd);
  492. vlan_deinit(hapd);
  493. hostapd_acl_deinit(hapd);
  494. radius_client_deinit(hapd->radius);
  495. hapd->radius = NULL;
  496. radius_server_deinit(hapd->radius_srv);
  497. hapd->radius_srv = NULL;
  498. #ifdef CONFIG_IEEE80211R
  499. l2_packet_deinit(hapd->l2);
  500. #endif /* CONFIG_IEEE80211R */
  501. hostapd_deinit_wps(hapd);
  502. hostapd_wireless_event_deinit(hapd);
  503. #ifdef EAP_TLS_FUNCS
  504. if (hapd->ssl_ctx) {
  505. tls_deinit(hapd->ssl_ctx);
  506. hapd->ssl_ctx = NULL;
  507. }
  508. #endif /* EAP_TLS_FUNCS */
  509. #ifdef EAP_SERVER
  510. if (hapd->eap_sim_db_priv) {
  511. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  512. hapd->eap_sim_db_priv = NULL;
  513. }
  514. #endif /* EAP_SERVER */
  515. if (hapd->interface_added &&
  516. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  517. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  518. hapd->conf->iface);
  519. }
  520. }
  521. /**
  522. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  523. * @iface: Pointer to interface data
  524. *
  525. * This function is called before per-BSS data structures are deinitialized
  526. * with hostapd_cleanup().
  527. */
  528. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  529. {
  530. }
  531. /**
  532. * hostapd_cleanup_iface - Complete per-interface cleanup
  533. * @iface: Pointer to interface data
  534. *
  535. * This function is called after per-BSS data structures are deinitialized
  536. * with hostapd_cleanup().
  537. */
  538. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  539. {
  540. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  541. iface->hw_features = NULL;
  542. os_free(iface->current_rates);
  543. iface->current_rates = NULL;
  544. ap_list_deinit(iface);
  545. hostapd_config_free(iface->conf);
  546. iface->conf = NULL;
  547. os_free(iface->config_fname);
  548. os_free(iface->bss);
  549. os_free(iface);
  550. }
  551. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  552. {
  553. int i;
  554. hostapd_broadcast_wep_set(hapd);
  555. if (hapd->conf->ssid.wep.default_len)
  556. return 0;
  557. for (i = 0; i < 4; i++) {
  558. if (hapd->conf->ssid.wep.key[i] &&
  559. hostapd_set_encryption(iface, hapd, "WEP", NULL,
  560. i, hapd->conf->ssid.wep.key[i],
  561. hapd->conf->ssid.wep.len[i],
  562. i == hapd->conf->ssid.wep.idx)) {
  563. wpa_printf(MSG_WARNING, "Could not set WEP "
  564. "encryption.");
  565. return -1;
  566. }
  567. if (hapd->conf->ssid.wep.key[i] &&
  568. i == hapd->conf->ssid.wep.idx)
  569. hostapd_set_privacy(hapd, 1);
  570. }
  571. return 0;
  572. }
  573. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  574. {
  575. int ret = 0;
  576. if (hostapd_drv_none(hapd))
  577. return 0;
  578. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  579. if (hostapd_flush(hapd)) {
  580. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  581. ret = -1;
  582. }
  583. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  584. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  585. * broadcast deauth frame. This gets the firmware in odd state where
  586. * nothing works correctly, so let's skip sending this for the hostap
  587. * driver. */
  588. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  589. u8 addr[ETH_ALEN];
  590. os_memset(addr, 0xff, ETH_ALEN);
  591. hostapd_sta_deauth(hapd, addr,
  592. WLAN_REASON_PREV_AUTH_NOT_VALID);
  593. }
  594. return ret;
  595. }
  596. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  597. logger_level level, const char *txt)
  598. {
  599. struct hostapd_data *hapd = ctx;
  600. int hlevel;
  601. switch (level) {
  602. case LOGGER_WARNING:
  603. hlevel = HOSTAPD_LEVEL_WARNING;
  604. break;
  605. case LOGGER_INFO:
  606. hlevel = HOSTAPD_LEVEL_INFO;
  607. break;
  608. case LOGGER_DEBUG:
  609. default:
  610. hlevel = HOSTAPD_LEVEL_DEBUG;
  611. break;
  612. }
  613. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  614. }
  615. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  616. u16 reason)
  617. {
  618. struct hostapd_data *hapd = ctx;
  619. struct sta_info *sta;
  620. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  621. "STA " MACSTR " reason %d",
  622. __func__, MAC2STR(addr), reason);
  623. sta = ap_get_sta(hapd, addr);
  624. hostapd_sta_deauth(hapd, addr, reason);
  625. if (sta == NULL)
  626. return;
  627. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  628. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  629. eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
  630. sta->timeout_next = STA_REMOVE;
  631. }
  632. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  633. {
  634. struct hostapd_data *hapd = ctx;
  635. michael_mic_failure(hapd, addr, 0);
  636. }
  637. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  638. wpa_eapol_variable var, int value)
  639. {
  640. struct hostapd_data *hapd = ctx;
  641. struct sta_info *sta = ap_get_sta(hapd, addr);
  642. if (sta == NULL)
  643. return;
  644. switch (var) {
  645. case WPA_EAPOL_portEnabled:
  646. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  647. break;
  648. case WPA_EAPOL_portValid:
  649. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  650. break;
  651. case WPA_EAPOL_authorized:
  652. ieee802_1x_set_sta_authorized(hapd, sta, value);
  653. break;
  654. case WPA_EAPOL_portControl_Auto:
  655. if (sta->eapol_sm)
  656. sta->eapol_sm->portControl = Auto;
  657. break;
  658. case WPA_EAPOL_keyRun:
  659. if (sta->eapol_sm)
  660. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  661. break;
  662. case WPA_EAPOL_keyAvailable:
  663. if (sta->eapol_sm)
  664. sta->eapol_sm->eap_if->eapKeyAvailable =
  665. value ? TRUE : FALSE;
  666. break;
  667. case WPA_EAPOL_keyDone:
  668. if (sta->eapol_sm)
  669. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  670. break;
  671. case WPA_EAPOL_inc_EapolFramesTx:
  672. if (sta->eapol_sm)
  673. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  674. break;
  675. }
  676. }
  677. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  678. wpa_eapol_variable var)
  679. {
  680. struct hostapd_data *hapd = ctx;
  681. struct sta_info *sta = ap_get_sta(hapd, addr);
  682. if (sta == NULL || sta->eapol_sm == NULL)
  683. return -1;
  684. switch (var) {
  685. case WPA_EAPOL_keyRun:
  686. return sta->eapol_sm->keyRun;
  687. case WPA_EAPOL_keyAvailable:
  688. return sta->eapol_sm->eap_if->eapKeyAvailable;
  689. default:
  690. return -1;
  691. }
  692. }
  693. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  694. const u8 *prev_psk)
  695. {
  696. struct hostapd_data *hapd = ctx;
  697. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  698. }
  699. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  700. size_t *len)
  701. {
  702. struct hostapd_data *hapd = ctx;
  703. const u8 *key;
  704. size_t keylen;
  705. struct sta_info *sta;
  706. sta = ap_get_sta(hapd, addr);
  707. if (sta == NULL)
  708. return -1;
  709. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  710. if (key == NULL)
  711. return -1;
  712. if (keylen > *len)
  713. keylen = *len;
  714. os_memcpy(msk, key, keylen);
  715. *len = keylen;
  716. return 0;
  717. }
  718. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
  719. const u8 *addr, int idx, u8 *key,
  720. size_t key_len)
  721. {
  722. struct hostapd_data *hapd = ctx;
  723. const char *ifname = hapd->conf->iface;
  724. if (vlan_id > 0) {
  725. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  726. if (ifname == NULL)
  727. return -1;
  728. }
  729. return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
  730. key, key_len, 1);
  731. }
  732. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  733. u8 *seq)
  734. {
  735. struct hostapd_data *hapd = ctx;
  736. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  737. }
  738. static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
  739. u8 *seq)
  740. {
  741. struct hostapd_data *hapd = ctx;
  742. return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
  743. seq);
  744. }
  745. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  746. const u8 *data, size_t data_len,
  747. int encrypt)
  748. {
  749. struct hostapd_data *hapd = ctx;
  750. return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
  751. }
  752. static int hostapd_wpa_auth_for_each_sta(
  753. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  754. void *cb_ctx)
  755. {
  756. struct hostapd_data *hapd = ctx;
  757. struct sta_info *sta;
  758. for (sta = hapd->sta_list; sta; sta = sta->next) {
  759. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  760. return 1;
  761. }
  762. return 0;
  763. }
  764. static int hostapd_wpa_auth_for_each_auth(
  765. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  766. void *cb_ctx)
  767. {
  768. struct hostapd_data *ohapd;
  769. size_t i, j;
  770. struct hapd_interfaces *interfaces = eloop_get_user_data();
  771. for (i = 0; i < interfaces->count; i++) {
  772. for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
  773. ohapd = interfaces->iface[i]->bss[j];
  774. if (cb(ohapd->wpa_auth, cb_ctx))
  775. return 1;
  776. }
  777. }
  778. return 0;
  779. }
  780. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  781. const u8 *data, size_t data_len)
  782. {
  783. struct hostapd_data *hapd = ctx;
  784. if (hapd->driver && hapd->driver->send_ether)
  785. return hapd->driver->send_ether(hapd->drv_priv, dst,
  786. hapd->own_addr, proto,
  787. data, data_len);
  788. if (hapd->l2 == NULL)
  789. return -1;
  790. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  791. }
  792. #ifdef CONFIG_IEEE80211R
  793. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  794. const u8 *data, size_t data_len)
  795. {
  796. struct hostapd_data *hapd = ctx;
  797. int res;
  798. struct ieee80211_mgmt *m;
  799. size_t mlen;
  800. struct sta_info *sta;
  801. sta = ap_get_sta(hapd, dst);
  802. if (sta == NULL || sta->wpa_sm == NULL)
  803. return -1;
  804. m = os_zalloc(sizeof(*m) + data_len);
  805. if (m == NULL)
  806. return -1;
  807. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  808. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  809. WLAN_FC_STYPE_ACTION);
  810. os_memcpy(m->da, dst, ETH_ALEN);
  811. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  812. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  813. os_memcpy(&m->u, data, data_len);
  814. res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
  815. os_free(m);
  816. return res;
  817. }
  818. static struct wpa_state_machine *
  819. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  820. {
  821. struct hostapd_data *hapd = ctx;
  822. struct sta_info *sta;
  823. sta = ap_sta_add(hapd, sta_addr);
  824. if (sta == NULL)
  825. return NULL;
  826. if (sta->wpa_sm)
  827. return sta->wpa_sm;
  828. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  829. if (sta->wpa_sm == NULL) {
  830. ap_free_sta(hapd, sta);
  831. return NULL;
  832. }
  833. sta->auth_alg = WLAN_AUTH_FT;
  834. return sta->wpa_sm;
  835. }
  836. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  837. size_t len)
  838. {
  839. struct hostapd_data *hapd = ctx;
  840. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  841. }
  842. #endif /* CONFIG_IEEE80211R */
  843. /**
  844. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  845. * @iface: Pointer to interface data
  846. * Returns: 0 on success, -1 on failure
  847. *
  848. * This function is used to validate that the configured BSSIDs are valid.
  849. */
  850. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  851. {
  852. u8 mask[ETH_ALEN] = { 0 };
  853. struct hostapd_data *hapd = iface->bss[0];
  854. unsigned int i = iface->conf->num_bss, bits = 0, j;
  855. int res;
  856. if (hostapd_drv_none(hapd))
  857. return 0;
  858. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  859. /* Determine the bits necessary to cover the number of BSSIDs. */
  860. for (i--; i; i >>= 1)
  861. bits++;
  862. /* Determine the bits necessary to any configured BSSIDs,
  863. if they are higher than the number of BSSIDs. */
  864. for (j = 0; j < iface->conf->num_bss; j++) {
  865. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
  866. continue;
  867. for (i = 0; i < ETH_ALEN; i++) {
  868. mask[i] |=
  869. iface->conf->bss[j].bssid[i] ^
  870. hapd->own_addr[i];
  871. }
  872. }
  873. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  874. ;
  875. j = 0;
  876. if (i < ETH_ALEN) {
  877. j = (5 - i) * 8;
  878. while (mask[i] != 0) {
  879. mask[i] >>= 1;
  880. j++;
  881. }
  882. }
  883. if (bits < j)
  884. bits = j;
  885. if (bits > 40)
  886. return -1;
  887. os_memset(mask, 0xff, ETH_ALEN);
  888. j = bits / 8;
  889. for (i = 5; i > 5 - j; i--)
  890. mask[i] = 0;
  891. j = bits % 8;
  892. while (j--)
  893. mask[i] <<= 1;
  894. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  895. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  896. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  897. if (res == 0)
  898. return 0;
  899. if (res < 0) {
  900. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  901. MACSTR " for start address " MACSTR ".",
  902. MAC2STR(mask), MAC2STR(hapd->own_addr));
  903. return -1;
  904. }
  905. for (i = 0; i < ETH_ALEN; i++) {
  906. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  907. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  908. " for start address " MACSTR ".",
  909. MAC2STR(mask), MAC2STR(hapd->own_addr));
  910. wpa_printf(MSG_ERROR, "Start address must be the "
  911. "first address in the block (i.e., addr "
  912. "AND mask == addr).");
  913. return -1;
  914. }
  915. }
  916. return 0;
  917. }
  918. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  919. {
  920. size_t i;
  921. for (i = 0; i < conf->num_bss; i++) {
  922. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  923. return 1;
  924. }
  925. }
  926. return 0;
  927. }
  928. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  929. {
  930. struct wpa_auth_config _conf;
  931. struct wpa_auth_callbacks cb;
  932. const u8 *wpa_ie;
  933. size_t wpa_ie_len;
  934. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  935. os_memset(&cb, 0, sizeof(cb));
  936. cb.ctx = hapd;
  937. cb.logger = hostapd_wpa_auth_logger;
  938. cb.disconnect = hostapd_wpa_auth_disconnect;
  939. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  940. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  941. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  942. cb.get_psk = hostapd_wpa_auth_get_psk;
  943. cb.get_msk = hostapd_wpa_auth_get_msk;
  944. cb.set_key = hostapd_wpa_auth_set_key;
  945. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  946. cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
  947. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  948. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  949. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  950. cb.send_ether = hostapd_wpa_auth_send_ether;
  951. #ifdef CONFIG_IEEE80211R
  952. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  953. cb.add_sta = hostapd_wpa_auth_add_sta;
  954. #endif /* CONFIG_IEEE80211R */
  955. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  956. if (hapd->wpa_auth == NULL) {
  957. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  958. return -1;
  959. }
  960. if (hostapd_set_privacy(hapd, 1)) {
  961. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  962. "for interface %s", hapd->conf->iface);
  963. return -1;
  964. }
  965. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  966. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  967. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  968. "the kernel driver.");
  969. return -1;
  970. }
  971. if (rsn_preauth_iface_init(hapd)) {
  972. wpa_printf(MSG_ERROR, "Initialization of RSN "
  973. "pre-authentication failed.");
  974. return -1;
  975. }
  976. return 0;
  977. }
  978. static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
  979. struct hostapd_bss_config *conf)
  980. {
  981. struct radius_server_conf srv;
  982. os_memset(&srv, 0, sizeof(srv));
  983. srv.client_file = conf->radius_server_clients;
  984. srv.auth_port = conf->radius_server_auth_port;
  985. srv.conf_ctx = conf;
  986. srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
  987. srv.ssl_ctx = hapd->ssl_ctx;
  988. srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
  989. srv.eap_fast_a_id = conf->eap_fast_a_id;
  990. srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
  991. srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
  992. srv.eap_fast_prov = conf->eap_fast_prov;
  993. srv.pac_key_lifetime = conf->pac_key_lifetime;
  994. srv.pac_key_refresh_time = conf->pac_key_refresh_time;
  995. srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  996. srv.tnc = conf->tnc;
  997. srv.wps = hapd->wps;
  998. srv.ipv6 = conf->radius_server_ipv6;
  999. srv.get_eap_user = hostapd_radius_get_eap_user;
  1000. srv.eap_req_id_text = conf->eap_req_id_text;
  1001. srv.eap_req_id_text_len = conf->eap_req_id_text_len;
  1002. hapd->radius_srv = radius_server_init(&srv);
  1003. if (hapd->radius_srv == NULL) {
  1004. wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
  1005. return -1;
  1006. }
  1007. return 0;
  1008. }
  1009. /**
  1010. * hostapd_setup_bss - Per-BSS setup (initialization)
  1011. * @hapd: Pointer to BSS data
  1012. * @first: Whether this BSS is the first BSS of an interface
  1013. *
  1014. * This function is used to initialize all per-BSS data structures and
  1015. * resources. This gets called in a loop for each BSS when an interface is
  1016. * initialized. Most of the modules that are initialized here will be
  1017. * deinitialized in hostapd_cleanup().
  1018. */
  1019. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  1020. {
  1021. struct hostapd_bss_config *conf = hapd->conf;
  1022. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  1023. int ssid_len, set_ssid;
  1024. if (!first) {
  1025. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  1026. /* Allocate the next available BSSID. */
  1027. do {
  1028. inc_byte_array(hapd->own_addr, ETH_ALEN);
  1029. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  1030. } else {
  1031. /* Allocate the configured BSSID. */
  1032. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  1033. if (hostapd_mac_comp(hapd->own_addr,
  1034. hapd->iface->bss[0]->own_addr) ==
  1035. 0) {
  1036. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  1037. "BSSID set to the MAC address of "
  1038. "the radio", hapd->conf->iface);
  1039. return -1;
  1040. }
  1041. }
  1042. hapd->interface_added = 1;
  1043. if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
  1044. hapd->own_addr)) {
  1045. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  1046. MACSTR ")", MAC2STR(hapd->own_addr));
  1047. return -1;
  1048. }
  1049. }
  1050. /*
  1051. * Fetch the SSID from the system and use it or,
  1052. * if one was specified in the config file, verify they
  1053. * match.
  1054. */
  1055. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  1056. if (ssid_len < 0) {
  1057. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  1058. return -1;
  1059. }
  1060. if (conf->ssid.ssid_set) {
  1061. /*
  1062. * If SSID is specified in the config file and it differs
  1063. * from what is being used then force installation of the
  1064. * new SSID.
  1065. */
  1066. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  1067. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  1068. } else {
  1069. /*
  1070. * No SSID in the config file; just use the one we got
  1071. * from the system.
  1072. */
  1073. set_ssid = 0;
  1074. conf->ssid.ssid_len = ssid_len;
  1075. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  1076. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  1077. }
  1078. if (!hostapd_drv_none(hapd)) {
  1079. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  1080. " and ssid '%s'",
  1081. hapd->conf->iface, MAC2STR(hapd->own_addr),
  1082. hapd->conf->ssid.ssid);
  1083. }
  1084. if (hostapd_setup_wpa_psk(conf)) {
  1085. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  1086. return -1;
  1087. }
  1088. /* Set flag for whether SSID is broadcast in beacons */
  1089. if (hostapd_set_broadcast_ssid(hapd,
  1090. !!hapd->conf->ignore_broadcast_ssid)) {
  1091. wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
  1092. "kernel driver");
  1093. return -1;
  1094. }
  1095. if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
  1096. wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
  1097. "driver");
  1098. return -1;
  1099. }
  1100. /* Set SSID for the kernel driver (to be used in beacon and probe
  1101. * response frames) */
  1102. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  1103. conf->ssid.ssid_len)) {
  1104. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  1105. return -1;
  1106. }
  1107. if (wpa_debug_level == MSG_MSGDUMP)
  1108. conf->radius->msg_dumps = 1;
  1109. hapd->radius = radius_client_init(hapd, conf->radius);
  1110. if (hapd->radius == NULL) {
  1111. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  1112. return -1;
  1113. }
  1114. if (hostapd_acl_init(hapd)) {
  1115. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  1116. return -1;
  1117. }
  1118. if (hostapd_init_wps(hapd, conf))
  1119. return -1;
  1120. if (ieee802_1x_init(hapd)) {
  1121. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  1122. return -1;
  1123. }
  1124. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1125. return -1;
  1126. if (accounting_init(hapd)) {
  1127. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  1128. return -1;
  1129. }
  1130. if (hapd->conf->ieee802_11f &&
  1131. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1132. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  1133. "failed.");
  1134. return -1;
  1135. }
  1136. if (hostapd_ctrl_iface_init(hapd)) {
  1137. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  1138. return -1;
  1139. }
  1140. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  1141. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  1142. return -1;
  1143. }
  1144. #ifdef CONFIG_IEEE80211R
  1145. if (!hostapd_drv_none(hapd)) {
  1146. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1147. hostapd_rrb_receive, hapd, 0);
  1148. if (hapd->l2 == NULL &&
  1149. (hapd->driver == NULL ||
  1150. hapd->driver->send_ether == NULL)) {
  1151. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1152. "interface");
  1153. return -1;
  1154. }
  1155. }
  1156. #endif /* CONFIG_IEEE80211R */
  1157. ieee802_11_set_beacon(hapd);
  1158. if (conf->radius_server_clients &&
  1159. hostapd_setup_radius_srv(hapd, conf))
  1160. return -1;
  1161. return 0;
  1162. }
  1163. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  1164. {
  1165. struct hostapd_data *hapd = iface->bss[0];
  1166. int i;
  1167. struct hostapd_tx_queue_params *p;
  1168. for (i = 0; i < NUM_TX_QUEUES; i++) {
  1169. p = &iface->conf->tx_queue[i];
  1170. if (!p->configured)
  1171. continue;
  1172. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  1173. p->cwmax, p->burst)) {
  1174. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  1175. "parameters for queue %d.", i);
  1176. /* Continue anyway */
  1177. }
  1178. }
  1179. }
  1180. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1181. size_t identity_len, int phase2,
  1182. struct eap_user *user)
  1183. {
  1184. const struct hostapd_eap_user *eap_user;
  1185. int i, count;
  1186. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1187. if (eap_user == NULL)
  1188. return -1;
  1189. if (user == NULL)
  1190. return 0;
  1191. os_memset(user, 0, sizeof(*user));
  1192. count = EAP_USER_MAX_METHODS;
  1193. if (count > EAP_MAX_METHODS)
  1194. count = EAP_MAX_METHODS;
  1195. for (i = 0; i < count; i++) {
  1196. user->methods[i].vendor = eap_user->methods[i].vendor;
  1197. user->methods[i].method = eap_user->methods[i].method;
  1198. }
  1199. if (eap_user->password) {
  1200. user->password = os_malloc(eap_user->password_len);
  1201. if (user->password == NULL)
  1202. return -1;
  1203. os_memcpy(user->password, eap_user->password,
  1204. eap_user->password_len);
  1205. user->password_len = eap_user->password_len;
  1206. user->password_hash = eap_user->password_hash;
  1207. }
  1208. user->force_version = eap_user->force_version;
  1209. user->ttls_auth = eap_user->ttls_auth;
  1210. return 0;
  1211. }
  1212. static int setup_interface(struct hostapd_iface *iface)
  1213. {
  1214. struct hostapd_data *hapd = iface->bss[0];
  1215. struct hostapd_bss_config *conf = hapd->conf;
  1216. size_t i;
  1217. char country[4];
  1218. u8 *b = conf->bssid;
  1219. int freq;
  1220. size_t j;
  1221. int ret = 0;
  1222. u8 *prev_addr;
  1223. /*
  1224. * Initialize the driver interface and make sure that all BSSes get
  1225. * configured with a pointer to this driver interface.
  1226. */
  1227. if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
  1228. hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
  1229. } else {
  1230. hapd->drv_priv = hostapd_driver_init(hapd);
  1231. }
  1232. if (hapd->drv_priv == NULL) {
  1233. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  1234. hapd->driver ? hapd->driver->name : "Unknown");
  1235. hapd->driver = NULL;
  1236. return -1;
  1237. }
  1238. for (i = 0; i < iface->num_bss; i++) {
  1239. iface->bss[i]->driver = hapd->driver;
  1240. iface->bss[i]->drv_priv = hapd->drv_priv;
  1241. }
  1242. if (hostapd_validate_bssid_configuration(iface))
  1243. return -1;
  1244. #ifdef CONFIG_IEEE80211N
  1245. SET_2BIT_LE16(&iface->ht_op_mode,
  1246. HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
  1247. OP_MODE_PURE);
  1248. #endif /* CONFIG_IEEE80211N */
  1249. os_memcpy(country, hapd->iconf->country, 3);
  1250. country[3] = '\0';
  1251. if (hostapd_set_country(hapd, country) < 0) {
  1252. wpa_printf(MSG_ERROR, "Failed to set country code");
  1253. return -1;
  1254. }
  1255. if (hapd->iconf->ieee80211d &&
  1256. hostapd_set_ieee80211d(hapd, 1) < 0) {
  1257. wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
  1258. hapd->iconf->ieee80211d);
  1259. return -1;
  1260. }
  1261. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1262. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1263. wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
  1264. "kernel driver");
  1265. return -1;
  1266. }
  1267. /* TODO: merge with hostapd_driver_init() ? */
  1268. if (hostapd_wireless_event_init(hapd) < 0)
  1269. return -1;
  1270. if (hostapd_get_hw_features(iface)) {
  1271. /* Not all drivers support this yet, so continue without hw
  1272. * feature data. */
  1273. } else {
  1274. int ret = hostapd_select_hw_mode(iface);
  1275. if (ret < 0) {
  1276. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1277. "channel. (%d)", ret);
  1278. return -1;
  1279. }
  1280. }
  1281. hostapd_flush_old_stations(hapd);
  1282. hostapd_set_privacy(hapd, 0);
  1283. if (hapd->iconf->channel) {
  1284. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1285. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1286. "Frequency: %d MHz",
  1287. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1288. hapd->iconf->channel, freq);
  1289. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  1290. hapd->iconf->ieee80211n,
  1291. hapd->iconf->secondary_channel)) {
  1292. wpa_printf(MSG_ERROR, "Could not set channel for "
  1293. "kernel driver");
  1294. return -1;
  1295. }
  1296. }
  1297. hostapd_broadcast_wep_clear(hapd);
  1298. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  1299. return -1;
  1300. hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
  1301. ieee802_11_set_beacon(hapd);
  1302. if (hapd->iconf->rts_threshold > -1 &&
  1303. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1304. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1305. "kernel driver");
  1306. return -1;
  1307. }
  1308. if (hapd->iconf->fragm_threshold > -1 &&
  1309. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1310. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1311. "for kernel driver");
  1312. return -1;
  1313. }
  1314. prev_addr = hapd->own_addr;
  1315. for (j = 0; j < iface->num_bss; j++) {
  1316. hapd = iface->bss[j];
  1317. if (j)
  1318. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1319. if (hostapd_setup_bss(hapd, j == 0))
  1320. return -1;
  1321. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1322. prev_addr = hapd->own_addr;
  1323. }
  1324. hostapd_tx_queue_params(iface);
  1325. ap_list_init(iface);
  1326. if (hostapd_driver_commit(hapd) < 0) {
  1327. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1328. "configuration", __func__);
  1329. return -1;
  1330. }
  1331. return ret;
  1332. }
  1333. /**
  1334. * hostapd_setup_interface - Setup of an interface
  1335. * @iface: Pointer to interface data.
  1336. * Returns: 0 on success, -1 on failure
  1337. *
  1338. * Initializes the driver interface, validates the configuration,
  1339. * and sets driver parameters based on the configuration.
  1340. * Flushes old stations, sets the channel, encryption,
  1341. * beacons, and WDS links based on the configuration.
  1342. */
  1343. static int hostapd_setup_interface(struct hostapd_iface *iface)
  1344. {
  1345. int ret;
  1346. ret = setup_interface(iface);
  1347. if (ret) {
  1348. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1349. iface->bss[0]->conf->iface);
  1350. eloop_terminate();
  1351. return -1;
  1352. } else if (!hostapd_drv_none(iface->bss[0])) {
  1353. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1354. iface->bss[0]->conf->iface);
  1355. }
  1356. return 0;
  1357. }
  1358. static void show_version(void)
  1359. {
  1360. fprintf(stderr,
  1361. "hostapd v" VERSION_STR "\n"
  1362. "User space daemon for IEEE 802.11 AP management,\n"
  1363. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  1364. "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
  1365. "and contributors\n");
  1366. }
  1367. static void usage(void)
  1368. {
  1369. show_version();
  1370. fprintf(stderr,
  1371. "\n"
  1372. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  1373. "<configuration file(s)>\n"
  1374. "\n"
  1375. "options:\n"
  1376. " -h show this usage\n"
  1377. " -d show more debug messages (-dd for even more)\n"
  1378. " -B run daemon in the background\n"
  1379. " -P PID file\n"
  1380. " -K include key data in debug messages\n"
  1381. " -t include timestamps in some debug messages\n"
  1382. " -v show hostapd version\n");
  1383. exit(1);
  1384. }
  1385. /**
  1386. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1387. * @hapd_iface: Pointer to interface data
  1388. * @conf: Pointer to per-interface configuration
  1389. * @bss: Pointer to per-BSS configuration for this BSS
  1390. * Returns: Pointer to allocated BSS data
  1391. *
  1392. * This function is used to allocate per-BSS data structure. This data will be
  1393. * freed after hostapd_cleanup() is called for it during interface
  1394. * deinitialization.
  1395. */
  1396. static struct hostapd_data *
  1397. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1398. struct hostapd_config *conf,
  1399. struct hostapd_bss_config *bss)
  1400. {
  1401. struct hostapd_data *hapd;
  1402. hapd = os_zalloc(sizeof(*hapd));
  1403. if (hapd == NULL)
  1404. return NULL;
  1405. hapd->iconf = conf;
  1406. hapd->conf = bss;
  1407. hapd->iface = hapd_iface;
  1408. if (hapd->conf->individual_wep_key_len > 0) {
  1409. /* use key0 in individual key and key1 in broadcast key */
  1410. hapd->default_wep_key_idx = 1;
  1411. }
  1412. #ifdef EAP_TLS_FUNCS
  1413. if (hapd->conf->eap_server &&
  1414. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1415. hapd->conf->dh_file)) {
  1416. struct tls_connection_params params;
  1417. hapd->ssl_ctx = tls_init(NULL);
  1418. if (hapd->ssl_ctx == NULL) {
  1419. wpa_printf(MSG_ERROR, "Failed to initialize TLS");
  1420. goto fail;
  1421. }
  1422. os_memset(&params, 0, sizeof(params));
  1423. params.ca_cert = hapd->conf->ca_cert;
  1424. params.client_cert = hapd->conf->server_cert;
  1425. params.private_key = hapd->conf->private_key;
  1426. params.private_key_passwd = hapd->conf->private_key_passwd;
  1427. params.dh_file = hapd->conf->dh_file;
  1428. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1429. wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
  1430. goto fail;
  1431. }
  1432. if (tls_global_set_verify(hapd->ssl_ctx,
  1433. hapd->conf->check_crl)) {
  1434. wpa_printf(MSG_ERROR, "Failed to enable check_crl");
  1435. goto fail;
  1436. }
  1437. }
  1438. #endif /* EAP_TLS_FUNCS */
  1439. #ifdef EAP_SERVER
  1440. if (hapd->conf->eap_sim_db) {
  1441. hapd->eap_sim_db_priv =
  1442. eap_sim_db_init(hapd->conf->eap_sim_db,
  1443. hostapd_sim_db_cb, hapd);
  1444. if (hapd->eap_sim_db_priv == NULL) {
  1445. wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
  1446. "database interface");
  1447. goto fail;
  1448. }
  1449. }
  1450. #endif /* EAP_SERVER */
  1451. hapd->driver = hapd->iconf->driver;
  1452. return hapd;
  1453. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1454. fail:
  1455. #endif
  1456. /* TODO: cleanup allocated resources(?) */
  1457. os_free(hapd);
  1458. return NULL;
  1459. }
  1460. /**
  1461. * hostapd_init - Allocate and initialize per-interface data
  1462. * @config_file: Path to the configuration file
  1463. * Returns: Pointer to the allocated interface data or %NULL on failure
  1464. *
  1465. * This function is used to allocate main data structures for per-interface
  1466. * data. The allocated data buffer will be freed by calling
  1467. * hostapd_cleanup_iface().
  1468. */
  1469. static struct hostapd_iface * hostapd_init(const char *config_file)
  1470. {
  1471. struct hostapd_iface *hapd_iface = NULL;
  1472. struct hostapd_config *conf = NULL;
  1473. struct hostapd_data *hapd;
  1474. size_t i;
  1475. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1476. if (hapd_iface == NULL)
  1477. goto fail;
  1478. hapd_iface->config_fname = os_strdup(config_file);
  1479. if (hapd_iface->config_fname == NULL)
  1480. goto fail;
  1481. conf = hostapd_config_read(hapd_iface->config_fname);
  1482. if (conf == NULL)
  1483. goto fail;
  1484. hapd_iface->conf = conf;
  1485. hapd_iface->num_bss = conf->num_bss;
  1486. hapd_iface->bss = os_zalloc(conf->num_bss *
  1487. sizeof(struct hostapd_data *));
  1488. if (hapd_iface->bss == NULL)
  1489. goto fail;
  1490. for (i = 0; i < conf->num_bss; i++) {
  1491. hapd = hapd_iface->bss[i] =
  1492. hostapd_alloc_bss_data(hapd_iface, conf,
  1493. &conf->bss[i]);
  1494. if (hapd == NULL)
  1495. goto fail;
  1496. }
  1497. return hapd_iface;
  1498. fail:
  1499. if (conf)
  1500. hostapd_config_free(conf);
  1501. if (hapd_iface) {
  1502. for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
  1503. hapd = hapd_iface->bss[i];
  1504. if (hapd && hapd->ssl_ctx)
  1505. tls_deinit(hapd->ssl_ctx);
  1506. }
  1507. os_free(hapd_iface->config_fname);
  1508. os_free(hapd_iface->bss);
  1509. os_free(hapd_iface);
  1510. }
  1511. return NULL;
  1512. }
  1513. int main(int argc, char *argv[])
  1514. {
  1515. struct hapd_interfaces interfaces;
  1516. int ret = 1, k;
  1517. size_t i, j;
  1518. int c, debug = 0, daemonize = 0, tnc = 0;
  1519. const char *pid_file = NULL;
  1520. hostapd_logger_register_cb(hostapd_logger_cb);
  1521. for (;;) {
  1522. c = getopt(argc, argv, "BdhKP:tv");
  1523. if (c < 0)
  1524. break;
  1525. switch (c) {
  1526. case 'h':
  1527. usage();
  1528. break;
  1529. case 'd':
  1530. debug++;
  1531. if (wpa_debug_level > 0)
  1532. wpa_debug_level--;
  1533. break;
  1534. case 'B':
  1535. daemonize++;
  1536. break;
  1537. case 'K':
  1538. wpa_debug_show_keys++;
  1539. break;
  1540. case 'P':
  1541. pid_file = optarg;
  1542. break;
  1543. case 't':
  1544. wpa_debug_timestamp++;
  1545. break;
  1546. case 'v':
  1547. show_version();
  1548. exit(1);
  1549. break;
  1550. default:
  1551. usage();
  1552. break;
  1553. }
  1554. }
  1555. if (optind == argc)
  1556. usage();
  1557. if (eap_server_register_methods()) {
  1558. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1559. return -1;
  1560. }
  1561. interfaces.count = argc - optind;
  1562. interfaces.iface = os_malloc(interfaces.count *
  1563. sizeof(struct hostapd_iface *));
  1564. if (interfaces.iface == NULL) {
  1565. wpa_printf(MSG_ERROR, "malloc failed\n");
  1566. return -1;
  1567. }
  1568. if (eloop_init(&interfaces)) {
  1569. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1570. return -1;
  1571. }
  1572. #ifndef CONFIG_NATIVE_WINDOWS
  1573. eloop_register_signal(SIGHUP, handle_reload, NULL);
  1574. eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
  1575. #endif /* CONFIG_NATIVE_WINDOWS */
  1576. eloop_register_signal_terminate(handle_term, NULL);
  1577. /* Initialize interfaces */
  1578. for (i = 0; i < interfaces.count; i++) {
  1579. wpa_printf(MSG_ERROR, "Configuration file: %s",
  1580. argv[optind + i]);
  1581. interfaces.iface[i] = hostapd_init(argv[optind + i]);
  1582. if (!interfaces.iface[i])
  1583. goto out;
  1584. for (k = 0; k < debug; k++) {
  1585. if (interfaces.iface[i]->bss[0]->conf->
  1586. logger_stdout_level > 0)
  1587. interfaces.iface[i]->bss[0]->conf->
  1588. logger_stdout_level--;
  1589. }
  1590. ret = hostapd_setup_interface(interfaces.iface[i]);
  1591. if (ret)
  1592. goto out;
  1593. for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
  1594. if (interfaces.iface[i]->bss[0]->conf->tnc)
  1595. tnc++;
  1596. }
  1597. }
  1598. #ifdef EAP_TNC
  1599. if (tnc && tncs_global_init() < 0) {
  1600. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  1601. goto out;
  1602. }
  1603. #endif /* EAP_TNC */
  1604. if (daemonize && os_daemonize(pid_file)) {
  1605. perror("daemon");
  1606. goto out;
  1607. }
  1608. #ifndef CONFIG_NATIVE_WINDOWS
  1609. openlog("hostapd", 0, LOG_DAEMON);
  1610. #endif /* CONFIG_NATIVE_WINDOWS */
  1611. eloop_run();
  1612. /* Disconnect associated stations from all interfaces and BSSes */
  1613. for (i = 0; i < interfaces.count; i++) {
  1614. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1615. struct hostapd_data *hapd =
  1616. interfaces.iface[i]->bss[j];
  1617. hostapd_free_stas(hapd);
  1618. hostapd_flush_old_stations(hapd);
  1619. }
  1620. }
  1621. ret = 0;
  1622. out:
  1623. /* Deinitialize all interfaces */
  1624. for (i = 0; i < interfaces.count; i++) {
  1625. if (!interfaces.iface[i])
  1626. continue;
  1627. hostapd_cleanup_iface_pre(interfaces.iface[i]);
  1628. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1629. struct hostapd_data *hapd =
  1630. interfaces.iface[i]->bss[j];
  1631. hostapd_cleanup(hapd);
  1632. if (j == interfaces.iface[i]->num_bss - 1 &&
  1633. hapd->driver)
  1634. hostapd_driver_deinit(hapd);
  1635. }
  1636. for (j = 0; j < interfaces.iface[i]->num_bss; j++)
  1637. os_free(interfaces.iface[i]->bss[j]);
  1638. hostapd_cleanup_iface(interfaces.iface[i]);
  1639. }
  1640. os_free(interfaces.iface);
  1641. #ifdef EAP_TNC
  1642. tncs_global_deinit();
  1643. #endif /* EAP_TNC */
  1644. eloop_destroy();
  1645. #ifndef CONFIG_NATIVE_WINDOWS
  1646. closelog();
  1647. #endif /* CONFIG_NATIVE_WINDOWS */
  1648. eap_server_unregister_methods();
  1649. os_daemonize_terminate(pid_file);
  1650. return ret;
  1651. }