hw_features.c 11 KB

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