hostapd.c 51 KB

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