hostapd.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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_encryption(ifname, hapd, "none", NULL, i, NULL,
  397. 0, i == 0 ? 1 : 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_encryption(ifname, hapd, "none", NULL,
  407. i, NULL, 0,
  408. i == 0 ? 1 : 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_encryption(hapd->conf->iface,
  429. hapd, "WEP", NULL, idx,
  430. ssid->wep.key[idx],
  431. ssid->wep.len[idx],
  432. idx == ssid->wep.idx)) {
  433. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  434. errors++;
  435. }
  436. if (ssid->dyn_vlan_keys) {
  437. size_t i;
  438. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  439. const char *ifname;
  440. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  441. if (key == NULL)
  442. continue;
  443. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  444. i);
  445. if (ifname == NULL)
  446. continue;
  447. idx = key->idx;
  448. if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
  449. idx, key->key[idx],
  450. key->len[idx],
  451. idx == key->idx)) {
  452. wpa_printf(MSG_WARNING, "Could not set "
  453. "dynamic VLAN WEP encryption.");
  454. errors++;
  455. }
  456. }
  457. }
  458. return errors;
  459. }
  460. /**
  461. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  462. * @hapd: Pointer to BSS data
  463. *
  464. * This function is used to free all per-BSS data structures and resources.
  465. * This gets called in a loop for each BSS between calls to
  466. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  467. * is deinitialized. Most of the modules that are initialized in
  468. * hostapd_setup_bss() are deinitialized here.
  469. */
  470. static void hostapd_cleanup(struct hostapd_data *hapd)
  471. {
  472. hostapd_ctrl_iface_deinit(hapd);
  473. os_free(hapd->default_wep_key);
  474. hapd->default_wep_key = NULL;
  475. iapp_deinit(hapd->iapp);
  476. hapd->iapp = NULL;
  477. accounting_deinit(hapd);
  478. rsn_preauth_iface_deinit(hapd);
  479. if (hapd->wpa_auth) {
  480. wpa_deinit(hapd->wpa_auth);
  481. hapd->wpa_auth = NULL;
  482. if (hostapd_set_privacy(hapd, 0)) {
  483. wpa_printf(MSG_DEBUG, "Could not disable "
  484. "PrivacyInvoked for interface %s",
  485. hapd->conf->iface);
  486. }
  487. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  488. wpa_printf(MSG_DEBUG, "Could not remove generic "
  489. "information element from interface %s",
  490. hapd->conf->iface);
  491. }
  492. }
  493. ieee802_1x_deinit(hapd);
  494. vlan_deinit(hapd);
  495. hostapd_acl_deinit(hapd);
  496. radius_client_deinit(hapd->radius);
  497. hapd->radius = NULL;
  498. radius_server_deinit(hapd->radius_srv);
  499. hapd->radius_srv = NULL;
  500. #ifdef CONFIG_IEEE80211R
  501. l2_packet_deinit(hapd->l2);
  502. #endif /* CONFIG_IEEE80211R */
  503. hostapd_deinit_wps(hapd);
  504. hostapd_wireless_event_deinit(hapd);
  505. #ifdef EAP_TLS_FUNCS
  506. if (hapd->ssl_ctx) {
  507. tls_deinit(hapd->ssl_ctx);
  508. hapd->ssl_ctx = NULL;
  509. }
  510. #endif /* EAP_TLS_FUNCS */
  511. #ifdef EAP_SERVER
  512. if (hapd->eap_sim_db_priv) {
  513. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  514. hapd->eap_sim_db_priv = NULL;
  515. }
  516. #endif /* EAP_SERVER */
  517. if (hapd->interface_added &&
  518. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  519. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  520. hapd->conf->iface);
  521. }
  522. }
  523. /**
  524. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  525. * @iface: Pointer to interface data
  526. *
  527. * This function is called before per-BSS data structures are deinitialized
  528. * with hostapd_cleanup().
  529. */
  530. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  531. {
  532. }
  533. /**
  534. * hostapd_cleanup_iface - Complete per-interface cleanup
  535. * @iface: Pointer to interface data
  536. *
  537. * This function is called after per-BSS data structures are deinitialized
  538. * with hostapd_cleanup().
  539. */
  540. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  541. {
  542. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  543. iface->hw_features = NULL;
  544. os_free(iface->current_rates);
  545. iface->current_rates = NULL;
  546. ap_list_deinit(iface);
  547. hostapd_config_free(iface->conf);
  548. iface->conf = NULL;
  549. os_free(iface->config_fname);
  550. os_free(iface->bss);
  551. os_free(iface);
  552. }
  553. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  554. {
  555. int i;
  556. hostapd_broadcast_wep_set(hapd);
  557. if (hapd->conf->ssid.wep.default_len)
  558. return 0;
  559. for (i = 0; i < 4; i++) {
  560. if (hapd->conf->ssid.wep.key[i] &&
  561. hostapd_set_encryption(iface, hapd, "WEP", NULL,
  562. i, hapd->conf->ssid.wep.key[i],
  563. hapd->conf->ssid.wep.len[i],
  564. i == hapd->conf->ssid.wep.idx)) {
  565. wpa_printf(MSG_WARNING, "Could not set WEP "
  566. "encryption.");
  567. return -1;
  568. }
  569. if (hapd->conf->ssid.wep.key[i] &&
  570. i == hapd->conf->ssid.wep.idx)
  571. hostapd_set_privacy(hapd, 1);
  572. }
  573. return 0;
  574. }
  575. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  576. {
  577. int ret = 0;
  578. if (hostapd_drv_none(hapd))
  579. return 0;
  580. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  581. if (hostapd_flush(hapd)) {
  582. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  583. ret = -1;
  584. }
  585. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  586. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  587. * broadcast deauth frame. This gets the firmware in odd state where
  588. * nothing works correctly, so let's skip sending this for the hostap
  589. * driver. */
  590. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  591. u8 addr[ETH_ALEN];
  592. os_memset(addr, 0xff, ETH_ALEN);
  593. hostapd_sta_deauth(hapd, addr,
  594. WLAN_REASON_PREV_AUTH_NOT_VALID);
  595. }
  596. return ret;
  597. }
  598. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  599. logger_level level, const char *txt)
  600. {
  601. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  602. struct hostapd_data *hapd = ctx;
  603. int hlevel;
  604. switch (level) {
  605. case LOGGER_WARNING:
  606. hlevel = HOSTAPD_LEVEL_WARNING;
  607. break;
  608. case LOGGER_INFO:
  609. hlevel = HOSTAPD_LEVEL_INFO;
  610. break;
  611. case LOGGER_DEBUG:
  612. default:
  613. hlevel = HOSTAPD_LEVEL_DEBUG;
  614. break;
  615. }
  616. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  617. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  618. }
  619. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  620. u16 reason)
  621. {
  622. struct hostapd_data *hapd = ctx;
  623. struct sta_info *sta;
  624. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  625. "STA " MACSTR " reason %d",
  626. __func__, MAC2STR(addr), reason);
  627. sta = ap_get_sta(hapd, addr);
  628. hostapd_sta_deauth(hapd, addr, reason);
  629. if (sta == NULL)
  630. return;
  631. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  632. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  633. eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
  634. sta->timeout_next = STA_REMOVE;
  635. }
  636. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  637. {
  638. struct hostapd_data *hapd = ctx;
  639. michael_mic_failure(hapd, addr, 0);
  640. }
  641. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  642. wpa_eapol_variable var, int value)
  643. {
  644. struct hostapd_data *hapd = ctx;
  645. struct sta_info *sta = ap_get_sta(hapd, addr);
  646. if (sta == NULL)
  647. return;
  648. switch (var) {
  649. case WPA_EAPOL_portEnabled:
  650. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  651. break;
  652. case WPA_EAPOL_portValid:
  653. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  654. break;
  655. case WPA_EAPOL_authorized:
  656. ieee802_1x_set_sta_authorized(hapd, sta, value);
  657. break;
  658. case WPA_EAPOL_portControl_Auto:
  659. if (sta->eapol_sm)
  660. sta->eapol_sm->portControl = Auto;
  661. break;
  662. case WPA_EAPOL_keyRun:
  663. if (sta->eapol_sm)
  664. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  665. break;
  666. case WPA_EAPOL_keyAvailable:
  667. if (sta->eapol_sm)
  668. sta->eapol_sm->eap_if->eapKeyAvailable =
  669. value ? TRUE : FALSE;
  670. break;
  671. case WPA_EAPOL_keyDone:
  672. if (sta->eapol_sm)
  673. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  674. break;
  675. case WPA_EAPOL_inc_EapolFramesTx:
  676. if (sta->eapol_sm)
  677. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  678. break;
  679. }
  680. }
  681. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  682. wpa_eapol_variable var)
  683. {
  684. struct hostapd_data *hapd = ctx;
  685. struct sta_info *sta = ap_get_sta(hapd, addr);
  686. if (sta == NULL || sta->eapol_sm == NULL)
  687. return -1;
  688. switch (var) {
  689. case WPA_EAPOL_keyRun:
  690. return sta->eapol_sm->keyRun;
  691. case WPA_EAPOL_keyAvailable:
  692. return sta->eapol_sm->eap_if->eapKeyAvailable;
  693. default:
  694. return -1;
  695. }
  696. }
  697. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  698. const u8 *prev_psk)
  699. {
  700. struct hostapd_data *hapd = ctx;
  701. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  702. }
  703. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  704. size_t *len)
  705. {
  706. struct hostapd_data *hapd = ctx;
  707. const u8 *key;
  708. size_t keylen;
  709. struct sta_info *sta;
  710. sta = ap_get_sta(hapd, addr);
  711. if (sta == NULL)
  712. return -1;
  713. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  714. if (key == NULL)
  715. return -1;
  716. if (keylen > *len)
  717. keylen = *len;
  718. os_memcpy(msk, key, keylen);
  719. *len = keylen;
  720. return 0;
  721. }
  722. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
  723. const u8 *addr, int idx, u8 *key,
  724. size_t key_len)
  725. {
  726. struct hostapd_data *hapd = ctx;
  727. const char *ifname = hapd->conf->iface;
  728. if (vlan_id > 0) {
  729. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  730. if (ifname == NULL)
  731. return -1;
  732. }
  733. return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
  734. key, key_len, 1);
  735. }
  736. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  737. u8 *seq)
  738. {
  739. struct hostapd_data *hapd = ctx;
  740. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  741. }
  742. static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
  743. u8 *seq)
  744. {
  745. struct hostapd_data *hapd = ctx;
  746. return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
  747. seq);
  748. }
  749. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  750. const u8 *data, size_t data_len,
  751. int encrypt)
  752. {
  753. struct hostapd_data *hapd = ctx;
  754. return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
  755. }
  756. static int hostapd_wpa_auth_for_each_sta(
  757. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  758. void *cb_ctx)
  759. {
  760. struct hostapd_data *hapd = ctx;
  761. struct sta_info *sta;
  762. for (sta = hapd->sta_list; sta; sta = sta->next) {
  763. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  764. return 1;
  765. }
  766. return 0;
  767. }
  768. static int hostapd_wpa_auth_for_each_auth(
  769. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  770. void *cb_ctx)
  771. {
  772. struct hostapd_data *ohapd;
  773. size_t i, j;
  774. struct hapd_interfaces *interfaces = eloop_get_user_data();
  775. for (i = 0; i < interfaces->count; i++) {
  776. for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
  777. ohapd = interfaces->iface[i]->bss[j];
  778. if (cb(ohapd->wpa_auth, cb_ctx))
  779. return 1;
  780. }
  781. }
  782. return 0;
  783. }
  784. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  785. const u8 *data, size_t data_len)
  786. {
  787. struct hostapd_data *hapd = ctx;
  788. if (hapd->driver && hapd->driver->send_ether)
  789. return hapd->driver->send_ether(hapd->drv_priv, dst,
  790. hapd->own_addr, proto,
  791. data, data_len);
  792. if (hapd->l2 == NULL)
  793. return -1;
  794. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  795. }
  796. #ifdef CONFIG_IEEE80211R
  797. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  798. const u8 *data, size_t data_len)
  799. {
  800. struct hostapd_data *hapd = ctx;
  801. int res;
  802. struct ieee80211_mgmt *m;
  803. size_t mlen;
  804. struct sta_info *sta;
  805. sta = ap_get_sta(hapd, dst);
  806. if (sta == NULL || sta->wpa_sm == NULL)
  807. return -1;
  808. m = os_zalloc(sizeof(*m) + data_len);
  809. if (m == NULL)
  810. return -1;
  811. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  812. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  813. WLAN_FC_STYPE_ACTION);
  814. os_memcpy(m->da, dst, ETH_ALEN);
  815. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  816. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  817. os_memcpy(&m->u, data, data_len);
  818. res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
  819. os_free(m);
  820. return res;
  821. }
  822. static struct wpa_state_machine *
  823. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  824. {
  825. struct hostapd_data *hapd = ctx;
  826. struct sta_info *sta;
  827. sta = ap_sta_add(hapd, sta_addr);
  828. if (sta == NULL)
  829. return NULL;
  830. if (sta->wpa_sm)
  831. return sta->wpa_sm;
  832. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  833. if (sta->wpa_sm == NULL) {
  834. ap_free_sta(hapd, sta);
  835. return NULL;
  836. }
  837. sta->auth_alg = WLAN_AUTH_FT;
  838. return sta->wpa_sm;
  839. }
  840. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  841. size_t len)
  842. {
  843. struct hostapd_data *hapd = ctx;
  844. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  845. }
  846. #endif /* CONFIG_IEEE80211R */
  847. /**
  848. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  849. * @iface: Pointer to interface data
  850. * Returns: 0 on success, -1 on failure
  851. *
  852. * This function is used to validate that the configured BSSIDs are valid.
  853. */
  854. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  855. {
  856. u8 mask[ETH_ALEN] = { 0 };
  857. struct hostapd_data *hapd = iface->bss[0];
  858. unsigned int i = iface->conf->num_bss, bits = 0, j;
  859. int res;
  860. int auto_addr = 0;
  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. if (j)
  872. auto_addr++;
  873. continue;
  874. }
  875. for (i = 0; i < ETH_ALEN; i++) {
  876. mask[i] |=
  877. iface->conf->bss[j].bssid[i] ^
  878. hapd->own_addr[i];
  879. }
  880. }
  881. if (!auto_addr)
  882. goto skip_mask_ext;
  883. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  884. ;
  885. j = 0;
  886. if (i < ETH_ALEN) {
  887. j = (5 - i) * 8;
  888. while (mask[i] != 0) {
  889. mask[i] >>= 1;
  890. j++;
  891. }
  892. }
  893. if (bits < j)
  894. bits = j;
  895. if (bits > 40) {
  896. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  897. bits);
  898. return -1;
  899. }
  900. os_memset(mask, 0xff, ETH_ALEN);
  901. j = bits / 8;
  902. for (i = 5; i > 5 - j; i--)
  903. mask[i] = 0;
  904. j = bits % 8;
  905. while (j--)
  906. mask[i] <<= 1;
  907. skip_mask_ext:
  908. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  909. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  910. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  911. if (res == 0)
  912. return 0;
  913. if (res < 0) {
  914. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  915. MACSTR " for start address " MACSTR ".",
  916. MAC2STR(mask), MAC2STR(hapd->own_addr));
  917. return -1;
  918. }
  919. if (!auto_addr)
  920. return 0;
  921. for (i = 0; i < ETH_ALEN; i++) {
  922. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  923. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  924. " for start address " MACSTR ".",
  925. MAC2STR(mask), MAC2STR(hapd->own_addr));
  926. wpa_printf(MSG_ERROR, "Start address must be the "
  927. "first address in the block (i.e., addr "
  928. "AND mask == addr).");
  929. return -1;
  930. }
  931. }
  932. return 0;
  933. }
  934. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  935. {
  936. size_t i;
  937. for (i = 0; i < conf->num_bss; i++) {
  938. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  939. return 1;
  940. }
  941. }
  942. return 0;
  943. }
  944. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  945. {
  946. struct wpa_auth_config _conf;
  947. struct wpa_auth_callbacks cb;
  948. const u8 *wpa_ie;
  949. size_t wpa_ie_len;
  950. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  951. os_memset(&cb, 0, sizeof(cb));
  952. cb.ctx = hapd;
  953. cb.logger = hostapd_wpa_auth_logger;
  954. cb.disconnect = hostapd_wpa_auth_disconnect;
  955. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  956. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  957. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  958. cb.get_psk = hostapd_wpa_auth_get_psk;
  959. cb.get_msk = hostapd_wpa_auth_get_msk;
  960. cb.set_key = hostapd_wpa_auth_set_key;
  961. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  962. cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
  963. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  964. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  965. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  966. cb.send_ether = hostapd_wpa_auth_send_ether;
  967. #ifdef CONFIG_IEEE80211R
  968. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  969. cb.add_sta = hostapd_wpa_auth_add_sta;
  970. #endif /* CONFIG_IEEE80211R */
  971. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  972. if (hapd->wpa_auth == NULL) {
  973. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  974. return -1;
  975. }
  976. if (hostapd_set_privacy(hapd, 1)) {
  977. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  978. "for interface %s", hapd->conf->iface);
  979. return -1;
  980. }
  981. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  982. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  983. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  984. "the kernel driver.");
  985. return -1;
  986. }
  987. if (rsn_preauth_iface_init(hapd)) {
  988. wpa_printf(MSG_ERROR, "Initialization of RSN "
  989. "pre-authentication failed.");
  990. return -1;
  991. }
  992. return 0;
  993. }
  994. static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
  995. struct hostapd_bss_config *conf)
  996. {
  997. struct radius_server_conf srv;
  998. os_memset(&srv, 0, sizeof(srv));
  999. srv.client_file = conf->radius_server_clients;
  1000. srv.auth_port = conf->radius_server_auth_port;
  1001. srv.conf_ctx = conf;
  1002. srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
  1003. srv.ssl_ctx = hapd->ssl_ctx;
  1004. srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
  1005. srv.eap_fast_a_id = conf->eap_fast_a_id;
  1006. srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
  1007. srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
  1008. srv.eap_fast_prov = conf->eap_fast_prov;
  1009. srv.pac_key_lifetime = conf->pac_key_lifetime;
  1010. srv.pac_key_refresh_time = conf->pac_key_refresh_time;
  1011. srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  1012. srv.tnc = conf->tnc;
  1013. srv.wps = hapd->wps;
  1014. srv.ipv6 = conf->radius_server_ipv6;
  1015. srv.get_eap_user = hostapd_radius_get_eap_user;
  1016. srv.eap_req_id_text = conf->eap_req_id_text;
  1017. srv.eap_req_id_text_len = conf->eap_req_id_text_len;
  1018. hapd->radius_srv = radius_server_init(&srv);
  1019. if (hapd->radius_srv == NULL) {
  1020. wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
  1021. return -1;
  1022. }
  1023. return 0;
  1024. }
  1025. /**
  1026. * hostapd_setup_bss - Per-BSS setup (initialization)
  1027. * @hapd: Pointer to BSS data
  1028. * @first: Whether this BSS is the first BSS of an interface
  1029. *
  1030. * This function is used to initialize all per-BSS data structures and
  1031. * resources. This gets called in a loop for each BSS when an interface is
  1032. * initialized. Most of the modules that are initialized here will be
  1033. * deinitialized in hostapd_cleanup().
  1034. */
  1035. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  1036. {
  1037. struct hostapd_bss_config *conf = hapd->conf;
  1038. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  1039. int ssid_len, set_ssid;
  1040. if (!first) {
  1041. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  1042. /* Allocate the next available BSSID. */
  1043. do {
  1044. inc_byte_array(hapd->own_addr, ETH_ALEN);
  1045. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  1046. } else {
  1047. /* Allocate the configured BSSID. */
  1048. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  1049. if (hostapd_mac_comp(hapd->own_addr,
  1050. hapd->iface->bss[0]->own_addr) ==
  1051. 0) {
  1052. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  1053. "BSSID set to the MAC address of "
  1054. "the radio", hapd->conf->iface);
  1055. return -1;
  1056. }
  1057. }
  1058. hapd->interface_added = 1;
  1059. if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
  1060. hapd->own_addr)) {
  1061. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  1062. MACSTR ")", MAC2STR(hapd->own_addr));
  1063. return -1;
  1064. }
  1065. }
  1066. /*
  1067. * Fetch the SSID from the system and use it or,
  1068. * if one was specified in the config file, verify they
  1069. * match.
  1070. */
  1071. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  1072. if (ssid_len < 0) {
  1073. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  1074. return -1;
  1075. }
  1076. if (conf->ssid.ssid_set) {
  1077. /*
  1078. * If SSID is specified in the config file and it differs
  1079. * from what is being used then force installation of the
  1080. * new SSID.
  1081. */
  1082. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  1083. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  1084. } else {
  1085. /*
  1086. * No SSID in the config file; just use the one we got
  1087. * from the system.
  1088. */
  1089. set_ssid = 0;
  1090. conf->ssid.ssid_len = ssid_len;
  1091. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  1092. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  1093. }
  1094. if (!hostapd_drv_none(hapd)) {
  1095. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  1096. " and ssid '%s'",
  1097. hapd->conf->iface, MAC2STR(hapd->own_addr),
  1098. hapd->conf->ssid.ssid);
  1099. }
  1100. if (hostapd_setup_wpa_psk(conf)) {
  1101. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  1102. return -1;
  1103. }
  1104. /* Set flag for whether SSID is broadcast in beacons */
  1105. if (hostapd_set_broadcast_ssid(hapd,
  1106. !!hapd->conf->ignore_broadcast_ssid)) {
  1107. wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
  1108. "kernel driver");
  1109. return -1;
  1110. }
  1111. /* Set SSID for the kernel driver (to be used in beacon and probe
  1112. * response frames) */
  1113. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  1114. conf->ssid.ssid_len)) {
  1115. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  1116. return -1;
  1117. }
  1118. if (wpa_debug_level == MSG_MSGDUMP)
  1119. conf->radius->msg_dumps = 1;
  1120. hapd->radius = radius_client_init(hapd, conf->radius);
  1121. if (hapd->radius == NULL) {
  1122. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  1123. return -1;
  1124. }
  1125. if (hostapd_acl_init(hapd)) {
  1126. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  1127. return -1;
  1128. }
  1129. if (hostapd_init_wps(hapd, conf))
  1130. return -1;
  1131. if (ieee802_1x_init(hapd)) {
  1132. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  1133. return -1;
  1134. }
  1135. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1136. return -1;
  1137. if (accounting_init(hapd)) {
  1138. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  1139. return -1;
  1140. }
  1141. if (hapd->conf->ieee802_11f &&
  1142. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1143. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  1144. "failed.");
  1145. return -1;
  1146. }
  1147. if (hostapd_ctrl_iface_init(hapd)) {
  1148. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  1149. return -1;
  1150. }
  1151. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  1152. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  1153. return -1;
  1154. }
  1155. #ifdef CONFIG_IEEE80211R
  1156. if (!hostapd_drv_none(hapd)) {
  1157. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1158. hostapd_rrb_receive, hapd, 0);
  1159. if (hapd->l2 == NULL &&
  1160. (hapd->driver == NULL ||
  1161. hapd->driver->send_ether == NULL)) {
  1162. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1163. "interface");
  1164. return -1;
  1165. }
  1166. }
  1167. #endif /* CONFIG_IEEE80211R */
  1168. ieee802_11_set_beacon(hapd);
  1169. if (conf->radius_server_clients &&
  1170. hostapd_setup_radius_srv(hapd, conf))
  1171. return -1;
  1172. return 0;
  1173. }
  1174. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  1175. {
  1176. struct hostapd_data *hapd = iface->bss[0];
  1177. int i;
  1178. struct hostapd_tx_queue_params *p;
  1179. for (i = 0; i < NUM_TX_QUEUES; i++) {
  1180. p = &iface->conf->tx_queue[i];
  1181. if (!p->configured)
  1182. continue;
  1183. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  1184. p->cwmax, p->burst)) {
  1185. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  1186. "parameters for queue %d.", i);
  1187. /* Continue anyway */
  1188. }
  1189. }
  1190. }
  1191. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1192. size_t identity_len, int phase2,
  1193. struct eap_user *user)
  1194. {
  1195. const struct hostapd_eap_user *eap_user;
  1196. int i, count;
  1197. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1198. if (eap_user == NULL)
  1199. return -1;
  1200. if (user == NULL)
  1201. return 0;
  1202. os_memset(user, 0, sizeof(*user));
  1203. count = EAP_USER_MAX_METHODS;
  1204. if (count > EAP_MAX_METHODS)
  1205. count = EAP_MAX_METHODS;
  1206. for (i = 0; i < count; i++) {
  1207. user->methods[i].vendor = eap_user->methods[i].vendor;
  1208. user->methods[i].method = eap_user->methods[i].method;
  1209. }
  1210. if (eap_user->password) {
  1211. user->password = os_malloc(eap_user->password_len);
  1212. if (user->password == NULL)
  1213. return -1;
  1214. os_memcpy(user->password, eap_user->password,
  1215. eap_user->password_len);
  1216. user->password_len = eap_user->password_len;
  1217. user->password_hash = eap_user->password_hash;
  1218. }
  1219. user->force_version = eap_user->force_version;
  1220. user->ttls_auth = eap_user->ttls_auth;
  1221. return 0;
  1222. }
  1223. static int setup_interface(struct hostapd_iface *iface)
  1224. {
  1225. struct hostapd_data *hapd = iface->bss[0];
  1226. struct hostapd_bss_config *conf = hapd->conf;
  1227. size_t i;
  1228. char country[4];
  1229. u8 *b = conf->bssid;
  1230. int freq;
  1231. size_t j;
  1232. int ret = 0;
  1233. u8 *prev_addr;
  1234. /*
  1235. * Initialize the driver interface and make sure that all BSSes get
  1236. * configured with a pointer to this driver interface.
  1237. */
  1238. if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
  1239. hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
  1240. } else {
  1241. hapd->drv_priv = hostapd_driver_init(hapd);
  1242. }
  1243. if (hapd->drv_priv == NULL) {
  1244. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  1245. hapd->driver ? hapd->driver->name : "Unknown");
  1246. hapd->driver = NULL;
  1247. return -1;
  1248. }
  1249. for (i = 0; i < iface->num_bss; i++) {
  1250. iface->bss[i]->driver = hapd->driver;
  1251. iface->bss[i]->drv_priv = hapd->drv_priv;
  1252. }
  1253. if (hostapd_validate_bssid_configuration(iface))
  1254. return -1;
  1255. #ifdef CONFIG_IEEE80211N
  1256. SET_2BIT_LE16(&iface->ht_op_mode,
  1257. HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
  1258. OP_MODE_PURE);
  1259. #endif /* CONFIG_IEEE80211N */
  1260. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  1261. os_memcpy(country, hapd->iconf->country, 3);
  1262. country[3] = '\0';
  1263. if (hostapd_set_country(hapd, country) < 0) {
  1264. wpa_printf(MSG_ERROR, "Failed to set country code");
  1265. return -1;
  1266. }
  1267. }
  1268. if (hapd->iconf->ieee80211d &&
  1269. hostapd_set_ieee80211d(hapd, 1) < 0) {
  1270. wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
  1271. hapd->iconf->ieee80211d);
  1272. return -1;
  1273. }
  1274. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1275. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1276. wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
  1277. "kernel driver");
  1278. return -1;
  1279. }
  1280. /* TODO: merge with hostapd_driver_init() ? */
  1281. if (hostapd_wireless_event_init(hapd) < 0)
  1282. return -1;
  1283. if (hostapd_get_hw_features(iface)) {
  1284. /* Not all drivers support this yet, so continue without hw
  1285. * feature data. */
  1286. } else {
  1287. int ret = hostapd_select_hw_mode(iface);
  1288. if (ret < 0) {
  1289. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1290. "channel. (%d)", ret);
  1291. return -1;
  1292. }
  1293. }
  1294. hostapd_flush_old_stations(hapd);
  1295. hostapd_set_privacy(hapd, 0);
  1296. if (hapd->iconf->channel) {
  1297. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1298. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1299. "Frequency: %d MHz",
  1300. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1301. hapd->iconf->channel, freq);
  1302. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  1303. hapd->iconf->ieee80211n,
  1304. hapd->iconf->secondary_channel)) {
  1305. wpa_printf(MSG_ERROR, "Could not set channel for "
  1306. "kernel driver");
  1307. return -1;
  1308. }
  1309. }
  1310. hostapd_broadcast_wep_clear(hapd);
  1311. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  1312. return -1;
  1313. hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
  1314. ieee802_11_set_beacon(hapd);
  1315. if (hapd->iconf->rts_threshold > -1 &&
  1316. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1317. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1318. "kernel driver");
  1319. return -1;
  1320. }
  1321. if (hapd->iconf->fragm_threshold > -1 &&
  1322. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1323. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1324. "for kernel driver");
  1325. return -1;
  1326. }
  1327. prev_addr = hapd->own_addr;
  1328. for (j = 0; j < iface->num_bss; j++) {
  1329. hapd = iface->bss[j];
  1330. if (j)
  1331. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1332. if (hostapd_setup_bss(hapd, j == 0))
  1333. return -1;
  1334. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1335. prev_addr = hapd->own_addr;
  1336. }
  1337. hostapd_tx_queue_params(iface);
  1338. ap_list_init(iface);
  1339. if (hostapd_driver_commit(hapd) < 0) {
  1340. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1341. "configuration", __func__);
  1342. return -1;
  1343. }
  1344. return ret;
  1345. }
  1346. /**
  1347. * hostapd_setup_interface - Setup of an interface
  1348. * @iface: Pointer to interface data.
  1349. * Returns: 0 on success, -1 on failure
  1350. *
  1351. * Initializes the driver interface, validates the configuration,
  1352. * and sets driver parameters based on the configuration.
  1353. * Flushes old stations, sets the channel, encryption,
  1354. * beacons, and WDS links based on the configuration.
  1355. */
  1356. static int hostapd_setup_interface(struct hostapd_iface *iface)
  1357. {
  1358. int ret;
  1359. ret = setup_interface(iface);
  1360. if (ret) {
  1361. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1362. iface->bss[0]->conf->iface);
  1363. eloop_terminate();
  1364. return -1;
  1365. } else if (!hostapd_drv_none(iface->bss[0])) {
  1366. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1367. iface->bss[0]->conf->iface);
  1368. }
  1369. return 0;
  1370. }
  1371. static void show_version(void)
  1372. {
  1373. fprintf(stderr,
  1374. "hostapd v" VERSION_STR "\n"
  1375. "User space daemon for IEEE 802.11 AP management,\n"
  1376. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  1377. "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
  1378. "and contributors\n");
  1379. }
  1380. static void usage(void)
  1381. {
  1382. show_version();
  1383. fprintf(stderr,
  1384. "\n"
  1385. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  1386. "<configuration file(s)>\n"
  1387. "\n"
  1388. "options:\n"
  1389. " -h show this usage\n"
  1390. " -d show more debug messages (-dd for even more)\n"
  1391. " -B run daemon in the background\n"
  1392. " -P PID file\n"
  1393. " -K include key data in debug messages\n"
  1394. " -t include timestamps in some debug messages\n"
  1395. " -v show hostapd version\n");
  1396. exit(1);
  1397. }
  1398. /**
  1399. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1400. * @hapd_iface: Pointer to interface data
  1401. * @conf: Pointer to per-interface configuration
  1402. * @bss: Pointer to per-BSS configuration for this BSS
  1403. * Returns: Pointer to allocated BSS data
  1404. *
  1405. * This function is used to allocate per-BSS data structure. This data will be
  1406. * freed after hostapd_cleanup() is called for it during interface
  1407. * deinitialization.
  1408. */
  1409. static struct hostapd_data *
  1410. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1411. struct hostapd_config *conf,
  1412. struct hostapd_bss_config *bss)
  1413. {
  1414. struct hostapd_data *hapd;
  1415. hapd = os_zalloc(sizeof(*hapd));
  1416. if (hapd == NULL)
  1417. return NULL;
  1418. hapd->iconf = conf;
  1419. hapd->conf = bss;
  1420. hapd->iface = hapd_iface;
  1421. if (hapd->conf->individual_wep_key_len > 0) {
  1422. /* use key0 in individual key and key1 in broadcast key */
  1423. hapd->default_wep_key_idx = 1;
  1424. }
  1425. #ifdef EAP_TLS_FUNCS
  1426. if (hapd->conf->eap_server &&
  1427. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1428. hapd->conf->dh_file)) {
  1429. struct tls_connection_params params;
  1430. hapd->ssl_ctx = tls_init(NULL);
  1431. if (hapd->ssl_ctx == NULL) {
  1432. wpa_printf(MSG_ERROR, "Failed to initialize TLS");
  1433. goto fail;
  1434. }
  1435. os_memset(&params, 0, sizeof(params));
  1436. params.ca_cert = hapd->conf->ca_cert;
  1437. params.client_cert = hapd->conf->server_cert;
  1438. params.private_key = hapd->conf->private_key;
  1439. params.private_key_passwd = hapd->conf->private_key_passwd;
  1440. params.dh_file = hapd->conf->dh_file;
  1441. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1442. wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
  1443. goto fail;
  1444. }
  1445. if (tls_global_set_verify(hapd->ssl_ctx,
  1446. hapd->conf->check_crl)) {
  1447. wpa_printf(MSG_ERROR, "Failed to enable check_crl");
  1448. goto fail;
  1449. }
  1450. }
  1451. #endif /* EAP_TLS_FUNCS */
  1452. #ifdef EAP_SERVER
  1453. if (hapd->conf->eap_sim_db) {
  1454. hapd->eap_sim_db_priv =
  1455. eap_sim_db_init(hapd->conf->eap_sim_db,
  1456. hostapd_sim_db_cb, hapd);
  1457. if (hapd->eap_sim_db_priv == NULL) {
  1458. wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
  1459. "database interface");
  1460. goto fail;
  1461. }
  1462. }
  1463. #endif /* EAP_SERVER */
  1464. hapd->driver = hapd->iconf->driver;
  1465. return hapd;
  1466. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1467. fail:
  1468. #endif
  1469. /* TODO: cleanup allocated resources(?) */
  1470. os_free(hapd);
  1471. return NULL;
  1472. }
  1473. /**
  1474. * hostapd_init - Allocate and initialize per-interface data
  1475. * @config_file: Path to the configuration file
  1476. * Returns: Pointer to the allocated interface data or %NULL on failure
  1477. *
  1478. * This function is used to allocate main data structures for per-interface
  1479. * data. The allocated data buffer will be freed by calling
  1480. * hostapd_cleanup_iface().
  1481. */
  1482. static struct hostapd_iface * hostapd_init(const char *config_file)
  1483. {
  1484. struct hostapd_iface *hapd_iface = NULL;
  1485. struct hostapd_config *conf = NULL;
  1486. struct hostapd_data *hapd;
  1487. size_t i;
  1488. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1489. if (hapd_iface == NULL)
  1490. goto fail;
  1491. hapd_iface->config_fname = os_strdup(config_file);
  1492. if (hapd_iface->config_fname == NULL)
  1493. goto fail;
  1494. conf = hostapd_config_read(hapd_iface->config_fname);
  1495. if (conf == NULL)
  1496. goto fail;
  1497. hapd_iface->conf = conf;
  1498. hapd_iface->num_bss = conf->num_bss;
  1499. hapd_iface->bss = os_zalloc(conf->num_bss *
  1500. sizeof(struct hostapd_data *));
  1501. if (hapd_iface->bss == NULL)
  1502. goto fail;
  1503. for (i = 0; i < conf->num_bss; i++) {
  1504. hapd = hapd_iface->bss[i] =
  1505. hostapd_alloc_bss_data(hapd_iface, conf,
  1506. &conf->bss[i]);
  1507. if (hapd == NULL)
  1508. goto fail;
  1509. }
  1510. return hapd_iface;
  1511. fail:
  1512. if (conf)
  1513. hostapd_config_free(conf);
  1514. if (hapd_iface) {
  1515. for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
  1516. hapd = hapd_iface->bss[i];
  1517. if (hapd && hapd->ssl_ctx)
  1518. tls_deinit(hapd->ssl_ctx);
  1519. }
  1520. os_free(hapd_iface->config_fname);
  1521. os_free(hapd_iface->bss);
  1522. os_free(hapd_iface);
  1523. }
  1524. return NULL;
  1525. }
  1526. static int hostapd_global_init(struct hapd_interfaces *interfaces)
  1527. {
  1528. hostapd_logger_register_cb(hostapd_logger_cb);
  1529. if (eap_server_register_methods()) {
  1530. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1531. return -1;
  1532. }
  1533. if (eloop_init(interfaces)) {
  1534. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1535. return -1;
  1536. }
  1537. #ifndef CONFIG_NATIVE_WINDOWS
  1538. eloop_register_signal(SIGHUP, handle_reload, NULL);
  1539. eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
  1540. #endif /* CONFIG_NATIVE_WINDOWS */
  1541. eloop_register_signal_terminate(handle_term, NULL);
  1542. #ifndef CONFIG_NATIVE_WINDOWS
  1543. openlog("hostapd", 0, LOG_DAEMON);
  1544. #endif /* CONFIG_NATIVE_WINDOWS */
  1545. return 0;
  1546. }
  1547. static void hostapd_global_deinit(const char *pid_file)
  1548. {
  1549. #ifdef EAP_SERVER_TNC
  1550. tncs_global_deinit();
  1551. #endif /* EAP_SERVER_TNC */
  1552. eloop_destroy();
  1553. #ifndef CONFIG_NATIVE_WINDOWS
  1554. closelog();
  1555. #endif /* CONFIG_NATIVE_WINDOWS */
  1556. eap_server_unregister_methods();
  1557. os_daemonize_terminate(pid_file);
  1558. }
  1559. static void hostapd_interface_deinit(struct hostapd_iface *iface)
  1560. {
  1561. size_t j;
  1562. if (iface == NULL)
  1563. return;
  1564. hostapd_cleanup_iface_pre(iface);
  1565. for (j = 0; j < iface->num_bss; j++) {
  1566. struct hostapd_data *hapd = iface->bss[j];
  1567. hostapd_free_stas(hapd);
  1568. hostapd_flush_old_stations(hapd);
  1569. hostapd_cleanup(hapd);
  1570. if (j == iface->num_bss - 1 && hapd->driver)
  1571. hostapd_driver_deinit(hapd);
  1572. }
  1573. for (j = 0; j < iface->num_bss; j++)
  1574. os_free(iface->bss[j]);
  1575. hostapd_cleanup_iface(iface);
  1576. }
  1577. static struct hostapd_iface * hostapd_interface_init(const char *config_fname,
  1578. int debug)
  1579. {
  1580. struct hostapd_iface *iface;
  1581. int k;
  1582. wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
  1583. iface = hostapd_init(config_fname);
  1584. if (!iface)
  1585. return NULL;
  1586. for (k = 0; k < debug; k++) {
  1587. if (iface->bss[0]->conf->logger_stdout_level > 0)
  1588. iface->bss[0]->conf->logger_stdout_level--;
  1589. }
  1590. if (hostapd_setup_interface(iface)) {
  1591. hostapd_interface_deinit(iface);
  1592. return NULL;
  1593. }
  1594. return iface;
  1595. }
  1596. static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
  1597. const char *pid_file)
  1598. {
  1599. #ifdef EAP_SERVER_TNC
  1600. int tnc = 0;
  1601. size_t i, k;
  1602. for (i = 0; !tnc && i < ifaces->count; i++) {
  1603. for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
  1604. if (ifaces->iface[i]->bss[0]->conf->tnc) {
  1605. tnc++;
  1606. break;
  1607. }
  1608. }
  1609. }
  1610. if (tnc && tncs_global_init() < 0) {
  1611. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  1612. return -1;
  1613. }
  1614. #endif /* EAP_SERVER_TNC */
  1615. if (daemonize && os_daemonize(pid_file)) {
  1616. perror("daemon");
  1617. return -1;
  1618. }
  1619. eloop_run();
  1620. return 0;
  1621. }
  1622. int main(int argc, char *argv[])
  1623. {
  1624. struct hapd_interfaces interfaces;
  1625. int ret = 1;
  1626. size_t i;
  1627. int c, debug = 0, daemonize = 0;
  1628. const char *pid_file = NULL;
  1629. for (;;) {
  1630. c = getopt(argc, argv, "BdhKP:tv");
  1631. if (c < 0)
  1632. break;
  1633. switch (c) {
  1634. case 'h':
  1635. usage();
  1636. break;
  1637. case 'd':
  1638. debug++;
  1639. if (wpa_debug_level > 0)
  1640. wpa_debug_level--;
  1641. break;
  1642. case 'B':
  1643. daemonize++;
  1644. break;
  1645. case 'K':
  1646. wpa_debug_show_keys++;
  1647. break;
  1648. case 'P':
  1649. pid_file = optarg;
  1650. break;
  1651. case 't':
  1652. wpa_debug_timestamp++;
  1653. break;
  1654. case 'v':
  1655. show_version();
  1656. exit(1);
  1657. break;
  1658. default:
  1659. usage();
  1660. break;
  1661. }
  1662. }
  1663. if (optind == argc)
  1664. usage();
  1665. interfaces.count = argc - optind;
  1666. interfaces.iface = os_malloc(interfaces.count *
  1667. sizeof(struct hostapd_iface *));
  1668. if (interfaces.iface == NULL) {
  1669. wpa_printf(MSG_ERROR, "malloc failed\n");
  1670. return -1;
  1671. }
  1672. if (hostapd_global_init(&interfaces))
  1673. return -1;
  1674. /* Initialize interfaces */
  1675. for (i = 0; i < interfaces.count; i++) {
  1676. interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
  1677. debug);
  1678. if (!interfaces.iface[i])
  1679. goto out;
  1680. }
  1681. if (hostapd_global_run(&interfaces, daemonize, pid_file))
  1682. goto out;
  1683. ret = 0;
  1684. out:
  1685. /* Deinitialize all interfaces */
  1686. for (i = 0; i < interfaces.count; i++)
  1687. hostapd_interface_deinit(interfaces.iface[i]);
  1688. os_free(interfaces.iface);
  1689. hostapd_global_deinit(pid_file);
  1690. return ret;
  1691. }