hostapd.c 40 KB

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