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 		os_memcpy(spr_param,
308 			  hapd->iface->conf->spr.srg_bss_color_bitmap, 8);
309 		spr_param += 8;
310 		os_memcpy(spr_param,
311 			  hapd->iface->conf->spr.srg_partial_bssid_bitmap, 8);
312 		pos += 18;
313 	}
314 
315 	return pos;
316 }
317 
318 
hostapd_eid_he_6ghz_band_cap(struct hostapd_data * hapd,u8 * eid)319 u8 * hostapd_eid_he_6ghz_band_cap(struct hostapd_data *hapd, u8 *eid)
320 {
321 	struct hostapd_config *conf = hapd->iface->conf;
322 	struct hostapd_hw_modes *mode = hapd->iface->current_mode;
323 	struct he_capabilities *he_cap;
324 	struct ieee80211_he_6ghz_band_cap *cap;
325 	u16 capab;
326 	u8 *pos;
327 
328 	if (!mode || !is_6ghz_op_class(hapd->iconf->op_class) ||
329 	    !is_6ghz_freq(hapd->iface->freq))
330 		return eid;
331 
332 	he_cap = &mode->he_capab[IEEE80211_MODE_AP];
333 	capab = he_cap->he_6ghz_capa & HE_6GHZ_BAND_CAP_MIN_MPDU_START;
334 	capab |= (conf->he_6ghz_max_ampdu_len_exp <<
335 		  HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_SHIFT) &
336 		HE_6GHZ_BAND_CAP_MAX_AMPDU_LEN_EXP_MASK;
337 	capab |= (conf->he_6ghz_max_mpdu <<
338 		  HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_SHIFT) &
339 		HE_6GHZ_BAND_CAP_MAX_MPDU_LEN_MASK;
340 	capab |= HE_6GHZ_BAND_CAP_SMPS_DISABLED;
341 	if (conf->he_6ghz_rx_ant_pat)
342 		capab |= HE_6GHZ_BAND_CAP_RX_ANTPAT_CONS;
343 	if (conf->he_6ghz_tx_ant_pat)
344 		capab |= HE_6GHZ_BAND_CAP_TX_ANTPAT_CONS;
345 
346 	pos = eid;
347 	*pos++ = WLAN_EID_EXTENSION;
348 	*pos++ = 1 + sizeof(*cap);
349 	*pos++ = WLAN_EID_EXT_HE_6GHZ_BAND_CAP;
350 
351 	cap = (struct ieee80211_he_6ghz_band_cap *) pos;
352 	cap->capab = host_to_le16(capab);
353 	pos += sizeof(*cap);
354 
355 	return pos;
356 }
357 
358 
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)359 void hostapd_get_he_capab(struct hostapd_data *hapd,
360 			  const struct ieee80211_he_capabilities *he_cap,
361 			  struct ieee80211_he_capabilities *neg_he_cap,
362 			  size_t he_capab_len)
363 {
364 	if (!he_cap)
365 		return;
366 
367 	if (he_capab_len > sizeof(*neg_he_cap))
368 		he_capab_len = sizeof(*neg_he_cap);
369 	/* TODO: mask out unsupported features */
370 
371 	os_memcpy(neg_he_cap, he_cap, he_capab_len);
372 }
373 
374 
check_valid_he_mcs(struct hostapd_data * hapd,const u8 * sta_he_capab,enum ieee80211_op_mode opmode)375 static int check_valid_he_mcs(struct hostapd_data *hapd, const u8 *sta_he_capab,
376 			      enum ieee80211_op_mode opmode)
377 {
378 	u16 sta_rx_mcs_set, ap_tx_mcs_set;
379 	u8 mcs_count = 0;
380 	const u16 *ap_mcs_set, *sta_mcs_set;
381 	int i;
382 
383 	if (!hapd->iface->current_mode)
384 		return 1;
385 	ap_mcs_set = (u16 *) hapd->iface->current_mode->he_capab[opmode].mcs;
386 	sta_mcs_set = (u16 *) ((const struct ieee80211_he_capabilities *)
387 			       sta_he_capab)->optional;
388 
389 	/*
390 	 * Disable HE capabilities for STAs for which there is not even a single
391 	 * allowed MCS in any supported number of streams, i.e., STA is
392 	 * advertising 3 (not supported) as HE MCS rates for all supported
393 	 * band/stream cases.
394 	 */
395 	switch (hapd->iface->conf->he_oper_chwidth) {
396 	case CHANWIDTH_80P80MHZ:
397 		mcs_count = 3;
398 		break;
399 	case CHANWIDTH_160MHZ:
400 		mcs_count = 2;
401 		break;
402 	default:
403 		mcs_count = 1;
404 		break;
405 	}
406 
407 	for (i = 0; i < mcs_count; i++) {
408 		int j;
409 
410 		/* AP Tx MCS map vs. STA Rx MCS map */
411 		sta_rx_mcs_set = WPA_GET_LE16((const u8 *) &sta_mcs_set[i * 2]);
412 		ap_tx_mcs_set = WPA_GET_LE16((const u8 *)
413 					     &ap_mcs_set[(i * 2) + 1]);
414 
415 		for (j = 0; j < HE_NSS_MAX_STREAMS; j++) {
416 			if (((ap_tx_mcs_set >> (j * 2)) & 0x3) == 3)
417 				continue;
418 
419 			if (((sta_rx_mcs_set >> (j * 2)) & 0x3) == 3)
420 				continue;
421 
422 			return 1;
423 		}
424 	}
425 
426 	wpa_printf(MSG_DEBUG,
427 		   "No matching HE MCS found between AP TX and STA RX");
428 
429 	return 0;
430 }
431 
432 
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)433 u16 copy_sta_he_capab(struct hostapd_data *hapd, struct sta_info *sta,
434 		      enum ieee80211_op_mode opmode, const u8 *he_capab,
435 		      size_t he_capab_len)
436 {
437 	if (!he_capab || !hapd->iconf->ieee80211ax ||
438 	    hapd->conf->disable_11ax ||
439 	    !check_valid_he_mcs(hapd, he_capab, opmode) ||
440 	    ieee80211_invalid_he_cap_size(he_capab, he_capab_len) ||
441 	    he_capab_len > sizeof(struct ieee80211_he_capabilities)) {
442 		sta->flags &= ~WLAN_STA_HE;
443 		os_free(sta->he_capab);
444 		sta->he_capab = NULL;
445 		return WLAN_STATUS_SUCCESS;
446 	}
447 
448 	if (!sta->he_capab) {
449 		sta->he_capab =
450 			os_zalloc(sizeof(struct ieee80211_he_capabilities));
451 		if (!sta->he_capab)
452 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
453 	}
454 
455 	sta->flags |= WLAN_STA_HE;
456 	os_memset(sta->he_capab, 0, sizeof(struct ieee80211_he_capabilities));
457 	os_memcpy(sta->he_capab, he_capab, he_capab_len);
458 	sta->he_capab_len = he_capab_len;
459 
460 	return WLAN_STATUS_SUCCESS;
461 }
462 
463 
copy_sta_he_6ghz_capab(struct hostapd_data * hapd,struct sta_info * sta,const u8 * he_6ghz_capab)464 u16 copy_sta_he_6ghz_capab(struct hostapd_data *hapd, struct sta_info *sta,
465 			   const u8 *he_6ghz_capab)
466 {
467 	if (!he_6ghz_capab || !hapd->iconf->ieee80211ax ||
468 	    hapd->conf->disable_11ax ||
469 	    !is_6ghz_op_class(hapd->iconf->op_class)) {
470 		sta->flags &= ~WLAN_STA_6GHZ;
471 		os_free(sta->he_6ghz_capab);
472 		sta->he_6ghz_capab = NULL;
473 		return WLAN_STATUS_SUCCESS;
474 	}
475 
476 	if (!sta->he_6ghz_capab) {
477 		sta->he_6ghz_capab =
478 			os_zalloc(sizeof(struct ieee80211_he_6ghz_band_cap));
479 		if (!sta->he_6ghz_capab)
480 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
481 	}
482 
483 	sta->flags |= WLAN_STA_6GHZ;
484 	os_memcpy(sta->he_6ghz_capab, he_6ghz_capab,
485 		  sizeof(struct ieee80211_he_6ghz_band_cap));
486 
487 	return WLAN_STATUS_SUCCESS;
488 }
489 
490 
hostapd_get_he_twt_responder(struct hostapd_data * hapd,enum ieee80211_op_mode mode)491 int hostapd_get_he_twt_responder(struct hostapd_data *hapd,
492 				 enum ieee80211_op_mode mode)
493 {
494 	u8 *mac_cap;
495 
496 	if (!hapd->iface->current_mode ||
497 	    !hapd->iface->current_mode->he_capab[mode].he_supported)
498 		return 0;
499 
500 	mac_cap = hapd->iface->current_mode->he_capab[mode].mac_cap;
501 
502 	return !!(mac_cap[HE_MAC_CAPAB_0] & HE_MACCAP_TWT_RESPONDER);
503 }
504