hostapd.c 52 KB

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