hostapd.c 38 KB

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