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