hostapd.c 38 KB

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