hw_features.c 11 KB

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