xref: /freebsd/sys/net80211/ieee80211_vht.c (revision 1ccaef9867461c42251922e3fc4d4f85cec682b5)
1 /*-
2  * Copyright (c) 2017 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 /*
27  * IEEE 802.11ac-2013 protocol support.
28  */
29 
30 #include "opt_inet.h"
31 #include "opt_wlan.h"
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 #include <sys/endian.h>
38 
39 #include <sys/socket.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_action.h>
48 #include <net80211/ieee80211_input.h>
49 #include <net80211/ieee80211_vht.h>
50 
51 #define	ADDSHORT(frm, v) do {			\
52 	frm[0] = (v) & 0xff;			\
53 	frm[1] = (v) >> 8;			\
54 	frm += 2;				\
55 } while (0)
56 #define	ADDWORD(frm, v) do {			\
57 	frm[0] = (v) & 0xff;			\
58 	frm[1] = ((v) >> 8) & 0xff;		\
59 	frm[2] = ((v) >> 16) & 0xff;		\
60 	frm[3] = ((v) >> 24) & 0xff;		\
61 	frm += 4;				\
62 } while (0)
63 
64 /*
65  * Immediate TODO:
66  *
67  * + handle WLAN_ACTION_VHT_OPMODE_NOTIF and other VHT action frames
68  * + ensure vhtinfo/vhtcap parameters correctly use the negotiated
69  *   capabilities and ratesets
70  * + group ID management operation
71  */
72 
73 /*
74  * XXX TODO: handle WLAN_ACTION_VHT_OPMODE_NOTIF
75  *
76  * Look at mac80211/vht.c:ieee80211_vht_handle_opmode() for further details.
77  */
78 
79 static int
vht_recv_action_placeholder(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)80 vht_recv_action_placeholder(struct ieee80211_node *ni,
81     const struct ieee80211_frame *wh,
82     const uint8_t *frm, const uint8_t *efrm)
83 {
84 
85 #ifdef IEEE80211_DEBUG
86 	ieee80211_note(ni->ni_vap, "%s: called; fc=0x%.2x/0x%.2x",
87 	    __func__, wh->i_fc[0], wh->i_fc[1]);
88 #endif
89 	return (0);
90 }
91 
92 static int
vht_send_action_placeholder(struct ieee80211_node * ni,int category,int action,void * arg0)93 vht_send_action_placeholder(struct ieee80211_node *ni,
94     int category, int action, void *arg0)
95 {
96 
97 #ifdef IEEE80211_DEBUG
98 	ieee80211_note(ni->ni_vap, "%s: called; category=%d, action=%d",
99 	    __func__, category, action);
100 #endif
101 	return (EINVAL);
102 }
103 
104 static void
ieee80211_vht_init(void * dummy __unused)105 ieee80211_vht_init(void *dummy __unused)
106 {
107 
108 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
109 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_recv_action_placeholder);
110 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
111 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_recv_action_placeholder);
112 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
113 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_recv_action_placeholder);
114 
115 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
116 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_send_action_placeholder);
117 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
118 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_send_action_placeholder);
119 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
120 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_send_action_placeholder);
121 }
122 
123 SYSINIT(wlan_vht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_vht_init, NULL);
124 
125 void
ieee80211_vht_attach(struct ieee80211com * ic)126 ieee80211_vht_attach(struct ieee80211com *ic)
127 {
128 }
129 
130 void
ieee80211_vht_detach(struct ieee80211com * ic)131 ieee80211_vht_detach(struct ieee80211com *ic)
132 {
133 }
134 
135 void
ieee80211_vht_vattach(struct ieee80211vap * vap)136 ieee80211_vht_vattach(struct ieee80211vap *vap)
137 {
138 	struct ieee80211com *ic = vap->iv_ic;
139 
140 	if (! IEEE80211_CONF_VHT(ic))
141 		return;
142 
143 	vap->iv_vht_cap.vht_cap_info = ic->ic_vht_cap.vht_cap_info;
144 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
145 
146 	/* XXX assume VHT80 support; should really check vhtcaps */
147 	vap->iv_vht_flags =
148 	    IEEE80211_FVHT_VHT
149 	    | IEEE80211_FVHT_USEVHT40
150 	    | IEEE80211_FVHT_USEVHT80;
151 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vht_cap.vht_cap_info))
152 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT160;
153 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vht_cap.vht_cap_info))
154 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT80P80;
155 
156 	memcpy(&vap->iv_vht_cap.supp_mcs, &ic->ic_vht_cap.supp_mcs,
157 	    sizeof(struct ieee80211_vht_mcs_info));
158 }
159 
160 void
ieee80211_vht_vdetach(struct ieee80211vap * vap)161 ieee80211_vht_vdetach(struct ieee80211vap *vap)
162 {
163 }
164 
165 #if 0
166 static void
167 vht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
168 {
169 }
170 #endif
171 
172 static int
vht_mcs_to_num(int m)173 vht_mcs_to_num(int m)
174 {
175 
176 	switch (m) {
177 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
178 		return (7);
179 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
180 		return (8);
181 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
182 		return (9);
183 	default:
184 		return (0);
185 	}
186 }
187 
188 void
ieee80211_vht_announce(struct ieee80211com * ic)189 ieee80211_vht_announce(struct ieee80211com *ic)
190 {
191 	int i, tx, rx;
192 
193 	if (! IEEE80211_CONF_VHT(ic))
194 		return;
195 
196 	/* Channel width */
197 	ic_printf(ic, "[VHT] Channel Widths: 20MHz, 40MHz, 80MHz%s%s\n",
198 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(ic->ic_vht_cap.vht_cap_info)) ?
199 		", 160MHz" : "",
200 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(ic->ic_vht_cap.vht_cap_info)) ?
201 		 ", 80+80MHz" : "");
202 	/* Features */
203 	ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vht_cap.vht_cap_info,
204 	    IEEE80211_VHTCAP_BITS);
205 
206 	/* For now, just 5GHz VHT.  Worry about 2GHz VHT later */
207 	for (i = 0; i < 8; i++) {
208 		/* Each stream is 2 bits */
209 		tx = (ic->ic_vht_cap.supp_mcs.tx_mcs_map >> (2*i)) & 0x3;
210 		rx = (ic->ic_vht_cap.supp_mcs.rx_mcs_map >> (2*i)) & 0x3;
211 		if (tx == 3 && rx == 3)
212 			continue;
213 		ic_printf(ic, "[VHT] NSS %d: TX MCS 0..%d, RX MCS 0..%d\n",
214 		    i + 1, vht_mcs_to_num(tx), vht_mcs_to_num(rx));
215 	}
216 }
217 
218 void
ieee80211_vht_node_init(struct ieee80211_node * ni)219 ieee80211_vht_node_init(struct ieee80211_node *ni)
220 {
221 
222 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
223 	    "%s: called", __func__);
224 	ni->ni_flags |= IEEE80211_NODE_VHT;
225 }
226 
227 void
ieee80211_vht_node_cleanup(struct ieee80211_node * ni)228 ieee80211_vht_node_cleanup(struct ieee80211_node *ni)
229 {
230 
231 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
232 	    "%s: called", __func__);
233 	ni->ni_flags &= ~IEEE80211_NODE_VHT;
234 	ni->ni_vhtcap = 0;
235 	bzero(&ni->ni_vht_mcsinfo, sizeof(struct ieee80211_vht_mcs_info));
236 }
237 
238 /**
239  * @brief Parse an 802.11ac VHT operation IE.
240  *
241  * This parses the VHT operation IE (channel width, basic MCS set)
242  * into the given ieee80211_node .
243  *
244  * 802.11-2020 9.4.2.158 (VHT Operation element)
245  *
246  * @param ni ieee80211_node to parse VHT operation IE into
247  * @param ie The VHT operation IE to parse, 802.11 endian
248  */
249 void
ieee80211_parse_vhtopmode(struct ieee80211_node * ni,const uint8_t * ie)250 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
251 {
252 	/* vht operation */
253 	ni->ni_vht_chanwidth = ie[2];
254 	ni->ni_vht_chan1 = ie[3];
255 	ni->ni_vht_chan2 = ie[4];
256 	ni->ni_vht_basicmcs = le16dec(ie + 5);
257 
258 #if 0
259 	net80211_vap_printf(ni->ni_vap,
260 	    "%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
261 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
262 	    ni->ni_vht_basicmcs);
263 #endif
264 }
265 
266 /**
267  * @brief Parse an 802.11ac VHT capability IE.
268  *
269  * Parse the VHT capability IE into the node vht fields
270  * (ni->ni_vht_mcsinfo, ni->ni_vhtcap).
271  *
272  * 802.11-2020 9.4.2.157 (VHT Capabilities element)
273  *
274  * @param ni ieee80211_node to parse VHT info into
275  * @param ie VHT capability IE to parse, 802.11 endian
276  */
277 void
ieee80211_parse_vhtcap(struct ieee80211_node * ni,const uint8_t * ie)278 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
279 {
280 
281 	/* vht capability */
282 	ni->ni_vhtcap = le32dec(ie + 2);
283 
284 	/* suppmcs */
285 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
286 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
287 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
288 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
289 }
290 
291 int
ieee80211_vht_updateparams(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)292 ieee80211_vht_updateparams(struct ieee80211_node *ni,
293     const uint8_t *vhtcap_ie,
294     const uint8_t *vhtop_ie)
295 {
296 
297 	//printf("%s: called\n", __func__);
298 
299 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
300 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
301 	return (0);
302 }
303 
304 /**
305  * @brief calculate the supported MCS rates for this node
306  *
307  * This is called once a node has finished association /
308  * joined a BSS.  The vhtcap / vhtop IEs are from the
309  * peer.  The transmit rate tables need to be combined
310  * together to setup the list of available rates.
311  *
312  * This must be called after the ieee80211_node VHT fields
313  * have been parsed / populated by either ieee80211_vht_updateparams() or
314  * ieee80211_parse_vhtcap(),
315  *
316  * This does not take into account the channel bandwidth,
317  * which (a) may change during operation, and (b) depends
318  * upon packet to packet rate transmission selection.
319  * There are various rate combinations which are not
320  * available in various channel widths and those will
321  * need to be masked off separately.
322  *
323  * (See 802.11-2020 21.5 Parameters for VHT-MCSs for the
324  * tables and supported rates.)
325  *
326  * ALSO: i need to do some filtering based on the HT set too.
327  * (That should be done here too, and in the negotiation, sigh.)
328  * (See 802.11-2016 10.7.12.3 Additional rate selection constraints
329  * for VHT PPDUs)
330  *
331  * @param ni	struct ieee80211_node to configure
332  */
333 void
ieee80211_setup_vht_rates(struct ieee80211_node * ni)334 ieee80211_setup_vht_rates(struct ieee80211_node *ni)
335 {
336 	struct ieee80211vap *vap = ni->ni_vap;
337 	uint32_t val, val1, val2;
338 	uint16_t tx_mcs_map = 0;
339 	int i;
340 
341 	/*
342 	 * Merge our tx_mcs_map with the peer rx_mcs_map to determine what
343 	 * can be actually transmitted to the peer.
344 	 */
345 
346 	for (i = 0; i < 8; i++) {
347 		/*
348 		 * Merge the two together; remember that 0..2 is in order
349 		 * of increasing MCS support, but 3 equals
350 		 * IEEE80211_VHT_MCS_NOT_SUPPORTED so must "win".
351 		 */
352 		val1 = (vap->iv_vht_cap.supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
353 		val2 = (ni->ni_vht_mcsinfo.rx_mcs_map >> (i*2)) & 0x3;
354 		val = MIN(val1, val2);
355 		if (val1 == IEEE80211_VHT_MCS_NOT_SUPPORTED ||
356 		    val2 == IEEE80211_VHT_MCS_NOT_SUPPORTED)
357 			val = IEEE80211_VHT_MCS_NOT_SUPPORTED;
358 		tx_mcs_map |= (val << (i*2));
359 	}
360 
361 	/* Store the TX MCS map somewhere in the node that can be used */
362 	ni->ni_vht_tx_map = tx_mcs_map;
363 }
364 
365 void
ieee80211_vht_timeout(struct ieee80211vap * vap)366 ieee80211_vht_timeout(struct ieee80211vap *vap)
367 {
368 }
369 
370 void
ieee80211_vht_node_join(struct ieee80211_node * ni)371 ieee80211_vht_node_join(struct ieee80211_node *ni)
372 {
373 
374 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
375 	    "%s: called", __func__);
376 }
377 
378 void
ieee80211_vht_node_leave(struct ieee80211_node * ni)379 ieee80211_vht_node_leave(struct ieee80211_node *ni)
380 {
381 
382 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
383 	    "%s: called", __func__);
384 }
385 
386 /**
387  * @brief Calculate the VHTCAP IE for a given node.
388  *
389  * This includes calculating the capability intersection based on the
390  * current operating mode and intersection of the TX/RX MCS maps.
391  *
392  * The standard only makes it clear about MCS rate negotiation
393  * and MCS basic rates (which must be a subset of the general
394  * negotiated rates).  It doesn't make it clear that the AP should
395  * figure out the minimum functional overlap with the STA and
396  * support that.
397  *
398  * Note: this is in host order, not in 802.11 endian order.
399  *
400  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
401  *
402  * TODO: investigate what we should negotiate for MU-MIMO beamforming
403  *       options.
404  *
405  * @param ni ieee80211_node to check
406  * @param vhtcap ieee80211_vht_cap to populate (in host order).
407  * @param opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
408  */
409 void
ieee80211_vht_get_vhtcap_ie(struct ieee80211_node * ni,struct ieee80211_vht_cap * vhtcap,int opmode)410 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
411     struct ieee80211_vht_cap *vhtcap, int opmode)
412 {
413 	struct ieee80211vap *vap = ni->ni_vap;
414 //	struct ieee80211com *ic = vap->iv_ic;
415 	uint32_t val, val1, val2;
416 	uint32_t new_vhtcap;
417 	int i;
418 
419 	/*
420 	 * Capabilities - it depends on whether we are a station
421 	 * or not.
422 	 */
423 	new_vhtcap = 0;
424 
425 	/*
426 	 * Station - use our desired configuration based on
427 	 * local config, local device bits and the already-learnt
428 	 * vhtcap/vhtinfo IE in the node.
429 	 */
430 
431 	/* Limit MPDU size to the smaller of the two */
432 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
433 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
434 	if (opmode == 1) {
435 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
436 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
437 	}
438 	val = MIN(val1, val2);
439 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
440 
441 	/* Limit supp channel config */
442 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
443 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
444 	if (opmode == 1) {
445 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
446 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
447 	}
448 	if ((val2 == 2) &&
449 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) == 0))
450 		val2 = 1;
451 	if ((val2 == 1) &&
452 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) == 0))
453 		val2 = 0;
454 	val = MIN(val1, val2);
455 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
456 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
457 
458 	/* RX LDPC */
459 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
460 	    IEEE80211_VHTCAP_RXLDPC);
461 	if (opmode == 1) {
462 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
463 		    IEEE80211_VHTCAP_RXLDPC);
464 	}
465 	val = MIN(val1, val2);
466 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
467 
468 	/* Short-GI 80 */
469 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
470 	    IEEE80211_VHTCAP_SHORT_GI_80);
471 	if (opmode == 1) {
472 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
473 		    IEEE80211_VHTCAP_SHORT_GI_80);
474 	}
475 	val = MIN(val1, val2);
476 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
477 
478 	/* Short-GI 160 */
479 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
480 	    IEEE80211_VHTCAP_SHORT_GI_160);
481 	if (opmode == 1) {
482 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
483 		    IEEE80211_VHTCAP_SHORT_GI_160);
484 	}
485 	val = MIN(val1, val2);
486 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
487 
488 	/*
489 	 * STBC is slightly more complicated.
490 	 *
491 	 * In non-STA mode, we just announce our capabilities and that
492 	 * is that.
493 	 *
494 	 * In STA mode, we should calculate our capabilities based on
495 	 * local capabilities /and/ what the remote says. So:
496 	 *
497 	 * + Only TX STBC if we support it and the remote supports RX STBC;
498 	 * + Only announce RX STBC if we support it and the remote supports
499 	 *   TX STBC;
500 	 * + RX STBC should be the minimum of local and remote RX STBC;
501 	 */
502 
503 	/* TX STBC */
504 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
505 	    IEEE80211_VHTCAP_TXSTBC);
506 	if (opmode == 1) {
507 		/* STA mode - enable it only if node RXSTBC is non-zero */
508 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
509 		    IEEE80211_VHTCAP_RXSTBC_MASK);
510 	}
511 	val = MIN(val1, val2);
512 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_TX) == 0)
513 		val = 0;
514 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
515 
516 	/* RX STBC1..4 */
517 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
518 	    IEEE80211_VHTCAP_RXSTBC_MASK);
519 	if (opmode == 1) {
520 		/* STA mode - enable it only if node TXSTBC is non-zero */
521 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
522 		   IEEE80211_VHTCAP_TXSTBC);
523 	}
524 	val = MIN(val1, val2);
525 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_RX) == 0)
526 		val = 0;
527 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
528 
529 	/*
530 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
531 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
532 	 * it would be silly.
533 	 */
534 	if (val == 0)
535 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
536 
537 	/*
538 	 * Some of these fields require other fields to exist.
539 	 * So before using it, the parent field needs to be checked
540 	 * otherwise the overridden value may be wrong.
541 	 *
542 	 * For example, if SU beamformee is set to 0, then BF STS
543 	 * needs to be 0.
544 	 */
545 
546 	/* SU Beamformer capable */
547 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
548 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
549 	if (opmode == 1) {
550 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
551 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
552 	}
553 	val = MIN(val1, val2);
554 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
555 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
556 
557 	/* SU Beamformee capable */
558 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
559 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
560 	if (opmode == 1) {
561 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
562 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
563 	}
564 	val = MIN(val1, val2);
565 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
566 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
567 
568 	/* Beamformee STS capability - only if SU beamformee capable */
569 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
570 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
571 	if (opmode == 1) {
572 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
573 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
574 	}
575 	val = MIN(val1, val2);
576 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
577 		val = 0;
578 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
579 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
580 
581 	/* Sounding dimensions - only if SU beamformer capable */
582 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
583 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
584 	if (opmode == 1)
585 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
586 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
587 	val = MIN(val1, val2);
588 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
589 		val = 0;
590 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
591 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
592 
593 	/*
594 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
595 	 * and STA (not AP)
596 	 */
597 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
598 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
599 	if (opmode == 1)
600 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
601 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
602 	val = MIN(val1, val2);
603 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
604 		val = 0;
605 	if (opmode != 1)	/* Only enable for STA mode */
606 		val = 0;
607 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
608 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
609 
610 	/*
611 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
612 	 * and AP (not STA)
613 	 */
614 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
615 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
616 	if (opmode == 1)
617 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
618 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
619 	val = MIN(val1, val2);
620 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
621 		val = 0;
622 	if (opmode != 0)	/* Only enable for AP mode */
623 		val = 0;
624 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
625 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
626 
627 	/* VHT TXOP PS */
628 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
629 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
630 	if (opmode == 1)
631 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
632 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
633 	val = MIN(val1, val2);
634 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
635 
636 	/* HTC_VHT */
637 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
638 	    IEEE80211_VHTCAP_HTC_VHT);
639 	if (opmode == 1)
640 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
641 		    IEEE80211_VHTCAP_HTC_VHT);
642 	val = MIN(val1, val2);
643 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
644 
645 	/* A-MPDU length max */
646 	/* XXX TODO: we need a userland config knob for this */
647 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
648 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
649 	if (opmode == 1)
650 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
651 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
652 	val = MIN(val1, val2);
653 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
654 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
655 
656 	/*
657 	 * Link adaptation is only valid if HTC-VHT capable is 1.
658 	 * Otherwise, always set it to 0.
659 	 */
660 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
661 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
662 	if (opmode == 1)
663 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
664 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
665 	val = MIN(val1, val2);
666 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
667 		val = 0;
668 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
669 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
670 
671 	/*
672 	 * The following two options are 0 if the pattern may change, 1 if it
673 	 * does not change.  So, downgrade to the higher value.
674 	 */
675 
676 	/* RX antenna pattern */
677 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
678 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
679 	if (opmode == 1)
680 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
681 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
682 	val = MAX(val1, val2);
683 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
684 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
685 
686 	/* TX antenna pattern */
687 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
688 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
689 	if (opmode == 1)
690 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
691 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
692 	val = MAX(val1, val2);
693 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
694 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
695 
696 	/*
697 	 * MCS set - again, we announce what we want to use
698 	 * based on configuration, device capabilities and
699 	 * already-learnt vhtcap/vhtinfo IE information.
700 	 */
701 
702 	/* MCS set - start with whatever the device supports */
703 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_cap.supp_mcs.rx_mcs_map;
704 	vhtcap->supp_mcs.rx_highest = 0;
705 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_cap.supp_mcs.tx_mcs_map;
706 	vhtcap->supp_mcs.tx_highest = 0;
707 
708 	vhtcap->vht_cap_info = new_vhtcap;
709 
710 	/*
711 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
712 	 * Ie, we continue to state we can receive whatever we can do,
713 	 * but we only announce that we will transmit rates that meet
714 	 * the AP requirement.
715 	 *
716 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
717 	 * We can't just use MIN() because '3' means "no", so special case it.
718 	 */
719 	if (opmode) {
720 		for (i = 0; i < 8; i++) {
721 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
722 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
723 			val = MIN(val1, val2);
724 			if (val1 == 3 || val2 == 3)
725 				val = 3;
726 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
727 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
728 		}
729 	}
730 }
731 
732 /**
733  * @brief Add a VHTCAP field.
734  *
735  * If in station mode, we announce what we would like our
736  * desired configuration to be.
737  *
738  * Else, we announce our capabilities based on our current
739  * configuration.
740  *
741  * TODO: This assumes that the passed in buffer has enough space for
742  * the VHT capabilitity IE and that seems error prone.
743  *
744  * @param frm buffer to start populating the IE into
745  * @param ni ieee80211_node to fetch the VHT capability from
746  * @returns a pointer to the first byte in the buffer after the newly
747  *          populated IE
748  */
749 uint8_t *
ieee80211_add_vhtcap(uint8_t * frm,struct ieee80211_node * ni)750 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
751 {
752 	struct ieee80211_vht_cap vhtcap;
753 
754 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, 1);
755 
756 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
757 	frm[1] = sizeof(vhtcap);
758 	frm += 2;
759 
760 	/* 32-bit VHT capability */
761 	ADDWORD(frm, vhtcap.vht_cap_info);
762 
763 	/* suppmcs */
764 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
765 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
766 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
767 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
768 
769 	return (frm);
770 }
771 
772 /*
773  * Non-associated probe requests.  Add VHT capabilities based on
774  * the current channel configuration.  No BSS yet.
775  */
776 uint8_t *
ieee80211_add_vhtcap_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)777 ieee80211_add_vhtcap_ch(uint8_t *frm, struct ieee80211vap *vap,
778     struct ieee80211_channel *c)
779 {
780 	struct ieee80211_vht_cap *vhtcap;
781 
782 	memset(frm, 0, 2 + sizeof(*vhtcap));
783 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
784 	frm[1] = sizeof(*vhtcap);
785 	frm += 2;
786 
787 	/* 32-bit VHT capability */
788 	ADDWORD(frm, vap->iv_vht_cap.vht_cap_info);
789 
790 	/* supp_mcs */
791 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_mcs_map);
792 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_highest);
793 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_mcs_map);
794 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_highest);
795 
796 	return (frm);
797 }
798 
799 static uint8_t
ieee80211_vht_get_chwidth_ie(const struct ieee80211vap * vap,const struct ieee80211_channel * c)800 ieee80211_vht_get_chwidth_ie(const struct ieee80211vap *vap,
801     const struct ieee80211_channel *c)
802 {
803 
804 	/*
805 	 * XXX TODO: look at the node configuration as
806 	 * well?
807 	 */
808 
809 	if (IEEE80211_IS_CHAN_VHT80P80(c))
810 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
811 	if (IEEE80211_IS_CHAN_VHT160(c))
812 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
813 	if (IEEE80211_IS_CHAN_VHT80(c))
814 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
815 	if (IEEE80211_IS_CHAN_VHT40(c))
816 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
817 	if (IEEE80211_IS_CHAN_VHT20(c))
818 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
819 
820 	/* We shouldn't get here */
821 	net80211_vap_printf(vap,
822 	    "%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
823 	    __func__, (int) c->ic_freq, c->ic_flags);
824 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
825 }
826 
827 /*
828  * Note: this just uses the current channel information;
829  * it doesn't use the node info after parsing.
830  *
831  * XXX TODO: need to make the basic MCS set configurable.
832  * XXX TODO: read 802.11-2013 to determine what to set
833  *           chwidth to when scanning.  I have a feeling
834  *           it isn't involved in scanning and we shouldn't
835  *           be sending it; and I don't yet know what to set
836  *           it to for IBSS or hostap where the peer may be
837  *           a completely different channel width to us.
838  */
839 uint8_t *
ieee80211_add_vhtinfo(uint8_t * frm,struct ieee80211_node * ni)840 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
841 {
842 
843 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
844 	frm[1] = sizeof(struct ieee80211_vht_operation);
845 	frm += 2;
846 
847 	/* 8-bit chanwidth */
848 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_vap, ni->ni_chan);
849 
850 	/* 8-bit freq1 */
851 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
852 
853 	/* 8-bit freq2 */
854 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
855 
856 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
857 	ADDSHORT(frm, 0xfffc);
858 
859 	return (frm);
860 }
861 
862 void
ieee80211_vht_update_cap(struct ieee80211_node * ni,const uint8_t * vhtcap_ie)863 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie)
864 {
865 
866 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
867 }
868 
869 static struct ieee80211_channel *
findvhtchan(struct ieee80211com * ic,struct ieee80211_channel * c,int vhtflags)870 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
871 {
872 
873 	return (ieee80211_find_channel(ic, c->ic_freq,
874 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
875 }
876 
877 /*
878  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
879  */
880 struct ieee80211_channel *
ieee80211_vht_adjust_channel(struct ieee80211com * ic,struct ieee80211_channel * chan,int flags)881 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
882     struct ieee80211_channel *chan, int flags)
883 {
884 	struct ieee80211_channel *c;
885 
886 	/* First case - handle channel demotion - if VHT isn't set */
887 	if ((flags & IEEE80211_FVHT_MASK) == 0) {
888 #if 0
889 		net80211_ic_printf(ic,
890 		    "%s: demoting channel %d/0x%08x\n", __func__,
891 		    chan->ic_ieee, chan->ic_flags);
892 #endif
893 		c = ieee80211_find_channel(ic, chan->ic_freq,
894 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
895 		if (c == NULL)
896 			c = chan;
897 #if 0
898 		net80211_ic_printf(ic, "%s: .. to %d/0x%08x\n", __func__,
899 		    c->ic_ieee, c->ic_flags);
900 #endif
901 		return (c);
902 	}
903 
904 	/*
905 	 * We can upgrade to VHT - attempt to do so
906 	 *
907 	 * Note: we don't clear the HT flags, these are the hints
908 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
909 	 */
910 	c = NULL;
911 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
912 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
913 
914 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
915 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
916 
917 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
918 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
919 
920 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
921 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
922 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
923 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
924 	/*
925 	 * If we get here, VHT20 is always possible because we checked
926 	 * for IEEE80211_FVHT_VHT above.
927 	 */
928 	if (c == NULL)
929 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
930 
931 	if (c != NULL)
932 		chan = c;
933 
934 #if 0
935 	net80211_ic_printf(ic, "%s: selected %d/0x%08x\n", __func__,
936 	    c->ic_ieee, c->ic_flags);
937 #endif
938 	return (chan);
939 }
940 
941 /*
942  * Calculate the VHT operation IE for a given node.
943  *
944  * This includes calculating the suitable channel width/parameters
945  * and basic MCS set.
946  *
947  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
948  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
949  */
950 void
ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node * ni,struct ieee80211_vht_operation * vhtop,int opmode)951 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
952     struct ieee80211_vht_operation *vhtop, int opmode)
953 {
954 	net80211_vap_printf(ni->ni_vap, "%s: called; TODO!\n", __func__);
955 }
956 
957 /**
958  * @brief Check if VHT rates can be used for the given node.
959  *
960  * This returns true if any VHT rates can be used to transmit
961  * to the given node.
962  *
963  * @param ni ieee80211_node to check
964  * @returns True if any VHT rates can be transmitted to the given node
965  */
966 bool
ieee80211_vht_check_tx_vht(const struct ieee80211_node * ni)967 ieee80211_vht_check_tx_vht(const struct ieee80211_node *ni)
968 {
969 	const struct ieee80211vap *vap;
970 	const struct ieee80211_channel *bss_chan;
971 
972 	if (ni == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC ||
973 	    ni->ni_vap == NULL || ni->ni_vap->iv_bss == NULL)
974 		return (false);
975 
976 	vap = ni->ni_vap;
977 	bss_chan = vap->iv_bss->ni_chan;
978 
979 	if (bss_chan == IEEE80211_CHAN_ANYC)
980 		return (false);
981 
982 	return (IEEE80211_IS_CHAN_VHT(ni->ni_chan));
983 }
984 
985 /**
986  * @brief Check if VHT40 rates can be transmitted to the given node.
987  *
988  * This verifies that the BSS is VHT40 capable and the current
989  * node channel width is 40MHz.
990  *
991  * @param ni ieee80211_node to check
992  * @returns True if 40MHz VHT rates can be transmitted to the given node
993  */
994 static bool
ieee80211_vht_check_tx_vht40(const struct ieee80211_node * ni)995 ieee80211_vht_check_tx_vht40(const struct ieee80211_node *ni)
996 {
997 	struct ieee80211vap *vap;
998 	struct ieee80211_channel *bss_chan;
999 
1000 	if (!ieee80211_vht_check_tx_vht(ni))
1001 		return (false);
1002 
1003 	vap = ni->ni_vap;
1004 	bss_chan = vap->iv_bss->ni_chan;
1005 
1006 	return (IEEE80211_IS_CHAN_VHT40(bss_chan) &&
1007 	    IEEE80211_IS_CHAN_VHT40(ni->ni_chan) &&
1008 	    (ni->ni_chw == NET80211_STA_RX_BW_40));
1009 }
1010 
1011 /**
1012  * @brief Check if VHT80 rates can be transmitted to the given node.
1013  *
1014  * This verifies that the BSS is VHT80 capable and the current
1015  * node channel width is 80MHz.
1016  *
1017  * @param ni ieee80211_node to check
1018  * @returns True if 80MHz VHT rates can be transmitted to the given node
1019  */
1020 static bool
ieee80211_vht_check_tx_vht80(const struct ieee80211_node * ni)1021 ieee80211_vht_check_tx_vht80(const struct ieee80211_node *ni)
1022 {
1023 	struct ieee80211vap *vap;
1024 	struct ieee80211_channel *bss_chan;
1025 
1026 	if (!ieee80211_vht_check_tx_vht(ni))
1027 		return (false);
1028 
1029 	vap = ni->ni_vap;
1030 	bss_chan = vap->iv_bss->ni_chan;
1031 
1032 	/*
1033 	 * ni_chw represents 20MHz or 40MHz from the HT
1034 	 * TX width action frame / HT channel negotiation.
1035 	 * If a HT TX width action frame sets it to 20MHz
1036 	 * then reject doing 80MHz.
1037 	 */
1038 	return (IEEE80211_IS_CHAN_VHT80(bss_chan) &&
1039 	    IEEE80211_IS_CHAN_VHT80(ni->ni_chan) &&
1040 	    (ni->ni_chw != NET80211_STA_RX_BW_20));
1041 }
1042 
1043 /**
1044  * @brief Check if VHT 160 rates can be transmitted to the given node.
1045  *
1046  * This verifies that the BSS is VHT80+80 or VHT160 capable and the current
1047  * node channel width is 80+80MHz or 160MHz.
1048  *
1049  * @param ni ieee80211_node to check
1050  * @returns True if 160MHz VHT rates can be transmitted to the given node
1051  */
1052 static bool
ieee80211_vht_check_tx_vht160(const struct ieee80211_node * ni)1053 ieee80211_vht_check_tx_vht160(const struct ieee80211_node *ni)
1054 {
1055 	struct ieee80211vap *vap;
1056 	struct ieee80211_channel *bss_chan;
1057 
1058 	if (!ieee80211_vht_check_tx_vht(ni))
1059 		return (false);
1060 
1061 	vap = ni->ni_vap;
1062 	bss_chan = vap->iv_bss->ni_chan;
1063 
1064 	/*
1065 	 * ni_chw represents 20MHz or 40MHz from the HT
1066 	 * TX width action frame / HT channel negotiation.
1067 	 * If a HT TX width action frame sets it to 20MHz
1068 	 * then reject doing 160MHz.
1069 	 */
1070 	if (ni->ni_chw == NET80211_STA_RX_BW_20)
1071 		return (false);
1072 
1073 	if (IEEE80211_IS_CHAN_VHT160(bss_chan) &&
1074 	    IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1075 		return (true);
1076 
1077 	if (IEEE80211_IS_CHAN_VHT80P80(bss_chan) &&
1078 	    IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1079 		return (true);
1080 
1081 	return (false);
1082 }
1083 
1084 /**
1085  * @brief Check if the given transmit bandwidth is available to the given node
1086  *
1087  * This checks that the node and BSS both allow the given bandwidth,
1088  * and that the current node bandwidth (which can dynamically change)
1089  * also allows said bandwidth.
1090  *
1091  * This relies on the channels having the flags for the narrower
1092  * channels as well - eg a VHT160 channel will have the CHAN_VHT80,
1093  * CHAN_VHT40, CHAN_VHT flags also set.
1094  *
1095  * @param ni		the ieee80211_node to check
1096  * @param bw		the required bandwidth to check
1097  *
1098  * @returns true if it is allowed, false otherwise
1099  */
1100 bool
ieee80211_vht_check_tx_bw(const struct ieee80211_node * ni,enum net80211_sta_rx_bw bw)1101 ieee80211_vht_check_tx_bw(const struct ieee80211_node *ni,
1102     enum net80211_sta_rx_bw bw)
1103 {
1104 
1105 	switch (bw) {
1106 	case NET80211_STA_RX_BW_20:
1107 		return (ieee80211_vht_check_tx_vht(ni));
1108 	case NET80211_STA_RX_BW_40:
1109 		return (ieee80211_vht_check_tx_vht40(ni));
1110 	case NET80211_STA_RX_BW_80:
1111 		return (ieee80211_vht_check_tx_vht80(ni));
1112 	case NET80211_STA_RX_BW_160:
1113 		return (ieee80211_vht_check_tx_vht160(ni));
1114 	case NET80211_STA_RX_BW_320:
1115 		return (false);
1116 	default:
1117 		return (false);
1118 	}
1119 }
1120 
1121 /**
1122  * @brief Check if the given VHT bw/nss/mcs combination is valid
1123  *        for the give node.
1124  *
1125  * This checks whether the given VHT bw/nss/mcs is valid based on
1126  * the negotiated rate mask in the node.
1127  *
1128  * @param ni	struct ieee80211_node node to check
1129  * @param bw	channel bandwidth to check
1130  * @param nss	NSS
1131  * @param mcs	MCS
1132  * @returns True if this combination is available, false otherwise.
1133  */
1134 bool
ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node * ni,enum net80211_sta_rx_bw bw,uint8_t nss,uint8_t mcs)1135 ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node *ni,
1136     enum net80211_sta_rx_bw bw, uint8_t nss, uint8_t mcs)
1137 {
1138 	uint8_t mc;
1139 
1140 	/* Validate arguments */
1141 	if (nss < 1 || nss > 8)
1142 		return (false);
1143 	if (mcs > 9)
1144 		return (false);
1145 
1146 	/* Check our choice of rate is actually valid */
1147 	if (!ieee80211_phy_vht_validate_mcs(bw, nss, mcs))
1148 		return (false);
1149 
1150 	/*
1151 	 * Next, check if the MCS rate is available for the
1152 	 * given NSS.
1153 	 */
1154 	mc = ni->ni_vht_tx_map >> (2*(nss-1)) & 0x3;
1155 	switch (mc) {
1156 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
1157 		/* Not supported at this NSS */
1158 		return (false);
1159 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
1160 		return (mcs <= 9);
1161 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
1162 		return (mcs <= 8);
1163 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
1164 		return (mcs <= 7);
1165 	default:
1166 		return (false);
1167 	}
1168 }
1169