hw_features.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * hostapd / Hardware feature query and different modes
  3. * Copyright 2002-2003, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include "hostapd.h"
  18. #include "hw_features.h"
  19. #include "driver.h"
  20. #include "config.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 max_tx_power=%d dBm",
  75. feature->mode,
  76. feature->channels[j].chan,
  77. feature->channels[j].freq,
  78. feature->channels[j].max_tx_power);
  79. }
  80. }
  81. return ret;
  82. }
  83. static int hostapd_prepare_rates(struct hostapd_data *hapd,
  84. struct hostapd_hw_modes *mode)
  85. {
  86. int i, num_basic_rates = 0;
  87. int basic_rates_a[] = { 60, 120, 240, -1 };
  88. int basic_rates_b[] = { 10, 20, -1 };
  89. int basic_rates_g[] = { 10, 20, 55, 110, -1 };
  90. int *basic_rates;
  91. if (hapd->iconf->basic_rates)
  92. basic_rates = hapd->iconf->basic_rates;
  93. else switch (mode->mode) {
  94. case HOSTAPD_MODE_IEEE80211A:
  95. basic_rates = basic_rates_a;
  96. break;
  97. case HOSTAPD_MODE_IEEE80211B:
  98. basic_rates = basic_rates_b;
  99. break;
  100. case HOSTAPD_MODE_IEEE80211G:
  101. basic_rates = basic_rates_g;
  102. break;
  103. default:
  104. return -1;
  105. }
  106. if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
  107. basic_rates, mode->mode)) {
  108. wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
  109. "module");
  110. }
  111. os_free(hapd->iface->current_rates);
  112. hapd->iface->num_rates = 0;
  113. hapd->iface->current_rates =
  114. os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
  115. if (!hapd->iface->current_rates) {
  116. wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
  117. "table.");
  118. return -1;
  119. }
  120. for (i = 0; i < mode->num_rates; i++) {
  121. struct hostapd_rate_data *rate;
  122. if (hapd->iconf->supported_rates &&
  123. !hostapd_rate_found(hapd->iconf->supported_rates,
  124. mode->rates[i].rate))
  125. continue;
  126. rate = &hapd->iface->current_rates[hapd->iface->num_rates];
  127. os_memcpy(rate, &mode->rates[i],
  128. sizeof(struct hostapd_rate_data));
  129. if (hostapd_rate_found(basic_rates, rate->rate)) {
  130. rate->flags |= HOSTAPD_RATE_BASIC;
  131. num_basic_rates++;
  132. } else
  133. rate->flags &= ~HOSTAPD_RATE_BASIC;
  134. wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
  135. hapd->iface->num_rates, rate->rate, rate->flags);
  136. hapd->iface->num_rates++;
  137. }
  138. if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
  139. wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
  140. "rate sets (%d,%d).",
  141. hapd->iface->num_rates, num_basic_rates);
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * hostapd_select_hw_mode - Select the hardware mode
  148. * @iface: Pointer to interface data.
  149. * Returns: 0 on success, -1 on failure
  150. *
  151. * Sets up the hardware mode, channel, rates, and passive scanning
  152. * based on the configuration.
  153. */
  154. int hostapd_select_hw_mode(struct hostapd_iface *iface)
  155. {
  156. int i, j, ok, ret;
  157. if (iface->num_hw_features < 1)
  158. return -1;
  159. iface->current_mode = NULL;
  160. for (i = 0; i < iface->num_hw_features; i++) {
  161. struct hostapd_hw_modes *mode = &iface->hw_features[i];
  162. if (mode->mode == (int) iface->conf->hw_mode) {
  163. iface->current_mode = mode;
  164. break;
  165. }
  166. }
  167. if (iface->current_mode == NULL) {
  168. wpa_printf(MSG_ERROR, "Hardware does not support configured "
  169. "mode");
  170. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  171. HOSTAPD_LEVEL_WARNING,
  172. "Hardware does not support configured mode "
  173. "(%d)", (int) iface->conf->hw_mode);
  174. return -1;
  175. }
  176. ok = 0;
  177. for (j = 0; j < iface->current_mode->num_channels; j++) {
  178. struct hostapd_channel_data *chan =
  179. &iface->current_mode->channels[j];
  180. if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
  181. (chan->chan == iface->conf->channel)) {
  182. ok = 1;
  183. break;
  184. }
  185. }
  186. if (ok == 0 && iface->conf->channel != 0) {
  187. hostapd_logger(iface->bss[0], NULL,
  188. HOSTAPD_MODULE_IEEE80211,
  189. HOSTAPD_LEVEL_WARNING,
  190. "Configured channel (%d) not found from the "
  191. "channel list of current mode (%d) %s",
  192. iface->conf->channel,
  193. iface->current_mode->mode,
  194. hostapd_hw_mode_txt(iface->current_mode->mode));
  195. iface->current_mode = NULL;
  196. }
  197. if (iface->current_mode == NULL) {
  198. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  199. HOSTAPD_LEVEL_WARNING,
  200. "Hardware does not support configured channel");
  201. return -1;
  202. }
  203. if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
  204. wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
  205. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  206. HOSTAPD_LEVEL_WARNING,
  207. "Failed to prepare rates table.");
  208. return -1;
  209. }
  210. ret = hostapd_passive_scan(iface->bss[0], 0,
  211. iface->conf->passive_scan_mode,
  212. iface->conf->passive_scan_interval,
  213. iface->conf->passive_scan_listen,
  214. NULL, NULL);
  215. if (ret) {
  216. if (ret == -1) {
  217. wpa_printf(MSG_DEBUG, "Passive scanning not "
  218. "supported");
  219. } else {
  220. wpa_printf(MSG_ERROR, "Could not set passive "
  221. "scanning: %s", strerror(ret));
  222. }
  223. ret = 0;
  224. }
  225. return ret;
  226. }
  227. const char * hostapd_hw_mode_txt(int mode)
  228. {
  229. switch (mode) {
  230. case HOSTAPD_MODE_IEEE80211A:
  231. return "IEEE 802.11a";
  232. case HOSTAPD_MODE_IEEE80211B:
  233. return "IEEE 802.11b";
  234. case HOSTAPD_MODE_IEEE80211G:
  235. return "IEEE 802.11g";
  236. default:
  237. return "UNKNOWN";
  238. }
  239. }
  240. int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
  241. {
  242. int i;
  243. if (!hapd->iface->current_mode)
  244. return 0;
  245. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  246. struct hostapd_channel_data *ch =
  247. &hapd->iface->current_mode->channels[i];
  248. if (ch->chan == chan)
  249. return ch->freq;
  250. }
  251. return 0;
  252. }
  253. int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
  254. {
  255. int i;
  256. if (!hapd->iface->current_mode)
  257. return 0;
  258. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  259. struct hostapd_channel_data *ch =
  260. &hapd->iface->current_mode->channels[i];
  261. if (ch->freq == freq)
  262. return ch->chan;
  263. }
  264. return 0;
  265. }