hw_features.c 21 KB

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