hostapd.c 49 KB

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