hostapd.c 50 KB

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