ieee802_11_ht.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. u8 * hostapd_eid_secondary_channel(struct hostapd_data *hapd, u8 *eid)
  87. {
  88. u8 sec_ch;
  89. if (!hapd->cs_freq_params.channel ||
  90. !hapd->cs_freq_params.sec_channel_offset)
  91. return eid;
  92. if (hapd->cs_freq_params.sec_channel_offset == -1)
  93. sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
  94. else if (hapd->cs_freq_params.sec_channel_offset == 1)
  95. sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
  96. else
  97. return eid;
  98. *eid++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
  99. *eid++ = 1;
  100. *eid++ = sec_ch;
  101. return eid;
  102. }
  103. /*
  104. op_mode
  105. Set to 0 (HT pure) under the followign conditions
  106. - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
  107. - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
  108. Set to 1 (HT non-member protection) if there may be non-HT STAs
  109. in both the primary and the secondary channel
  110. Set to 2 if only HT STAs are associated in BSS,
  111. however and at least one 20 MHz HT STA is associated
  112. Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
  113. */
  114. int hostapd_ht_operation_update(struct hostapd_iface *iface)
  115. {
  116. u16 cur_op_mode, new_op_mode;
  117. int op_mode_changes = 0;
  118. if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
  119. return 0;
  120. wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
  121. __func__, iface->ht_op_mode);
  122. if (!(iface->ht_op_mode & HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT)
  123. && iface->num_sta_ht_no_gf) {
  124. iface->ht_op_mode |= HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
  125. op_mode_changes++;
  126. } else if ((iface->ht_op_mode &
  127. HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT) &&
  128. iface->num_sta_ht_no_gf == 0) {
  129. iface->ht_op_mode &= ~HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
  130. op_mode_changes++;
  131. }
  132. if (!(iface->ht_op_mode & HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
  133. (iface->num_sta_no_ht || iface->olbc_ht)) {
  134. iface->ht_op_mode |= HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
  135. op_mode_changes++;
  136. } else if ((iface->ht_op_mode &
  137. HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
  138. (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
  139. iface->ht_op_mode &= ~HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
  140. op_mode_changes++;
  141. }
  142. if (iface->num_sta_no_ht)
  143. new_op_mode = HT_PROT_NON_HT_MIXED;
  144. else if (iface->conf->secondary_channel && iface->num_sta_ht_20mhz)
  145. new_op_mode = HT_PROT_20MHZ_PROTECTION;
  146. else if (iface->olbc_ht)
  147. new_op_mode = HT_PROT_NONMEMBER_PROTECTION;
  148. else
  149. new_op_mode = HT_PROT_NO_PROTECTION;
  150. cur_op_mode = iface->ht_op_mode & HT_OPER_OP_MODE_HT_PROT_MASK;
  151. if (cur_op_mode != new_op_mode) {
  152. iface->ht_op_mode &= ~HT_OPER_OP_MODE_HT_PROT_MASK;
  153. iface->ht_op_mode |= new_op_mode;
  154. op_mode_changes++;
  155. }
  156. wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
  157. __func__, iface->ht_op_mode, op_mode_changes);
  158. return op_mode_changes;
  159. }
  160. static int is_40_allowed(struct hostapd_iface *iface, int channel)
  161. {
  162. int pri_freq, sec_freq;
  163. int affected_start, affected_end;
  164. int pri = 2407 + 5 * channel;
  165. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  166. return 1;
  167. pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
  168. if (iface->conf->secondary_channel > 0)
  169. sec_freq = pri_freq + 20;
  170. else
  171. sec_freq = pri_freq - 20;
  172. affected_start = (pri_freq + sec_freq) / 2 - 25;
  173. affected_end = (pri_freq + sec_freq) / 2 + 25;
  174. if ((pri < affected_start || pri > affected_end))
  175. return 1; /* not within affected channel range */
  176. wpa_printf(MSG_ERROR, "40 MHz affected channel range: [%d,%d] MHz",
  177. affected_start, affected_end);
  178. wpa_printf(MSG_ERROR, "Neighboring BSS: freq=%d", pri);
  179. return 0;
  180. }
  181. void hostapd_2040_coex_action(struct hostapd_data *hapd,
  182. const struct ieee80211_mgmt *mgmt, size_t len)
  183. {
  184. struct hostapd_iface *iface = hapd->iface;
  185. struct ieee80211_2040_bss_coex_ie *bc_ie;
  186. struct ieee80211_2040_intol_chan_report *ic_report;
  187. int is_ht40_allowed = 1;
  188. int i;
  189. const u8 *start = (const u8 *) mgmt;
  190. const u8 *data = start + IEEE80211_HDRLEN + 2;
  191. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  192. HOSTAPD_LEVEL_DEBUG, "hostapd_public_action - action=%d",
  193. mgmt->u.action.u.public_action.action);
  194. if (!(iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  195. return;
  196. if (len < IEEE80211_HDRLEN + 2 + sizeof(*bc_ie))
  197. return;
  198. bc_ie = (struct ieee80211_2040_bss_coex_ie *) data;
  199. if (bc_ie->element_id != WLAN_EID_20_40_BSS_COEXISTENCE ||
  200. bc_ie->length < 1) {
  201. wpa_printf(MSG_DEBUG, "Unexpected IE (%u,%u) in coex report",
  202. bc_ie->element_id, bc_ie->length);
  203. return;
  204. }
  205. if (len < IEEE80211_HDRLEN + 2 + 2 + bc_ie->length)
  206. return;
  207. data += 2 + bc_ie->length;
  208. wpa_printf(MSG_DEBUG, "20/40 BSS Coexistence Information field: 0x%x",
  209. bc_ie->coex_param);
  210. if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ) {
  211. hostapd_logger(hapd, mgmt->sa,
  212. HOSTAPD_MODULE_IEEE80211,
  213. HOSTAPD_LEVEL_DEBUG,
  214. "20 MHz BSS width request bit is set in BSS coexistence information field");
  215. is_ht40_allowed = 0;
  216. }
  217. if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_40MHZ_INTOL) {
  218. hostapd_logger(hapd, mgmt->sa,
  219. HOSTAPD_MODULE_IEEE80211,
  220. HOSTAPD_LEVEL_DEBUG,
  221. "40 MHz intolerant bit is set in BSS coexistence information field");
  222. is_ht40_allowed = 0;
  223. }
  224. if (start + len - data >= 3 &&
  225. data[0] == WLAN_EID_20_40_BSS_INTOLERANT && data[1] >= 1) {
  226. u8 ielen = data[1];
  227. if (ielen > start + len - data - 2)
  228. return;
  229. ic_report = (struct ieee80211_2040_intol_chan_report *) data;
  230. wpa_printf(MSG_DEBUG,
  231. "20/40 BSS Intolerant Channel Report: Operating Class %u",
  232. ic_report->op_class);
  233. /* Go through the channel report to find any BSS there in the
  234. * affected channel range */
  235. for (i = 0; i < ielen - 1; i++) {
  236. u8 chan = ic_report->variable[i];
  237. if (is_40_allowed(iface, chan))
  238. continue;
  239. hostapd_logger(hapd, mgmt->sa,
  240. HOSTAPD_MODULE_IEEE80211,
  241. HOSTAPD_LEVEL_DEBUG,
  242. "20_40_INTOLERANT channel %d reported",
  243. chan);
  244. is_ht40_allowed = 0;
  245. }
  246. }
  247. wpa_printf(MSG_DEBUG, "is_ht40_allowed=%d num_sta_ht40_intolerant=%d",
  248. is_ht40_allowed, iface->num_sta_ht40_intolerant);
  249. if (!is_ht40_allowed &&
  250. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  251. if (iface->conf->secondary_channel) {
  252. hostapd_logger(hapd, mgmt->sa,
  253. HOSTAPD_MODULE_IEEE80211,
  254. HOSTAPD_LEVEL_INFO,
  255. "Switching to 20 MHz operation");
  256. iface->conf->secondary_channel = 0;
  257. ieee802_11_set_beacons(iface);
  258. }
  259. if (!iface->num_sta_ht40_intolerant &&
  260. iface->conf->obss_interval) {
  261. unsigned int delay_time;
  262. delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
  263. iface->conf->obss_interval;
  264. eloop_cancel_timeout(ap_ht2040_timeout, hapd->iface,
  265. NULL);
  266. eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
  267. hapd->iface, NULL);
  268. wpa_printf(MSG_DEBUG,
  269. "Reschedule HT 20/40 timeout to occur in %u seconds",
  270. delay_time);
  271. }
  272. }
  273. }
  274. u16 copy_sta_ht_capab(struct hostapd_data *hapd, struct sta_info *sta,
  275. const u8 *ht_capab)
  276. {
  277. /*
  278. * Disable HT caps for STAs associated to no-HT BSSes, or for stations
  279. * that did not specify a valid WMM IE in the (Re)Association Request
  280. * frame.
  281. */
  282. if (!ht_capab ||
  283. !(sta->flags & WLAN_STA_WMM) || hapd->conf->disable_11n) {
  284. sta->flags &= ~WLAN_STA_HT;
  285. os_free(sta->ht_capabilities);
  286. sta->ht_capabilities = NULL;
  287. return WLAN_STATUS_SUCCESS;
  288. }
  289. if (sta->ht_capabilities == NULL) {
  290. sta->ht_capabilities =
  291. os_zalloc(sizeof(struct ieee80211_ht_capabilities));
  292. if (sta->ht_capabilities == NULL)
  293. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  294. }
  295. sta->flags |= WLAN_STA_HT;
  296. os_memcpy(sta->ht_capabilities, ht_capab,
  297. sizeof(struct ieee80211_ht_capabilities));
  298. return WLAN_STATUS_SUCCESS;
  299. }
  300. void ht40_intolerant_add(struct hostapd_iface *iface, struct sta_info *sta)
  301. {
  302. if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  303. return;
  304. wpa_printf(MSG_INFO, "HT: Forty MHz Intolerant is set by STA " MACSTR
  305. " in Association Request", MAC2STR(sta->addr));
  306. if (sta->ht40_intolerant_set)
  307. return;
  308. sta->ht40_intolerant_set = 1;
  309. iface->num_sta_ht40_intolerant++;
  310. eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
  311. if (iface->conf->secondary_channel &&
  312. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  313. iface->conf->secondary_channel = 0;
  314. ieee802_11_set_beacons(iface);
  315. }
  316. }
  317. void ht40_intolerant_remove(struct hostapd_iface *iface, struct sta_info *sta)
  318. {
  319. if (!sta->ht40_intolerant_set)
  320. return;
  321. sta->ht40_intolerant_set = 0;
  322. iface->num_sta_ht40_intolerant--;
  323. if (iface->num_sta_ht40_intolerant == 0 &&
  324. (iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
  325. (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
  326. unsigned int delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
  327. iface->conf->obss_interval;
  328. wpa_printf(MSG_DEBUG,
  329. "HT: Start 20->40 MHz transition timer (%d seconds)",
  330. delay_time);
  331. eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
  332. eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
  333. iface, NULL);
  334. }
  335. }
  336. static void update_sta_ht(struct hostapd_data *hapd, struct sta_info *sta)
  337. {
  338. u16 ht_capab;
  339. ht_capab = le_to_host16(sta->ht_capabilities->ht_capabilities_info);
  340. wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities Info: "
  341. "0x%04x", MAC2STR(sta->addr), ht_capab);
  342. if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
  343. if (!sta->no_ht_gf_set) {
  344. sta->no_ht_gf_set = 1;
  345. hapd->iface->num_sta_ht_no_gf++;
  346. }
  347. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no greenfield, num "
  348. "of non-gf stations %d",
  349. __func__, MAC2STR(sta->addr),
  350. hapd->iface->num_sta_ht_no_gf);
  351. }
  352. if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
  353. if (!sta->ht_20mhz_set) {
  354. sta->ht_20mhz_set = 1;
  355. hapd->iface->num_sta_ht_20mhz++;
  356. }
  357. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, num of "
  358. "20MHz HT STAs %d",
  359. __func__, MAC2STR(sta->addr),
  360. hapd->iface->num_sta_ht_20mhz);
  361. }
  362. if (ht_capab & HT_CAP_INFO_40MHZ_INTOLERANT)
  363. ht40_intolerant_add(hapd->iface, sta);
  364. }
  365. static void update_sta_no_ht(struct hostapd_data *hapd, struct sta_info *sta)
  366. {
  367. if (!sta->no_ht_set) {
  368. sta->no_ht_set = 1;
  369. hapd->iface->num_sta_no_ht++;
  370. }
  371. if (hapd->iconf->ieee80211n) {
  372. wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no HT, num of "
  373. "non-HT stations %d",
  374. __func__, MAC2STR(sta->addr),
  375. hapd->iface->num_sta_no_ht);
  376. }
  377. }
  378. void update_ht_state(struct hostapd_data *hapd, struct sta_info *sta)
  379. {
  380. if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities)
  381. update_sta_ht(hapd, sta);
  382. else
  383. update_sta_no_ht(hapd, sta);
  384. if (hostapd_ht_operation_update(hapd->iface) > 0)
  385. ieee802_11_set_beacons(hapd->iface);
  386. }
  387. void hostapd_get_ht_capab(struct hostapd_data *hapd,
  388. struct ieee80211_ht_capabilities *ht_cap,
  389. struct ieee80211_ht_capabilities *neg_ht_cap)
  390. {
  391. u16 cap;
  392. if (ht_cap == NULL)
  393. return;
  394. os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
  395. cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
  396. /*
  397. * Mask out HT features we don't support, but don't overwrite
  398. * non-symmetric features like STBC and SMPS. Just because
  399. * we're not in dynamic SMPS mode the STA might still be.
  400. */
  401. cap &= (hapd->iconf->ht_capab | HT_CAP_INFO_RX_STBC_MASK |
  402. HT_CAP_INFO_TX_STBC | HT_CAP_INFO_SMPS_MASK);
  403. /*
  404. * STBC needs to be handled specially
  405. * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
  406. * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
  407. */
  408. if (!(hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK))
  409. cap &= ~HT_CAP_INFO_TX_STBC;
  410. if (!(hapd->iconf->ht_capab & HT_CAP_INFO_TX_STBC))
  411. cap &= ~HT_CAP_INFO_RX_STBC_MASK;
  412. neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
  413. }
  414. void ap_ht2040_timeout(void *eloop_data, void *user_data)
  415. {
  416. struct hostapd_iface *iface = eloop_data;
  417. wpa_printf(MSG_INFO, "Switching to 40 MHz operation");
  418. iface->conf->secondary_channel = iface->secondary_ch;
  419. ieee802_11_set_beacons(iface);
  420. }