ieee802_11_vht.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * hostapd / IEEE 802.11ac VHT
  3. * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of BSD license
  7. *
  8. * See README and COPYING for more details.
  9. */
  10. #include "utils/includes.h"
  11. #include "utils/common.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 "dfs.h"
  19. u8 * hostapd_eid_vht_capabilities(struct hostapd_data *hapd, u8 *eid)
  20. {
  21. struct ieee80211_vht_capabilities *cap;
  22. struct hostapd_hw_modes *mode = hapd->iface->current_mode;
  23. u8 *pos = eid;
  24. if (!mode)
  25. return eid;
  26. if (mode->mode == HOSTAPD_MODE_IEEE80211G && hapd->conf->vendor_vht &&
  27. mode->vht_capab == 0 && hapd->iface->hw_features) {
  28. int i;
  29. for (i = 0; i < hapd->iface->num_hw_features; i++) {
  30. if (hapd->iface->hw_features[i].mode ==
  31. HOSTAPD_MODE_IEEE80211A) {
  32. mode = &hapd->iface->hw_features[i];
  33. break;
  34. }
  35. }
  36. }
  37. *pos++ = WLAN_EID_VHT_CAP;
  38. *pos++ = sizeof(*cap);
  39. cap = (struct ieee80211_vht_capabilities *) pos;
  40. os_memset(cap, 0, sizeof(*cap));
  41. cap->vht_capabilities_info = host_to_le32(
  42. hapd->iface->conf->vht_capab);
  43. /* Supported MCS set comes from hw */
  44. os_memcpy(&cap->vht_supported_mcs_set, mode->vht_mcs_set, 8);
  45. pos += sizeof(*cap);
  46. return pos;
  47. }
  48. u8 * hostapd_eid_vht_operation(struct hostapd_data *hapd, u8 *eid)
  49. {
  50. struct ieee80211_vht_operation *oper;
  51. u8 *pos = eid;
  52. *pos++ = WLAN_EID_VHT_OPERATION;
  53. *pos++ = sizeof(*oper);
  54. oper = (struct ieee80211_vht_operation *) pos;
  55. os_memset(oper, 0, sizeof(*oper));
  56. /*
  57. * center freq = 5 GHz + (5 * index)
  58. * So index 42 gives center freq 5.210 GHz
  59. * which is channel 42 in 5G band
  60. */
  61. oper->vht_op_info_chan_center_freq_seg0_idx =
  62. hapd->iconf->vht_oper_centr_freq_seg0_idx;
  63. oper->vht_op_info_chan_center_freq_seg1_idx =
  64. hapd->iconf->vht_oper_centr_freq_seg1_idx;
  65. oper->vht_op_info_chwidth = hapd->iconf->vht_oper_chwidth;
  66. if (hapd->iconf->vht_oper_chwidth == 2) {
  67. /*
  68. * Convert 160 MHz channel width to new style as interop
  69. * workaround.
  70. */
  71. oper->vht_op_info_chwidth = 1;
  72. oper->vht_op_info_chan_center_freq_seg1_idx =
  73. oper->vht_op_info_chan_center_freq_seg0_idx;
  74. if (hapd->iconf->channel <
  75. hapd->iconf->vht_oper_centr_freq_seg0_idx)
  76. oper->vht_op_info_chan_center_freq_seg0_idx -= 8;
  77. else
  78. oper->vht_op_info_chan_center_freq_seg0_idx += 8;
  79. } else if (hapd->iconf->vht_oper_chwidth == 3) {
  80. /*
  81. * Convert 80+80 MHz channel width to new style as interop
  82. * workaround.
  83. */
  84. oper->vht_op_info_chwidth = 1;
  85. }
  86. /* VHT Basic MCS set comes from hw */
  87. /* Hard code 1 stream, MCS0-7 is a min Basic VHT MCS rates */
  88. oper->vht_basic_mcs_set = host_to_le16(0xfffc);
  89. pos += sizeof(*oper);
  90. return pos;
  91. }
  92. static int check_valid_vht_mcs(struct hostapd_hw_modes *mode,
  93. const u8 *sta_vht_capab)
  94. {
  95. const struct ieee80211_vht_capabilities *vht_cap;
  96. struct ieee80211_vht_capabilities ap_vht_cap;
  97. u16 sta_rx_mcs_set, ap_tx_mcs_set;
  98. int i;
  99. if (!mode)
  100. return 1;
  101. /*
  102. * Disable VHT caps for STAs for which there is not even a single
  103. * allowed MCS in any supported number of streams, i.e., STA is
  104. * advertising 3 (not supported) as VHT MCS rates for all supported
  105. * stream cases.
  106. */
  107. os_memcpy(&ap_vht_cap.vht_supported_mcs_set, mode->vht_mcs_set,
  108. sizeof(ap_vht_cap.vht_supported_mcs_set));
  109. vht_cap = (const struct ieee80211_vht_capabilities *) sta_vht_capab;
  110. /* AP Tx MCS map vs. STA Rx MCS map */
  111. sta_rx_mcs_set = le_to_host16(vht_cap->vht_supported_mcs_set.rx_map);
  112. ap_tx_mcs_set = le_to_host16(ap_vht_cap.vht_supported_mcs_set.tx_map);
  113. for (i = 0; i < VHT_RX_NSS_MAX_STREAMS; i++) {
  114. if ((ap_tx_mcs_set & (0x3 << (i * 2))) == 3)
  115. continue;
  116. if ((sta_rx_mcs_set & (0x3 << (i * 2))) == 3)
  117. continue;
  118. return 1;
  119. }
  120. wpa_printf(MSG_DEBUG,
  121. "No matching VHT MCS found between AP TX and STA RX");
  122. return 0;
  123. }
  124. u8 * hostapd_eid_wb_chsw_wrapper(struct hostapd_data *hapd, u8 *eid)
  125. {
  126. u8 bw, chan1, chan2 = 0;
  127. int freq1;
  128. if (!hapd->cs_freq_params.channel ||
  129. !hapd->cs_freq_params.vht_enabled)
  130. return eid;
  131. /* bandwidth: 0: 40, 1: 80, 2: 160, 3: 80+80 */
  132. switch (hapd->cs_freq_params.bandwidth) {
  133. case 40:
  134. bw = 0;
  135. break;
  136. case 80:
  137. /* check if it's 80+80 */
  138. if (!hapd->cs_freq_params.center_freq2)
  139. bw = 1;
  140. else
  141. bw = 3;
  142. break;
  143. case 160:
  144. bw = 2;
  145. break;
  146. default:
  147. /* not valid VHT bandwidth or not in CSA */
  148. return eid;
  149. }
  150. freq1 = hapd->cs_freq_params.center_freq1 ?
  151. hapd->cs_freq_params.center_freq1 :
  152. hapd->cs_freq_params.freq;
  153. if (ieee80211_freq_to_chan(freq1, &chan1) !=
  154. HOSTAPD_MODE_IEEE80211A)
  155. return eid;
  156. if (hapd->cs_freq_params.center_freq2 &&
  157. ieee80211_freq_to_chan(hapd->cs_freq_params.center_freq2,
  158. &chan2) != HOSTAPD_MODE_IEEE80211A)
  159. return eid;
  160. *eid++ = WLAN_EID_VHT_CHANNEL_SWITCH_WRAPPER;
  161. *eid++ = 5; /* Length of Channel Switch Wrapper */
  162. *eid++ = WLAN_EID_VHT_WIDE_BW_CHSWITCH;
  163. *eid++ = 3; /* Length of Wide Bandwidth Channel Switch element */
  164. *eid++ = bw; /* New Channel Width */
  165. *eid++ = chan1; /* New Channel Center Frequency Segment 0 */
  166. *eid++ = chan2; /* New Channel Center Frequency Segment 1 */
  167. return eid;
  168. }
  169. u8 * hostapd_eid_txpower_envelope(struct hostapd_data *hapd, u8 *eid)
  170. {
  171. struct hostapd_iface *iface = hapd->iface;
  172. struct hostapd_config *iconf = iface->conf;
  173. struct hostapd_hw_modes *mode = iface->current_mode;
  174. struct hostapd_channel_data *chan;
  175. int dfs, i;
  176. u8 channel, tx_pwr_count, local_pwr_constraint;
  177. int max_tx_power;
  178. u8 tx_pwr;
  179. if (!mode)
  180. return eid;
  181. if (ieee80211_freq_to_chan(iface->freq, &channel) == NUM_HOSTAPD_MODES)
  182. return eid;
  183. for (i = 0; i < mode->num_channels; i++) {
  184. if (mode->channels[i].freq == iface->freq)
  185. break;
  186. }
  187. if (i == mode->num_channels)
  188. return eid;
  189. switch (iface->conf->vht_oper_chwidth) {
  190. case VHT_CHANWIDTH_USE_HT:
  191. if (iconf->secondary_channel == 0) {
  192. /* Max Transmit Power count = 0 (20 MHz) */
  193. tx_pwr_count = 0;
  194. } else {
  195. /* Max Transmit Power count = 1 (20, 40 MHz) */
  196. tx_pwr_count = 1;
  197. }
  198. break;
  199. case VHT_CHANWIDTH_80MHZ:
  200. /* Max Transmit Power count = 2 (20, 40, and 80 MHz) */
  201. tx_pwr_count = 2;
  202. break;
  203. case VHT_CHANWIDTH_80P80MHZ:
  204. case VHT_CHANWIDTH_160MHZ:
  205. /* Max Transmit Power count = 3 (20, 40, 80, 160/80+80 MHz) */
  206. tx_pwr_count = 3;
  207. break;
  208. default:
  209. return eid;
  210. }
  211. /*
  212. * Below local_pwr_constraint logic is referred from
  213. * hostapd_eid_pwr_constraint.
  214. *
  215. * Check if DFS is required by regulatory.
  216. */
  217. dfs = hostapd_is_dfs_required(hapd->iface);
  218. if (dfs < 0)
  219. dfs = 0;
  220. /*
  221. * In order to meet regulations when TPC is not implemented using
  222. * a transmit power that is below the legal maximum (including any
  223. * mitigation factor) should help. In this case, indicate 3 dB below
  224. * maximum allowed transmit power.
  225. */
  226. if (hapd->iconf->local_pwr_constraint == -1)
  227. local_pwr_constraint = (dfs == 0) ? 0 : 3;
  228. else
  229. local_pwr_constraint = hapd->iconf->local_pwr_constraint;
  230. /*
  231. * A STA that is not an AP shall use a transmit power less than or
  232. * equal to the local maximum transmit power level for the channel.
  233. * The local maximum transmit power can be calculated from the formula:
  234. * local max TX pwr = max TX pwr - local pwr constraint
  235. * Where max TX pwr is maximum transmit power level specified for
  236. * channel in Country element and local pwr constraint is specified
  237. * for channel in this Power Constraint element.
  238. */
  239. chan = &mode->channels[i];
  240. max_tx_power = chan->max_tx_power - local_pwr_constraint;
  241. /*
  242. * Local Maximum Transmit power is encoded as two's complement
  243. * with a 0.5 dB step.
  244. */
  245. max_tx_power *= 2; /* in 0.5 dB steps */
  246. if (max_tx_power > 127) {
  247. /* 63.5 has special meaning of 63.5 dBm or higher */
  248. max_tx_power = 127;
  249. }
  250. if (max_tx_power < -128)
  251. max_tx_power = -128;
  252. if (max_tx_power < 0)
  253. tx_pwr = 0x80 + max_tx_power + 128;
  254. else
  255. tx_pwr = max_tx_power;
  256. *eid++ = WLAN_EID_VHT_TRANSMIT_POWER_ENVELOPE;
  257. *eid++ = 2 + tx_pwr_count;
  258. /*
  259. * Max Transmit Power count and
  260. * Max Transmit Power units = 0 (EIRP)
  261. */
  262. *eid++ = tx_pwr_count;
  263. for (i = 0; i <= tx_pwr_count; i++)
  264. *eid++ = tx_pwr;
  265. return eid;
  266. }
  267. u16 copy_sta_vht_capab(struct hostapd_data *hapd, struct sta_info *sta,
  268. const u8 *vht_capab)
  269. {
  270. /* Disable VHT caps for STAs associated to no-VHT BSSes. */
  271. if (!vht_capab ||
  272. hapd->conf->disable_11ac ||
  273. !check_valid_vht_mcs(hapd->iface->current_mode, vht_capab)) {
  274. sta->flags &= ~WLAN_STA_VHT;
  275. os_free(sta->vht_capabilities);
  276. sta->vht_capabilities = NULL;
  277. return WLAN_STATUS_SUCCESS;
  278. }
  279. if (sta->vht_capabilities == NULL) {
  280. sta->vht_capabilities =
  281. os_zalloc(sizeof(struct ieee80211_vht_capabilities));
  282. if (sta->vht_capabilities == NULL)
  283. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  284. }
  285. sta->flags |= WLAN_STA_VHT;
  286. os_memcpy(sta->vht_capabilities, vht_capab,
  287. sizeof(struct ieee80211_vht_capabilities));
  288. return WLAN_STATUS_SUCCESS;
  289. }
  290. u16 copy_sta_vendor_vht(struct hostapd_data *hapd, struct sta_info *sta,
  291. const u8 *ie, size_t len)
  292. {
  293. const u8 *vht_capab;
  294. unsigned int vht_capab_len;
  295. if (!ie || len < 5 + 2 + sizeof(struct ieee80211_vht_capabilities) ||
  296. hapd->conf->disable_11ac)
  297. goto no_capab;
  298. /* The VHT Capabilities element embedded in vendor VHT */
  299. vht_capab = ie + 5;
  300. if (vht_capab[0] != WLAN_EID_VHT_CAP)
  301. goto no_capab;
  302. vht_capab_len = vht_capab[1];
  303. if (vht_capab_len < sizeof(struct ieee80211_vht_capabilities) ||
  304. (int) vht_capab_len > ie + len - vht_capab - 2)
  305. goto no_capab;
  306. vht_capab += 2;
  307. if (sta->vht_capabilities == NULL) {
  308. sta->vht_capabilities =
  309. os_zalloc(sizeof(struct ieee80211_vht_capabilities));
  310. if (sta->vht_capabilities == NULL)
  311. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  312. }
  313. sta->flags |= WLAN_STA_VHT | WLAN_STA_VENDOR_VHT;
  314. os_memcpy(sta->vht_capabilities, vht_capab,
  315. sizeof(struct ieee80211_vht_capabilities));
  316. return WLAN_STATUS_SUCCESS;
  317. no_capab:
  318. sta->flags &= ~WLAN_STA_VENDOR_VHT;
  319. return WLAN_STATUS_SUCCESS;
  320. }
  321. u8 * hostapd_eid_vendor_vht(struct hostapd_data *hapd, u8 *eid)
  322. {
  323. u8 *pos = eid;
  324. if (!hapd->iface->current_mode)
  325. return eid;
  326. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  327. *pos++ = (5 + /* The Vendor OUI, type and subtype */
  328. 2 + sizeof(struct ieee80211_vht_capabilities) +
  329. 2 + sizeof(struct ieee80211_vht_operation));
  330. WPA_PUT_BE32(pos, (OUI_BROADCOM << 8) | VENDOR_VHT_TYPE);
  331. pos += 4;
  332. *pos++ = VENDOR_VHT_SUBTYPE;
  333. pos = hostapd_eid_vht_capabilities(hapd, pos);
  334. pos = hostapd_eid_vht_operation(hapd, pos);
  335. return pos;
  336. }
  337. u16 set_sta_vht_opmode(struct hostapd_data *hapd, struct sta_info *sta,
  338. const u8 *vht_oper_notif)
  339. {
  340. if (!vht_oper_notif) {
  341. sta->flags &= ~WLAN_STA_VHT_OPMODE_ENABLED;
  342. return WLAN_STATUS_SUCCESS;
  343. }
  344. sta->flags |= WLAN_STA_VHT_OPMODE_ENABLED;
  345. sta->vht_opmode = *vht_oper_notif;
  346. return WLAN_STATUS_SUCCESS;
  347. }
  348. void hostapd_get_vht_capab(struct hostapd_data *hapd,
  349. struct ieee80211_vht_capabilities *vht_cap,
  350. struct ieee80211_vht_capabilities *neg_vht_cap)
  351. {
  352. u32 cap, own_cap, sym_caps;
  353. if (vht_cap == NULL)
  354. return;
  355. os_memcpy(neg_vht_cap, vht_cap, sizeof(*neg_vht_cap));
  356. cap = le_to_host32(neg_vht_cap->vht_capabilities_info);
  357. own_cap = hapd->iconf->vht_capab;
  358. /* mask out symmetric VHT capabilities we don't support */
  359. sym_caps = VHT_CAP_SHORT_GI_80 | VHT_CAP_SHORT_GI_160;
  360. cap &= ~sym_caps | (own_cap & sym_caps);
  361. /* mask out beamformer/beamformee caps if not supported */
  362. if (!(own_cap & VHT_CAP_SU_BEAMFORMER_CAPABLE))
  363. cap &= ~(VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  364. VHT_CAP_BEAMFORMEE_STS_MAX);
  365. if (!(own_cap & VHT_CAP_SU_BEAMFORMEE_CAPABLE))
  366. cap &= ~(VHT_CAP_SU_BEAMFORMER_CAPABLE |
  367. VHT_CAP_SOUNDING_DIMENSION_MAX);
  368. if (!(own_cap & VHT_CAP_MU_BEAMFORMER_CAPABLE))
  369. cap &= ~VHT_CAP_MU_BEAMFORMEE_CAPABLE;
  370. if (!(own_cap & VHT_CAP_MU_BEAMFORMEE_CAPABLE))
  371. cap &= ~VHT_CAP_MU_BEAMFORMER_CAPABLE;
  372. /* mask channel widths we don't support */
  373. switch (own_cap & VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
  374. case VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
  375. break;
  376. case VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
  377. if (cap & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) {
  378. cap &= ~VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
  379. cap |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  380. }
  381. break;
  382. default:
  383. cap &= ~VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  384. break;
  385. }
  386. if (!(cap & VHT_CAP_SUPP_CHAN_WIDTH_MASK))
  387. cap &= ~VHT_CAP_SHORT_GI_160;
  388. /*
  389. * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
  390. * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
  391. */
  392. if (!(own_cap & VHT_CAP_RXSTBC_MASK))
  393. cap &= ~VHT_CAP_TXSTBC;
  394. if (!(own_cap & VHT_CAP_TXSTBC))
  395. cap &= ~VHT_CAP_RXSTBC_MASK;
  396. neg_vht_cap->vht_capabilities_info = host_to_le32(cap);
  397. }