hw_features.c 19 KB

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