hostapd.c 49 KB

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