ieee802_11_vht.c 13 KB

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