hostapd.c 22 KB

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