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