hostapd.c 41 KB

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