hw_features.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 "ieee802_11_defs.h"
  19. #include "ieee802_11_common.h"
  20. #include "eloop.h"
  21. #include "hw_features.h"
  22. #include "driver_i.h"
  23. #include "config.h"
  24. void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
  25. size_t num_hw_features)
  26. {
  27. size_t i;
  28. if (hw_features == NULL)
  29. return;
  30. for (i = 0; i < num_hw_features; i++) {
  31. os_free(hw_features[i].channels);
  32. os_free(hw_features[i].rates);
  33. }
  34. os_free(hw_features);
  35. }
  36. int hostapd_get_hw_features(struct hostapd_iface *iface)
  37. {
  38. struct hostapd_data *hapd = iface->bss[0];
  39. int ret = 0, i, j;
  40. u16 num_modes, flags;
  41. struct hostapd_hw_modes *modes;
  42. if (hostapd_drv_none(hapd))
  43. return -1;
  44. modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
  45. if (modes == NULL) {
  46. hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
  47. HOSTAPD_LEVEL_DEBUG,
  48. "Fetching hardware channel/rate support not "
  49. "supported.");
  50. return -1;
  51. }
  52. iface->hw_flags = flags;
  53. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  54. iface->hw_features = modes;
  55. iface->num_hw_features = num_modes;
  56. for (i = 0; i < num_modes; i++) {
  57. struct hostapd_hw_modes *feature = &modes[i];
  58. /* set flag for channels we can use in current regulatory
  59. * domain */
  60. for (j = 0; j < feature->num_channels; j++) {
  61. /*
  62. * Disable all channels that are marked not to allow
  63. * IBSS operation or active scanning. In addition,
  64. * disable all channels that require radar detection,
  65. * since that (in addition to full DFS) is not yet
  66. * supported.
  67. */
  68. if (feature->channels[j].flag &
  69. (HOSTAPD_CHAN_NO_IBSS |
  70. HOSTAPD_CHAN_PASSIVE_SCAN |
  71. HOSTAPD_CHAN_RADAR))
  72. feature->channels[j].flag |=
  73. HOSTAPD_CHAN_DISABLED;
  74. if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  75. continue;
  76. wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
  77. "chan=%d freq=%d MHz max_tx_power=%d dBm",
  78. feature->mode,
  79. feature->channels[j].chan,
  80. feature->channels[j].freq,
  81. feature->channels[j].max_tx_power);
  82. }
  83. }
  84. return ret;
  85. }
  86. static int hostapd_prepare_rates(struct hostapd_data *hapd,
  87. struct hostapd_hw_modes *mode)
  88. {
  89. int i, num_basic_rates = 0;
  90. int basic_rates_a[] = { 60, 120, 240, -1 };
  91. int basic_rates_b[] = { 10, 20, -1 };
  92. int basic_rates_g[] = { 10, 20, 55, 110, -1 };
  93. int *basic_rates;
  94. if (hapd->iconf->basic_rates)
  95. basic_rates = hapd->iconf->basic_rates;
  96. else switch (mode->mode) {
  97. case HOSTAPD_MODE_IEEE80211A:
  98. basic_rates = basic_rates_a;
  99. break;
  100. case HOSTAPD_MODE_IEEE80211B:
  101. basic_rates = basic_rates_b;
  102. break;
  103. case HOSTAPD_MODE_IEEE80211G:
  104. basic_rates = basic_rates_g;
  105. break;
  106. default:
  107. return -1;
  108. }
  109. if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
  110. basic_rates, mode->mode)) {
  111. wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
  112. "module");
  113. }
  114. os_free(hapd->iface->current_rates);
  115. hapd->iface->num_rates = 0;
  116. hapd->iface->current_rates =
  117. os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
  118. if (!hapd->iface->current_rates) {
  119. wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
  120. "table.");
  121. return -1;
  122. }
  123. for (i = 0; i < mode->num_rates; i++) {
  124. struct hostapd_rate_data *rate;
  125. if (hapd->iconf->supported_rates &&
  126. !hostapd_rate_found(hapd->iconf->supported_rates,
  127. mode->rates[i].rate))
  128. continue;
  129. rate = &hapd->iface->current_rates[hapd->iface->num_rates];
  130. os_memcpy(rate, &mode->rates[i],
  131. sizeof(struct hostapd_rate_data));
  132. if (hostapd_rate_found(basic_rates, rate->rate)) {
  133. rate->flags |= HOSTAPD_RATE_BASIC;
  134. num_basic_rates++;
  135. } else
  136. rate->flags &= ~HOSTAPD_RATE_BASIC;
  137. wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
  138. hapd->iface->num_rates, rate->rate, rate->flags);
  139. hapd->iface->num_rates++;
  140. }
  141. if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
  142. wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
  143. "rate sets (%d,%d).",
  144. hapd->iface->num_rates, num_basic_rates);
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. #ifdef CONFIG_IEEE80211N
  150. static int ieee80211n_allowed_ht40_channel_pair(struct hostapd_iface *iface)
  151. {
  152. int sec_chan, ok, j, first;
  153. int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
  154. 184, 192 };
  155. size_t k;
  156. if (!iface->conf->secondary_channel)
  157. return 1; /* HT40 not used */
  158. sec_chan = iface->conf->channel + iface->conf->secondary_channel * 4;
  159. wpa_printf(MSG_DEBUG, "HT40: control channel: %d "
  160. "secondary channel: %d",
  161. iface->conf->channel, sec_chan);
  162. /* Verify that HT40 secondary channel is an allowed 20 MHz
  163. * channel */
  164. ok = 0;
  165. for (j = 0; j < iface->current_mode->num_channels; j++) {
  166. struct hostapd_channel_data *chan =
  167. &iface->current_mode->channels[j];
  168. if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
  169. chan->chan == sec_chan) {
  170. ok = 1;
  171. break;
  172. }
  173. }
  174. if (!ok) {
  175. wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
  176. sec_chan);
  177. return 0;
  178. }
  179. /*
  180. * Verify that HT40 primary,secondary channel pair is allowed per
  181. * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
  182. * 2.4 GHz rules allow all cases where the secondary channel fits into
  183. * the list of allowed channels (already checked above).
  184. */
  185. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
  186. return 1;
  187. if (iface->conf->secondary_channel > 0)
  188. first = iface->conf->channel;
  189. else
  190. first = sec_chan;
  191. ok = 0;
  192. for (k = 0; k < sizeof(allowed) / sizeof(allowed[0]); k++) {
  193. if (first == allowed[k]) {
  194. ok = 1;
  195. break;
  196. }
  197. }
  198. if (!ok) {
  199. wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
  200. iface->conf->channel,
  201. iface->conf->secondary_channel);
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. static void ieee80211n_switch_pri_sec(struct hostapd_iface *iface)
  207. {
  208. if (iface->conf->secondary_channel > 0) {
  209. iface->conf->channel += 4;
  210. iface->conf->secondary_channel = -1;
  211. } else {
  212. iface->conf->channel -= 4;
  213. iface->conf->secondary_channel = 1;
  214. }
  215. }
  216. static void ieee80211n_get_pri_sec_chan(struct wpa_scan_res *bss,
  217. int *pri_chan, int *sec_chan)
  218. {
  219. struct ieee80211_ht_operation *oper;
  220. struct ieee802_11_elems elems;
  221. *pri_chan = *sec_chan = 0;
  222. ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
  223. if (elems.ht_operation &&
  224. elems.ht_operation_len >= sizeof(*oper)) {
  225. oper = (struct ieee80211_ht_operation *) elems.ht_operation;
  226. *pri_chan = oper->control_chan;
  227. if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
  228. if (oper->ht_param &
  229. HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  230. *sec_chan = *pri_chan + 4;
  231. else if (oper->ht_param &
  232. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  233. *sec_chan = *pri_chan - 4;
  234. }
  235. }
  236. }
  237. static int ieee80211n_check_40mhz_5g(struct hostapd_iface *iface,
  238. struct wpa_scan_results *scan_res)
  239. {
  240. int pri_chan, sec_chan, pri_freq, sec_freq, pri_bss, sec_bss;
  241. int bss_pri_chan, bss_sec_chan;
  242. size_t i;
  243. int match;
  244. pri_chan = iface->conf->channel;
  245. sec_chan = iface->conf->secondary_channel * 4;
  246. pri_freq = hostapd_hw_get_freq(iface->bss[0], pri_chan);
  247. if (iface->conf->secondary_channel > 0)
  248. sec_freq = pri_freq + 20;
  249. else
  250. sec_freq = pri_freq - 20;
  251. /*
  252. * Switch PRI/SEC channels if Beacons were detected on selected SEC
  253. * channel, but not on selected PRI channel.
  254. */
  255. pri_bss = sec_bss = 0;
  256. for (i = 0; i < scan_res->num; i++) {
  257. struct wpa_scan_res *bss = scan_res->res[i];
  258. if (bss->freq == pri_freq)
  259. pri_bss++;
  260. else if (bss->freq == sec_freq)
  261. sec_bss++;
  262. }
  263. if (sec_bss && !pri_bss) {
  264. wpa_printf(MSG_INFO, "Switch own primary and secondary "
  265. "channel to get secondary channel with no Beacons "
  266. "from other BSSes");
  267. ieee80211n_switch_pri_sec(iface);
  268. }
  269. /*
  270. * Match PRI/SEC channel with any existing HT40 BSS on the same
  271. * channels that we are about to use (if already mixed order in
  272. * existing BSSes, use own preference).
  273. */
  274. match = 0;
  275. for (i = 0; i < scan_res->num; i++) {
  276. struct wpa_scan_res *bss = scan_res->res[i];
  277. ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
  278. if (pri_chan == bss_pri_chan &&
  279. sec_chan == bss_sec_chan) {
  280. match = 1;
  281. break;
  282. }
  283. }
  284. if (!match) {
  285. for (i = 0; i < scan_res->num; i++) {
  286. struct wpa_scan_res *bss = scan_res->res[i];
  287. ieee80211n_get_pri_sec_chan(bss, &bss_pri_chan,
  288. &bss_sec_chan);
  289. if (pri_chan == bss_sec_chan &&
  290. sec_chan == bss_pri_chan) {
  291. wpa_printf(MSG_INFO, "Switch own primary and "
  292. "secondary channel due to BSS "
  293. "overlap with " MACSTR,
  294. MAC2STR(bss->bssid));
  295. ieee80211n_switch_pri_sec(iface);
  296. break;
  297. }
  298. }
  299. }
  300. return 1;
  301. }
  302. static int ieee80211n_check_40mhz_2g4(struct hostapd_iface *iface,
  303. struct wpa_scan_results *scan_res)
  304. {
  305. int pri_freq, sec_freq;
  306. int affected_start, affected_end;
  307. size_t i;
  308. pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
  309. if (iface->conf->secondary_channel > 0)
  310. sec_freq = pri_freq + 20;
  311. else
  312. sec_freq = pri_freq - 20;
  313. affected_start = (pri_freq + sec_freq) / 2 - 25;
  314. affected_end = (pri_freq + sec_freq) / 2 + 25;
  315. wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
  316. affected_start, affected_end);
  317. for (i = 0; i < scan_res->num; i++) {
  318. struct wpa_scan_res *bss = scan_res->res[i];
  319. int pri = bss->freq;
  320. int sec = pri;
  321. int sec_chan, pri_chan;
  322. ieee80211n_get_pri_sec_chan(bss, &pri_chan, &sec_chan);
  323. if (sec_chan) {
  324. if (sec_chan < pri_chan)
  325. sec = pri - 20;
  326. else
  327. sec = pri + 20;
  328. }
  329. if ((pri < affected_start || pri > affected_end) &&
  330. (sec < affected_start || sec > affected_end))
  331. continue; /* not within affected channel range */
  332. wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
  333. " freq=%d pri=%d sec=%d",
  334. MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
  335. if (sec_chan) {
  336. if (pri_freq != pri || sec_freq != sec) {
  337. wpa_printf(MSG_DEBUG, "40 MHz pri/sec "
  338. "mismatch with BSS " MACSTR
  339. " <%d,%d> (chan=%d%c) vs. <%d,%d>",
  340. MAC2STR(bss->bssid),
  341. pri, sec, pri_chan,
  342. sec > pri ? '+' : '-',
  343. pri_freq, sec_freq);
  344. return 0;
  345. }
  346. }
  347. /* TODO: 40 MHz intolerant */
  348. }
  349. return 1;
  350. }
  351. static void ieee80211n_check_scan(struct hostapd_iface *iface)
  352. {
  353. struct wpa_scan_results *scan_res;
  354. int oper40;
  355. /* Check list of neighboring BSSes (from scan) to see whether 40 MHz is
  356. * allowed per IEEE 802.11n/D7.0, 11.14.3.2 */
  357. iface->scan_cb = NULL;
  358. scan_res = hostapd_driver_get_scan_results(iface->bss[0]);
  359. if (scan_res == NULL) {
  360. hostapd_setup_interface_complete(iface, 1);
  361. return;
  362. }
  363. if (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A)
  364. oper40 = ieee80211n_check_40mhz_5g(iface, scan_res);
  365. else
  366. oper40 = ieee80211n_check_40mhz_2g4(iface, scan_res);
  367. wpa_scan_results_free(scan_res);
  368. if (!oper40) {
  369. wpa_printf(MSG_INFO, "20/40 MHz operation not permitted on "
  370. "channel pri=%d sec=%d based on overlapping BSSes",
  371. iface->conf->channel,
  372. iface->conf->channel +
  373. iface->conf->secondary_channel * 4);
  374. iface->conf->secondary_channel = 0;
  375. iface->conf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
  376. }
  377. hostapd_setup_interface_complete(iface, 0);
  378. }
  379. static int ieee80211n_check_40mhz(struct hostapd_iface *iface)
  380. {
  381. struct wpa_driver_scan_params params;
  382. if (!iface->conf->secondary_channel)
  383. return 0; /* HT40 not used */
  384. wpa_printf(MSG_DEBUG, "Scan for neighboring BSSes prior to enabling "
  385. "40 MHz channel");
  386. os_memset(&params, 0, sizeof(params));
  387. /* TODO: scan only the needed frequency */
  388. if (hostapd_driver_scan(iface->bss[0], &params) < 0) {
  389. wpa_printf(MSG_ERROR, "Failed to request a scan of "
  390. "neighboring BSSes");
  391. return -1;
  392. }
  393. iface->scan_cb = ieee80211n_check_scan;
  394. return 1;
  395. }
  396. static int ieee80211n_supported_ht_capab(struct hostapd_iface *iface)
  397. {
  398. u16 hw = iface->current_mode->ht_capab;
  399. u16 conf = iface->conf->ht_capab;
  400. if (!iface->conf->ieee80211n)
  401. return 1;
  402. if ((conf & HT_CAP_INFO_LDPC_CODING_CAP) &&
  403. !(hw & HT_CAP_INFO_LDPC_CODING_CAP)) {
  404. wpa_printf(MSG_ERROR, "Driver does not support configured "
  405. "HT capability [LDPC]");
  406. return 0;
  407. }
  408. if ((conf & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
  409. !(hw & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
  410. wpa_printf(MSG_ERROR, "Driver does not support configured "
  411. "HT capability [HT40*]");
  412. return 0;
  413. }
  414. if ((conf & HT_CAP_INFO_SMPS_MASK) != (hw & HT_CAP_INFO_SMPS_MASK) &&
  415. (conf & HT_CAP_INFO_SMPS_MASK) != HT_CAP_INFO_SMPS_DISABLED) {
  416. wpa_printf(MSG_ERROR, "Driver does not support configured "
  417. "HT capability [SMPS-*]");
  418. return 0;
  419. }
  420. if ((conf & HT_CAP_INFO_GREEN_FIELD) &&
  421. !(hw & HT_CAP_INFO_GREEN_FIELD)) {
  422. wpa_printf(MSG_ERROR, "Driver does not support configured "
  423. "HT capability [GF]");
  424. return 0;
  425. }
  426. if ((conf & HT_CAP_INFO_SHORT_GI20MHZ) &&
  427. !(hw & HT_CAP_INFO_SHORT_GI20MHZ)) {
  428. wpa_printf(MSG_ERROR, "Driver does not support configured "
  429. "HT capability [SHORT-GI-20]");
  430. return 0;
  431. }
  432. if ((conf & HT_CAP_INFO_SHORT_GI40MHZ) &&
  433. !(hw & HT_CAP_INFO_SHORT_GI40MHZ)) {
  434. wpa_printf(MSG_ERROR, "Driver does not support configured "
  435. "HT capability [SHORT-GI-40]");
  436. return 0;
  437. }
  438. if ((conf & HT_CAP_INFO_TX_STBC) && !(hw & HT_CAP_INFO_TX_STBC)) {
  439. wpa_printf(MSG_ERROR, "Driver does not support configured "
  440. "HT capability [TX-STBC]");
  441. return 0;
  442. }
  443. if ((conf & HT_CAP_INFO_RX_STBC_MASK) >
  444. (hw & HT_CAP_INFO_RX_STBC_MASK)) {
  445. wpa_printf(MSG_ERROR, "Driver does not support configured "
  446. "HT capability [RX-STBC*]");
  447. return 0;
  448. }
  449. if ((conf & HT_CAP_INFO_DELAYED_BA) &&
  450. !(hw & HT_CAP_INFO_DELAYED_BA)) {
  451. wpa_printf(MSG_ERROR, "Driver does not support configured "
  452. "HT capability [DELAYED-BA]");
  453. return 0;
  454. }
  455. if ((conf & HT_CAP_INFO_MAX_AMSDU_SIZE) &&
  456. !(hw & HT_CAP_INFO_MAX_AMSDU_SIZE)) {
  457. wpa_printf(MSG_ERROR, "Driver does not support configured "
  458. "HT capability [MAX-AMSDU-7935]");
  459. return 0;
  460. }
  461. if ((conf & HT_CAP_INFO_DSSS_CCK40MHZ) &&
  462. !(hw & HT_CAP_INFO_DSSS_CCK40MHZ)) {
  463. wpa_printf(MSG_ERROR, "Driver does not support configured "
  464. "HT capability [DSSS_CCK-40]");
  465. return 0;
  466. }
  467. if ((conf & HT_CAP_INFO_PSMP_SUPP) && !(hw & HT_CAP_INFO_PSMP_SUPP)) {
  468. wpa_printf(MSG_ERROR, "Driver does not support configured "
  469. "HT capability [PSMP]");
  470. return 0;
  471. }
  472. if ((conf & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT) &&
  473. !(hw & HT_CAP_INFO_LSIG_TXOP_PROTECT_SUPPORT)) {
  474. wpa_printf(MSG_ERROR, "Driver does not support configured "
  475. "HT capability [LSIG-TXOP-PROT]");
  476. return 0;
  477. }
  478. return 1;
  479. }
  480. #endif /* CONFIG_IEEE80211N */
  481. int hostapd_check_ht_capab(struct hostapd_iface *iface)
  482. {
  483. #ifdef CONFIG_IEEE80211N
  484. int ret;
  485. ret = ieee80211n_check_40mhz(iface);
  486. if (ret)
  487. return ret;
  488. if (!ieee80211n_allowed_ht40_channel_pair(iface))
  489. return -1;
  490. if (!ieee80211n_supported_ht_capab(iface))
  491. return -1;
  492. #endif /* CONFIG_IEEE80211N */
  493. return 0;
  494. }
  495. /**
  496. * hostapd_select_hw_mode - Select the hardware mode
  497. * @iface: Pointer to interface data.
  498. * Returns: 0 on success, -1 on failure
  499. *
  500. * Sets up the hardware mode, channel, rates, and passive scanning
  501. * based on the configuration.
  502. */
  503. int hostapd_select_hw_mode(struct hostapd_iface *iface)
  504. {
  505. int i, j, ok;
  506. if (iface->num_hw_features < 1)
  507. return -1;
  508. iface->current_mode = NULL;
  509. for (i = 0; i < iface->num_hw_features; i++) {
  510. struct hostapd_hw_modes *mode = &iface->hw_features[i];
  511. if (mode->mode == iface->conf->hw_mode) {
  512. iface->current_mode = mode;
  513. break;
  514. }
  515. }
  516. if (iface->current_mode == NULL) {
  517. wpa_printf(MSG_ERROR, "Hardware does not support configured "
  518. "mode");
  519. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  520. HOSTAPD_LEVEL_WARNING,
  521. "Hardware does not support configured mode "
  522. "(%d)", (int) iface->conf->hw_mode);
  523. return -1;
  524. }
  525. ok = 0;
  526. for (j = 0; j < iface->current_mode->num_channels; j++) {
  527. struct hostapd_channel_data *chan =
  528. &iface->current_mode->channels[j];
  529. if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
  530. (chan->chan == iface->conf->channel)) {
  531. ok = 1;
  532. break;
  533. }
  534. }
  535. if (iface->conf->channel == 0) {
  536. /* TODO: could request a scan of neighboring BSSes and select
  537. * the channel automatically */
  538. wpa_printf(MSG_ERROR, "Channel not configured "
  539. "(hw_mode/channel in hostapd.conf)");
  540. return -1;
  541. }
  542. if (ok == 0 && iface->conf->channel != 0) {
  543. hostapd_logger(iface->bss[0], NULL,
  544. HOSTAPD_MODULE_IEEE80211,
  545. HOSTAPD_LEVEL_WARNING,
  546. "Configured channel (%d) not found from the "
  547. "channel list of current mode (%d) %s",
  548. iface->conf->channel,
  549. iface->current_mode->mode,
  550. hostapd_hw_mode_txt(iface->current_mode->mode));
  551. iface->current_mode = NULL;
  552. }
  553. if (iface->current_mode == NULL) {
  554. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  555. HOSTAPD_LEVEL_WARNING,
  556. "Hardware does not support configured channel");
  557. return -1;
  558. }
  559. if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
  560. wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
  561. hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
  562. HOSTAPD_LEVEL_WARNING,
  563. "Failed to prepare rates table.");
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. const char * hostapd_hw_mode_txt(int mode)
  569. {
  570. switch (mode) {
  571. case HOSTAPD_MODE_IEEE80211A:
  572. return "IEEE 802.11a";
  573. case HOSTAPD_MODE_IEEE80211B:
  574. return "IEEE 802.11b";
  575. case HOSTAPD_MODE_IEEE80211G:
  576. return "IEEE 802.11g";
  577. default:
  578. return "UNKNOWN";
  579. }
  580. }
  581. int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
  582. {
  583. int i;
  584. if (!hapd->iface->current_mode)
  585. return 0;
  586. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  587. struct hostapd_channel_data *ch =
  588. &hapd->iface->current_mode->channels[i];
  589. if (ch->chan == chan)
  590. return ch->freq;
  591. }
  592. return 0;
  593. }
  594. int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
  595. {
  596. int i;
  597. if (!hapd->iface->current_mode)
  598. return 0;
  599. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  600. struct hostapd_channel_data *ch =
  601. &hapd->iface->current_mode->channels[i];
  602. if (ch->freq == freq)
  603. return ch->chan;
  604. }
  605. return 0;
  606. }