hostapd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "radius/radius_client.h"
  19. #include "drivers/driver.h"
  20. #include "hostapd.h"
  21. #include "authsrv.h"
  22. #include "sta_info.h"
  23. #include "accounting.h"
  24. #include "ap_list.h"
  25. #include "beacon.h"
  26. #include "iapp.h"
  27. #include "ieee802_1x.h"
  28. #include "ieee802_11_auth.h"
  29. #include "vlan_init.h"
  30. #include "wpa_auth.h"
  31. #include "wps_hostapd.h"
  32. #include "hw_features.h"
  33. #include "wpa_auth_glue.h"
  34. #include "ap_drv_ops.h"
  35. #include "ap_config.h"
  36. #include "p2p_hostapd.h"
  37. static int hostapd_flush_old_stations(struct hostapd_data *hapd);
  38. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
  39. extern int wpa_debug_level;
  40. int hostapd_reload_config(struct hostapd_iface *iface)
  41. {
  42. struct hostapd_data *hapd = iface->bss[0];
  43. struct hostapd_config *newconf, *oldconf;
  44. size_t j;
  45. if (iface->config_read_cb == NULL)
  46. return -1;
  47. newconf = iface->config_read_cb(iface->config_fname);
  48. if (newconf == NULL)
  49. return -1;
  50. /*
  51. * Deauthenticate all stations since the new configuration may not
  52. * allow them to use the BSS anymore.
  53. */
  54. for (j = 0; j < iface->num_bss; j++)
  55. hostapd_flush_old_stations(iface->bss[j]);
  56. #ifndef CONFIG_NO_RADIUS
  57. /* TODO: update dynamic data based on changed configuration
  58. * items (e.g., open/close sockets, etc.) */
  59. radius_client_flush(hapd->radius, 0);
  60. #endif /* CONFIG_NO_RADIUS */
  61. oldconf = hapd->iconf;
  62. hapd->iconf = newconf;
  63. hapd->conf = &newconf->bss[0];
  64. iface->conf = newconf;
  65. if (hostapd_setup_wpa_psk(hapd->conf)) {
  66. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  67. "after reloading configuration");
  68. }
  69. if (hapd->conf->ieee802_1x || hapd->conf->wpa)
  70. hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
  71. else
  72. hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
  73. if (hapd->conf->wpa && hapd->wpa_auth == NULL)
  74. hostapd_setup_wpa(hapd);
  75. else if (hapd->conf->wpa) {
  76. const u8 *wpa_ie;
  77. size_t wpa_ie_len;
  78. hostapd_reconfig_wpa(hapd);
  79. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  80. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
  81. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  82. "the kernel driver.");
  83. } else if (hapd->wpa_auth) {
  84. wpa_deinit(hapd->wpa_auth);
  85. hapd->wpa_auth = NULL;
  86. hostapd_set_privacy(hapd, 0);
  87. hostapd_setup_encryption(hapd->conf->iface, hapd);
  88. hostapd_set_generic_elem(hapd, (u8 *) "", 0);
  89. }
  90. ieee802_11_set_beacon(hapd);
  91. hostapd_update_wps(hapd);
  92. if (hapd->conf->ssid.ssid_set &&
  93. hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
  94. hapd->conf->ssid.ssid_len)) {
  95. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  96. /* try to continue */
  97. }
  98. hostapd_config_free(oldconf);
  99. wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
  100. return 0;
  101. }
  102. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  103. char *ifname)
  104. {
  105. int i;
  106. for (i = 0; i < NUM_WEP_KEYS; i++) {
  107. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
  108. i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
  109. wpa_printf(MSG_DEBUG, "Failed to clear default "
  110. "encryption keys (ifname=%s keyidx=%d)",
  111. ifname, i);
  112. }
  113. }
  114. #ifdef CONFIG_IEEE80211W
  115. if (hapd->conf->ieee80211w) {
  116. for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
  117. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL,
  118. i, i == 0 ? 1 : 0, NULL, 0,
  119. NULL, 0)) {
  120. wpa_printf(MSG_DEBUG, "Failed to clear "
  121. "default mgmt encryption keys "
  122. "(ifname=%s keyidx=%d)", ifname, i);
  123. }
  124. }
  125. }
  126. #endif /* CONFIG_IEEE80211W */
  127. }
  128. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  129. {
  130. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  131. return 0;
  132. }
  133. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  134. {
  135. int errors = 0, idx;
  136. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  137. idx = ssid->wep.idx;
  138. if (ssid->wep.default_len &&
  139. hapd->drv.set_key(hapd->conf->iface,
  140. hapd, WPA_ALG_WEP, NULL, idx,
  141. idx == ssid->wep.idx,
  142. NULL, 0, ssid->wep.key[idx],
  143. ssid->wep.len[idx])) {
  144. wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
  145. errors++;
  146. }
  147. if (ssid->dyn_vlan_keys) {
  148. size_t i;
  149. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  150. const char *ifname;
  151. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  152. if (key == NULL)
  153. continue;
  154. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  155. i);
  156. if (ifname == NULL)
  157. continue;
  158. idx = key->idx;
  159. if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL,
  160. idx, idx == key->idx, NULL, 0,
  161. key->key[idx], key->len[idx])) {
  162. wpa_printf(MSG_WARNING, "Could not set "
  163. "dynamic VLAN WEP encryption.");
  164. errors++;
  165. }
  166. }
  167. }
  168. return errors;
  169. }
  170. /**
  171. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  172. * @hapd: Pointer to BSS data
  173. *
  174. * This function is used to free all per-BSS data structures and resources.
  175. * This gets called in a loop for each BSS between calls to
  176. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  177. * is deinitialized. Most of the modules that are initialized in
  178. * hostapd_setup_bss() are deinitialized here.
  179. */
  180. static void hostapd_cleanup(struct hostapd_data *hapd)
  181. {
  182. if (hapd->iface->ctrl_iface_deinit)
  183. hapd->iface->ctrl_iface_deinit(hapd);
  184. iapp_deinit(hapd->iapp);
  185. hapd->iapp = NULL;
  186. accounting_deinit(hapd);
  187. hostapd_deinit_wpa(hapd);
  188. vlan_deinit(hapd);
  189. hostapd_acl_deinit(hapd);
  190. #ifndef CONFIG_NO_RADIUS
  191. radius_client_deinit(hapd->radius);
  192. hapd->radius = NULL;
  193. #endif /* CONFIG_NO_RADIUS */
  194. hostapd_deinit_wps(hapd);
  195. authsrv_deinit(hapd);
  196. if (hapd->interface_added &&
  197. hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
  198. wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
  199. hapd->conf->iface);
  200. }
  201. os_free(hapd->probereq_cb);
  202. hapd->probereq_cb = NULL;
  203. #ifdef CONFIG_P2P
  204. wpabuf_free(hapd->p2p_beacon_ie);
  205. hapd->p2p_beacon_ie = NULL;
  206. wpabuf_free(hapd->p2p_probe_resp_ie);
  207. hapd->p2p_probe_resp_ie = NULL;
  208. #endif /* CONFIG_P2P */
  209. }
  210. /**
  211. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  212. * @iface: Pointer to interface data
  213. *
  214. * This function is called before per-BSS data structures are deinitialized
  215. * with hostapd_cleanup().
  216. */
  217. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  218. {
  219. }
  220. /**
  221. * hostapd_cleanup_iface - Complete per-interface cleanup
  222. * @iface: Pointer to interface data
  223. *
  224. * This function is called after per-BSS data structures are deinitialized
  225. * with hostapd_cleanup().
  226. */
  227. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  228. {
  229. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  230. iface->hw_features = NULL;
  231. os_free(iface->current_rates);
  232. iface->current_rates = NULL;
  233. ap_list_deinit(iface);
  234. hostapd_config_free(iface->conf);
  235. iface->conf = NULL;
  236. os_free(iface->config_fname);
  237. os_free(iface->bss);
  238. os_free(iface);
  239. }
  240. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  241. {
  242. int i;
  243. hostapd_broadcast_wep_set(hapd);
  244. if (hapd->conf->ssid.wep.default_len) {
  245. hostapd_set_privacy(hapd, 1);
  246. return 0;
  247. }
  248. for (i = 0; i < 4; i++) {
  249. if (hapd->conf->ssid.wep.key[i] &&
  250. hapd->drv.set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
  251. i == hapd->conf->ssid.wep.idx, NULL, 0,
  252. hapd->conf->ssid.wep.key[i],
  253. hapd->conf->ssid.wep.len[i])) {
  254. wpa_printf(MSG_WARNING, "Could not set WEP "
  255. "encryption.");
  256. return -1;
  257. }
  258. if (hapd->conf->ssid.wep.key[i] &&
  259. i == hapd->conf->ssid.wep.idx)
  260. hostapd_set_privacy(hapd, 1);
  261. }
  262. return 0;
  263. }
  264. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  265. {
  266. int ret = 0;
  267. if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
  268. return 0;
  269. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  270. if (hostapd_flush(hapd)) {
  271. wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
  272. ret = -1;
  273. }
  274. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  275. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  276. * broadcast deauth frame. This gets the firmware in odd state where
  277. * nothing works correctly, so let's skip sending this for the hostap
  278. * driver. */
  279. if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
  280. u8 addr[ETH_ALEN];
  281. os_memset(addr, 0xff, ETH_ALEN);
  282. hapd->drv.sta_deauth(hapd, addr,
  283. WLAN_REASON_PREV_AUTH_NOT_VALID);
  284. }
  285. return ret;
  286. }
  287. /**
  288. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  289. * @iface: Pointer to interface data
  290. * Returns: 0 on success, -1 on failure
  291. *
  292. * This function is used to validate that the configured BSSIDs are valid.
  293. */
  294. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  295. {
  296. u8 mask[ETH_ALEN] = { 0 };
  297. struct hostapd_data *hapd = iface->bss[0];
  298. unsigned int i = iface->conf->num_bss, bits = 0, j;
  299. int res;
  300. int auto_addr = 0;
  301. if (hostapd_drv_none(hapd))
  302. return 0;
  303. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  304. /* Determine the bits necessary to cover the number of BSSIDs. */
  305. for (i--; i; i >>= 1)
  306. bits++;
  307. /* Determine the bits necessary to any configured BSSIDs,
  308. if they are higher than the number of BSSIDs. */
  309. for (j = 0; j < iface->conf->num_bss; j++) {
  310. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
  311. if (j)
  312. auto_addr++;
  313. continue;
  314. }
  315. for (i = 0; i < ETH_ALEN; i++) {
  316. mask[i] |=
  317. iface->conf->bss[j].bssid[i] ^
  318. hapd->own_addr[i];
  319. }
  320. }
  321. if (!auto_addr)
  322. goto skip_mask_ext;
  323. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  324. ;
  325. j = 0;
  326. if (i < ETH_ALEN) {
  327. j = (5 - i) * 8;
  328. while (mask[i] != 0) {
  329. mask[i] >>= 1;
  330. j++;
  331. }
  332. }
  333. if (bits < j)
  334. bits = j;
  335. if (bits > 40) {
  336. wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
  337. bits);
  338. return -1;
  339. }
  340. os_memset(mask, 0xff, ETH_ALEN);
  341. j = bits / 8;
  342. for (i = 5; i > 5 - j; i--)
  343. mask[i] = 0;
  344. j = bits % 8;
  345. while (j--)
  346. mask[i] <<= 1;
  347. skip_mask_ext:
  348. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  349. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  350. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  351. if (res == 0)
  352. return 0;
  353. if (res < 0) {
  354. wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
  355. MACSTR " for start address " MACSTR ".",
  356. MAC2STR(mask), MAC2STR(hapd->own_addr));
  357. return -1;
  358. }
  359. if (!auto_addr)
  360. return 0;
  361. for (i = 0; i < ETH_ALEN; i++) {
  362. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  363. wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
  364. " for start address " MACSTR ".",
  365. MAC2STR(mask), MAC2STR(hapd->own_addr));
  366. wpa_printf(MSG_ERROR, "Start address must be the "
  367. "first address in the block (i.e., addr "
  368. "AND mask == addr).");
  369. return -1;
  370. }
  371. }
  372. return 0;
  373. }
  374. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  375. {
  376. size_t i;
  377. for (i = 0; i < conf->num_bss; i++) {
  378. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  379. return 1;
  380. }
  381. }
  382. return 0;
  383. }
  384. /**
  385. * hostapd_setup_bss - Per-BSS setup (initialization)
  386. * @hapd: Pointer to BSS data
  387. * @first: Whether this BSS is the first BSS of an interface
  388. *
  389. * This function is used to initialize all per-BSS data structures and
  390. * resources. This gets called in a loop for each BSS when an interface is
  391. * initialized. Most of the modules that are initialized here will be
  392. * deinitialized in hostapd_cleanup().
  393. */
  394. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  395. {
  396. struct hostapd_bss_config *conf = hapd->conf;
  397. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  398. int ssid_len, set_ssid;
  399. char force_ifname[IFNAMSIZ];
  400. u8 if_addr[ETH_ALEN];
  401. if (!first) {
  402. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  403. /* Allocate the next available BSSID. */
  404. do {
  405. inc_byte_array(hapd->own_addr, ETH_ALEN);
  406. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  407. } else {
  408. /* Allocate the configured BSSID. */
  409. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  410. if (hostapd_mac_comp(hapd->own_addr,
  411. hapd->iface->bss[0]->own_addr) ==
  412. 0) {
  413. wpa_printf(MSG_ERROR, "BSS '%s' may not have "
  414. "BSSID set to the MAC address of "
  415. "the radio", hapd->conf->iface);
  416. return -1;
  417. }
  418. }
  419. hapd->interface_added = 1;
  420. if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
  421. hapd->conf->iface, hapd->own_addr, hapd,
  422. &hapd->drv_priv, force_ifname, if_addr)) {
  423. wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
  424. MACSTR ")", MAC2STR(hapd->own_addr));
  425. return -1;
  426. }
  427. }
  428. hostapd_flush_old_stations(hapd);
  429. hostapd_set_privacy(hapd, 0);
  430. hostapd_broadcast_wep_clear(hapd);
  431. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  432. return -1;
  433. /*
  434. * Fetch the SSID from the system and use it or,
  435. * if one was specified in the config file, verify they
  436. * match.
  437. */
  438. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  439. if (ssid_len < 0) {
  440. wpa_printf(MSG_ERROR, "Could not read SSID from system");
  441. return -1;
  442. }
  443. if (conf->ssid.ssid_set) {
  444. /*
  445. * If SSID is specified in the config file and it differs
  446. * from what is being used then force installation of the
  447. * new SSID.
  448. */
  449. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  450. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  451. } else {
  452. /*
  453. * No SSID in the config file; just use the one we got
  454. * from the system.
  455. */
  456. set_ssid = 0;
  457. conf->ssid.ssid_len = ssid_len;
  458. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  459. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  460. }
  461. if (!hostapd_drv_none(hapd)) {
  462. wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
  463. " and ssid '%s'",
  464. hapd->conf->iface, MAC2STR(hapd->own_addr),
  465. hapd->conf->ssid.ssid);
  466. }
  467. if (hostapd_setup_wpa_psk(conf)) {
  468. wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
  469. return -1;
  470. }
  471. /* Set SSID for the kernel driver (to be used in beacon and probe
  472. * response frames) */
  473. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  474. conf->ssid.ssid_len)) {
  475. wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
  476. return -1;
  477. }
  478. if (wpa_debug_level == MSG_MSGDUMP)
  479. conf->radius->msg_dumps = 1;
  480. #ifndef CONFIG_NO_RADIUS
  481. hapd->radius = radius_client_init(hapd, conf->radius);
  482. if (hapd->radius == NULL) {
  483. wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
  484. return -1;
  485. }
  486. #endif /* CONFIG_NO_RADIUS */
  487. if (hostapd_acl_init(hapd)) {
  488. wpa_printf(MSG_ERROR, "ACL initialization failed.");
  489. return -1;
  490. }
  491. if (hostapd_init_wps(hapd, conf))
  492. return -1;
  493. if (authsrv_init(hapd) < 0)
  494. return -1;
  495. if (ieee802_1x_init(hapd)) {
  496. wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
  497. return -1;
  498. }
  499. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  500. return -1;
  501. if (accounting_init(hapd)) {
  502. wpa_printf(MSG_ERROR, "Accounting initialization failed.");
  503. return -1;
  504. }
  505. if (hapd->conf->ieee802_11f &&
  506. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  507. wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
  508. "failed.");
  509. return -1;
  510. }
  511. if (hapd->iface->ctrl_iface_init &&
  512. hapd->iface->ctrl_iface_init(hapd)) {
  513. wpa_printf(MSG_ERROR, "Failed to setup control interface");
  514. return -1;
  515. }
  516. if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
  517. wpa_printf(MSG_ERROR, "VLAN initialization failed.");
  518. return -1;
  519. }
  520. ieee802_11_set_beacon(hapd);
  521. if (hapd->driver && hapd->driver->set_operstate)
  522. hapd->driver->set_operstate(hapd->drv_priv, 1);
  523. return 0;
  524. }
  525. static void hostapd_tx_queue_params(struct hostapd_iface *iface)
  526. {
  527. struct hostapd_data *hapd = iface->bss[0];
  528. int i;
  529. struct hostapd_tx_queue_params *p;
  530. for (i = 0; i < NUM_TX_QUEUES; i++) {
  531. p = &iface->conf->tx_queue[i];
  532. if (!p->configured)
  533. continue;
  534. if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
  535. p->cwmax, p->burst)) {
  536. wpa_printf(MSG_DEBUG, "Failed to set TX queue "
  537. "parameters for queue %d.", i);
  538. /* Continue anyway */
  539. }
  540. }
  541. }
  542. static int setup_interface(struct hostapd_iface *iface)
  543. {
  544. struct hostapd_data *hapd = iface->bss[0];
  545. size_t i;
  546. char country[4];
  547. /*
  548. * Make sure that all BSSes get configured with a pointer to the same
  549. * driver interface.
  550. */
  551. for (i = 1; i < iface->num_bss; i++) {
  552. iface->bss[i]->driver = hapd->driver;
  553. iface->bss[i]->drv_priv = hapd->drv_priv;
  554. }
  555. if (hostapd_validate_bssid_configuration(iface))
  556. return -1;
  557. if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
  558. os_memcpy(country, hapd->iconf->country, 3);
  559. country[3] = '\0';
  560. if (hostapd_set_country(hapd, country) < 0) {
  561. wpa_printf(MSG_ERROR, "Failed to set country code");
  562. return -1;
  563. }
  564. }
  565. if (hostapd_get_hw_features(iface)) {
  566. /* Not all drivers support this yet, so continue without hw
  567. * feature data. */
  568. } else {
  569. int ret = hostapd_select_hw_mode(iface);
  570. if (ret < 0) {
  571. wpa_printf(MSG_ERROR, "Could not select hw_mode and "
  572. "channel. (%d)", ret);
  573. return -1;
  574. }
  575. ret = hostapd_check_ht_capab(iface);
  576. if (ret < 0)
  577. return -1;
  578. if (ret == 1) {
  579. wpa_printf(MSG_DEBUG, "Interface initialization will "
  580. "be completed in a callback");
  581. return 0;
  582. }
  583. }
  584. return hostapd_setup_interface_complete(iface, 0);
  585. }
  586. int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
  587. {
  588. struct hostapd_data *hapd = iface->bss[0];
  589. size_t j;
  590. u8 *prev_addr;
  591. if (err) {
  592. wpa_printf(MSG_ERROR, "Interface initialization failed");
  593. eloop_terminate();
  594. return -1;
  595. }
  596. wpa_printf(MSG_DEBUG, "Completing interface initialization");
  597. if (hapd->iconf->channel) {
  598. iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  599. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
  600. "Frequency: %d MHz",
  601. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  602. hapd->iconf->channel, iface->freq);
  603. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
  604. hapd->iconf->channel,
  605. hapd->iconf->ieee80211n,
  606. hapd->iconf->secondary_channel)) {
  607. wpa_printf(MSG_ERROR, "Could not set channel for "
  608. "kernel driver");
  609. return -1;
  610. }
  611. }
  612. if (iface->current_mode) {
  613. if (hostapd_prepare_rates(hapd, iface->current_mode)) {
  614. wpa_printf(MSG_ERROR, "Failed to prepare rates "
  615. "table.");
  616. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  617. HOSTAPD_LEVEL_WARNING,
  618. "Failed to prepare rates table.");
  619. return -1;
  620. }
  621. }
  622. if (hapd->iconf->rts_threshold > -1 &&
  623. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  624. wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
  625. "kernel driver");
  626. return -1;
  627. }
  628. if (hapd->iconf->fragm_threshold > -1 &&
  629. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  630. wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
  631. "for kernel driver");
  632. return -1;
  633. }
  634. prev_addr = hapd->own_addr;
  635. for (j = 0; j < iface->num_bss; j++) {
  636. hapd = iface->bss[j];
  637. if (j)
  638. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  639. if (hostapd_setup_bss(hapd, j == 0))
  640. return -1;
  641. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  642. prev_addr = hapd->own_addr;
  643. }
  644. hostapd_tx_queue_params(iface);
  645. ap_list_init(iface);
  646. if (hostapd_driver_commit(hapd) < 0) {
  647. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  648. "configuration", __func__);
  649. return -1;
  650. }
  651. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  652. iface->bss[0]->conf->iface);
  653. return 0;
  654. }
  655. /**
  656. * hostapd_setup_interface - Setup of an interface
  657. * @iface: Pointer to interface data.
  658. * Returns: 0 on success, -1 on failure
  659. *
  660. * Initializes the driver interface, validates the configuration,
  661. * and sets driver parameters based on the configuration.
  662. * Flushes old stations, sets the channel, encryption,
  663. * beacons, and WDS links based on the configuration.
  664. */
  665. int hostapd_setup_interface(struct hostapd_iface *iface)
  666. {
  667. int ret;
  668. ret = setup_interface(iface);
  669. if (ret) {
  670. wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
  671. iface->bss[0]->conf->iface);
  672. return -1;
  673. }
  674. return 0;
  675. }
  676. /**
  677. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  678. * @hapd_iface: Pointer to interface data
  679. * @conf: Pointer to per-interface configuration
  680. * @bss: Pointer to per-BSS configuration for this BSS
  681. * Returns: Pointer to allocated BSS data
  682. *
  683. * This function is used to allocate per-BSS data structure. This data will be
  684. * freed after hostapd_cleanup() is called for it during interface
  685. * deinitialization.
  686. */
  687. struct hostapd_data *
  688. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  689. struct hostapd_config *conf,
  690. struct hostapd_bss_config *bss)
  691. {
  692. struct hostapd_data *hapd;
  693. hapd = os_zalloc(sizeof(*hapd));
  694. if (hapd == NULL)
  695. return NULL;
  696. hostapd_set_driver_ops(&hapd->drv);
  697. hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
  698. hapd->iconf = conf;
  699. hapd->conf = bss;
  700. hapd->iface = hapd_iface;
  701. hapd->driver = hapd->iconf->driver;
  702. return hapd;
  703. }
  704. void hostapd_interface_deinit(struct hostapd_iface *iface)
  705. {
  706. size_t j;
  707. if (iface == NULL)
  708. return;
  709. hostapd_cleanup_iface_pre(iface);
  710. for (j = 0; j < iface->num_bss; j++) {
  711. struct hostapd_data *hapd = iface->bss[j];
  712. hostapd_free_stas(hapd);
  713. hostapd_flush_old_stations(hapd);
  714. hostapd_cleanup(hapd);
  715. }
  716. }
  717. void hostapd_interface_free(struct hostapd_iface *iface)
  718. {
  719. size_t j;
  720. for (j = 0; j < iface->num_bss; j++)
  721. os_free(iface->bss[j]);
  722. hostapd_cleanup_iface(iface);
  723. }
  724. /**
  725. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  726. * @hapd: Pointer to BSS data
  727. * @sta: Pointer to the associated STA data
  728. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  729. *
  730. * This function will be called whenever a station associates with the AP. It
  731. * can be called from ieee802_11.c for drivers that export MLME to hostapd and
  732. * from drv_callbacks.c based on driver events for drivers that take care of
  733. * management frames (IEEE 802.11 authentication and association) internally.
  734. */
  735. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  736. int reassoc)
  737. {
  738. if (hapd->tkip_countermeasures) {
  739. hapd->drv.sta_deauth(hapd, sta->addr,
  740. WLAN_REASON_MICHAEL_MIC_FAILURE);
  741. return;
  742. }
  743. hostapd_prune_associations(hapd, sta->addr);
  744. /* IEEE 802.11F (IAPP) */
  745. if (hapd->conf->ieee802_11f)
  746. iapp_new_station(hapd->iapp, sta);
  747. #ifdef CONFIG_P2P
  748. if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
  749. sta->no_p2p_set = 1;
  750. hapd->num_sta_no_p2p++;
  751. if (hapd->num_sta_no_p2p == 1)
  752. hostapd_p2p_non_p2p_sta_connected(hapd);
  753. }
  754. #endif /* CONFIG_P2P */
  755. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  756. * IEEE 802.1X/WPA code will start accounting after the station has
  757. * been authorized. */
  758. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
  759. accounting_sta_start(hapd, sta);
  760. /* Start IEEE 802.1X authentication process for new stations */
  761. ieee802_1x_new_station(hapd, sta);
  762. if (reassoc) {
  763. if (sta->auth_alg != WLAN_AUTH_FT &&
  764. !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
  765. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  766. } else
  767. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  768. }