hostapd.c 49 KB

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