hostapd.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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 "common.h"
  16. #include "eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "eapol_auth/eapol_auth_sm.h"
  19. #include "eapol_auth/eapol_auth_sm_i.h"
  20. #include "radius/radius_client.h"
  21. #include "eap_server/eap.h"
  22. #include "l2_packet/l2_packet.h"
  23. #include "ap/hostapd.h"
  24. #include "ap/authsrv.h"
  25. #include "ap/sta_info.h"
  26. #include "ap/accounting.h"
  27. #include "ap/ap_list.h"
  28. #include "ap/beacon.h"
  29. #include "ap/iapp.h"
  30. #include "ap/ieee802_1x.h"
  31. #include "ap/ieee802_11_auth.h"
  32. #include "ap/preauth.h"
  33. #include "ap/tkip_countermeasures.h"
  34. #include "ap/vlan_init.h"
  35. #include "ap/wpa.h"
  36. #include "ap/wps_hostapd.h"
  37. #include "hw_features.h"
  38. #include "driver_i.h"
  39. #include "ctrl_iface.h"
  40. static int hostapd_flush_old_stations(struct hostapd_data *hapd);
  41. static int hostapd_setup_wpa(struct hostapd_data *hapd);
  42. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  43. extern int wpa_debug_level;
  44. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  45. struct wpa_auth_config *wconf)
  46. {
  47. wconf->wpa = conf->wpa;
  48. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  49. wconf->wpa_pairwise = conf->wpa_pairwise;
  50. wconf->wpa_group = conf->wpa_group;
  51. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  52. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  53. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  54. wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
  55. wconf->rsn_pairwise = conf->rsn_pairwise;
  56. wconf->rsn_preauth = conf->rsn_preauth;
  57. wconf->eapol_version = conf->eapol_version;
  58. wconf->peerkey = conf->peerkey;
  59. wconf->wmm_enabled = conf->wmm_enabled;
  60. wconf->okc = conf->okc;
  61. #ifdef CONFIG_IEEE80211W
  62. wconf->ieee80211w = conf->ieee80211w;
  63. #endif /* CONFIG_IEEE80211W */
  64. #ifdef CONFIG_IEEE80211R
  65. wconf->ssid_len = conf->ssid.ssid_len;
  66. if (wconf->ssid_len > SSID_LEN)
  67. wconf->ssid_len = SSID_LEN;
  68. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  69. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  70. MOBILITY_DOMAIN_ID_LEN);
  71. if (conf->nas_identifier &&
  72. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  73. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  74. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  75. wconf->r0_key_holder_len);
  76. }
  77. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  78. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  79. wconf->reassociation_deadline = conf->reassociation_deadline;
  80. wconf->r0kh_list = conf->r0kh_list;
  81. wconf->r1kh_list = conf->r1kh_list;
  82. wconf->pmk_r1_push = conf->pmk_r1_push;
  83. #endif /* CONFIG_IEEE80211R */
  84. }
  85. int hostapd_reload_config(struct hostapd_iface *iface)
  86. {
  87. struct hostapd_data *hapd = iface->bss[0];
  88. struct hostapd_config *newconf, *oldconf;
  89. struct wpa_auth_config wpa_auth_conf;
  90. size_t j;
  91. if (iface->config_read_cb == NULL)
  92. return -1;
  93. newconf = iface->config_read_cb(iface->config_fname);
  94. if (newconf == NULL)
  95. return -1;
  96. /*
  97. * Deauthenticate all stations since the new configuration may not
  98. * allow them to use the BSS anymore.
  99. */
  100. for (j = 0; j < iface->num_bss; j++)
  101. hostapd_flush_old_stations(iface->bss[j]);
  102. #ifndef CONFIG_NO_RADIUS
  103. /* TODO: update dynamic data based on changed configuration
  104. * items (e.g., open/close sockets, etc.) */
  105. radius_client_flush(hapd->radius, 0);
  106. #endif /* CONFIG_NO_RADIUS */
  107. oldconf = hapd->iconf;
  108. hapd->iconf = newconf;
  109. hapd->conf = &newconf->bss[0];
  110. iface->conf = newconf;
  111. if (hostapd_setup_wpa_psk(hapd->conf)) {
  112. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  113. "after reloading configuration");
  114. }
  115. if (hapd->conf->wpa && hapd->wpa_auth == NULL)
  116. hostapd_setup_wpa(hapd);
  117. else if (hapd->conf->wpa) {
  118. hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
  119. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  120. } else if (hapd->wpa_auth) {
  121. wpa_deinit(hapd->wpa_auth);
  122. hapd->wpa_auth = NULL;
  123. hostapd_set_privacy(hapd, 0);
  124. hostapd_setup_encryption(hapd->conf->iface, hapd);
  125. }
  126. ieee802_11_set_beacon(hapd);
  127. if (hapd->conf->ssid.ssid_set &&
  128. hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
  129. hapd->conf->ssid.ssid_len)) {
  130. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  131. /* try to continue */
  132. }
  133. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  134. hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
  135. else
  136. hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  137. hostapd_config_free(oldconf);
  138. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  139. return 0;
  140. }
  141. int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
  142. {
  143. if (hostapd_reload_config(iface) < 0) {
  144. wpa_printf(MSG_WARNING, "Failed to read new configuration "
  145. "file - continuing with old.");
  146. }
  147. return 0;
  148. }
  149. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  150. char *ifname)
  151. {
  152. int i;
  153. for (i = 0; i < NUM_WEP_KEYS; i++) {
  154. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  155. i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
  156. wpa_printf(MSG_DEBUG, "Failed to clear default "
  157. "encryption keys (ifname=%s keyidx=%d)",
  158. ifname, i);
  159. }
  160. }
  161. #ifdef CONFIG_IEEE80211W
  162. if (hapd->conf->ieee80211w) {
  163. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  164. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL,
  165. i, i == 0 ? 1 : 0, NULL, 0,
  166. NULL, 0)) {
  167. wpa_printf(MSG_DEBUG, "Failed to clear "
  168. "default mgmt encryption keys "
  169. "(ifname=%s keyidx=%d)", ifname, i);
  170. }
  171. }
  172. }
  173. #endif /* CONFIG_IEEE80211W */
  174. }
  175. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  176. {
  177. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  178. return 0;
  179. }
  180. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  181. {
  182. int errors = 0, idx;
  183. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  184. idx = ssid->wep.idx;
  185. if (ssid->wep.default_len &&
  186. hapd->drv.set_key(hapd->conf->iface,
  187. hapd, WPA_ALG_WEP, NULL, idx,
  188. idx == ssid->wep.idx,
  189. NULL, 0, ssid->wep.key[idx],
  190. ssid->wep.len[idx])) {
  191. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  192. errors++;
  193. }
  194. if (ssid->dyn_vlan_keys) {
  195. size_t i;
  196. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  197. const char *ifname;
  198. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  199. if (key == NULL)
  200. continue;
  201. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  202. i);
  203. if (ifname == NULL)
  204. continue;
  205. idx = key->idx;
  206. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL,
  207. idx, idx == key->idx, NULL, 0,
  208. key->key[idx], key->len[idx])) {
  209. wpa_printf(MSG_WARNING, "Could not set "
  210. "dynamic VLAN WEP encryption.");
  211. errors++;
  212. }
  213. }
  214. }
  215. return errors;
  216. }
  217. /**
  218. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  219. * @hapd: Pointer to BSS data
  220. *
  221. * This function is used to free all per-BSS data structures and resources.
  222. * This gets called in a loop for each BSS between calls to
  223. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  224. * is deinitialized. Most of the modules that are initialized in
  225. * hostapd_setup_bss() are deinitialized here.
  226. */
  227. static void hostapd_cleanup(struct hostapd_data *hapd)
  228. {
  229. hostapd_ctrl_iface_deinit(hapd);
  230. iapp_deinit(hapd->iapp);
  231. hapd->iapp = NULL;
  232. accounting_deinit(hapd);
  233. rsn_preauth_iface_deinit(hapd);
  234. if (hapd->wpa_auth) {
  235. wpa_deinit(hapd->wpa_auth);
  236. hapd->wpa_auth = NULL;
  237. if (hostapd_set_privacy(hapd, 0)) {
  238. wpa_printf(MSG_DEBUG, "Could not disable "
  239. "PrivacyInvoked for interface %s",
  240. hapd->conf->iface);
  241. }
  242. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  243. wpa_printf(MSG_DEBUG, "Could not remove generic "
  244. "information element from interface %s",
  245. hapd->conf->iface);
  246. }
  247. }
  248. ieee802_1x_deinit(hapd);
  249. vlan_deinit(hapd);
  250. hostapd_acl_deinit(hapd);
  251. #ifndef CONFIG_NO_RADIUS
  252. radius_client_deinit(hapd->radius);
  253. hapd->radius = NULL;
  254. #endif /* CONFIG_NO_RADIUS */
  255. #ifdef CONFIG_IEEE80211R
  256. l2_packet_deinit(hapd->l2);
  257. #endif /* CONFIG_IEEE80211R */
  258. hostapd_deinit_wps(hapd);
  259. authsrv_deinit(hapd);
  260. if (hapd->interface_added &&
  261. hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
  262. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  263. hapd->conf->iface);
  264. }
  265. os_free(hapd->probereq_cb);
  266. hapd->probereq_cb = NULL;
  267. }
  268. /**
  269. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  270. * @iface: Pointer to interface data
  271. *
  272. * This function is called before per-BSS data structures are deinitialized
  273. * with hostapd_cleanup().
  274. */
  275. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  276. {
  277. }
  278. /**
  279. * hostapd_cleanup_iface - Complete per-interface cleanup
  280. * @iface: Pointer to interface data
  281. *
  282. * This function is called after per-BSS data structures are deinitialized
  283. * with hostapd_cleanup().
  284. */
  285. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  286. {
  287. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  288. iface->hw_features = NULL;
  289. os_free(iface->current_rates);
  290. iface->current_rates = NULL;
  291. ap_list_deinit(iface);
  292. hostapd_config_free(iface->conf);
  293. iface->conf = NULL;
  294. os_free(iface->config_fname);
  295. os_free(iface->bss);
  296. os_free(iface);
  297. }
  298. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  299. {
  300. int i;
  301. hostapd_broadcast_wep_set(hapd);
  302. if (hapd->conf->ssid.wep.default_len) {
  303. hostapd_set_privacy(hapd, 1);
  304. return 0;
  305. }
  306. for (i = 0; i < 4; i++) {
  307. if (hapd->conf->ssid.wep.key[i] &&
  308. hapd->drv.set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  309. i == hapd->conf->ssid.wep.idx, NULL, 0,
  310. hapd->conf->ssid.wep.key[i],
  311. hapd->conf->ssid.wep.len[i])) {
  312. wpa_printf(MSG_WARNING, "Could not set WEP "
  313. "encryption.");
  314. return -1;
  315. }
  316. if (hapd->conf->ssid.wep.key[i] &&
  317. i == hapd->conf->ssid.wep.idx)
  318. hostapd_set_privacy(hapd, 1);
  319. }
  320. return 0;
  321. }
  322. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  323. {
  324. int ret = 0;
  325. if (hostapd_drv_none(hapd))
  326. return 0;
  327. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  328. if (hostapd_flush(hapd)) {
  329. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  330. ret = -1;
  331. }
  332. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  333. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  334. * broadcast deauth frame. This gets the firmware in odd state where
  335. * nothing works correctly, so let's skip sending this for the hostap
  336. * driver. */
  337. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  338. u8 addr[ETH_ALEN];
  339. os_memset(addr, 0xff, ETH_ALEN);
  340. hapd->drv.sta_deauth(hapd, addr,
  341. WLAN_REASON_PREV_AUTH_NOT_VALID);
  342. }
  343. return ret;
  344. }
  345. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  346. logger_level level, const char *txt)
  347. {
  348. #ifndef CONFIG_NO_HOSTAPD_LOGGER
  349. struct hostapd_data *hapd = ctx;
  350. int hlevel;
  351. switch (level) {
  352. case LOGGER_WARNING:
  353. hlevel = HOSTAPD_LEVEL_WARNING;
  354. break;
  355. case LOGGER_INFO:
  356. hlevel = HOSTAPD_LEVEL_INFO;
  357. break;
  358. case LOGGER_DEBUG:
  359. default:
  360. hlevel = HOSTAPD_LEVEL_DEBUG;
  361. break;
  362. }
  363. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  364. #endif /* CONFIG_NO_HOSTAPD_LOGGER */
  365. }
  366. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  367. u16 reason)
  368. {
  369. struct hostapd_data *hapd = ctx;
  370. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  371. "STA " MACSTR " reason %d",
  372. __func__, MAC2STR(addr), reason);
  373. ap_sta_disconnect(hapd, NULL, addr, reason);
  374. }
  375. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  376. {
  377. struct hostapd_data *hapd = ctx;
  378. michael_mic_failure(hapd, addr, 0);
  379. }
  380. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  381. wpa_eapol_variable var, int value)
  382. {
  383. struct hostapd_data *hapd = ctx;
  384. struct sta_info *sta = ap_get_sta(hapd, addr);
  385. if (sta == NULL)
  386. return;
  387. switch (var) {
  388. case WPA_EAPOL_portEnabled:
  389. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  390. break;
  391. case WPA_EAPOL_portValid:
  392. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  393. break;
  394. case WPA_EAPOL_authorized:
  395. ieee802_1x_set_sta_authorized(hapd, sta, value);
  396. break;
  397. case WPA_EAPOL_portControl_Auto:
  398. if (sta->eapol_sm)
  399. sta->eapol_sm->portControl = Auto;
  400. break;
  401. case WPA_EAPOL_keyRun:
  402. if (sta->eapol_sm)
  403. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  404. break;
  405. case WPA_EAPOL_keyAvailable:
  406. if (sta->eapol_sm)
  407. sta->eapol_sm->eap_if->eapKeyAvailable =
  408. value ? TRUE : FALSE;
  409. break;
  410. case WPA_EAPOL_keyDone:
  411. if (sta->eapol_sm)
  412. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  413. break;
  414. case WPA_EAPOL_inc_EapolFramesTx:
  415. if (sta->eapol_sm)
  416. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  417. break;
  418. }
  419. }
  420. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  421. wpa_eapol_variable var)
  422. {
  423. struct hostapd_data *hapd = ctx;
  424. struct sta_info *sta = ap_get_sta(hapd, addr);
  425. if (sta == NULL || sta->eapol_sm == NULL)
  426. return -1;
  427. switch (var) {
  428. case WPA_EAPOL_keyRun:
  429. return sta->eapol_sm->keyRun;
  430. case WPA_EAPOL_keyAvailable:
  431. return sta->eapol_sm->eap_if->eapKeyAvailable;
  432. default:
  433. return -1;
  434. }
  435. }
  436. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  437. const u8 *prev_psk)
  438. {
  439. struct hostapd_data *hapd = ctx;
  440. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  441. }
  442. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  443. size_t *len)
  444. {
  445. struct hostapd_data *hapd = ctx;
  446. const u8 *key;
  447. size_t keylen;
  448. struct sta_info *sta;
  449. sta = ap_get_sta(hapd, addr);
  450. if (sta == NULL)
  451. return -1;
  452. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  453. if (key == NULL)
  454. return -1;
  455. if (keylen > *len)
  456. keylen = *len;
  457. os_memcpy(msk, key, keylen);
  458. *len = keylen;
  459. return 0;
  460. }
  461. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
  462. const u8 *addr, int idx, u8 *key,
  463. size_t key_len)
  464. {
  465. struct hostapd_data *hapd = ctx;
  466. const char *ifname = hapd->conf->iface;
  467. if (vlan_id > 0) {
  468. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  469. if (ifname == NULL)
  470. return -1;
  471. }
  472. return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
  473. key, key_len);
  474. }
  475. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  476. u8 *seq)
  477. {
  478. struct hostapd_data *hapd = ctx;
  479. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  480. }
  481. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  482. const u8 *data, size_t data_len,
  483. int encrypt)
  484. {
  485. struct hostapd_data *hapd = ctx;
  486. return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
  487. }
  488. static int hostapd_wpa_auth_for_each_sta(
  489. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  490. void *cb_ctx)
  491. {
  492. struct hostapd_data *hapd = ctx;
  493. struct sta_info *sta;
  494. for (sta = hapd->sta_list; sta; sta = sta->next) {
  495. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. struct wpa_auth_iface_iter_data {
  501. int (*cb)(struct wpa_authenticator *sm, void *ctx);
  502. void *cb_ctx;
  503. };
  504. static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
  505. {
  506. struct wpa_auth_iface_iter_data *data = ctx;
  507. size_t i;
  508. for (i = 0; i < iface->num_bss; i++) {
  509. if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. static int hostapd_wpa_auth_for_each_auth(
  515. void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
  516. void *cb_ctx)
  517. {
  518. struct hostapd_data *hapd = ctx;
  519. struct wpa_auth_iface_iter_data data;
  520. data.cb = cb;
  521. data.cb_ctx = cb_ctx;
  522. return hostapd_for_each_interface(hapd->iface->interfaces,
  523. wpa_auth_iface_iter, &data);
  524. }
  525. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  526. const u8 *data, size_t data_len)
  527. {
  528. struct hostapd_data *hapd = ctx;
  529. if (hapd->driver && hapd->driver->send_ether)
  530. return hapd->driver->send_ether(hapd->drv_priv, dst,
  531. hapd->own_addr, proto,
  532. data, data_len);
  533. if (hapd->l2 == NULL)
  534. return -1;
  535. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  536. }
  537. #ifdef CONFIG_IEEE80211R
  538. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  539. const u8 *data, size_t data_len)
  540. {
  541. struct hostapd_data *hapd = ctx;
  542. int res;
  543. struct ieee80211_mgmt *m;
  544. size_t mlen;
  545. struct sta_info *sta;
  546. sta = ap_get_sta(hapd, dst);
  547. if (sta == NULL || sta->wpa_sm == NULL)
  548. return -1;
  549. m = os_zalloc(sizeof(*m) + data_len);
  550. if (m == NULL)
  551. return -1;
  552. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  553. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  554. WLAN_FC_STYPE_ACTION);
  555. os_memcpy(m->da, dst, ETH_ALEN);
  556. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  557. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  558. os_memcpy(&m->u, data, data_len);
  559. res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
  560. os_free(m);
  561. return res;
  562. }
  563. static struct wpa_state_machine *
  564. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  565. {
  566. struct hostapd_data *hapd = ctx;
  567. struct sta_info *sta;
  568. sta = ap_sta_add(hapd, sta_addr);
  569. if (sta == NULL)
  570. return NULL;
  571. if (sta->wpa_sm)
  572. return sta->wpa_sm;
  573. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  574. if (sta->wpa_sm == NULL) {
  575. ap_free_sta(hapd, sta);
  576. return NULL;
  577. }
  578. sta->auth_alg = WLAN_AUTH_FT;
  579. return sta->wpa_sm;
  580. }
  581. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  582. size_t len)
  583. {
  584. struct hostapd_data *hapd = ctx;
  585. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  586. }
  587. #endif /* CONFIG_IEEE80211R */
  588. /**
  589. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  590. * @iface: Pointer to interface data
  591. * Returns: 0 on success, -1 on failure
  592. *
  593. * This function is used to validate that the configured BSSIDs are valid.
  594. */
  595. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  596. {
  597. u8 mask[ETH_ALEN] = { 0 };
  598. struct hostapd_data *hapd = iface->bss[0];
  599. unsigned int i = iface->conf->num_bss, bits = 0, j;
  600. int res;
  601. int auto_addr = 0;
  602. if (hostapd_drv_none(hapd))
  603. return 0;
  604. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  605. /* Determine the bits necessary to cover the number of BSSIDs. */
  606. for (i--; i; i >>= 1)
  607. bits++;
  608. /* Determine the bits necessary to any configured BSSIDs,
  609. if they are higher than the number of BSSIDs. */
  610. for (j = 0; j < iface->conf->num_bss; j++) {
  611. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
  612. if (j)
  613. auto_addr++;
  614. continue;
  615. }
  616. for (i = 0; i < ETH_ALEN; i++) {
  617. mask[i] |=
  618. iface->conf->bss[j].bssid[i] ^
  619. hapd->own_addr[i];
  620. }
  621. }
  622. if (!auto_addr)
  623. goto skip_mask_ext;
  624. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  625. ;
  626. j = 0;
  627. if (i < ETH_ALEN) {
  628. j = (5 - i) * 8;
  629. while (mask[i] != 0) {
  630. mask[i] >>= 1;
  631. j++;
  632. }
  633. }
  634. if (bits < j)
  635. bits = j;
  636. if (bits > 40) {
  637. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  638. bits);
  639. return -1;
  640. }
  641. os_memset(mask, 0xff, ETH_ALEN);
  642. j = bits / 8;
  643. for (i = 5; i > 5 - j; i--)
  644. mask[i] = 0;
  645. j = bits % 8;
  646. while (j--)
  647. mask[i] <<= 1;
  648. skip_mask_ext:
  649. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  650. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  651. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  652. if (res == 0)
  653. return 0;
  654. if (res < 0) {
  655. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  656. MACSTR " for start address " MACSTR ".",
  657. MAC2STR(mask), MAC2STR(hapd->own_addr));
  658. return -1;
  659. }
  660. if (!auto_addr)
  661. return 0;
  662. for (i = 0; i < ETH_ALEN; i++) {
  663. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  664. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  665. " for start address " MACSTR ".",
  666. MAC2STR(mask), MAC2STR(hapd->own_addr));
  667. wpa_printf(MSG_ERROR, "Start address must be the "
  668. "first address in the block (i.e., addr "
  669. "AND mask == addr).");
  670. return -1;
  671. }
  672. }
  673. return 0;
  674. }
  675. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  676. {
  677. size_t i;
  678. for (i = 0; i < conf->num_bss; i++) {
  679. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  680. return 1;
  681. }
  682. }
  683. return 0;
  684. }
  685. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  686. {
  687. struct wpa_auth_config _conf;
  688. struct wpa_auth_callbacks cb;
  689. const u8 *wpa_ie;
  690. size_t wpa_ie_len;
  691. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  692. os_memset(&cb, 0, sizeof(cb));
  693. cb.ctx = hapd;
  694. cb.logger = hostapd_wpa_auth_logger;
  695. cb.disconnect = hostapd_wpa_auth_disconnect;
  696. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  697. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  698. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  699. cb.get_psk = hostapd_wpa_auth_get_psk;
  700. cb.get_msk = hostapd_wpa_auth_get_msk;
  701. cb.set_key = hostapd_wpa_auth_set_key;
  702. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  703. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  704. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  705. cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
  706. cb.send_ether = hostapd_wpa_auth_send_ether;
  707. #ifdef CONFIG_IEEE80211R
  708. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  709. cb.add_sta = hostapd_wpa_auth_add_sta;
  710. #endif /* CONFIG_IEEE80211R */
  711. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  712. if (hapd->wpa_auth == NULL) {
  713. wpa_printf(MSG_ERROR, "WPA initialization failed.");
  714. return -1;
  715. }
  716. if (hostapd_set_privacy(hapd, 1)) {
  717. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  718. "for interface %s", hapd->conf->iface);
  719. return -1;
  720. }
  721. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  722. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  723. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  724. "the kernel driver.");
  725. return -1;
  726. }
  727. if (rsn_preauth_iface_init(hapd)) {
  728. wpa_printf(MSG_ERROR, "Initialization of RSN "
  729. "pre-authentication failed.");
  730. return -1;
  731. }
  732. return 0;
  733. }
  734. /**
  735. * hostapd_setup_bss - Per-BSS setup (initialization)
  736. * @hapd: Pointer to BSS data
  737. * @first: Whether this BSS is the first BSS of an interface
  738. *
  739. * This function is used to initialize all per-BSS data structures and
  740. * resources. This gets called in a loop for each BSS when an interface is
  741. * initialized. Most of the modules that are initialized here will be
  742. * deinitialized in hostapd_cleanup().
  743. */
  744. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  745. {
  746. struct hostapd_bss_config *conf = hapd->conf;
  747. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  748. int ssid_len, set_ssid;
  749. if (!first) {
  750. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  751. /* Allocate the next available BSSID. */
  752. do {
  753. inc_byte_array(hapd->own_addr, ETH_ALEN);
  754. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  755. } else {
  756. /* Allocate the configured BSSID. */
  757. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  758. if (hostapd_mac_comp(hapd->own_addr,
  759. hapd->iface->bss[0]->own_addr) ==
  760. 0) {
  761. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  762. "BSSID set to the MAC address of "
  763. "the radio", hapd->conf->iface);
  764. return -1;
  765. }
  766. }
  767. hapd->interface_added = 1;
  768. if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
  769. hapd->conf->iface, hapd->own_addr, hapd)) {
  770. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  771. MACSTR ")", MAC2STR(hapd->own_addr));
  772. return -1;
  773. }
  774. }
  775. hostapd_flush_old_stations(hapd);
  776. hostapd_set_privacy(hapd, 0);
  777. hostapd_broadcast_wep_clear(hapd);
  778. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  779. return -1;
  780. /*
  781. * Fetch the SSID from the system and use it or,
  782. * if one was specified in the config file, verify they
  783. * match.
  784. */
  785. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  786. if (ssid_len < 0) {
  787. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  788. return -1;
  789. }
  790. if (conf->ssid.ssid_set) {
  791. /*
  792. * If SSID is specified in the config file and it differs
  793. * from what is being used then force installation of the
  794. * new SSID.
  795. */
  796. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  797. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  798. } else {
  799. /*
  800. * No SSID in the config file; just use the one we got
  801. * from the system.
  802. */
  803. set_ssid = 0;
  804. conf->ssid.ssid_len = ssid_len;
  805. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  806. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  807. }
  808. if (!hostapd_drv_none(hapd)) {
  809. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  810. " and ssid '%s'",
  811. hapd->conf->iface, MAC2STR(hapd->own_addr),
  812. hapd->conf->ssid.ssid);
  813. }
  814. if (hostapd_setup_wpa_psk(conf)) {
  815. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  816. return -1;
  817. }
  818. /* Set SSID for the kernel driver (to be used in beacon and probe
  819. * response frames) */
  820. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  821. conf->ssid.ssid_len)) {
  822. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  823. return -1;
  824. }
  825. if (wpa_debug_level == MSG_MSGDUMP)
  826. conf->radius->msg_dumps = 1;
  827. #ifndef CONFIG_NO_RADIUS
  828. hapd->radius = radius_client_init(hapd, conf->radius);
  829. if (hapd->radius == NULL) {
  830. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  831. return -1;
  832. }
  833. #endif /* CONFIG_NO_RADIUS */
  834. if (hostapd_acl_init(hapd)) {
  835. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  836. return -1;
  837. }
  838. if (hostapd_init_wps(hapd, conf))
  839. return -1;
  840. if (ieee802_1x_init(hapd)) {
  841. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  842. return -1;
  843. }
  844. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  845. return -1;
  846. if (accounting_init(hapd)) {
  847. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  848. return -1;
  849. }
  850. if (hapd->conf->ieee802_11f &&
  851. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  852. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  853. "failed.");
  854. return -1;
  855. }
  856. if (hostapd_ctrl_iface_init(hapd)) {
  857. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  858. return -1;
  859. }
  860. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  861. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  862. return -1;
  863. }
  864. #ifdef CONFIG_IEEE80211R
  865. if (!hostapd_drv_none(hapd)) {
  866. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  867. hostapd_rrb_receive, hapd, 0);
  868. if (hapd->l2 == NULL &&
  869. (hapd->driver == NULL ||
  870. hapd->driver->send_ether == NULL)) {
  871. wpa_printf(MSG_ERROR, "Failed to open l2_packet "
  872. "interface");
  873. return -1;
  874. }
  875. }
  876. #endif /* CONFIG_IEEE80211R */
  877. ieee802_11_set_beacon(hapd);
  878. if (authsrv_init(hapd) < 0)
  879. return -1;
  880. return 0;
  881. }
  882. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  883. {
  884. struct hostapd_data *hapd = iface->bss[0];
  885. int i;
  886. struct hostapd_tx_queue_params *p;
  887. for (i = 0; i < NUM_TX_QUEUES; i++) {
  888. p = &iface->conf->tx_queue[i];
  889. if (!p->configured)
  890. continue;
  891. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  892. p->cwmax, p->burst)) {
  893. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  894. "parameters for queue %d.", i);
  895. /* Continue anyway */
  896. }
  897. }
  898. }
  899. static int setup_interface(struct hostapd_iface *iface)
  900. {
  901. struct hostapd_data *hapd = iface->bss[0];
  902. size_t i;
  903. char country[4];
  904. /*
  905. * Make sure that all BSSes get configured with a pointer to the same
  906. * driver interface.
  907. */
  908. for (i = 1; i < iface->num_bss; i++) {
  909. iface->bss[i]->driver = hapd->driver;
  910. iface->bss[i]->drv_priv = hapd->drv_priv;
  911. }
  912. if (hostapd_validate_bssid_configuration(iface))
  913. return -1;
  914. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  915. os_memcpy(country, hapd->iconf->country, 3);
  916. country[3] = '\0';
  917. if (hostapd_set_country(hapd, country) < 0) {
  918. wpa_printf(MSG_ERROR, "Failed to set country code");
  919. return -1;
  920. }
  921. }
  922. if (hostapd_get_hw_features(iface)) {
  923. /* Not all drivers support this yet, so continue without hw
  924. * feature data. */
  925. } else {
  926. int ret = hostapd_select_hw_mode(iface);
  927. if (ret < 0) {
  928. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  929. "channel. (%d)", ret);
  930. return -1;
  931. }
  932. ret = hostapd_check_ht_capab(iface);
  933. if (ret < 0)
  934. return -1;
  935. if (ret == 1) {
  936. wpa_printf(MSG_DEBUG, "Interface initialization will "
  937. "be completed in a callback");
  938. return 0;
  939. }
  940. }
  941. return hostapd_setup_interface_complete(iface, 0);
  942. }
  943. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  944. {
  945. struct hostapd_data *hapd = iface->bss[0];
  946. int freq;
  947. size_t j;
  948. u8 *prev_addr;
  949. if (err) {
  950. wpa_printf(MSG_ERROR, "Interface initialization failed");
  951. eloop_terminate();
  952. return -1;
  953. }
  954. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  955. if (hapd->iconf->channel) {
  956. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  957. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  958. "Frequency: %d MHz",
  959. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  960. hapd->iconf->channel, freq);
  961. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
  962. hapd->iconf->channel,
  963. hapd->iconf->ieee80211n,
  964. hapd->iconf->secondary_channel)) {
  965. wpa_printf(MSG_ERROR, "Could not set channel for "
  966. "kernel driver");
  967. return -1;
  968. }
  969. }
  970. if (hapd->iconf->rts_threshold > -1 &&
  971. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  972. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  973. "kernel driver");
  974. return -1;
  975. }
  976. if (hapd->iconf->fragm_threshold > -1 &&
  977. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  978. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  979. "for kernel driver");
  980. return -1;
  981. }
  982. prev_addr = hapd->own_addr;
  983. for (j = 0; j < iface->num_bss; j++) {
  984. hapd = iface->bss[j];
  985. if (j)
  986. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  987. if (hostapd_setup_bss(hapd, j == 0))
  988. return -1;
  989. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  990. prev_addr = hapd->own_addr;
  991. }
  992. hostapd_tx_queue_params(iface);
  993. ap_list_init(iface);
  994. if (hostapd_driver_commit(hapd) < 0) {
  995. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  996. "configuration", __func__);
  997. return -1;
  998. }
  999. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1000. iface->bss[0]->conf->iface);
  1001. return 0;
  1002. }
  1003. /**
  1004. * hostapd_setup_interface - Setup of an interface
  1005. * @iface: Pointer to interface data.
  1006. * Returns: 0 on success, -1 on failure
  1007. *
  1008. * Initializes the driver interface, validates the configuration,
  1009. * and sets driver parameters based on the configuration.
  1010. * Flushes old stations, sets the channel, encryption,
  1011. * beacons, and WDS links based on the configuration.
  1012. */
  1013. int hostapd_setup_interface(struct hostapd_iface *iface)
  1014. {
  1015. int ret;
  1016. ret = setup_interface(iface);
  1017. if (ret) {
  1018. wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
  1019. iface->bss[0]->conf->iface);
  1020. return -1;
  1021. }
  1022. return 0;
  1023. }
  1024. /**
  1025. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1026. * @hapd_iface: Pointer to interface data
  1027. * @conf: Pointer to per-interface configuration
  1028. * @bss: Pointer to per-BSS configuration for this BSS
  1029. * Returns: Pointer to allocated BSS data
  1030. *
  1031. * This function is used to allocate per-BSS data structure. This data will be
  1032. * freed after hostapd_cleanup() is called for it during interface
  1033. * deinitialization.
  1034. */
  1035. struct hostapd_data *
  1036. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1037. struct hostapd_config *conf,
  1038. struct hostapd_bss_config *bss)
  1039. {
  1040. struct hostapd_data *hapd;
  1041. hapd = os_zalloc(sizeof(*hapd));
  1042. if (hapd == NULL)
  1043. return NULL;
  1044. hostapd_set_driver_ops(&hapd->drv);
  1045. hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
  1046. hapd->iconf = conf;
  1047. hapd->conf = bss;
  1048. hapd->iface = hapd_iface;
  1049. hapd->driver = hapd->iconf->driver;
  1050. return hapd;
  1051. }
  1052. void hostapd_interface_deinit(struct hostapd_iface *iface)
  1053. {
  1054. size_t j;
  1055. if (iface == NULL)
  1056. return;
  1057. hostapd_cleanup_iface_pre(iface);
  1058. for (j = 0; j < iface->num_bss; j++) {
  1059. struct hostapd_data *hapd = iface->bss[j];
  1060. hostapd_free_stas(hapd);
  1061. hostapd_flush_old_stations(hapd);
  1062. hostapd_cleanup(hapd);
  1063. if (j == iface->num_bss - 1 && hapd->driver)
  1064. hostapd_driver_deinit(hapd);
  1065. }
  1066. for (j = 0; j < iface->num_bss; j++)
  1067. os_free(iface->bss[j]);
  1068. hostapd_cleanup_iface(iface);
  1069. }
  1070. struct prune_data {
  1071. struct hostapd_data *hapd;
  1072. const u8 *addr;
  1073. };
  1074. static int prune_associations(struct hostapd_iface *iface, void *ctx)
  1075. {
  1076. struct prune_data *data = ctx;
  1077. struct sta_info *osta;
  1078. struct hostapd_data *ohapd;
  1079. size_t j;
  1080. for (j = 0; j < iface->num_bss; j++) {
  1081. ohapd = iface->bss[j];
  1082. if (ohapd == data->hapd)
  1083. continue;
  1084. osta = ap_get_sta(ohapd, data->addr);
  1085. if (!osta)
  1086. continue;
  1087. ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
  1088. }
  1089. return 0;
  1090. }
  1091. /**
  1092. * hostapd_prune_associations - Remove extraneous associations
  1093. * @hapd: Pointer to BSS data for the most recent association
  1094. * @sta: Pointer to the associated STA data
  1095. *
  1096. * This function looks through all radios and BSS's for previous
  1097. * (stale) associations of STA. If any are found they are removed.
  1098. */
  1099. static void hostapd_prune_associations(struct hostapd_data *hapd,
  1100. struct sta_info *sta)
  1101. {
  1102. struct prune_data data;
  1103. data.hapd = hapd;
  1104. data.addr = sta->addr;
  1105. hostapd_for_each_interface(hapd->iface->interfaces,
  1106. prune_associations, &data);
  1107. }
  1108. /**
  1109. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  1110. * @hapd: Pointer to BSS data
  1111. * @sta: Pointer to the associated STA data
  1112. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  1113. *
  1114. * This function will be called whenever a station associates with the AP. It
  1115. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  1116. * from drv_callbacks.c based on driver events for drivers that take care of
  1117. * management frames (IEEE 802.11 authentication and association) internally.
  1118. */
  1119. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  1120. int reassoc)
  1121. {
  1122. if (hapd->tkip_countermeasures) {
  1123. hapd->drv.sta_deauth(hapd, sta->addr,
  1124. WLAN_REASON_MICHAEL_MIC_FAILURE);
  1125. return;
  1126. }
  1127. hostapd_prune_associations(hapd, sta);
  1128. /* IEEE 802.11F (IAPP) */
  1129. if (hapd->conf->ieee802_11f)
  1130. iapp_new_station(hapd->iapp, sta);
  1131. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  1132. * IEEE 802.1X/WPA code will start accounting after the station has
  1133. * been authorized. */
  1134. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
  1135. accounting_sta_start(hapd, sta);
  1136. /* Start IEEE 802.1X authentication process for new stations */
  1137. ieee802_1x_new_station(hapd, sta);
  1138. if (reassoc) {
  1139. if (sta->auth_alg != WLAN_AUTH_FT &&
  1140. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  1141. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  1142. } else
  1143. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  1144. }