hostapd.c 37 KB

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