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