hostapd.c 40 KB

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