xref: /freebsd/sys/net80211/ieee80211_vht.c (revision 674362e27015394e105125598cb1518506ae1efa)
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)105 ieee80211_vht_init(void)
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  * Parse an 802.11ac VHT operation IE.
240  *
241  * 802.11-2020 9.4.2.158 (VHT Operation element)
242  */
243 void
ieee80211_parse_vhtopmode(struct ieee80211_node * ni,const uint8_t * ie)244 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
245 {
246 	/* vht operation */
247 	ni->ni_vht_chanwidth = ie[2];
248 	ni->ni_vht_chan1 = ie[3];
249 	ni->ni_vht_chan2 = ie[4];
250 	ni->ni_vht_basicmcs = le16dec(ie + 5);
251 
252 #if 0
253 	net80211_vap_printf(ni->ni_vap,
254 	    "%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
255 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
256 	    ni->ni_vht_basicmcs);
257 #endif
258 }
259 
260 /*
261  * Parse an 802.11ac VHT capability IE.
262  *
263  * 802.11-2020 9.4.2.157 (VHT Capabilities element)
264  */
265 void
ieee80211_parse_vhtcap(struct ieee80211_node * ni,const uint8_t * ie)266 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
267 {
268 
269 	/* vht capability */
270 	ni->ni_vhtcap = le32dec(ie + 2);
271 
272 	/* suppmcs */
273 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
274 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
275 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
276 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
277 }
278 
279 int
ieee80211_vht_updateparams(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)280 ieee80211_vht_updateparams(struct ieee80211_node *ni,
281     const uint8_t *vhtcap_ie,
282     const uint8_t *vhtop_ie)
283 {
284 
285 	//printf("%s: called\n", __func__);
286 
287 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
288 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
289 	return (0);
290 }
291 
292 /**
293  * @brief calculate the supported MCS rates for this node
294  *
295  * This is called once a node has finished association /
296  * joined a BSS.  The vhtcap / vhtop IEs are from the
297  * peer.  The transmit rate tables need to be combined
298  * together to setup the list of available rates.
299  *
300  * This must be called after the ieee80211_node VHT fields
301  * have been parsed / populated by either ieee80211_vht_updateparams() or
302  * ieee80211_parse_vhtcap(),
303  *
304  * This does not take into account the channel bandwidth,
305  * which (a) may change during operation, and (b) depends
306  * upon packet to packet rate transmission selection.
307  * There are various rate combinations which are not
308  * available in various channel widths and those will
309  * need to be masked off separately.
310  *
311  * (See 802.11-2020 21.5 Parameters for VHT-MCSs for the
312  * tables and supported rates.)
313  *
314  * ALSO: i need to do some filtering based on the HT set too.
315  * (That should be done here too, and in the negotiation, sigh.)
316  * (See 802.11-2016 10.7.12.3 Additional rate selection constraints
317  * for VHT PPDUs)
318  *
319  * @param ni	struct ieee80211_node to configure
320  */
321 void
ieee80211_setup_vht_rates(struct ieee80211_node * ni)322 ieee80211_setup_vht_rates(struct ieee80211_node *ni)
323 {
324 	struct ieee80211vap *vap = ni->ni_vap;
325 	uint32_t val, val1, val2;
326 	uint16_t tx_mcs_map = 0;
327 	int i;
328 
329 	/*
330 	 * Merge our tx_mcs_map with the peer rx_mcs_map to determine what
331 	 * can be actually transmitted to the peer.
332 	 */
333 
334 	for (i = 0; i < 8; i++) {
335 		/*
336 		 * Merge the two together; remember that 0..2 is in order
337 		 * of increasing MCS support, but 3 equals
338 		 * IEEE80211_VHT_MCS_NOT_SUPPORTED so must "win".
339 		 */
340 		val1 = (vap->iv_vht_cap.supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
341 		val2 = (ni->ni_vht_mcsinfo.rx_mcs_map >> (i*2)) & 0x3;
342 		val = MIN(val1, val2);
343 		if (val1 == IEEE80211_VHT_MCS_NOT_SUPPORTED ||
344 		    val2 == IEEE80211_VHT_MCS_NOT_SUPPORTED)
345 			val = IEEE80211_VHT_MCS_NOT_SUPPORTED;
346 		tx_mcs_map |= (val << (i*2));
347 	}
348 
349 	/* Store the TX MCS map somewhere in the node that can be used */
350 	ni->ni_vht_tx_map = tx_mcs_map;
351 }
352 
353 void
ieee80211_vht_timeout(struct ieee80211vap * vap)354 ieee80211_vht_timeout(struct ieee80211vap *vap)
355 {
356 }
357 
358 void
ieee80211_vht_node_join(struct ieee80211_node * ni)359 ieee80211_vht_node_join(struct ieee80211_node *ni)
360 {
361 
362 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
363 	    "%s: called", __func__);
364 }
365 
366 void
ieee80211_vht_node_leave(struct ieee80211_node * ni)367 ieee80211_vht_node_leave(struct ieee80211_node *ni)
368 {
369 
370 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
371 	    "%s: called", __func__);
372 }
373 
374 /*
375  * Calculate the VHTCAP IE for a given node.
376  *
377  * This includes calculating the capability intersection based on the
378  * current operating mode and intersection of the TX/RX MCS maps.
379  *
380  * The standard only makes it clear about MCS rate negotiation
381  * and MCS basic rates (which must be a subset of the general
382  * negotiated rates).  It doesn't make it clear that the AP should
383  * figure out the minimum functional overlap with the STA and
384  * support that.
385  *
386  * Note: this is in host order, not in 802.11 endian order.
387  *
388  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
389  *
390  * TODO: investigate what we should negotiate for MU-MIMO beamforming
391  *       options.
392  *
393  * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
394  */
395 void
ieee80211_vht_get_vhtcap_ie(struct ieee80211_node * ni,struct ieee80211_vht_cap * vhtcap,int opmode)396 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
397     struct ieee80211_vht_cap *vhtcap, int opmode)
398 {
399 	struct ieee80211vap *vap = ni->ni_vap;
400 //	struct ieee80211com *ic = vap->iv_ic;
401 	uint32_t val, val1, val2;
402 	uint32_t new_vhtcap;
403 	int i;
404 
405 	/*
406 	 * Capabilities - it depends on whether we are a station
407 	 * or not.
408 	 */
409 	new_vhtcap = 0;
410 
411 	/*
412 	 * Station - use our desired configuration based on
413 	 * local config, local device bits and the already-learnt
414 	 * vhtcap/vhtinfo IE in the node.
415 	 */
416 
417 	/* Limit MPDU size to the smaller of the two */
418 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
419 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
420 	if (opmode == 1) {
421 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
422 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
423 	}
424 	val = MIN(val1, val2);
425 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
426 
427 	/* Limit supp channel config */
428 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
429 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
430 	if (opmode == 1) {
431 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
432 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
433 	}
434 	if ((val2 == 2) &&
435 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) == 0))
436 		val2 = 1;
437 	if ((val2 == 1) &&
438 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) == 0))
439 		val2 = 0;
440 	val = MIN(val1, val2);
441 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
442 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
443 
444 	/* RX LDPC */
445 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
446 	    IEEE80211_VHTCAP_RXLDPC);
447 	if (opmode == 1) {
448 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
449 		    IEEE80211_VHTCAP_RXLDPC);
450 	}
451 	val = MIN(val1, val2);
452 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
453 
454 	/* Short-GI 80 */
455 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
456 	    IEEE80211_VHTCAP_SHORT_GI_80);
457 	if (opmode == 1) {
458 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
459 		    IEEE80211_VHTCAP_SHORT_GI_80);
460 	}
461 	val = MIN(val1, val2);
462 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
463 
464 	/* Short-GI 160 */
465 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
466 	    IEEE80211_VHTCAP_SHORT_GI_160);
467 	if (opmode == 1) {
468 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
469 		    IEEE80211_VHTCAP_SHORT_GI_160);
470 	}
471 	val = MIN(val1, val2);
472 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
473 
474 	/*
475 	 * STBC is slightly more complicated.
476 	 *
477 	 * In non-STA mode, we just announce our capabilities and that
478 	 * is that.
479 	 *
480 	 * In STA mode, we should calculate our capabilities based on
481 	 * local capabilities /and/ what the remote says. So:
482 	 *
483 	 * + Only TX STBC if we support it and the remote supports RX STBC;
484 	 * + Only announce RX STBC if we support it and the remote supports
485 	 *   TX STBC;
486 	 * + RX STBC should be the minimum of local and remote RX STBC;
487 	 */
488 
489 	/* TX STBC */
490 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
491 	    IEEE80211_VHTCAP_TXSTBC);
492 	if (opmode == 1) {
493 		/* STA mode - enable it only if node RXSTBC is non-zero */
494 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
495 		    IEEE80211_VHTCAP_RXSTBC_MASK);
496 	}
497 	val = MIN(val1, val2);
498 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_TX) == 0)
499 		val = 0;
500 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
501 
502 	/* RX STBC1..4 */
503 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
504 	    IEEE80211_VHTCAP_RXSTBC_MASK);
505 	if (opmode == 1) {
506 		/* STA mode - enable it only if node TXSTBC is non-zero */
507 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
508 		   IEEE80211_VHTCAP_TXSTBC);
509 	}
510 	val = MIN(val1, val2);
511 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_RX) == 0)
512 		val = 0;
513 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
514 
515 	/*
516 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
517 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
518 	 * it would be silly.
519 	 */
520 	if (val == 0)
521 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
522 
523 	/*
524 	 * Some of these fields require other fields to exist.
525 	 * So before using it, the parent field needs to be checked
526 	 * otherwise the overridden value may be wrong.
527 	 *
528 	 * For example, if SU beamformee is set to 0, then BF STS
529 	 * needs to be 0.
530 	 */
531 
532 	/* SU Beamformer capable */
533 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
534 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
535 	if (opmode == 1) {
536 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
537 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
538 	}
539 	val = MIN(val1, val2);
540 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
541 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
542 
543 	/* SU Beamformee capable */
544 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
545 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
546 	if (opmode == 1) {
547 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
548 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
549 	}
550 	val = MIN(val1, val2);
551 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
552 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
553 
554 	/* Beamformee STS capability - only if SU beamformee capable */
555 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
556 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
557 	if (opmode == 1) {
558 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
559 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
560 	}
561 	val = MIN(val1, val2);
562 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
563 		val = 0;
564 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
565 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
566 
567 	/* Sounding dimensions - only if SU beamformer capable */
568 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
569 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
570 	if (opmode == 1)
571 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
572 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
573 	val = MIN(val1, val2);
574 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
575 		val = 0;
576 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
577 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
578 
579 	/*
580 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
581 	 * and STA (not AP)
582 	 */
583 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
584 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
585 	if (opmode == 1)
586 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
587 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
588 	val = MIN(val1, val2);
589 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
590 		val = 0;
591 	if (opmode != 1)	/* Only enable for STA mode */
592 		val = 0;
593 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
594 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
595 
596 	/*
597 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
598 	 * and AP (not STA)
599 	 */
600 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
601 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
602 	if (opmode == 1)
603 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
604 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
605 	val = MIN(val1, val2);
606 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
607 		val = 0;
608 	if (opmode != 0)	/* Only enable for AP mode */
609 		val = 0;
610 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
611 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
612 
613 	/* VHT TXOP PS */
614 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
615 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
616 	if (opmode == 1)
617 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
618 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
619 	val = MIN(val1, val2);
620 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
621 
622 	/* HTC_VHT */
623 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
624 	    IEEE80211_VHTCAP_HTC_VHT);
625 	if (opmode == 1)
626 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
627 		    IEEE80211_VHTCAP_HTC_VHT);
628 	val = MIN(val1, val2);
629 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
630 
631 	/* A-MPDU length max */
632 	/* XXX TODO: we need a userland config knob for this */
633 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
634 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
635 	if (opmode == 1)
636 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
637 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
638 	val = MIN(val1, val2);
639 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
640 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
641 
642 	/*
643 	 * Link adaptation is only valid if HTC-VHT capable is 1.
644 	 * Otherwise, always set it to 0.
645 	 */
646 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
647 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
648 	if (opmode == 1)
649 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
650 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
651 	val = MIN(val1, val2);
652 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
653 		val = 0;
654 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
655 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
656 
657 	/*
658 	 * The following two options are 0 if the pattern may change, 1 if it
659 	 * does not change.  So, downgrade to the higher value.
660 	 */
661 
662 	/* RX antenna pattern */
663 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
664 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
665 	if (opmode == 1)
666 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
667 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
668 	val = MAX(val1, val2);
669 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
670 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
671 
672 	/* TX antenna pattern */
673 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
674 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
675 	if (opmode == 1)
676 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
677 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
678 	val = MAX(val1, val2);
679 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
680 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
681 
682 	/*
683 	 * MCS set - again, we announce what we want to use
684 	 * based on configuration, device capabilities and
685 	 * already-learnt vhtcap/vhtinfo IE information.
686 	 */
687 
688 	/* MCS set - start with whatever the device supports */
689 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_cap.supp_mcs.rx_mcs_map;
690 	vhtcap->supp_mcs.rx_highest = 0;
691 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_cap.supp_mcs.tx_mcs_map;
692 	vhtcap->supp_mcs.tx_highest = 0;
693 
694 	vhtcap->vht_cap_info = new_vhtcap;
695 
696 	/*
697 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
698 	 * Ie, we continue to state we can receive whatever we can do,
699 	 * but we only announce that we will transmit rates that meet
700 	 * the AP requirement.
701 	 *
702 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
703 	 * We can't just use MIN() because '3' means "no", so special case it.
704 	 */
705 	if (opmode) {
706 		for (i = 0; i < 8; i++) {
707 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
708 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
709 			val = MIN(val1, val2);
710 			if (val1 == 3 || val2 == 3)
711 				val = 3;
712 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
713 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
714 		}
715 	}
716 }
717 
718 /*
719  * Add a VHTCAP field.
720  *
721  * If in station mode, we announce what we would like our
722  * desired configuration to be.
723  *
724  * Else, we announce our capabilities based on our current
725  * configuration.
726  */
727 uint8_t *
ieee80211_add_vhtcap(uint8_t * frm,struct ieee80211_node * ni)728 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
729 {
730 	struct ieee80211_vht_cap vhtcap;
731 
732 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, 1);
733 
734 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
735 	frm[1] = sizeof(vhtcap);
736 	frm += 2;
737 
738 	/* 32-bit VHT capability */
739 	ADDWORD(frm, vhtcap.vht_cap_info);
740 
741 	/* suppmcs */
742 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
743 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
744 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
745 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
746 
747 	return (frm);
748 }
749 
750 /*
751  * Non-associated probe requests.  Add VHT capabilities based on
752  * the current channel configuration.  No BSS yet.
753  */
754 uint8_t *
ieee80211_add_vhtcap_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)755 ieee80211_add_vhtcap_ch(uint8_t *frm, struct ieee80211vap *vap,
756     struct ieee80211_channel *c)
757 {
758 	struct ieee80211_vht_cap *vhtcap;
759 
760 	memset(frm, 0, 2 + sizeof(*vhtcap));
761 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
762 	frm[1] = sizeof(*vhtcap);
763 	frm += 2;
764 
765 	/* 32-bit VHT capability */
766 	ADDWORD(frm, vap->iv_vht_cap.vht_cap_info);
767 
768 	/* supp_mcs */
769 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_mcs_map);
770 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_highest);
771 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_mcs_map);
772 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_highest);
773 
774 	return (frm);
775 }
776 
777 static uint8_t
ieee80211_vht_get_chwidth_ie(const struct ieee80211vap * vap,const struct ieee80211_channel * c)778 ieee80211_vht_get_chwidth_ie(const struct ieee80211vap *vap,
779     const struct ieee80211_channel *c)
780 {
781 
782 	/*
783 	 * XXX TODO: look at the node configuration as
784 	 * well?
785 	 */
786 
787 	if (IEEE80211_IS_CHAN_VHT80P80(c))
788 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
789 	if (IEEE80211_IS_CHAN_VHT160(c))
790 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
791 	if (IEEE80211_IS_CHAN_VHT80(c))
792 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
793 	if (IEEE80211_IS_CHAN_VHT40(c))
794 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
795 	if (IEEE80211_IS_CHAN_VHT20(c))
796 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
797 
798 	/* We shouldn't get here */
799 	net80211_vap_printf(vap,
800 	    "%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
801 	    __func__, (int) c->ic_freq, c->ic_flags);
802 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
803 }
804 
805 /*
806  * Note: this just uses the current channel information;
807  * it doesn't use the node info after parsing.
808  *
809  * XXX TODO: need to make the basic MCS set configurable.
810  * XXX TODO: read 802.11-2013 to determine what to set
811  *           chwidth to when scanning.  I have a feeling
812  *           it isn't involved in scanning and we shouldn't
813  *           be sending it; and I don't yet know what to set
814  *           it to for IBSS or hostap where the peer may be
815  *           a completely different channel width to us.
816  */
817 uint8_t *
ieee80211_add_vhtinfo(uint8_t * frm,struct ieee80211_node * ni)818 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
819 {
820 
821 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
822 	frm[1] = sizeof(struct ieee80211_vht_operation);
823 	frm += 2;
824 
825 	/* 8-bit chanwidth */
826 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_vap, ni->ni_chan);
827 
828 	/* 8-bit freq1 */
829 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
830 
831 	/* 8-bit freq2 */
832 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
833 
834 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
835 	ADDSHORT(frm, 0xfffc);
836 
837 	return (frm);
838 }
839 
840 void
ieee80211_vht_update_cap(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)841 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
842     const uint8_t *vhtop_ie)
843 {
844 
845 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
846 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
847 }
848 
849 static struct ieee80211_channel *
findvhtchan(struct ieee80211com * ic,struct ieee80211_channel * c,int vhtflags)850 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
851 {
852 
853 	return (ieee80211_find_channel(ic, c->ic_freq,
854 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
855 }
856 
857 /*
858  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
859  */
860 struct ieee80211_channel *
ieee80211_vht_adjust_channel(struct ieee80211com * ic,struct ieee80211_channel * chan,int flags)861 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
862     struct ieee80211_channel *chan, int flags)
863 {
864 	struct ieee80211_channel *c;
865 
866 	/* First case - handle channel demotion - if VHT isn't set */
867 	if ((flags & IEEE80211_FVHT_MASK) == 0) {
868 #if 0
869 		net80211_ic_printf(ic,
870 		    "%s: demoting channel %d/0x%08x\n", __func__,
871 		    chan->ic_ieee, chan->ic_flags);
872 #endif
873 		c = ieee80211_find_channel(ic, chan->ic_freq,
874 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
875 		if (c == NULL)
876 			c = chan;
877 #if 0
878 		net80211_ic_printf(ic, "%s: .. to %d/0x%08x\n", __func__,
879 		    c->ic_ieee, c->ic_flags);
880 #endif
881 		return (c);
882 	}
883 
884 	/*
885 	 * We can upgrade to VHT - attempt to do so
886 	 *
887 	 * Note: we don't clear the HT flags, these are the hints
888 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
889 	 */
890 	c = NULL;
891 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
892 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
893 
894 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
895 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
896 
897 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
898 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
899 
900 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
901 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
902 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
903 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
904 	/*
905 	 * If we get here, VHT20 is always possible because we checked
906 	 * for IEEE80211_FVHT_VHT above.
907 	 */
908 	if (c == NULL)
909 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
910 
911 	if (c != NULL)
912 		chan = c;
913 
914 #if 0
915 	net80211_ic_printf(ic, "%s: selected %d/0x%08x\n", __func__,
916 	    c->ic_ieee, c->ic_flags);
917 #endif
918 	return (chan);
919 }
920 
921 /*
922  * Calculate the VHT operation IE for a given node.
923  *
924  * This includes calculating the suitable channel width/parameters
925  * and basic MCS set.
926  *
927  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
928  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
929  */
930 void
ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node * ni,struct ieee80211_vht_operation * vhtop,int opmode)931 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
932     struct ieee80211_vht_operation *vhtop, int opmode)
933 {
934 	net80211_vap_printf(ni->ni_vap, "%s: called; TODO!\n", __func__);
935 }
936 
937 /*
938  * Return true if VHT rates can be used for the given node.
939  */
940 bool
ieee80211_vht_check_tx_vht(const struct ieee80211_node * ni)941 ieee80211_vht_check_tx_vht(const struct ieee80211_node *ni)
942 {
943 	const struct ieee80211vap *vap;
944 	const struct ieee80211_channel *bss_chan;
945 
946 	if (ni == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC ||
947 	    ni->ni_vap == NULL || ni->ni_vap->iv_bss == NULL)
948 		return (false);
949 
950 	vap = ni->ni_vap;
951 	bss_chan = vap->iv_bss->ni_chan;
952 
953 	if (bss_chan == IEEE80211_CHAN_ANYC)
954 		return (false);
955 
956 	return (IEEE80211_IS_CHAN_VHT(ni->ni_chan));
957 }
958 
959 /*
960  * Return true if VHT40 rates can be transmitted to the given node.
961  *
962  * This verifies that the BSS is VHT40 capable and the current
963  * node channel width is 40MHz.
964  */
965 static bool
ieee80211_vht_check_tx_vht40(const struct ieee80211_node * ni)966 ieee80211_vht_check_tx_vht40(const struct ieee80211_node *ni)
967 {
968 	struct ieee80211vap *vap;
969 	struct ieee80211_channel *bss_chan;
970 
971 	if (!ieee80211_vht_check_tx_vht(ni))
972 		return (false);
973 
974 	vap = ni->ni_vap;
975 	bss_chan = vap->iv_bss->ni_chan;
976 
977 	return (IEEE80211_IS_CHAN_VHT40(bss_chan) &&
978 	    IEEE80211_IS_CHAN_VHT40(ni->ni_chan) &&
979 	    (ni->ni_chw == IEEE80211_STA_RX_BW_40));
980 }
981 
982 /*
983  * Return true if VHT80 rates can be transmitted to the given node.
984  *
985  * This verifies that the BSS is VHT80 capable and the current
986  * node channel width is 80MHz.
987  */
988 static bool
ieee80211_vht_check_tx_vht80(const struct ieee80211_node * ni)989 ieee80211_vht_check_tx_vht80(const struct ieee80211_node *ni)
990 {
991 	struct ieee80211vap *vap;
992 	struct ieee80211_channel *bss_chan;
993 
994 	if (!ieee80211_vht_check_tx_vht(ni))
995 		return (false);
996 
997 	vap = ni->ni_vap;
998 	bss_chan = vap->iv_bss->ni_chan;
999 
1000 	/*
1001 	 * ni_chw represents 20MHz or 40MHz from the HT
1002 	 * TX width action frame / HT channel negotiation.
1003 	 * If a HT TX width action frame sets it to 20MHz
1004 	 * then reject doing 80MHz.
1005 	 */
1006 	return (IEEE80211_IS_CHAN_VHT80(bss_chan) &&
1007 	    IEEE80211_IS_CHAN_VHT80(ni->ni_chan) &&
1008 	    (ni->ni_chw != IEEE80211_STA_RX_BW_20));
1009 }
1010 
1011 /*
1012  * Return true if VHT 160 rates can be transmitted to the given node.
1013  *
1014  * This verifies that the BSS is VHT80+80 or VHT160 capable and the current
1015  * node channel width is 80+80MHz or 160MHz.
1016  */
1017 static bool
ieee80211_vht_check_tx_vht160(const struct ieee80211_node * ni)1018 ieee80211_vht_check_tx_vht160(const struct ieee80211_node *ni)
1019 {
1020 	struct ieee80211vap *vap;
1021 	struct ieee80211_channel *bss_chan;
1022 
1023 	if (!ieee80211_vht_check_tx_vht(ni))
1024 		return (false);
1025 
1026 	vap = ni->ni_vap;
1027 	bss_chan = vap->iv_bss->ni_chan;
1028 
1029 	/*
1030 	 * ni_chw represents 20MHz or 40MHz from the HT
1031 	 * TX width action frame / HT channel negotiation.
1032 	 * If a HT TX width action frame sets it to 20MHz
1033 	 * then reject doing 160MHz.
1034 	 */
1035 	if (ni->ni_chw == IEEE80211_STA_RX_BW_20)
1036 		return (false);
1037 
1038 	if (IEEE80211_IS_CHAN_VHT160(bss_chan) &&
1039 	    IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1040 		return (true);
1041 
1042 	if (IEEE80211_IS_CHAN_VHT80P80(bss_chan) &&
1043 	    IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
1044 		return (true);
1045 
1046 	return (false);
1047 }
1048 
1049 /**
1050  * @brief Check if the given transmit bandwidth is available to the given node
1051  *
1052  * This checks that the node and BSS both allow the given bandwidth,
1053  * and that the current node bandwidth (which can dynamically change)
1054  * also allows said bandwidth.
1055  *
1056  * This relies on the channels having the flags for the narrower
1057  * channels as well - eg a VHT160 channel will have the CHAN_VHT80,
1058  * CHAN_VHT40, CHAN_VHT flags also set.
1059  *
1060  * @param ni		the ieee80211_node to check
1061  * @param bw		the required bandwidth to check
1062  *
1063  * @returns true if it is allowed, false otherwise
1064  */
1065 bool
ieee80211_vht_check_tx_bw(const struct ieee80211_node * ni,enum ieee80211_sta_rx_bw bw)1066 ieee80211_vht_check_tx_bw(const struct ieee80211_node *ni,
1067     enum ieee80211_sta_rx_bw bw)
1068 {
1069 
1070 	switch (bw) {
1071 	case IEEE80211_STA_RX_BW_20:
1072 		return (ieee80211_vht_check_tx_vht(ni));
1073 	case IEEE80211_STA_RX_BW_40:
1074 		return (ieee80211_vht_check_tx_vht40(ni));
1075 	case IEEE80211_STA_RX_BW_80:
1076 		return (ieee80211_vht_check_tx_vht80(ni));
1077 	case IEEE80211_STA_RX_BW_160:
1078 		return (ieee80211_vht_check_tx_vht160(ni));
1079 	case IEEE80211_STA_RX_BW_320:
1080 		return (false);
1081 	default:
1082 		return (false);
1083 	}
1084 }
1085 
1086 /**
1087  * @brief Check if the given VHT bw/nss/mcs combination is valid
1088  *        for the give node.
1089  *
1090  * This checks whether the given VHT bw/nss/mcs is valid based on
1091  * the negotiated rate mask in the node.
1092  *
1093  * @param ni	struct ieee80211_node node to check
1094  * @param bw	channel bandwidth to check
1095  * @param nss	NSS
1096  * @param mcs	MCS
1097  * @returns True if this combination is available, false otherwise.
1098  */
1099 bool
ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node * ni,enum ieee80211_sta_rx_bw bw,uint8_t nss,uint8_t mcs)1100 ieee80211_vht_node_check_tx_valid_mcs(const struct ieee80211_node *ni,
1101     enum ieee80211_sta_rx_bw bw, uint8_t nss, uint8_t mcs)
1102 {
1103 	uint8_t mc;
1104 
1105 	/* Validate arguments */
1106 	if (nss < 1 || nss > 8)
1107 		return (false);
1108 	if (mcs > 9)
1109 		return (false);
1110 
1111 	/* Check our choice of rate is actually valid */
1112 	if (!ieee80211_phy_vht_validate_mcs(bw, nss, mcs))
1113 		return (false);
1114 
1115 	/*
1116 	 * Next, check if the MCS rate is available for the
1117 	 * given NSS.
1118 	 */
1119 	mc = ni->ni_vht_tx_map >> (2*(nss-1)) & 0x3;
1120 	switch (mc) {
1121 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
1122 		/* Not supported at this NSS */
1123 		return (false);
1124 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
1125 		return (mcs <= 9);
1126 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
1127 		return (mcs <= 8);
1128 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
1129 		return (mcs <= 7);
1130 	default:
1131 		return (false);
1132 	}
1133 }
1134