hostapd.c 40 KB

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