hostapd.c 23 KB

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