hw_features.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * hostapd / Hardware feature query and different modes
  3. * Copyright 2002-2003, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "hostapd.h"
  17. #include "hw_features.h"
  18. #include "driver.h"
  19. #include "config.h"
  20. #include "eloop.h"
  21. void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
  22. size_t num_hw_features)
  23. {
  24. size_t i;
  25. if (hw_features == NULL)
  26. return;
  27. for (i = 0; i < num_hw_features; i++) {
  28. os_free(hw_features[i].channels);
  29. os_free(hw_features[i].rates);
  30. }
  31. os_free(hw_features);
  32. }
  33. int hostapd_get_hw_features(struct hostapd_iface *iface)
  34. {
  35. struct hostapd_data *hapd = iface->bss[0];
  36. int ret = 0, i, j;
  37. u16 num_modes, flags;
  38. struct hostapd_hw_modes *modes;
  39. if (hostapd_drv_none(hapd))
  40. return -1;
  41. modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
  42. if (modes == NULL) {
  43. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  44. HOSTAPD_LEVEL_DEBUG,
  45. "Fetching hardware channel/rate support not "
  46. "supported.");
  47. return -1;
  48. }
  49. iface->hw_flags = flags;
  50. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  51. iface->hw_features = modes;
  52. iface->num_hw_features = num_modes;
  53. for (i = 0; i < num_modes; i++) {
  54. struct hostapd_hw_modes *feature = &modes[i];
  55. /* set flag for channels we can use in current regulatory
  56. * domain */
  57. for (j = 0; j < feature->num_channels; j++) {
  58. /*
  59. * Disable all channels that are marked not to allow
  60. * IBSS operation or active scanning. In addition,
  61. * disable all channels that require radar detection,
  62. * since that (in addition to full DFS) is not yet
  63. * supported.
  64. */
  65. if (feature->channels[j].flag &
  66. (HOSTAPD_CHAN_NO_IBSS |
  67. HOSTAPD_CHAN_PASSIVE_SCAN |
  68. HOSTAPD_CHAN_RADAR))
  69. feature->channels[j].flag |=
  70. HOSTAPD_CHAN_DISABLED;
  71. if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  72. continue;
  73. wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
  74. "chan=%d freq=%d MHz",
  75. feature->mode,
  76. feature->channels[j].chan,
  77. feature->channels[j].freq);
  78. }
  79. }
  80. return ret;
  81. }
  82. static int hostapd_prepare_rates(struct hostapd_data *hapd,
  83. struct hostapd_hw_modes *mode)
  84. {
  85. int i, num_basic_rates = 0;
  86. int basic_rates_a[] = { 60, 120, 240, -1 };
  87. int basic_rates_b[] = { 10, 20, -1 };
  88. int basic_rates_g[] = { 10, 20, 55, 110, -1 };
  89. int *basic_rates;
  90. if (hapd->iconf->basic_rates)
  91. basic_rates = hapd->iconf->basic_rates;
  92. else switch (mode->mode) {
  93. case HOSTAPD_MODE_IEEE80211A:
  94. basic_rates = basic_rates_a;
  95. break;
  96. case HOSTAPD_MODE_IEEE80211B:
  97. basic_rates = basic_rates_b;
  98. break;
  99. case HOSTAPD_MODE_IEEE80211G:
  100. basic_rates = basic_rates_g;
  101. break;
  102. default:
  103. return -1;
  104. }
  105. if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
  106. basic_rates, mode->mode)) {
  107. wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
  108. "module");
  109. }
  110. os_free(hapd->iface->current_rates);
  111. hapd->iface->num_rates = 0;
  112. hapd->iface->current_rates =
  113. os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
  114. if (!hapd->iface->current_rates) {
  115. wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
  116. "table.");
  117. return -1;
  118. }
  119. for (i = 0; i < mode->num_rates; i++) {
  120. struct hostapd_rate_data *rate;
  121. if (hapd->iconf->supported_rates &&
  122. !hostapd_rate_found(hapd->iconf->supported_rates,
  123. mode->rates[i].rate))
  124. continue;
  125. rate = &hapd->iface->current_rates[hapd->iface->num_rates];
  126. os_memcpy(rate, &mode->rates[i],
  127. sizeof(struct hostapd_rate_data));
  128. if (hostapd_rate_found(basic_rates, rate->rate)) {
  129. rate->flags |= HOSTAPD_RATE_BASIC;
  130. num_basic_rates++;
  131. } else
  132. rate->flags &= ~HOSTAPD_RATE_BASIC;
  133. wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
  134. hapd->iface->num_rates, rate->rate, rate->flags);
  135. hapd->iface->num_rates++;
  136. }
  137. if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
  138. wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
  139. "rate sets (%d,%d).",
  140. hapd->iface->num_rates, num_basic_rates);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. static void select_hw_mode_start(void *eloop_data, void *user_ctx);
  146. static void select_hw_mode2_handler(void *eloop_data, void *user_ctx);
  147. /**
  148. * select_hw_mode_finalize - Finish select HW mode & call the callback
  149. * @iface: Pointer to interface data.
  150. * @status: Status of the select HW mode (0 on success; -1 on failure).
  151. * Returns: 0 on success; -1 on failure (e.g., was not in progress).
  152. */
  153. static int select_hw_mode_finalize(struct hostapd_iface *iface, int status)
  154. {
  155. hostapd_iface_cb cb;
  156. if (!iface->hw_mode_sel_cb)
  157. return -1;
  158. eloop_cancel_timeout(select_hw_mode_start, iface, NULL);
  159. eloop_cancel_timeout(select_hw_mode2_handler, iface, NULL);
  160. cb = iface->hw_mode_sel_cb;
  161. iface->hw_mode_sel_cb = NULL;
  162. cb(iface, status);
  163. return 0;
  164. }
  165. /**
  166. * select_hw_mode2 - Select the hardware mode (part 2)
  167. * @iface: Pointer to interface data.
  168. * @status: Status of auto chanel selection.
  169. *
  170. * Setup the rates and passive scanning based on the configuration.
  171. */
  172. static void select_hw_mode2(struct hostapd_iface *iface, int status)
  173. {
  174. int ret = status;
  175. if (ret)
  176. goto fail;
  177. if (iface->current_mode == NULL) {
  178. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  179. HOSTAPD_LEVEL_WARNING,
  180. "Hardware does not support configured channel");
  181. ret = -1;
  182. goto fail;
  183. }
  184. if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
  185. wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
  186. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  187. HOSTAPD_LEVEL_WARNING,
  188. "Failed to prepare rates table.");
  189. ret = -1;
  190. goto fail;
  191. }
  192. ret = hostapd_passive_scan(iface->bss[0], 0,
  193. iface->conf->passive_scan_mode,
  194. iface->conf->passive_scan_interval,
  195. iface->conf->passive_scan_listen,
  196. NULL, NULL);
  197. if (ret) {
  198. if (ret == -1) {
  199. wpa_printf(MSG_DEBUG, "Passive scanning not "
  200. "supported");
  201. } else {
  202. wpa_printf(MSG_ERROR, "Could not set passive "
  203. "scanning: %s", strerror(ret));
  204. }
  205. ret = 0;
  206. }
  207. fail:
  208. select_hw_mode_finalize(iface, ret);
  209. }
  210. /**
  211. * select_hw_mode2_handler - Calls select_hw_mode2 when auto chan isn't used
  212. * @eloop_data: Stores the struct hostapd_iface * for the interface.
  213. * @user_ctx: Unused.
  214. */
  215. static void select_hw_mode2_handler(void *eloop_data, void *user_ctx)
  216. {
  217. struct hostapd_iface *iface = eloop_data;
  218. select_hw_mode2(iface, 0);
  219. }
  220. /**
  221. * select_hw_mode1 - Select the hardware mode (part 1)
  222. * @iface: Pointer to interface data.
  223. * Returns: 0 on success; -1 on failure.
  224. *
  225. * Setup the hardware mode and channel based on the configuration.
  226. * Schedules select_hw_mode2() to be called immediately or after automatic
  227. * channel selection takes place.
  228. */
  229. static int select_hw_mode1(struct hostapd_iface *iface)
  230. {
  231. int i, j, ok;
  232. if (iface->num_hw_features < 1)
  233. return -1;
  234. iface->current_mode = NULL;
  235. for (i = 0; i < iface->num_hw_features; i++) {
  236. struct hostapd_hw_modes *mode = &iface->hw_features[i];
  237. if (mode->mode == (int) iface->conf->hw_mode) {
  238. iface->current_mode = mode;
  239. break;
  240. }
  241. }
  242. if (iface->current_mode == NULL) {
  243. wpa_printf(MSG_ERROR, "Hardware does not support configured "
  244. "mode");
  245. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  246. HOSTAPD_LEVEL_WARNING,
  247. "Hardware does not support configured mode "
  248. "(%d)", (int) iface->conf->hw_mode);
  249. return -1;
  250. }
  251. ok = 0;
  252. for (j = 0; j < iface->current_mode->num_channels; j++) {
  253. struct hostapd_channel_data *chan =
  254. &iface->current_mode->channels[j];
  255. if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
  256. (chan->chan == iface->conf->channel)) {
  257. ok = 1;
  258. break;
  259. }
  260. }
  261. if (ok == 0 && iface->conf->channel != 0) {
  262. hostapd_logger(iface->bss[0], NULL,
  263. HOSTAPD_MODULE_IEEE80211,
  264. HOSTAPD_LEVEL_WARNING,
  265. "Configured channel (%d) not found from the "
  266. "channel list of current mode (%d) %s",
  267. iface->conf->channel,
  268. iface->current_mode->mode,
  269. hostapd_hw_mode_txt(iface->current_mode->mode));
  270. iface->current_mode = NULL;
  271. }
  272. /*
  273. * Calls select_hw_mode2() via a handler, so that the function is
  274. * always executed from eloop.
  275. */
  276. eloop_register_timeout(0, 0, select_hw_mode2_handler, iface, NULL);
  277. return 0;
  278. }
  279. /**
  280. * select_hw_mode_start - Handler to start select HW mode
  281. * @eloop_data: Stores the struct hostapd_iface * for the interface.
  282. * @user_ctx: Unused.
  283. *
  284. * An eloop handler is used so that all errors can be processed by the
  285. * callback without introducing stack recursion.
  286. */
  287. static void select_hw_mode_start(void *eloop_data, void *user_ctx)
  288. {
  289. struct hostapd_iface *iface = (struct hostapd_iface *)eloop_data;
  290. int ret;
  291. ret = select_hw_mode1(iface);
  292. if (ret)
  293. select_hw_mode_finalize(iface, ret);
  294. }
  295. /**
  296. * hostapd_select_hw_mode_start - Start selection of the hardware mode
  297. * @iface: Pointer to interface data.
  298. * @cb: The function to callback when done.
  299. * Returns: 0 if it starts successfully; cb will be called when done.
  300. * -1 on failure; cb will not be called.
  301. *
  302. * Sets up the hardware mode, channel, rates, and passive scanning
  303. * based on the configuration.
  304. */
  305. int hostapd_select_hw_mode_start(struct hostapd_iface *iface,
  306. hostapd_iface_cb cb)
  307. {
  308. if (iface->hw_mode_sel_cb) {
  309. wpa_printf(MSG_DEBUG,
  310. "%s: Hardware mode select already in progress.",
  311. iface->bss[0]->conf->iface);
  312. return -1;
  313. }
  314. iface->hw_mode_sel_cb = cb;
  315. eloop_register_timeout(0, 0, select_hw_mode_start, iface, NULL);
  316. return 0;
  317. }
  318. /**
  319. * hostapd_auto_chan_select_stop - Stops automatic channel selection
  320. * @iface: Pointer to interface data.
  321. * Returns: 0 if successfully stopped;
  322. * -1 on failure (i.e., was not in progress)
  323. */
  324. int hostapd_select_hw_mode_stop(struct hostapd_iface *iface)
  325. {
  326. return select_hw_mode_finalize(iface, -1);
  327. }
  328. const char * hostapd_hw_mode_txt(int mode)
  329. {
  330. switch (mode) {
  331. case HOSTAPD_MODE_IEEE80211A:
  332. return "IEEE 802.11a";
  333. case HOSTAPD_MODE_IEEE80211B:
  334. return "IEEE 802.11b";
  335. case HOSTAPD_MODE_IEEE80211G:
  336. return "IEEE 802.11g";
  337. default:
  338. return "UNKNOWN";
  339. }
  340. }
  341. int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
  342. {
  343. int i;
  344. if (!hapd->iface->current_mode)
  345. return 0;
  346. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  347. struct hostapd_channel_data *ch =
  348. &hapd->iface->current_mode->channels[i];
  349. if (ch->chan == chan)
  350. return ch->freq;
  351. }
  352. return 0;
  353. }
  354. int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
  355. {
  356. int i;
  357. if (!hapd->iface->current_mode)
  358. return 0;
  359. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  360. struct hostapd_channel_data *ch =
  361. &hapd->iface->current_mode->channels[i];
  362. if (ch->freq == freq)
  363. return ch->chan;
  364. }
  365. return 0;
  366. }