hostapd.c 40 KB

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