ieee802_11_ht.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * hostapd / IEEE 802.11n HT
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2007-2008, Intel Corporation
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "hostapd.h"
  14. #include "ap_config.h"
  15. #include "sta_info.h"
  16. #include "beacon.h"
  17. #include "ieee802_11.h"
  18. #include "hw_features.h"
  19. #include "ap_drv_ops.h"
  20. u8 * hostapd_eid_ht_capabilities(struct hostapd_data *hapd, u8 *eid)
  21. {
  22. struct ieee80211_ht_capabilities *cap;
  23. u8 *pos = eid;
  24. if (!hapd->iconf->ieee80211n || !hapd->iface->current_mode ||
  25. hapd->conf->disable_11n)
  26. return eid;
  27. *pos++ = WLAN_EID_HT_CAP;
  28. *pos++ = sizeof(*cap);
  29. cap = (struct ieee80211_ht_capabilities *) pos;
  30. os_memset(cap, 0, sizeof(*cap));
  31. cap->ht_capabilities_info = host_to_le16(hapd->iconf->ht_capab);
  32. cap->a_mpdu_params = hapd->iface->current_mode->a_mpdu_params;
  33. os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
  34. 16);
  35. /* TODO: ht_extended_capabilities (now fully disabled) */
  36. /* TODO: tx_bf_capability_info (now fully disabled) */
  37. /* TODO: asel_capabilities (now fully disabled) */
  38. pos += sizeof(*cap);
  39. if (hapd->iconf->obss_interval) {
  40. struct ieee80211_obss_scan_parameters *scan_params;
  41. *pos++ = WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS;
  42. *pos++ = sizeof(*scan_params);
  43. scan_params = (struct ieee80211_obss_scan_parameters *) pos;
  44. os_memset(scan_params, 0, sizeof(*scan_params));
  45. scan_params->width_trigger_scan_interval =
  46. host_to_le16(hapd->iconf->obss_interval);
  47. /* Fill in default values for remaining parameters
  48. * (IEEE Std 802.11-2012, 8.4.2.61 and MIB defval) */
  49. scan_params->scan_passive_dwell =
  50. host_to_le16(20);
  51. scan_params->scan_active_dwell =
  52. host_to_le16(10);
  53. scan_params->scan_passive_total_per_channel =
  54. host_to_le16(200);
  55. scan_params->scan_active_total_per_channel =
  56. host_to_le16(20);
  57. scan_params->channel_transition_delay_factor =
  58. host_to_le16(5);
  59. scan_params->scan_activity_threshold =
  60. host_to_le16(25);
  61. pos += sizeof(*scan_params);
  62. }
  63. return pos;
  64. }
  65. u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
  66. {
  67. struct ieee80211_ht_operation *oper;
  68. u8 *pos = eid;
  69. if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
  70. return eid;
  71. *pos++ = WLAN_EID_HT_OPERATION;
  72. *pos++ = sizeof(*oper);
  73. oper = (struct ieee80211_ht_operation *) pos;
  74. os_memset(oper, 0, sizeof(*oper));
  75. oper->primary_chan = hapd->iconf->channel;
  76. oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
  77. if (hapd->iconf->secondary_channel == 1)
  78. oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
  79. HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
  80. if (hapd->iconf->secondary_channel == -1)
  81. oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
  82. HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
  83. pos += sizeof(*oper);
  84. return pos;
  85. }
  86. /*
  87. op_mode
  88. Set to 0 (HT pure) under the followign conditions
  89. - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
  90. - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
  91. Set to 1 (HT non-member protection) if there may be non-HT STAs
  92. in both the primary and the secondary channel
  93. Set to 2 if only HT STAs are associated in BSS,
  94. however and at least one 20 MHz HT STA is associated
  95. Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
  96. */
  97. int hostapd_ht_operation_update(struct hostapd_iface *iface)
  98. {
  99. u16 cur_op_mode, new_op_mode;
  100. int op_mode_changes = 0;
  101. if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
  102. return 0;
  103. wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
  104. __func__, iface->ht_op_mode);
  105. if (!(iface->ht_op_mode & HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT)
  106. && iface->num_sta_ht_no_gf) {
  107. iface->ht_op_mode |= HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
  108. op_mode_changes++;
  109. } else if ((iface->ht_op_mode &
  110. HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT) &&
  111. iface->num_sta_ht_no_gf == 0) {
  112. iface->ht_op_mode &= ~HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
  113. op_mode_changes++;
  114. }
  115. if (!(iface->ht_op_mode & HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
  116. (iface->num_sta_no_ht || iface->olbc_ht)) {
  117. iface->ht_op_mode |= HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
  118. op_mode_changes++;
  119. } else if ((iface->ht_op_mode &
  120. HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
  121. (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
  122. iface->ht_op_mode &= ~HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
  123. op_mode_changes++;
  124. }
  125. if (iface->num_sta_no_ht)
  126. new_op_mode = HT_PROT_NON_HT_MIXED;
  127. else if (iface->conf->secondary_channel && iface->num_sta_ht_20mhz)
  128. new_op_mode = HT_PROT_20MHZ_PROTECTION;
  129. else if (iface->olbc_ht)
  130. new_op_mode = HT_PROT_NONMEMBER_PROTECTION;
  131. else
  132. new_op_mode = HT_PROT_NO_PROTECTION;
  133. cur_op_mode = iface->ht_op_mode & HT_OPER_OP_MODE_HT_PROT_MASK;
  134. if (cur_op_mode != new_op_mode) {
  135. iface->ht_op_mode &= ~HT_OPER_OP_MODE_HT_PROT_MASK;
  136. iface->ht_op_mode |= new_op_mode;
  137. op_mode_changes++;
  138. }
  139. wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
  140. __func__, iface->ht_op_mode, op_mode_changes);
  141. return op_mode_changes;
  142. }
  143. static int is_40_allowed(struct hostapd_iface *iface, int channel)
  144. {
  145. int pri_freq, sec_freq;
  146. int affected_start, affected_end;
  147. int pri = 2407 + 5 * channel;
  148. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  149. return 1;
  150. pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
  151. if (iface->conf->secondary_channel > 0)
  152. sec_freq = pri_freq + 20;
  153. else
  154. sec_freq = pri_freq - 20;
  155. affected_start = (pri_freq + sec_freq) / 2 - 25;
  156. affected_end = (pri_freq + sec_freq) / 2 + 25;
  157. if ((pri < affected_start || pri > affected_end))
  158. return 1; /* not within affected channel range */
  159. wpa_printf(MSG_ERROR, "40 MHz affected channel range: [%d,%d] MHz",
  160. affected_start, affected_end);
  161. wpa_printf(MSG_ERROR, "Neighboring BSS: freq=%d", pri);
  162. return 0;
  163. }
  164. void hostapd_2040_coex_action(struct hostapd_data *hapd,
  165. const struct ieee80211_mgmt *mgmt, size_t len)
  166. {
  167. struct hostapd_iface *iface = hapd->iface;
  168. struct ieee80211_2040_bss_coex_ie *bc_ie;
  169. struct ieee80211_2040_intol_chan_report *ic_report;
  170. int is_ht_allowed = 1;
  171. int i;
  172. const u8 *data = ((const u8 *) mgmt) + 1;
  173. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  174. HOSTAPD_LEVEL_DEBUG, "hostapd_public_action - action=%d",
  175. mgmt->u.action.u.public_action.action);
  176. if (!(iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  177. return;
  178. if (len < IEEE80211_HDRLEN + 1)
  179. return;
  180. data++;
  181. bc_ie = (struct ieee80211_2040_bss_coex_ie *) &data[0];
  182. ic_report = (struct ieee80211_2040_intol_chan_report *)
  183. (&data[0] + sizeof(*bc_ie));
  184. if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ) {
  185. hostapd_logger(hapd, mgmt->sa,
  186. HOSTAPD_MODULE_IEEE80211,
  187. HOSTAPD_LEVEL_DEBUG,
  188. "20 MHz BSS width request bit is set in BSS coexistence information field");
  189. is_ht_allowed = 0;
  190. }
  191. if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_40MHZ_INTOL) {
  192. hostapd_logger(hapd, mgmt->sa,
  193. HOSTAPD_MODULE_IEEE80211,
  194. HOSTAPD_LEVEL_DEBUG,
  195. "40 MHz intolerant bit is set in BSS coexistence information field");
  196. is_ht_allowed = 0;
  197. }
  198. if (ic_report &&
  199. (ic_report->element_id == WLAN_EID_20_40_BSS_INTOLERANT)) {
  200. /* Go through the channel report to find any BSS there in the
  201. * affected channel range */
  202. for (i = 0; i < ic_report->length - 1; i++) {
  203. if (is_40_allowed(iface, ic_report->variable[i]))
  204. continue;
  205. hostapd_logger(hapd, mgmt->sa,
  206. HOSTAPD_MODULE_IEEE80211,
  207. HOSTAPD_LEVEL_DEBUG,
  208. "20_40_INTOLERANT channel %d reported",
  209. ic_report->variable[i]);
  210. is_ht_allowed = 0;
  211. break;
  212. }
  213. }
  214. if (!is_ht_allowed &&
  215. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  216. if (iface->conf->secondary_channel) {
  217. hostapd_logger(hapd, mgmt->sa,
  218. HOSTAPD_MODULE_IEEE80211,
  219. HOSTAPD_LEVEL_INFO,
  220. "Switching to 20 MHz operation");
  221. iface->conf->secondary_channel = 0;
  222. ieee802_11_set_beacons(iface);
  223. }
  224. if (!iface->num_sta_ht40_intolerant) {
  225. unsigned int delay_time;
  226. delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
  227. iface->conf->obss_interval;
  228. eloop_cancel_timeout(ap_ht2040_timeout, hapd->iface,
  229. NULL);
  230. eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
  231. hapd->iface, NULL);
  232. }
  233. }
  234. }
  235. u16 copy_sta_ht_capab(struct hostapd_data *hapd, struct sta_info *sta,
  236. const u8 *ht_capab, size_t ht_capab_len)
  237. {
  238. /* Disable HT caps for STAs associated to no-HT BSSes. */
  239. if (!ht_capab ||
  240. ht_capab_len < sizeof(struct ieee80211_ht_capabilities) ||
  241. hapd->conf->disable_11n) {
  242. sta->flags &= ~WLAN_STA_HT;
  243. os_free(sta->ht_capabilities);
  244. sta->ht_capabilities = NULL;
  245. return WLAN_STATUS_SUCCESS;
  246. }
  247. if (sta->ht_capabilities == NULL) {
  248. sta->ht_capabilities =
  249. os_zalloc(sizeof(struct ieee80211_ht_capabilities));
  250. if (sta->ht_capabilities == NULL)
  251. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  252. }
  253. sta->flags |= WLAN_STA_HT;
  254. os_memcpy(sta->ht_capabilities, ht_capab,
  255. sizeof(struct ieee80211_ht_capabilities));
  256. return WLAN_STATUS_SUCCESS;
  257. }
  258. void ht40_intolerant_add(struct hostapd_iface *iface, struct sta_info *sta)
  259. {
  260. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  261. return;
  262. wpa_printf(MSG_INFO, "HT: Forty MHz Intolerant is set by STA " MACSTR
  263. " in Association Request", MAC2STR(sta->addr));
  264. if (sta->ht40_intolerant_set)
  265. return;
  266. sta->ht40_intolerant_set = 1;
  267. iface->num_sta_ht40_intolerant++;
  268. eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
  269. if (iface->conf->secondary_channel &&
  270. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  271. iface->conf->secondary_channel = 0;
  272. ieee802_11_set_beacons(iface);
  273. }
  274. }
  275. void ht40_intolerant_remove(struct hostapd_iface *iface, struct sta_info *sta)
  276. {
  277. if (!sta->ht40_intolerant_set)
  278. return;
  279. sta->ht40_intolerant_set = 0;
  280. iface->num_sta_ht40_intolerant--;
  281. if (iface->num_sta_ht40_intolerant == 0 &&
  282. (iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
  283. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  284. unsigned int delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
  285. iface->conf->obss_interval;
  286. wpa_printf(MSG_DEBUG,
  287. "HT: Start 20->40 MHz transition timer (%d seconds)",
  288. delay_time);
  289. eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
  290. eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
  291. iface, NULL);
  292. }
  293. }
  294. static void update_sta_ht(struct hostapd_data *hapd, struct sta_info *sta)
  295. {
  296. u16 ht_capab;
  297. ht_capab = le_to_host16(sta->ht_capabilities->ht_capabilities_info);
  298. wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities Info: "
  299. "0x%04x", MAC2STR(sta->addr), ht_capab);
  300. if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
  301. if (!sta->no_ht_gf_set) {
  302. sta->no_ht_gf_set = 1;
  303. hapd->iface->num_sta_ht_no_gf++;
  304. }
  305. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no greenfield, num "
  306. "of non-gf stations %d",
  307. __func__, MAC2STR(sta->addr),
  308. hapd->iface->num_sta_ht_no_gf);
  309. }
  310. if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  311. if (!sta->ht_20mhz_set) {
  312. sta->ht_20mhz_set = 1;
  313. hapd->iface->num_sta_ht_20mhz++;
  314. }
  315. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, num of "
  316. "20MHz HT STAs %d",
  317. __func__, MAC2STR(sta->addr),
  318. hapd->iface->num_sta_ht_20mhz);
  319. }
  320. if (ht_capab & HT_CAP_INFO_40MHZ_INTOLERANT)
  321. ht40_intolerant_add(hapd->iface, sta);
  322. }
  323. static void update_sta_no_ht(struct hostapd_data *hapd, struct sta_info *sta)
  324. {
  325. if (!sta->no_ht_set) {
  326. sta->no_ht_set = 1;
  327. hapd->iface->num_sta_no_ht++;
  328. }
  329. if (hapd->iconf->ieee80211n) {
  330. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no HT, num of "
  331. "non-HT stations %d",
  332. __func__, MAC2STR(sta->addr),
  333. hapd->iface->num_sta_no_ht);
  334. }
  335. }
  336. void update_ht_state(struct hostapd_data *hapd, struct sta_info *sta)
  337. {
  338. if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities)
  339. update_sta_ht(hapd, sta);
  340. else
  341. update_sta_no_ht(hapd, sta);
  342. if (hostapd_ht_operation_update(hapd->iface) > 0)
  343. ieee802_11_set_beacons(hapd->iface);
  344. }
  345. void hostapd_get_ht_capab(struct hostapd_data *hapd,
  346. struct ieee80211_ht_capabilities *ht_cap,
  347. struct ieee80211_ht_capabilities *neg_ht_cap)
  348. {
  349. u16 cap;
  350. if (ht_cap == NULL)
  351. return;
  352. os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
  353. cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
  354. /*
  355. * Mask out HT features we don't support, but don't overwrite
  356. * non-symmetric features like STBC and SMPS. Just because
  357. * we're not in dynamic SMPS mode the STA might still be.
  358. */
  359. cap &= (hapd->iconf->ht_capab | HT_CAP_INFO_RX_STBC_MASK |
  360. HT_CAP_INFO_TX_STBC | HT_CAP_INFO_SMPS_MASK);
  361. /*
  362. * STBC needs to be handled specially
  363. * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
  364. * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
  365. */
  366. if (!(hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK))
  367. cap &= ~HT_CAP_INFO_TX_STBC;
  368. if (!(hapd->iconf->ht_capab & HT_CAP_INFO_TX_STBC))
  369. cap &= ~HT_CAP_INFO_RX_STBC_MASK;
  370. neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
  371. }
  372. void ap_ht2040_timeout(void *eloop_data, void *user_data)
  373. {
  374. struct hostapd_iface *iface = eloop_data;
  375. wpa_printf(MSG_INFO, "Switching to 40 MHz operation");
  376. iface->conf->secondary_channel = iface->secondary_ch;
  377. ieee802_11_set_beacons(iface);
  378. }