hostapd.c 38 KB

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