1 /*
2 * hostapd / IEEE 802.11ax HE
3 * Copyright (c) 2016-2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2019 John Crispin <john@phrozen.org>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "hostapd.h"
16 #include "ap_config.h"
17 #include "beacon.h"
18 #include "sta_info.h"
19 #include "ieee802_11.h"
20 #include "dfs.h"
21
ieee80211_he_ppet_size(u8 ppe_thres_hdr,const u8 * phy_cap_info)22 static u8 ieee80211_he_ppet_size(u8 ppe_thres_hdr, const u8 *phy_cap_info)
23 {
24 u8 sz = 0, ru;
25
26 if ((phy_cap_info[HE_PHYCAP_PPE_THRESHOLD_PRESENT_IDX] &
27 HE_PHYCAP_PPE_THRESHOLD_PRESENT) == 0)
28 return 0;
29
30 ru = (ppe_thres_hdr >> HE_PPE_THRES_RU_INDEX_BITMASK_SHIFT) &
31 HE_PPE_THRES_RU_INDEX_BITMASK_MASK;
32 while (ru) {
33 if (ru & 0x1)
34 sz++;
35 ru >>= 1;
36 }
37
38 sz *= 1 + (ppe_thres_hdr & HE_PPE_THRES_NSS_MASK);
39 sz = (sz * 6) + 7;
40 if (sz % 8)
41 sz += 8;
42 sz /= 8;
43
44 return sz;
45 }
46
47
ieee80211_he_mcs_set_size(const u8 * phy_cap_info)48 static u8 ieee80211_he_mcs_set_size(const u8 *phy_cap_info)
49 {
50 u8 sz = 4;
51
52 if (phy_cap_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
53 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)
54 sz += 4;
55 if (phy_cap_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
56 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
57 sz += 4;
58
59 return sz;
60 }
61
62
ieee80211_invalid_he_cap_size(const u8 * buf,size_t len)63 static int ieee80211_invalid_he_cap_size(const u8 *buf, size_t len)
64 {
65 struct ieee80211_he_capabilities *cap;
66 size_t cap_len;
67
68 cap = (struct ieee80211_he_capabilities *) buf;
69 cap_len = sizeof(*cap) - sizeof(cap->optional);
70 if (len < cap_len)
71 return 1;
72
73 cap_len += ieee80211_he_mcs_set_size(cap->he_phy_capab_info);
74 if (len < cap_len)
75 return 1;
76
77 cap_len += ieee80211_he_ppet_size(buf[cap_len], cap->he_phy_capab_info);
78
79 return len != cap_len;
80 }
81
82
hostapd_eid_he_capab(struct hostapd_data * hapd,u8 * eid,enum ieee80211_op_mode opmode)83 u8 * hostapd_eid_he_capab(struct hostapd_data *hapd, u8 *eid,
84 enum ieee80211_op_mode opmode)
85 {
86 struct ieee80211_he_capabilities *cap;
87 struct hostapd_hw_modes *mode = hapd->iface->current_mode;
88 u8 he_oper_chwidth = ~HE_PHYCAP_CHANNEL_WIDTH_MASK;
89 u8 *pos = eid;
90 u8 ie_size = 0, mcs_nss_size = 4, ppet_size = 0;
91
92 if (!mode)
93 return eid;
94
95 ie_size = sizeof(*cap) - sizeof(cap->optional);
96 ppet_size = ieee80211_he_ppet_size(mode->he_capab[opmode].ppet[0],
97 mode->he_capab[opmode].phy_cap);
98
99 switch (hapd->iface->conf->he_oper_chwidth) {
100 case CHANWIDTH_80P80MHZ:
101 he_oper_chwidth |=
102 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G;
103 mcs_nss_size += 4;
104 /* fall through */
105 case CHANWIDTH_160MHZ:
106 he_oper_chwidth |= HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
107 mcs_nss_size += 4;
108 /* fall through */
109 case CHANWIDTH_80MHZ:
110 case CHANWIDTH_USE_HT:
111 he_oper_chwidth |= HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
112 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
113 break;
114 }
115
116 ie_size += mcs_nss_size + ppet_size;
117
118 *pos++ = WLAN_EID_EXTENSION;
119 *pos++ = 1 + ie_size;
120 *pos++ = WLAN_EID_EXT_HE_CAPABILITIES;
121
122 cap = (struct ieee80211_he_capabilities *) pos;
123 os_memset(cap, 0, sizeof(*cap));
124
125 os_memcpy(cap->he_mac_capab_info, mode->he_capab[opmode].mac_cap,
126 HE_MAX_MAC_CAPAB_SIZE);
127 os_memcpy(cap->he_phy_capab_info, mode->he_capab[opmode].phy_cap,
128 HE_MAX_PHY_CAPAB_SIZE);
129 os_memcpy(cap->optional, mode->he_capab[opmode].mcs, mcs_nss_size);
130 if (ppet_size)
131 os_memcpy(&cap->optional[mcs_nss_size],
132 mode->he_capab[opmode].ppet, ppet_size);
133
134 if (hapd->iface->conf->he_phy_capab.he_su_beamformer)
135 cap->he_phy_capab_info[HE_PHYCAP_SU_BEAMFORMER_CAPAB_IDX] |=
136 HE_PHYCAP_SU_BEAMFORMER_CAPAB;
137 else
138 cap->he_phy_capab_info[HE_PHYCAP_SU_BEAMFORMER_CAPAB_IDX] &=
139 ~HE_PHYCAP_SU_BEAMFORMER_CAPAB;
140
141 if (hapd->iface->conf->he_phy_capab.he_su_beamformee)
142 cap->he_phy_capab_info[HE_PHYCAP_SU_BEAMFORMEE_CAPAB_IDX] |=
143 HE_PHYCAP_SU_BEAMFORMEE_CAPAB;
144 else
145 cap->he_phy_capab_info[HE_PHYCAP_SU_BEAMFORMEE_CAPAB_IDX] &=
146 ~HE_PHYCAP_SU_BEAMFORMEE_CAPAB;
147
148 if (hapd->iface->conf->he_phy_capab.he_mu_beamformer)
149 cap->he_phy_capab_info[HE_PHYCAP_MU_BEAMFORMER_CAPAB_IDX] |=
150 HE_PHYCAP_MU_BEAMFORMER_CAPAB;
151 else
152 cap->he_phy_capab_info[HE_PHYCAP_MU_BEAMFORMER_CAPAB_IDX] &=
153 ~HE_PHYCAP_MU_BEAMFORMER_CAPAB;
154
155 cap->he_phy_capab_info[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &=
156 he_oper_chwidth;
157
158 pos += ie_size;
159
160 return pos;
161 }
162
163
hostapd_eid_he_operation(struct hostapd_data * hapd,u8 * eid)164 u8 * hostapd_eid_he_operation(struct hostapd_data *hapd, u8 *eid)
165 {
166 struct ieee80211_he_operation *oper;
167 u8 *pos = eid;
168 int oper_size = 6;
169 u32 params = 0;
170
171 if (!hapd->iface->current_mode)
172 return eid;
173
174 if (is_6ghz_op_class(hapd->iconf->op_class))
175 oper_size += 5;
176
177 *pos++ = WLAN_EID_EXTENSION;
178 *pos++ = 1 + oper_size;
179 *pos++ = WLAN_EID_EXT_HE_OPERATION;
180
181 oper = (struct ieee80211_he_operation *) pos;
182 os_memset(oper, 0, sizeof(*oper));
183
184 if (hapd->iface->conf->he_op.he_default_pe_duration)
185 params |= (hapd->iface->conf->he_op.he_default_pe_duration <<
186 HE_OPERATION_DFLT_PE_DURATION_OFFSET);
187
188 if (hapd->iface->conf->he_op.he_twt_required)
189 params |= HE_OPERATION_TWT_REQUIRED;
190
191 if (hapd->iface->conf->he_op.he_rts_threshold)
192 params |= (hapd->iface->conf->he_op.he_rts_threshold <<
193 HE_OPERATION_RTS_THRESHOLD_OFFSET);
194
195 if (hapd->iface->conf->he_op.he_bss_color_disabled)
196 params |= HE_OPERATION_BSS_COLOR_DISABLED;
197 if (hapd->iface->conf->he_op.he_bss_color_partial)
198 params |= HE_OPERATION_BSS_COLOR_PARTIAL;
199 params |= hapd->iface->conf->he_op.he_bss_color <<
200 HE_OPERATION_BSS_COLOR_OFFSET;
201
202 /* HE minimum required basic MCS and NSS for STAs */
203 oper->he_mcs_nss_set =
204 host_to_le16(hapd->iface->conf->he_op.he_basic_mcs_nss_set);
205
206 /* TODO: conditional MaxBSSID Indicator subfield */
207
208 pos += 6; /* skip the fixed part */
209
210 if (is_6ghz_op_class(hapd->iconf->op_class)) {
211 u8 seg0 = hostapd_get_oper_centr_freq_seg0_idx(hapd->iconf);
212 u8 seg1 = hostapd_get_oper_centr_freq_seg1_idx(hapd->iconf);
213
214 if (!seg0)
215 seg0 = hapd->iconf->channel;
216
217 params |= HE_OPERATION_6GHZ_OPER_INFO;
218
219 /* 6 GHz Operation Information field */
220 *pos++ = hapd->iconf->channel; /* Primary Channel */
221
222 /* Control: Channel Width */
223 if (seg1)
224 *pos++ = 3;
225 else
226 *pos++ = center_idx_to_bw_6ghz(seg0);
227
228 /* Channel Center Freq Seg0/Seg1 */
229 *pos++ = seg0;
230 *pos++ = seg1;
231 /* Minimum Rate */
232 *pos++ = 6; /* TODO: what should be set here? */
233 }
234
235 oper->he_oper_params = host_to_le32(params);
236
237 return pos;
238 }
239
240
hostapd_eid_he_mu_edca_parameter_set(struct hostapd_data * hapd,u8 * eid)241 u8 * hostapd_eid_he_mu_edca_parameter_set(struct hostapd_data *hapd, u8 *eid)
242 {
243 struct ieee80211_he_mu_edca_parameter_set *edca;
244 u8 *pos;
245 size_t i;
246
247 pos = (u8 *) &hapd->iface->conf->he_mu_edca;
248 for (i = 0; i < sizeof(*edca); i++) {
249 if (pos[i])
250 break;
251 }
252 if (i == sizeof(*edca))
253 return eid; /* no MU EDCA Parameters configured */
254
255 pos = eid;
256 *pos++ = WLAN_EID_EXTENSION;
257 *pos++ = 1 + sizeof(*edca);
258 *pos++ = WLAN_EID_EXT_HE_MU_EDCA_PARAMS;
259
260 edca = (struct ieee80211_he_mu_edca_parameter_set *) pos;
261 os_memcpy(edca, &hapd->iface->conf->he_mu_edca, sizeof(*edca));
262
263 wpa_hexdump(MSG_DEBUG, "HE: MU EDCA Parameter Set element",
264 pos, sizeof(*edca));
265
266 pos += sizeof(*edca);
267
268 return pos;
269 }
270
271
hostapd_eid_spatial_reuse(struct hostapd_data * hapd,u8 * eid)272 u8 * hostapd_eid_spatial_reuse(struct hostapd_data *hapd, u8 *eid)
273 {
274 struct ieee80211_spatial_reuse *spr;
275 u8 *pos = eid, *spr_param;
276 u8 sz = 1;
277
278 if (!hapd->iface->conf->spr.sr_control)
279 return eid;
280
281 if (hapd->iface->conf->spr.sr_control &
282 SPATIAL_REUSE_NON_SRG_OFFSET_PRESENT)
283 sz++;
284
285 if (hapd->iface->conf->spr.sr_control &
286 SPATIAL_REUSE_SRG_INFORMATION_PRESENT)
287 sz += 18;
288
289 *pos++ = WLAN_EID_EXTENSION;
290 *pos++ = 1 + sz;
291 *pos++ = WLAN_EID_EXT_SPATIAL_REUSE;
292
293 spr = (struct ieee80211_spatial_reuse *) pos;
294 os_memset(spr, 0, sizeof(*spr));
295
296 spr->sr_ctrl = hapd->iface->conf->spr.sr_control;
297 pos++;
298 spr_param = spr->params;
299 if (spr->sr_ctrl & SPATIAL_REUSE_NON_SRG_OFFSET_PRESENT) {
300 *spr_param++ =
301 hapd->iface->conf->spr.non_srg_obss_pd_max_offset;
302 pos++;
303 }
304 if (spr->sr_ctrl & SPATIAL_REUSE_SRG_INFORMATION_PRESENT) {
305 *spr_param++ = hapd->iface->conf->spr.srg_obss_pd_min_offset;
306 *spr_param++ = hapd->iface->conf->spr.srg_obss_pd_max_offset;
307 pos += 18;
308 }
309
310 return pos;
311 }
312
313
hostapd_eid_he_6ghz_band_cap(struct hostapd_data * hapd,u8 * eid)314 u8 * hostapd_eid_he_6ghz_band_cap(struct hostapd_data *hapd, u8 *eid)
315 {
316 struct hostapd_hw_modes *mode = hapd->iface->current_mode;
317 struct ieee80211_he_6ghz_band_cap *cap;
318 u32 vht_cap;
319 u8 ht_info;
320 u8 params;
321 u8 *pos;
322
323 if (!mode || !is_6ghz_op_class(hapd->iconf->op_class))
324 return eid;
325
326 vht_cap = hapd->iface->conf->vht_capab;
327 ht_info = mode->a_mpdu_params;
328
329 pos = eid;
330 *pos++ = WLAN_EID_EXTENSION;
331 *pos++ = 1 + sizeof(*cap);
332 *pos++ = WLAN_EID_EXT_HE_6GHZ_BAND_CAP;
333
334 /* Minimum MPDU Start Spacing B0..B2 */
335 params = (ht_info >> 2) & HE_6GHZ_BAND_CAP_MIN_MPDU_START_SPACE_MASK;
336
337 /* Maximum A-MPDU Length Exponent B3..B5 */
338 params |= ((((vht_cap & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
339 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT) &
340 HE_6GHZ_BAND_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) <<
341 HE_6GHZ_BAND_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
342
343 /* Maximum MPDU Length B6..B7 */
344 params |= ((((vht_cap & VHT_CAP_MAX_MPDU_LENGTH_MASK) >>
345 VHT_CAP_MAX_MPDU_LENGTH_MASK_SHIFT) &
346 HE_6GHZ_BAND_CAP_MAX_MPDU_LENGTH_MASK) <<
347 HE_6GHZ_BAND_CAP_MAX_MPDU_LENGTH_SHIFT);
348
349 cap = (struct ieee80211_he_6ghz_band_cap *) pos;
350 cap->a_mpdu_params = params;
351 cap->info = HE_6GHZ_BAND_CAP_SMPS_DISABLED;
352 if (vht_cap & VHT_CAP_RX_ANTENNA_PATTERN)
353 cap->info |= HE_6GHZ_BAND_CAP_RX_ANTENNA_PATTERN;
354 if (vht_cap & VHT_CAP_TX_ANTENNA_PATTERN)
355 cap->info |= HE_6GHZ_BAND_CAP_TX_ANTENNA_PATTERN;
356 pos += sizeof(*cap);
357
358 return pos;
359 }
360
361
hostapd_get_he_capab(struct hostapd_data * hapd,const struct ieee80211_he_capabilities * he_cap,struct ieee80211_he_capabilities * neg_he_cap,size_t he_capab_len)362 void hostapd_get_he_capab(struct hostapd_data *hapd,
363 const struct ieee80211_he_capabilities *he_cap,
364 struct ieee80211_he_capabilities *neg_he_cap,
365 size_t he_capab_len)
366 {
367 if (!he_cap)
368 return;
369
370 if (he_capab_len > sizeof(*neg_he_cap))
371 he_capab_len = sizeof(*neg_he_cap);
372 /* TODO: mask out unsupported features */
373
374 os_memcpy(neg_he_cap, he_cap, he_capab_len);
375 }
376
377
check_valid_he_mcs(struct hostapd_data * hapd,const u8 * sta_he_capab,enum ieee80211_op_mode opmode)378 static int check_valid_he_mcs(struct hostapd_data *hapd, const u8 *sta_he_capab,
379 enum ieee80211_op_mode opmode)
380 {
381 u16 sta_rx_mcs_set, ap_tx_mcs_set;
382 u8 mcs_count = 0;
383 const u16 *ap_mcs_set, *sta_mcs_set;
384 int i;
385
386 if (!hapd->iface->current_mode)
387 return 1;
388 ap_mcs_set = (u16 *) hapd->iface->current_mode->he_capab[opmode].mcs;
389 sta_mcs_set = (u16 *) ((const struct ieee80211_he_capabilities *)
390 sta_he_capab)->optional;
391
392 /*
393 * Disable HE capabilities for STAs for which there is not even a single
394 * allowed MCS in any supported number of streams, i.e., STA is
395 * advertising 3 (not supported) as HE MCS rates for all supported
396 * band/stream cases.
397 */
398 switch (hapd->iface->conf->he_oper_chwidth) {
399 case CHANWIDTH_80P80MHZ:
400 mcs_count = 3;
401 break;
402 case CHANWIDTH_160MHZ:
403 mcs_count = 2;
404 break;
405 default:
406 mcs_count = 1;
407 break;
408 }
409
410 for (i = 0; i < mcs_count; i++) {
411 int j;
412
413 /* AP Tx MCS map vs. STA Rx MCS map */
414 sta_rx_mcs_set = WPA_GET_LE16((const u8 *) &sta_mcs_set[i * 2]);
415 ap_tx_mcs_set = WPA_GET_LE16((const u8 *)
416 &ap_mcs_set[(i * 2) + 1]);
417
418 for (j = 0; j < HE_NSS_MAX_STREAMS; j++) {
419 if (((ap_tx_mcs_set >> (j * 2)) & 0x3) == 3)
420 continue;
421
422 if (((sta_rx_mcs_set >> (j * 2)) & 0x3) == 3)
423 continue;
424
425 return 1;
426 }
427 }
428
429 wpa_printf(MSG_DEBUG,
430 "No matching HE MCS found between AP TX and STA RX");
431
432 return 0;
433 }
434
435
copy_sta_he_capab(struct hostapd_data * hapd,struct sta_info * sta,enum ieee80211_op_mode opmode,const u8 * he_capab,size_t he_capab_len)436 u16 copy_sta_he_capab(struct hostapd_data *hapd, struct sta_info *sta,
437 enum ieee80211_op_mode opmode, const u8 *he_capab,
438 size_t he_capab_len)
439 {
440 if (!he_capab || !hapd->iconf->ieee80211ax ||
441 !check_valid_he_mcs(hapd, he_capab, opmode) ||
442 ieee80211_invalid_he_cap_size(he_capab, he_capab_len) ||
443 he_capab_len > sizeof(struct ieee80211_he_capabilities)) {
444 sta->flags &= ~WLAN_STA_HE;
445 os_free(sta->he_capab);
446 sta->he_capab = NULL;
447 return WLAN_STATUS_SUCCESS;
448 }
449
450 if (!sta->he_capab) {
451 sta->he_capab =
452 os_zalloc(sizeof(struct ieee80211_he_capabilities));
453 if (!sta->he_capab)
454 return WLAN_STATUS_UNSPECIFIED_FAILURE;
455 }
456
457 sta->flags |= WLAN_STA_HE;
458 os_memset(sta->he_capab, 0, sizeof(struct ieee80211_he_capabilities));
459 os_memcpy(sta->he_capab, he_capab, he_capab_len);
460 sta->he_capab_len = he_capab_len;
461
462 return WLAN_STATUS_SUCCESS;
463 }
464
465
copy_sta_he_6ghz_capab(struct hostapd_data * hapd,struct sta_info * sta,const u8 * he_6ghz_capab)466 u16 copy_sta_he_6ghz_capab(struct hostapd_data *hapd, struct sta_info *sta,
467 const u8 *he_6ghz_capab)
468 {
469 if (!he_6ghz_capab || !hapd->iconf->ieee80211ax ||
470 !is_6ghz_op_class(hapd->iconf->op_class)) {
471 sta->flags &= ~WLAN_STA_6GHZ;
472 os_free(sta->he_6ghz_capab);
473 sta->he_6ghz_capab = NULL;
474 return WLAN_STATUS_SUCCESS;
475 }
476
477 if (!sta->he_6ghz_capab) {
478 sta->he_6ghz_capab =
479 os_zalloc(sizeof(struct ieee80211_he_6ghz_band_cap));
480 if (!sta->he_6ghz_capab)
481 return WLAN_STATUS_UNSPECIFIED_FAILURE;
482 }
483
484 sta->flags |= WLAN_STA_6GHZ;
485 os_memcpy(sta->he_6ghz_capab, he_6ghz_capab,
486 sizeof(struct ieee80211_he_6ghz_band_cap));
487
488 return WLAN_STATUS_SUCCESS;
489 }
490
491
hostapd_get_he_twt_responder(struct hostapd_data * hapd,enum ieee80211_op_mode mode)492 int hostapd_get_he_twt_responder(struct hostapd_data *hapd,
493 enum ieee80211_op_mode mode)
494 {
495 u8 *mac_cap;
496
497 if (!hapd->iface->current_mode ||
498 !hapd->iface->current_mode->he_capab[mode].he_supported)
499 return 0;
500
501 mac_cap = hapd->iface->current_mode->he_capab[mode].mac_cap;
502
503 return !!(mac_cap[HE_MAC_CAPAB_0] & HE_MACCAP_TWT_RESPONDER);
504 }
505