hostapd.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. hostapd_wireless_event_deinit(hapd);
  369. #ifdef EAP_TLS_FUNCS
  370. if (hapd->ssl_ctx) {
  371. tls_deinit(hapd->ssl_ctx);
  372. hapd->ssl_ctx = NULL;
  373. }
  374. #endif /* EAP_TLS_FUNCS */
  375. #ifdef EAP_SERVER
  376. if (hapd->eap_sim_db_priv) {
  377. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  378. hapd->eap_sim_db_priv = NULL;
  379. }
  380. #endif /* EAP_SERVER */
  381. if (hapd->interface_added &&
  382. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  383. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  384. hapd->conf->iface);
  385. }
  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, 0);
  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. /*
  938. * Fetch the SSID from the system and use it or,
  939. * if one was specified in the config file, verify they
  940. * match.
  941. */
  942. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  943. if (ssid_len < 0) {
  944. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  945. return -1;
  946. }
  947. if (conf->ssid.ssid_set) {
  948. /*
  949. * If SSID is specified in the config file and it differs
  950. * from what is being used then force installation of the
  951. * new SSID.
  952. */
  953. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  954. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  955. } else {
  956. /*
  957. * No SSID in the config file; just use the one we got
  958. * from the system.
  959. */
  960. set_ssid = 0;
  961. conf->ssid.ssid_len = ssid_len;
  962. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  963. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  964. }
  965. if (!hostapd_drv_none(hapd)) {
  966. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  967. " and ssid '%s'",
  968. hapd->conf->iface, MAC2STR(hapd->own_addr),
  969. hapd->conf->ssid.ssid);
  970. }
  971. if (hostapd_setup_wpa_psk(conf)) {
  972. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  973. return -1;
  974. }
  975. /* Set flag for whether SSID is broadcast in beacons */
  976. if (hostapd_set_broadcast_ssid(hapd,
  977. !!hapd->conf->ignore_broadcast_ssid)) {
  978. wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
  979. "kernel driver");
  980. return -1;
  981. }
  982. /* Set SSID for the kernel driver (to be used in beacon and probe
  983. * response frames) */
  984. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  985. conf->ssid.ssid_len)) {
  986. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  987. return -1;
  988. }
  989. if (wpa_debug_level == MSG_MSGDUMP)
  990. conf->radius->msg_dumps = 1;
  991. hapd->radius = radius_client_init(hapd, conf->radius);
  992. if (hapd->radius == NULL) {
  993. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  994. return -1;
  995. }
  996. if (hostapd_acl_init(hapd)) {
  997. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  998. return -1;
  999. }
  1000. if (hostapd_init_wps(hapd, conf))
  1001. return -1;
  1002. if (ieee802_1x_init(hapd)) {
  1003. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  1004. return -1;
  1005. }
  1006. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1007. return -1;
  1008. if (accounting_init(hapd)) {
  1009. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  1010. return -1;
  1011. }
  1012. if (hapd->conf->ieee802_11f &&
  1013. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1014. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  1015. "failed.");
  1016. return -1;
  1017. }
  1018. if (hostapd_ctrl_iface_init(hapd)) {
  1019. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  1020. return -1;
  1021. }
  1022. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  1023. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  1024. return -1;
  1025. }
  1026. #ifdef CONFIG_IEEE80211R
  1027. if (!hostapd_drv_none(hapd)) {
  1028. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1029. hostapd_rrb_receive, hapd, 0);
  1030. if (hapd->l2 == NULL &&
  1031. (hapd->driver == NULL ||
  1032. hapd->driver->send_ether == NULL)) {
  1033. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  1034. "interface");
  1035. return -1;
  1036. }
  1037. }
  1038. #endif /* CONFIG_IEEE80211R */
  1039. ieee802_11_set_beacon(hapd);
  1040. if (conf->radius_server_clients &&
  1041. hostapd_setup_radius_srv(hapd, conf))
  1042. return -1;
  1043. return 0;
  1044. }
  1045. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  1046. {
  1047. struct hostapd_data *hapd = iface->bss[0];
  1048. int i;
  1049. struct hostapd_tx_queue_params *p;
  1050. for (i = 0; i < NUM_TX_QUEUES; i++) {
  1051. p = &iface->conf->tx_queue[i];
  1052. if (!p->configured)
  1053. continue;
  1054. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  1055. p->cwmax, p->burst)) {
  1056. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  1057. "parameters for queue %d.", i);
  1058. /* Continue anyway */
  1059. }
  1060. }
  1061. }
  1062. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1063. size_t identity_len, int phase2,
  1064. struct eap_user *user)
  1065. {
  1066. const struct hostapd_eap_user *eap_user;
  1067. int i, count;
  1068. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1069. if (eap_user == NULL)
  1070. return -1;
  1071. if (user == NULL)
  1072. return 0;
  1073. os_memset(user, 0, sizeof(*user));
  1074. count = EAP_USER_MAX_METHODS;
  1075. if (count > EAP_MAX_METHODS)
  1076. count = EAP_MAX_METHODS;
  1077. for (i = 0; i < count; i++) {
  1078. user->methods[i].vendor = eap_user->methods[i].vendor;
  1079. user->methods[i].method = eap_user->methods[i].method;
  1080. }
  1081. if (eap_user->password) {
  1082. user->password = os_malloc(eap_user->password_len);
  1083. if (user->password == NULL)
  1084. return -1;
  1085. os_memcpy(user->password, eap_user->password,
  1086. eap_user->password_len);
  1087. user->password_len = eap_user->password_len;
  1088. user->password_hash = eap_user->password_hash;
  1089. }
  1090. user->force_version = eap_user->force_version;
  1091. user->ttls_auth = eap_user->ttls_auth;
  1092. return 0;
  1093. }
  1094. static int setup_interface(struct hostapd_iface *iface)
  1095. {
  1096. struct hostapd_data *hapd = iface->bss[0];
  1097. struct hostapd_bss_config *conf = hapd->conf;
  1098. size_t i;
  1099. char country[4];
  1100. u8 *b = conf->bssid;
  1101. int freq;
  1102. size_t j;
  1103. u8 *prev_addr;
  1104. /*
  1105. * Initialize the driver interface and make sure that all BSSes get
  1106. * configured with a pointer to this driver interface.
  1107. */
  1108. if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
  1109. hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
  1110. } else {
  1111. hapd->drv_priv = hostapd_driver_init(hapd);
  1112. }
  1113. if (hapd->drv_priv == NULL) {
  1114. wpa_printf(MSG_ERROR, "%s driver initialization failed.",
  1115. hapd->driver ? hapd->driver->name : "Unknown");
  1116. hapd->driver = NULL;
  1117. return -1;
  1118. }
  1119. for (i = 0; i < iface->num_bss; i++) {
  1120. iface->bss[i]->driver = hapd->driver;
  1121. iface->bss[i]->drv_priv = hapd->drv_priv;
  1122. }
  1123. if (hostapd_validate_bssid_configuration(iface))
  1124. return -1;
  1125. #ifdef CONFIG_IEEE80211N
  1126. SET_2BIT_LE16(&iface->ht_op_mode,
  1127. HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
  1128. OP_MODE_PURE);
  1129. #endif /* CONFIG_IEEE80211N */
  1130. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  1131. os_memcpy(country, hapd->iconf->country, 3);
  1132. country[3] = '\0';
  1133. if (hostapd_set_country(hapd, country) < 0) {
  1134. wpa_printf(MSG_ERROR, "Failed to set country code");
  1135. return -1;
  1136. }
  1137. }
  1138. if (hapd->iconf->ieee80211d &&
  1139. hostapd_set_ieee80211d(hapd, 1) < 0) {
  1140. wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
  1141. hapd->iconf->ieee80211d);
  1142. return -1;
  1143. }
  1144. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1145. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1146. wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
  1147. "kernel driver");
  1148. return -1;
  1149. }
  1150. /* TODO: merge with hostapd_driver_init() ? */
  1151. if (hostapd_wireless_event_init(hapd) < 0)
  1152. return -1;
  1153. if (hostapd_get_hw_features(iface)) {
  1154. /* Not all drivers support this yet, so continue without hw
  1155. * feature data. */
  1156. } else {
  1157. int ret = hostapd_select_hw_mode(iface);
  1158. if (ret < 0) {
  1159. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  1160. "channel. (%d)", ret);
  1161. return -1;
  1162. }
  1163. }
  1164. hostapd_flush_old_stations(hapd);
  1165. hostapd_set_privacy(hapd, 0);
  1166. if (hapd->iconf->channel) {
  1167. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1168. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  1169. "Frequency: %d MHz",
  1170. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1171. hapd->iconf->channel, freq);
  1172. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  1173. hapd->iconf->channel,
  1174. hapd->iconf->ieee80211n,
  1175. hapd->iconf->secondary_channel)) {
  1176. wpa_printf(MSG_ERROR, "Could not set channel for "
  1177. "kernel driver");
  1178. return -1;
  1179. }
  1180. }
  1181. hostapd_broadcast_wep_clear(hapd);
  1182. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  1183. return -1;
  1184. hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
  1185. ieee802_11_set_beacon(hapd);
  1186. if (hapd->iconf->rts_threshold > -1 &&
  1187. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1188. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  1189. "kernel driver");
  1190. return -1;
  1191. }
  1192. if (hapd->iconf->fragm_threshold > -1 &&
  1193. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1194. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  1195. "for kernel driver");
  1196. return -1;
  1197. }
  1198. prev_addr = hapd->own_addr;
  1199. for (j = 0; j < iface->num_bss; j++) {
  1200. hapd = iface->bss[j];
  1201. if (j)
  1202. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1203. if (hostapd_setup_bss(hapd, j == 0))
  1204. return -1;
  1205. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1206. prev_addr = hapd->own_addr;
  1207. }
  1208. hostapd_tx_queue_params(iface);
  1209. ap_list_init(iface);
  1210. if (hostapd_driver_commit(hapd) < 0) {
  1211. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1212. "configuration", __func__);
  1213. return -1;
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * hostapd_setup_interface - Setup of an interface
  1219. * @iface: Pointer to interface data.
  1220. * Returns: 0 on success, -1 on failure
  1221. *
  1222. * Initializes the driver interface, validates the configuration,
  1223. * and sets driver parameters based on the configuration.
  1224. * Flushes old stations, sets the channel, encryption,
  1225. * beacons, and WDS links based on the configuration.
  1226. */
  1227. int hostapd_setup_interface(struct hostapd_iface *iface)
  1228. {
  1229. int ret;
  1230. ret = setup_interface(iface);
  1231. if (ret) {
  1232. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1233. iface->bss[0]->conf->iface);
  1234. eloop_terminate();
  1235. return -1;
  1236. } else if (!hostapd_drv_none(iface->bss[0])) {
  1237. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1238. iface->bss[0]->conf->iface);
  1239. }
  1240. return 0;
  1241. }
  1242. /**
  1243. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1244. * @hapd_iface: Pointer to interface data
  1245. * @conf: Pointer to per-interface configuration
  1246. * @bss: Pointer to per-BSS configuration for this BSS
  1247. * Returns: Pointer to allocated BSS data
  1248. *
  1249. * This function is used to allocate per-BSS data structure. This data will be
  1250. * freed after hostapd_cleanup() is called for it during interface
  1251. * deinitialization.
  1252. */
  1253. struct hostapd_data *
  1254. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1255. struct hostapd_config *conf,
  1256. struct hostapd_bss_config *bss)
  1257. {
  1258. struct hostapd_data *hapd;
  1259. hapd = os_zalloc(sizeof(*hapd));
  1260. if (hapd == NULL)
  1261. return NULL;
  1262. hapd->iconf = conf;
  1263. hapd->conf = bss;
  1264. hapd->iface = hapd_iface;
  1265. if (hapd->conf->individual_wep_key_len > 0) {
  1266. /* use key0 in individual key and key1 in broadcast key */
  1267. hapd->default_wep_key_idx = 1;
  1268. }
  1269. #ifdef EAP_TLS_FUNCS
  1270. if (hapd->conf->eap_server &&
  1271. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1272. hapd->conf->dh_file)) {
  1273. struct tls_connection_params params;
  1274. hapd->ssl_ctx = tls_init(NULL);
  1275. if (hapd->ssl_ctx == NULL) {
  1276. wpa_printf(MSG_ERROR, "Failed to initialize TLS");
  1277. goto fail;
  1278. }
  1279. os_memset(&params, 0, sizeof(params));
  1280. params.ca_cert = hapd->conf->ca_cert;
  1281. params.client_cert = hapd->conf->server_cert;
  1282. params.private_key = hapd->conf->private_key;
  1283. params.private_key_passwd = hapd->conf->private_key_passwd;
  1284. params.dh_file = hapd->conf->dh_file;
  1285. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1286. wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
  1287. goto fail;
  1288. }
  1289. if (tls_global_set_verify(hapd->ssl_ctx,
  1290. hapd->conf->check_crl)) {
  1291. wpa_printf(MSG_ERROR, "Failed to enable check_crl");
  1292. goto fail;
  1293. }
  1294. }
  1295. #endif /* EAP_TLS_FUNCS */
  1296. #ifdef EAP_SERVER
  1297. if (hapd->conf->eap_sim_db) {
  1298. hapd->eap_sim_db_priv =
  1299. eap_sim_db_init(hapd->conf->eap_sim_db,
  1300. hostapd_sim_db_cb, hapd);
  1301. if (hapd->eap_sim_db_priv == NULL) {
  1302. wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
  1303. "database interface");
  1304. goto fail;
  1305. }
  1306. }
  1307. #endif /* EAP_SERVER */
  1308. hapd->driver = hapd->iconf->driver;
  1309. return hapd;
  1310. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1311. fail:
  1312. #endif
  1313. /* TODO: cleanup allocated resources(?) */
  1314. os_free(hapd);
  1315. return NULL;
  1316. }
  1317. void hostapd_interface_deinit(struct hostapd_iface *iface)
  1318. {
  1319. size_t j;
  1320. if (iface == NULL)
  1321. return;
  1322. hostapd_cleanup_iface_pre(iface);
  1323. for (j = 0; j < iface->num_bss; j++) {
  1324. struct hostapd_data *hapd = iface->bss[j];
  1325. hostapd_free_stas(hapd);
  1326. hostapd_flush_old_stations(hapd);
  1327. hostapd_cleanup(hapd);
  1328. if (j == iface->num_bss - 1 && hapd->driver)
  1329. hostapd_driver_deinit(hapd);
  1330. }
  1331. for (j = 0; j < iface->num_bss; j++)
  1332. os_free(iface->bss[j]);
  1333. hostapd_cleanup_iface(iface);
  1334. }