ieee802_11_vht.c 12 KB

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