xref: /freebsd-14-stable/sys/net80211/ieee80211_vht.c (revision 36ace2a923e1e1d51dc775d85c95001cb7e9430e)
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 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 #endif
29 
30 /*
31  * IEEE 802.11ac-2013 protocol support.
32  */
33 
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/endian.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
49 
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_action.h>
52 #include <net80211/ieee80211_input.h>
53 #include <net80211/ieee80211_vht.h>
54 
55 #define	ADDSHORT(frm, v) do {			\
56 	frm[0] = (v) & 0xff;			\
57 	frm[1] = (v) >> 8;			\
58 	frm += 2;				\
59 } while (0)
60 #define	ADDWORD(frm, v) do {			\
61 	frm[0] = (v) & 0xff;			\
62 	frm[1] = ((v) >> 8) & 0xff;		\
63 	frm[2] = ((v) >> 16) & 0xff;		\
64 	frm[3] = ((v) >> 24) & 0xff;		\
65 	frm += 4;				\
66 } while (0)
67 
68 /*
69  * Immediate TODO:
70  *
71  * + handle WLAN_ACTION_VHT_OPMODE_NOTIF and other VHT action frames
72  * + ensure vhtinfo/vhtcap parameters correctly use the negotiated
73  *   capabilities and ratesets
74  * + group ID management operation
75  */
76 
77 /*
78  * XXX TODO: handle WLAN_ACTION_VHT_OPMODE_NOTIF
79  *
80  * Look at mac80211/vht.c:ieee80211_vht_handle_opmode() for further details.
81  */
82 
83 static int
vht_recv_action_placeholder(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)84 vht_recv_action_placeholder(struct ieee80211_node *ni,
85     const struct ieee80211_frame *wh,
86     const uint8_t *frm, const uint8_t *efrm)
87 {
88 
89 #ifdef IEEE80211_DEBUG
90 	ieee80211_note(ni->ni_vap, "%s: called; fc=0x%.2x/0x%.2x",
91 	    __func__, wh->i_fc[0], wh->i_fc[1]);
92 #endif
93 	return (0);
94 }
95 
96 static int
vht_send_action_placeholder(struct ieee80211_node * ni,int category,int action,void * arg0)97 vht_send_action_placeholder(struct ieee80211_node *ni,
98     int category, int action, void *arg0)
99 {
100 
101 #ifdef IEEE80211_DEBUG
102 	ieee80211_note(ni->ni_vap, "%s: called; category=%d, action=%d",
103 	    __func__, category, action);
104 #endif
105 	return (EINVAL);
106 }
107 
108 static void
ieee80211_vht_init(void)109 ieee80211_vht_init(void)
110 {
111 
112 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
113 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_recv_action_placeholder);
114 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
115 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_recv_action_placeholder);
116 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT,
117 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_recv_action_placeholder);
118 
119 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
120 	    WLAN_ACTION_VHT_COMPRESSED_BF, vht_send_action_placeholder);
121 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
122 	    WLAN_ACTION_VHT_GROUPID_MGMT, vht_send_action_placeholder);
123 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT,
124 	    WLAN_ACTION_VHT_OPMODE_NOTIF, vht_send_action_placeholder);
125 }
126 
127 SYSINIT(wlan_vht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_vht_init, NULL);
128 
129 void
ieee80211_vht_attach(struct ieee80211com * ic)130 ieee80211_vht_attach(struct ieee80211com *ic)
131 {
132 }
133 
134 void
ieee80211_vht_detach(struct ieee80211com * ic)135 ieee80211_vht_detach(struct ieee80211com *ic)
136 {
137 }
138 
139 void
ieee80211_vht_vattach(struct ieee80211vap * vap)140 ieee80211_vht_vattach(struct ieee80211vap *vap)
141 {
142 	struct ieee80211com *ic = vap->iv_ic;
143 
144 	if (! IEEE80211_CONF_VHT(ic))
145 		return;
146 
147 	vap->iv_vht_cap.vht_cap_info = ic->ic_vht_cap.vht_cap_info;
148 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
149 
150 	/* XXX assume VHT80 support; should really check vhtcaps */
151 	vap->iv_vht_flags =
152 	    IEEE80211_FVHT_VHT
153 	    | IEEE80211_FVHT_USEVHT40
154 	    | IEEE80211_FVHT_USEVHT80;
155 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vht_cap.vht_cap_info))
156 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT160;
157 	if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vht_cap.vht_cap_info))
158 		vap->iv_vht_flags |= IEEE80211_FVHT_USEVHT80P80;
159 
160 	memcpy(&vap->iv_vht_cap.supp_mcs, &ic->ic_vht_cap.supp_mcs,
161 	    sizeof(struct ieee80211_vht_mcs_info));
162 }
163 
164 void
ieee80211_vht_vdetach(struct ieee80211vap * vap)165 ieee80211_vht_vdetach(struct ieee80211vap *vap)
166 {
167 }
168 
169 #if 0
170 static void
171 vht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
172 {
173 }
174 #endif
175 
176 static int
vht_mcs_to_num(int m)177 vht_mcs_to_num(int m)
178 {
179 
180 	switch (m) {
181 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
182 		return (7);
183 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
184 		return (8);
185 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
186 		return (9);
187 	default:
188 		return (0);
189 	}
190 }
191 
192 void
ieee80211_vht_announce(struct ieee80211com * ic)193 ieee80211_vht_announce(struct ieee80211com *ic)
194 {
195 	int i, tx, rx;
196 
197 	if (! IEEE80211_CONF_VHT(ic))
198 		return;
199 
200 	/* Channel width */
201 	ic_printf(ic, "[VHT] Channel Widths: 20MHz, 40MHz, 80MHz%s%s\n",
202 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(ic->ic_vht_cap.vht_cap_info)) ?
203 		", 160MHz" : "",
204 	    (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(ic->ic_vht_cap.vht_cap_info)) ?
205 		 ", 80+80MHz" : "");
206 	/* Features */
207 	ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vht_cap.vht_cap_info,
208 	    IEEE80211_VHTCAP_BITS);
209 
210 	/* For now, just 5GHz VHT.  Worry about 2GHz VHT later */
211 	for (i = 0; i < 8; i++) {
212 		/* Each stream is 2 bits */
213 		tx = (ic->ic_vht_cap.supp_mcs.tx_mcs_map >> (2*i)) & 0x3;
214 		rx = (ic->ic_vht_cap.supp_mcs.rx_mcs_map >> (2*i)) & 0x3;
215 		if (tx == 3 && rx == 3)
216 			continue;
217 		ic_printf(ic, "[VHT] NSS %d: TX MCS 0..%d, RX MCS 0..%d\n",
218 		    i + 1, vht_mcs_to_num(tx), vht_mcs_to_num(rx));
219 	}
220 }
221 
222 void
ieee80211_vht_node_init(struct ieee80211_node * ni)223 ieee80211_vht_node_init(struct ieee80211_node *ni)
224 {
225 
226 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
227 	    "%s: called", __func__);
228 	ni->ni_flags |= IEEE80211_NODE_VHT;
229 }
230 
231 void
ieee80211_vht_node_cleanup(struct ieee80211_node * ni)232 ieee80211_vht_node_cleanup(struct ieee80211_node *ni)
233 {
234 
235 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
236 	    "%s: called", __func__);
237 	ni->ni_flags &= ~IEEE80211_NODE_VHT;
238 	ni->ni_vhtcap = 0;
239 	bzero(&ni->ni_vht_mcsinfo, sizeof(struct ieee80211_vht_mcs_info));
240 }
241 
242 /*
243  * Parse an 802.11ac VHT operation IE.
244  */
245 void
ieee80211_parse_vhtopmode(struct ieee80211_node * ni,const uint8_t * ie)246 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie)
247 {
248 	/* vht operation */
249 	ni->ni_vht_chanwidth = ie[2];
250 	ni->ni_vht_chan1 = ie[3];
251 	ni->ni_vht_chan2 = ie[4];
252 	ni->ni_vht_basicmcs = le16dec(ie + 5);
253 
254 #if 0
255 	printf("%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n",
256 	    __func__, ni->ni_vht_chan1, ni->ni_vht_chan2, ni->ni_vht_chanwidth,
257 	    ni->ni_vht_basicmcs);
258 #endif
259 }
260 
261 /*
262  * Parse an 802.11ac VHT capability IE.
263  */
264 void
ieee80211_parse_vhtcap(struct ieee80211_node * ni,const uint8_t * ie)265 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie)
266 {
267 
268 	/* vht capability */
269 	ni->ni_vhtcap = le32dec(ie + 2);
270 
271 	/* suppmcs */
272 	ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6);
273 	ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8);
274 	ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10);
275 	ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12);
276 }
277 
278 int
ieee80211_vht_updateparams(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)279 ieee80211_vht_updateparams(struct ieee80211_node *ni,
280     const uint8_t *vhtcap_ie,
281     const uint8_t *vhtop_ie)
282 {
283 
284 	//printf("%s: called\n", __func__);
285 
286 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
287 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
288 	return (0);
289 }
290 
291 void
ieee80211_setup_vht_rates(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)292 ieee80211_setup_vht_rates(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 	/* XXX TODO */
299 }
300 
301 void
ieee80211_vht_timeout(struct ieee80211vap * vap)302 ieee80211_vht_timeout(struct ieee80211vap *vap)
303 {
304 }
305 
306 void
ieee80211_vht_node_join(struct ieee80211_node * ni)307 ieee80211_vht_node_join(struct ieee80211_node *ni)
308 {
309 
310 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
311 	    "%s: called", __func__);
312 }
313 
314 void
ieee80211_vht_node_leave(struct ieee80211_node * ni)315 ieee80211_vht_node_leave(struct ieee80211_node *ni)
316 {
317 
318 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
319 	    "%s: called", __func__);
320 }
321 
322 /*
323  * Calculate the VHTCAP IE for a given node.
324  *
325  * This includes calculating the capability intersection based on the
326  * current operating mode and intersection of the TX/RX MCS maps.
327  *
328  * The standard only makes it clear about MCS rate negotiation
329  * and MCS basic rates (which must be a subset of the general
330  * negotiated rates).  It doesn't make it clear that the AP should
331  * figure out the minimum functional overlap with the STA and
332  * support that.
333  *
334  * Note: this is in host order, not in 802.11 endian order.
335  *
336  * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs.
337  *
338  * TODO: investigate what we should negotiate for MU-MIMO beamforming
339  *       options.
340  *
341  * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise.
342  */
343 void
ieee80211_vht_get_vhtcap_ie(struct ieee80211_node * ni,struct ieee80211_vht_cap * vhtcap,int opmode)344 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni,
345     struct ieee80211_vht_cap *vhtcap, int opmode)
346 {
347 	struct ieee80211vap *vap = ni->ni_vap;
348 //	struct ieee80211com *ic = vap->iv_ic;
349 	uint32_t val, val1, val2;
350 	uint32_t new_vhtcap;
351 	int i;
352 
353 	/*
354 	 * Capabilities - it depends on whether we are a station
355 	 * or not.
356 	 */
357 	new_vhtcap = 0;
358 
359 	/*
360 	 * Station - use our desired configuration based on
361 	 * local config, local device bits and the already-learnt
362 	 * vhtcap/vhtinfo IE in the node.
363 	 */
364 
365 	/* Limit MPDU size to the smaller of the two */
366 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
367 	    IEEE80211_VHTCAP_MAX_MPDU_MASK);
368 	if (opmode == 1) {
369 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
370 		    IEEE80211_VHTCAP_MAX_MPDU_MASK);
371 	}
372 	val = MIN(val1, val2);
373 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_MAX_MPDU_MASK);
374 
375 	/* Limit supp channel config */
376 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
377 	    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
378 	if (opmode == 1) {
379 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
380 		    IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
381 	}
382 	if ((val2 == 2) &&
383 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) == 0))
384 		val2 = 1;
385 	if ((val2 == 1) &&
386 	    ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) == 0))
387 		val2 = 0;
388 	val = MIN(val1, val2);
389 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
390 	     IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK);
391 
392 	/* RX LDPC */
393 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
394 	    IEEE80211_VHTCAP_RXLDPC);
395 	if (opmode == 1) {
396 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
397 		    IEEE80211_VHTCAP_RXLDPC);
398 	}
399 	val = MIN(val1, val2);
400 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXLDPC);
401 
402 	/* Short-GI 80 */
403 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
404 	    IEEE80211_VHTCAP_SHORT_GI_80);
405 	if (opmode == 1) {
406 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
407 		    IEEE80211_VHTCAP_SHORT_GI_80);
408 	}
409 	val = MIN(val1, val2);
410 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_80);
411 
412 	/* Short-GI 160 */
413 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
414 	    IEEE80211_VHTCAP_SHORT_GI_160);
415 	if (opmode == 1) {
416 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
417 		    IEEE80211_VHTCAP_SHORT_GI_160);
418 	}
419 	val = MIN(val1, val2);
420 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_SHORT_GI_160);
421 
422 	/*
423 	 * STBC is slightly more complicated.
424 	 *
425 	 * In non-STA mode, we just announce our capabilities and that
426 	 * is that.
427 	 *
428 	 * In STA mode, we should calculate our capabilities based on
429 	 * local capabilities /and/ what the remote says. So:
430 	 *
431 	 * + Only TX STBC if we support it and the remote supports RX STBC;
432 	 * + Only announce RX STBC if we support it and the remote supports
433 	 *   TX STBC;
434 	 * + RX STBC should be the minimum of local and remote RX STBC;
435 	 */
436 
437 	/* TX STBC */
438 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
439 	    IEEE80211_VHTCAP_TXSTBC);
440 	if (opmode == 1) {
441 		/* STA mode - enable it only if node RXSTBC is non-zero */
442 		val2 = !! _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
443 		    IEEE80211_VHTCAP_RXSTBC_MASK);
444 	}
445 	val = MIN(val1, val2);
446 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_TX) == 0)
447 		val = 0;
448 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_TXSTBC);
449 
450 	/* RX STBC1..4 */
451 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
452 	    IEEE80211_VHTCAP_RXSTBC_MASK);
453 	if (opmode == 1) {
454 		/* STA mode - enable it only if node TXSTBC is non-zero */
455 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
456 		   IEEE80211_VHTCAP_TXSTBC);
457 	}
458 	val = MIN(val1, val2);
459 	if ((vap->iv_vht_flags & IEEE80211_FVHT_STBC_RX) == 0)
460 		val = 0;
461 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_RXSTBC_MASK);
462 
463 	/*
464 	 * Finally - if RXSTBC is 0, then don't enable TXSTBC.
465 	 * Strictly speaking a device can TXSTBC and not RXSTBC, but
466 	 * it would be silly.
467 	 */
468 	if (val == 0)
469 		new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC;
470 
471 	/*
472 	 * Some of these fields require other fields to exist.
473 	 * So before using it, the parent field needs to be checked
474 	 * otherwise the overridden value may be wrong.
475 	 *
476 	 * For example, if SU beamformee is set to 0, then BF STS
477 	 * needs to be 0.
478 	 */
479 
480 	/* SU Beamformer capable */
481 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
482 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
483 	if (opmode == 1) {
484 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
485 		    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
486 	}
487 	val = MIN(val1, val2);
488 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
489 	    IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
490 
491 	/* SU Beamformee capable */
492 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
493 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
494 	if (opmode == 1) {
495 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
496 		    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
497 	}
498 	val = MIN(val1, val2);
499 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
500 	    IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
501 
502 	/* Beamformee STS capability - only if SU beamformee capable */
503 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
504 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
505 	if (opmode == 1) {
506 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
507 		    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
508 	}
509 	val = MIN(val1, val2);
510 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
511 		val = 0;
512 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
513 	    IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK);
514 
515 	/* Sounding dimensions - only if SU beamformer capable */
516 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
517 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
518 	if (opmode == 1)
519 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
520 		    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
521 	val = MIN(val1, val2);
522 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
523 		val = 0;
524 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
525 	    IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK);
526 
527 	/*
528 	 * MU Beamformer capable - only if SU BFF capable, MU BFF capable
529 	 * and STA (not AP)
530 	 */
531 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
532 	    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
533 	if (opmode == 1)
534 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
535 		    IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE);
536 	val = MIN(val1, val2);
537 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0)
538 		val = 0;
539 	if (opmode != 1)	/* Only enable for STA mode */
540 		val = 0;
541 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
542 	   IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE);
543 
544 	/*
545 	 * MU Beamformee capable - only if SU BFE capable, MU BFE capable
546 	 * and AP (not STA)
547 	 */
548 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
549 	    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
550 	if (opmode == 1)
551 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
552 		    IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE);
553 	val = MIN(val1, val2);
554 	if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0)
555 		val = 0;
556 	if (opmode != 0)	/* Only enable for AP mode */
557 		val = 0;
558 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
559 	   IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE);
560 
561 	/* VHT TXOP PS */
562 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
563 	    IEEE80211_VHTCAP_VHT_TXOP_PS);
564 	if (opmode == 1)
565 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
566 		    IEEE80211_VHTCAP_VHT_TXOP_PS);
567 	val = MIN(val1, val2);
568 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_VHT_TXOP_PS);
569 
570 	/* HTC_VHT */
571 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
572 	    IEEE80211_VHTCAP_HTC_VHT);
573 	if (opmode == 1)
574 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
575 		    IEEE80211_VHTCAP_HTC_VHT);
576 	val = MIN(val1, val2);
577 	new_vhtcap |= _IEEE80211_SHIFTMASK(val, IEEE80211_VHTCAP_HTC_VHT);
578 
579 	/* A-MPDU length max */
580 	/* XXX TODO: we need a userland config knob for this */
581 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
582 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
583 	if (opmode == 1)
584 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
585 		    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
586 	val = MIN(val1, val2);
587 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
588 	    IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
589 
590 	/*
591 	 * Link adaptation is only valid if HTC-VHT capable is 1.
592 	 * Otherwise, always set it to 0.
593 	 */
594 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
595 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
596 	if (opmode == 1)
597 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
598 		    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
599 	val = MIN(val1, val2);
600 	if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0)
601 		val = 0;
602 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
603 	    IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK);
604 
605 	/*
606 	 * The following two options are 0 if the pattern may change, 1 if it
607 	 * does not change.  So, downgrade to the higher value.
608 	 */
609 
610 	/* RX antenna pattern */
611 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
612 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
613 	if (opmode == 1)
614 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
615 		    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
616 	val = MAX(val1, val2);
617 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
618 	    IEEE80211_VHTCAP_RX_ANTENNA_PATTERN);
619 
620 	/* TX antenna pattern */
621 	val2 = val1 = _IEEE80211_MASKSHIFT(vap->iv_vht_cap.vht_cap_info,
622 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
623 	if (opmode == 1)
624 		val2 = _IEEE80211_MASKSHIFT(ni->ni_vhtcap,
625 		    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
626 	val = MAX(val1, val2);
627 	new_vhtcap |= _IEEE80211_SHIFTMASK(val,
628 	    IEEE80211_VHTCAP_TX_ANTENNA_PATTERN);
629 
630 	/*
631 	 * MCS set - again, we announce what we want to use
632 	 * based on configuration, device capabilities and
633 	 * already-learnt vhtcap/vhtinfo IE information.
634 	 */
635 
636 	/* MCS set - start with whatever the device supports */
637 	vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_cap.supp_mcs.rx_mcs_map;
638 	vhtcap->supp_mcs.rx_highest = 0;
639 	vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_cap.supp_mcs.tx_mcs_map;
640 	vhtcap->supp_mcs.tx_highest = 0;
641 
642 	vhtcap->vht_cap_info = new_vhtcap;
643 
644 	/*
645 	 * Now, if we're a STA, mask off whatever the AP doesn't support.
646 	 * Ie, we continue to state we can receive whatever we can do,
647 	 * but we only announce that we will transmit rates that meet
648 	 * the AP requirement.
649 	 *
650 	 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported.
651 	 * We can't just use MIN() because '3' means "no", so special case it.
652 	 */
653 	if (opmode) {
654 		for (i = 0; i < 8; i++) {
655 			val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3;
656 			val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3;
657 			val = MIN(val1, val2);
658 			if (val1 == 3 || val2 == 3)
659 				val = 3;
660 			vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2));
661 			vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2));
662 		}
663 	}
664 }
665 
666 /*
667  * Add a VHTCAP field.
668  *
669  * If in station mode, we announce what we would like our
670  * desired configuration to be.
671  *
672  * Else, we announce our capabilities based on our current
673  * configuration.
674  */
675 uint8_t *
ieee80211_add_vhtcap(uint8_t * frm,struct ieee80211_node * ni)676 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni)
677 {
678 	struct ieee80211_vht_cap vhtcap;
679 
680 	ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, 1);
681 
682 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
683 	frm[1] = sizeof(vhtcap);
684 	frm += 2;
685 
686 	/* 32-bit VHT capability */
687 	ADDWORD(frm, vhtcap.vht_cap_info);
688 
689 	/* suppmcs */
690 	ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map);
691 	ADDSHORT(frm, vhtcap.supp_mcs.rx_highest);
692 	ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map);
693 	ADDSHORT(frm, vhtcap.supp_mcs.tx_highest);
694 
695 	return (frm);
696 }
697 
698 /*
699  * Non-associated probe requests.  Add VHT capabilities based on
700  * the current channel configuration.  No BSS yet.
701  */
702 uint8_t *
ieee80211_add_vhtcap_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)703 ieee80211_add_vhtcap_ch(uint8_t *frm, struct ieee80211vap *vap,
704     struct ieee80211_channel *c)
705 {
706 	struct ieee80211_vht_cap *vhtcap;
707 
708 	memset(frm, 0, 2 + sizeof(*vhtcap));
709 	frm[0] = IEEE80211_ELEMID_VHT_CAP;
710 	frm[1] = sizeof(*vhtcap);
711 	frm += 2;
712 
713 	/* 32-bit VHT capability */
714 	ADDWORD(frm, vap->iv_vht_cap.vht_cap_info);
715 
716 	/* supp_mcs */
717 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_mcs_map);
718 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.rx_highest);
719 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_mcs_map);
720 	ADDSHORT(frm, vap->iv_vht_cap.supp_mcs.tx_highest);
721 
722 	return (frm);
723 }
724 
725 static uint8_t
ieee80211_vht_get_chwidth_ie(struct ieee80211_channel * c)726 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c)
727 {
728 
729 	/*
730 	 * XXX TODO: look at the node configuration as
731 	 * well?
732 	 */
733 
734 	if (IEEE80211_IS_CHAN_VHT80P80(c))
735 		return IEEE80211_VHT_CHANWIDTH_80P80MHZ;
736 	if (IEEE80211_IS_CHAN_VHT160(c))
737 		return IEEE80211_VHT_CHANWIDTH_160MHZ;
738 	if (IEEE80211_IS_CHAN_VHT80(c))
739 		return IEEE80211_VHT_CHANWIDTH_80MHZ;
740 	if (IEEE80211_IS_CHAN_VHT40(c))
741 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
742 	if (IEEE80211_IS_CHAN_VHT20(c))
743 		return IEEE80211_VHT_CHANWIDTH_USE_HT;
744 
745 	/* We shouldn't get here */
746 	printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n",
747 	    __func__, (int) c->ic_freq, c->ic_flags);
748 	return IEEE80211_VHT_CHANWIDTH_USE_HT;
749 }
750 
751 /*
752  * Note: this just uses the current channel information;
753  * it doesn't use the node info after parsing.
754  *
755  * XXX TODO: need to make the basic MCS set configurable.
756  * XXX TODO: read 802.11-2013 to determine what to set
757  *           chwidth to when scanning.  I have a feeling
758  *           it isn't involved in scanning and we shouldn't
759  *           be sending it; and I don't yet know what to set
760  *           it to for IBSS or hostap where the peer may be
761  *           a completely different channel width to us.
762  */
763 uint8_t *
ieee80211_add_vhtinfo(uint8_t * frm,struct ieee80211_node * ni)764 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni)
765 {
766 
767 	frm[0] = IEEE80211_ELEMID_VHT_OPMODE;
768 	frm[1] = sizeof(struct ieee80211_vht_operation);
769 	frm += 2;
770 
771 	/* 8-bit chanwidth */
772 	*frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan);
773 
774 	/* 8-bit freq1 */
775 	*frm++ = ni->ni_chan->ic_vht_ch_freq1;
776 
777 	/* 8-bit freq2 */
778 	*frm++ = ni->ni_chan->ic_vht_ch_freq2;
779 
780 	/* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */
781 	ADDSHORT(frm, 0xfffc);
782 
783 	return (frm);
784 }
785 
786 void
ieee80211_vht_update_cap(struct ieee80211_node * ni,const uint8_t * vhtcap_ie,const uint8_t * vhtop_ie)787 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie,
788     const uint8_t *vhtop_ie)
789 {
790 
791 	ieee80211_parse_vhtcap(ni, vhtcap_ie);
792 	ieee80211_parse_vhtopmode(ni, vhtop_ie);
793 }
794 
795 static struct ieee80211_channel *
findvhtchan(struct ieee80211com * ic,struct ieee80211_channel * c,int vhtflags)796 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags)
797 {
798 
799 	return (ieee80211_find_channel(ic, c->ic_freq,
800 	    (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags));
801 }
802 
803 /*
804  * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel().
805  */
806 struct ieee80211_channel *
ieee80211_vht_adjust_channel(struct ieee80211com * ic,struct ieee80211_channel * chan,int flags)807 ieee80211_vht_adjust_channel(struct ieee80211com *ic,
808     struct ieee80211_channel *chan, int flags)
809 {
810 	struct ieee80211_channel *c;
811 
812 	/* First case - handle channel demotion - if VHT isn't set */
813 	if ((flags & IEEE80211_FVHT_MASK) == 0) {
814 #if 0
815 		printf("%s: demoting channel %d/0x%08x\n", __func__,
816 		    chan->ic_ieee, chan->ic_flags);
817 #endif
818 		c = ieee80211_find_channel(ic, chan->ic_freq,
819 		    chan->ic_flags & ~IEEE80211_CHAN_VHT);
820 		if (c == NULL)
821 			c = chan;
822 #if 0
823 		printf("%s: .. to %d/0x%08x\n", __func__,
824 		    c->ic_ieee, c->ic_flags);
825 #endif
826 		return (c);
827 	}
828 
829 	/*
830 	 * We can upgrade to VHT - attempt to do so
831 	 *
832 	 * Note: we don't clear the HT flags, these are the hints
833 	 * for HT40U/HT40D when selecting VHT40 or larger channels.
834 	 */
835 	c = NULL;
836 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160))
837 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT160);
838 
839 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80))
840 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80P80);
841 
842 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80))
843 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80);
844 
845 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
846 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U);
847 	if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40))
848 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D);
849 	/*
850 	 * If we get here, VHT20 is always possible because we checked
851 	 * for IEEE80211_FVHT_VHT above.
852 	 */
853 	if (c == NULL)
854 		c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20);
855 
856 	if (c != NULL)
857 		chan = c;
858 
859 #if 0
860 	printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags);
861 #endif
862 	return (chan);
863 }
864 
865 /*
866  * Calculate the VHT operation IE for a given node.
867  *
868  * This includes calculating the suitable channel width/parameters
869  * and basic MCS set.
870  *
871  * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs.
872  * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation.
873  */
874 void
ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node * ni,struct ieee80211_vht_operation * vhtop,int opmode)875 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni,
876     struct ieee80211_vht_operation *vhtop, int opmode)
877 {
878 	printf("%s: called; TODO!\n", __func__);
879 }
880