hostapd.c 49 KB

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