1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * IEEE 802.11n protocol support.
30 */
31
32 #include "opt_inet.h"
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/endian.h>
40
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/ethernet.h>
47
48 #include <net80211/ieee80211_var.h>
49 #include <net80211/ieee80211_action.h>
50 #include <net80211/ieee80211_input.h>
51
52 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = {
53 { 13, 14, 27, 30 }, /* MCS 0 */
54 { 26, 29, 54, 60 }, /* MCS 1 */
55 { 39, 43, 81, 90 }, /* MCS 2 */
56 { 52, 58, 108, 120 }, /* MCS 3 */
57 { 78, 87, 162, 180 }, /* MCS 4 */
58 { 104, 116, 216, 240 }, /* MCS 5 */
59 { 117, 130, 243, 270 }, /* MCS 6 */
60 { 130, 144, 270, 300 }, /* MCS 7 */
61 { 26, 29, 54, 60 }, /* MCS 8 */
62 { 52, 58, 108, 120 }, /* MCS 9 */
63 { 78, 87, 162, 180 }, /* MCS 10 */
64 { 104, 116, 216, 240 }, /* MCS 11 */
65 { 156, 173, 324, 360 }, /* MCS 12 */
66 { 208, 231, 432, 480 }, /* MCS 13 */
67 { 234, 260, 486, 540 }, /* MCS 14 */
68 { 260, 289, 540, 600 }, /* MCS 15 */
69 { 39, 43, 81, 90 }, /* MCS 16 */
70 { 78, 87, 162, 180 }, /* MCS 17 */
71 { 117, 130, 243, 270 }, /* MCS 18 */
72 { 156, 173, 324, 360 }, /* MCS 19 */
73 { 234, 260, 486, 540 }, /* MCS 20 */
74 { 312, 347, 648, 720 }, /* MCS 21 */
75 { 351, 390, 729, 810 }, /* MCS 22 */
76 { 390, 433, 810, 900 }, /* MCS 23 */
77 { 52, 58, 108, 120 }, /* MCS 24 */
78 { 104, 116, 216, 240 }, /* MCS 25 */
79 { 156, 173, 324, 360 }, /* MCS 26 */
80 { 208, 231, 432, 480 }, /* MCS 27 */
81 { 312, 347, 648, 720 }, /* MCS 28 */
82 { 416, 462, 864, 960 }, /* MCS 29 */
83 { 468, 520, 972, 1080 }, /* MCS 30 */
84 { 520, 578, 1080, 1200 }, /* MCS 31 */
85 { 0, 0, 12, 13 }, /* MCS 32 */
86 { 78, 87, 162, 180 }, /* MCS 33 */
87 { 104, 116, 216, 240 }, /* MCS 34 */
88 { 130, 144, 270, 300 }, /* MCS 35 */
89 { 117, 130, 243, 270 }, /* MCS 36 */
90 { 156, 173, 324, 360 }, /* MCS 37 */
91 { 195, 217, 405, 450 }, /* MCS 38 */
92 { 104, 116, 216, 240 }, /* MCS 39 */
93 { 130, 144, 270, 300 }, /* MCS 40 */
94 { 130, 144, 270, 300 }, /* MCS 41 */
95 { 156, 173, 324, 360 }, /* MCS 42 */
96 { 182, 202, 378, 420 }, /* MCS 43 */
97 { 182, 202, 378, 420 }, /* MCS 44 */
98 { 208, 231, 432, 480 }, /* MCS 45 */
99 { 156, 173, 324, 360 }, /* MCS 46 */
100 { 195, 217, 405, 450 }, /* MCS 47 */
101 { 195, 217, 405, 450 }, /* MCS 48 */
102 { 234, 260, 486, 540 }, /* MCS 49 */
103 { 273, 303, 567, 630 }, /* MCS 50 */
104 { 273, 303, 567, 630 }, /* MCS 51 */
105 { 312, 347, 648, 720 }, /* MCS 52 */
106 { 130, 144, 270, 300 }, /* MCS 53 */
107 { 156, 173, 324, 360 }, /* MCS 54 */
108 { 182, 202, 378, 420 }, /* MCS 55 */
109 { 156, 173, 324, 360 }, /* MCS 56 */
110 { 182, 202, 378, 420 }, /* MCS 57 */
111 { 208, 231, 432, 480 }, /* MCS 58 */
112 { 234, 260, 486, 540 }, /* MCS 59 */
113 { 208, 231, 432, 480 }, /* MCS 60 */
114 { 234, 260, 486, 540 }, /* MCS 61 */
115 { 260, 289, 540, 600 }, /* MCS 62 */
116 { 260, 289, 540, 600 }, /* MCS 63 */
117 { 286, 318, 594, 660 }, /* MCS 64 */
118 { 195, 217, 405, 450 }, /* MCS 65 */
119 { 234, 260, 486, 540 }, /* MCS 66 */
120 { 273, 303, 567, 630 }, /* MCS 67 */
121 { 234, 260, 486, 540 }, /* MCS 68 */
122 { 273, 303, 567, 630 }, /* MCS 69 */
123 { 312, 347, 648, 720 }, /* MCS 70 */
124 { 351, 390, 729, 810 }, /* MCS 71 */
125 { 312, 347, 648, 720 }, /* MCS 72 */
126 { 351, 390, 729, 810 }, /* MCS 73 */
127 { 390, 433, 810, 900 }, /* MCS 74 */
128 { 390, 433, 810, 900 }, /* MCS 75 */
129 { 429, 477, 891, 990 }, /* MCS 76 */
130 };
131
132 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */
133 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age,
134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
135 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I",
136 "AMPDU max reorder age (ms)");
137
138 static int ieee80211_recv_bar_ena = 1;
139 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena,
140 0, "BAR frame processing (ena/dis)");
141
142 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */
143 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout,
144 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
145 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I",
146 "ADDBA request timeout (ms)");
147 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */
148 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff,
149 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
150 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I",
151 "ADDBA request backoff (ms)");
152 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */
153 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW,
154 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff");
155
156 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */
157 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */
158
159 static ieee80211_recv_action_func ht_recv_action_ba_addba_request;
160 static ieee80211_recv_action_func ht_recv_action_ba_addba_response;
161 static ieee80211_recv_action_func ht_recv_action_ba_delba;
162 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave;
163 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth;
164
165 static ieee80211_send_action_func ht_send_action_ba_addba;
166 static ieee80211_send_action_func ht_send_action_ba_delba;
167 static ieee80211_send_action_func ht_send_action_ht_txchwidth;
168
169 static void
ieee80211_ht_init(void)170 ieee80211_ht_init(void)
171 {
172 /*
173 * Setup HT parameters that depends on the clock frequency.
174 */
175 ieee80211_ampdu_age = msecs_to_ticks(500);
176 ieee80211_addba_timeout = msecs_to_ticks(250);
177 ieee80211_addba_backoff = msecs_to_ticks(10*1000);
178 ieee80211_bar_timeout = msecs_to_ticks(250);
179 /*
180 * Register action frame handlers.
181 */
182 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
183 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request);
184 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
185 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response);
186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA,
187 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba);
188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
189 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave);
190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT,
191 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth);
192
193 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
194 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba);
195 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
196 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba);
197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA,
198 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba);
199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT,
200 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth);
201 }
202 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL);
203
204 static int ieee80211_ampdu_enable(struct ieee80211_node *ni,
205 struct ieee80211_tx_ampdu *tap);
206 static int ieee80211_addba_request(struct ieee80211_node *ni,
207 struct ieee80211_tx_ampdu *tap,
208 int dialogtoken, int baparamset, int batimeout);
209 static int ieee80211_addba_response(struct ieee80211_node *ni,
210 struct ieee80211_tx_ampdu *tap,
211 int code, int baparamset, int batimeout);
212 static void ieee80211_addba_stop(struct ieee80211_node *ni,
213 struct ieee80211_tx_ampdu *tap);
214 static void null_addba_response_timeout(struct ieee80211_node *ni,
215 struct ieee80211_tx_ampdu *tap);
216
217 static void ieee80211_bar_response(struct ieee80211_node *ni,
218 struct ieee80211_tx_ampdu *tap, int status);
219 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap);
220 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap);
221 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *,
222 int baparamset, int batimeout, int baseqctl);
223 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *);
224
225 void
ieee80211_ht_attach(struct ieee80211com * ic)226 ieee80211_ht_attach(struct ieee80211com *ic)
227 {
228 /* setup default aggregation policy */
229 ic->ic_recv_action = ieee80211_recv_action;
230 ic->ic_send_action = ieee80211_send_action;
231 ic->ic_ampdu_enable = ieee80211_ampdu_enable;
232 ic->ic_addba_request = ieee80211_addba_request;
233 ic->ic_addba_response = ieee80211_addba_response;
234 ic->ic_addba_response_timeout = null_addba_response_timeout;
235 ic->ic_addba_stop = ieee80211_addba_stop;
236 ic->ic_bar_response = ieee80211_bar_response;
237 ic->ic_ampdu_rx_start = ampdu_rx_start;
238 ic->ic_ampdu_rx_stop = ampdu_rx_stop;
239
240 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
241 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
242 }
243
244 void
ieee80211_ht_detach(struct ieee80211com * ic)245 ieee80211_ht_detach(struct ieee80211com *ic)
246 {
247 }
248
249 void
ieee80211_ht_vattach(struct ieee80211vap * vap)250 ieee80211_ht_vattach(struct ieee80211vap *vap)
251 {
252
253 /* driver can override defaults */
254 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
255 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
256 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
257 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU;
258 /* tx aggregation traffic thresholds */
259 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128;
260 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64;
261 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32;
262 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32;
263
264 vap->iv_htprotmode = IEEE80211_PROT_RTSCTS;
265 vap->iv_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
266
267 if (vap->iv_htcaps & IEEE80211_HTC_HT) {
268 /*
269 * Device is HT capable; enable all HT-related
270 * facilities by default.
271 * XXX these choices may be too aggressive.
272 */
273 vap->iv_flags_ht |= IEEE80211_FHT_HT
274 | IEEE80211_FHT_HTCOMPAT
275 ;
276 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20)
277 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
278 /* XXX infer from channel list? */
279 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
280 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40;
281 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40)
282 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
283 }
284 /* enable RIFS if capable */
285 if (vap->iv_htcaps & IEEE80211_HTC_RIFS)
286 vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
287
288 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
289 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
290 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU)
291 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
292 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
293 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU)
294 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
295
296 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC)
297 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
298 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC)
299 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
300
301 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC)
302 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
303 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC)
304 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
305 }
306 /* NB: disable default legacy WDS, too many issues right now */
307 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)
308 vap->iv_flags_ht &= ~IEEE80211_FHT_HT;
309 }
310
311 void
ieee80211_ht_vdetach(struct ieee80211vap * vap)312 ieee80211_ht_vdetach(struct ieee80211vap *vap)
313 {
314 }
315
316 static int
ht_getrate(struct ieee80211com * ic,int index,enum ieee80211_phymode mode,int ratetype)317 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode,
318 int ratetype)
319 {
320 struct ieee80211_node_txrate tr;
321 int mword, rate;
322
323 tr = IEEE80211_NODE_TXRATE_INIT_HT(index);
324
325 mword = ieee80211_rate2media(ic, &tr, mode);
326 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
327 return (0);
328 switch (ratetype) {
329 case 0:
330 rate = ieee80211_htrates[index].ht20_rate_800ns;
331 break;
332 case 1:
333 rate = ieee80211_htrates[index].ht20_rate_400ns;
334 break;
335 case 2:
336 rate = ieee80211_htrates[index].ht40_rate_800ns;
337 break;
338 default:
339 rate = ieee80211_htrates[index].ht40_rate_400ns;
340 break;
341 }
342 return (rate);
343 }
344
345 static struct printranges {
346 int minmcs;
347 int maxmcs;
348 int txstream;
349 int ratetype;
350 int htcapflags;
351 } ranges[] = {
352 { 0, 7, 1, 0, 0 },
353 { 8, 15, 2, 0, 0 },
354 { 16, 23, 3, 0, 0 },
355 { 24, 31, 4, 0, 0 },
356 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 },
357 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL },
358 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL },
359 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL },
360 { 0, 0, 0, 0, 0 },
361 };
362
363 static void
ht_rateprint(struct ieee80211com * ic,enum ieee80211_phymode mode,int ratetype)364 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype)
365 {
366 int minrate, maxrate;
367 struct printranges *range;
368
369 for (range = ranges; range->txstream != 0; range++) {
370 if (ic->ic_txstream < range->txstream)
371 continue;
372 if (range->htcapflags &&
373 (ic->ic_htcaps & range->htcapflags) == 0)
374 continue;
375 if (ratetype < range->ratetype)
376 continue;
377 minrate = ht_getrate(ic, range->minmcs, mode, ratetype);
378 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype);
379 if (range->maxmcs) {
380 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n",
381 range->minmcs, range->maxmcs,
382 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""),
383 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : ""));
384 } else {
385 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs,
386 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""));
387 }
388 }
389 }
390
391 static void
ht_announce(struct ieee80211com * ic,enum ieee80211_phymode mode)392 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode)
393 {
394 const char *modestr = ieee80211_phymode_name[mode];
395
396 ic_printf(ic, "%s MCS 20MHz\n", modestr);
397 ht_rateprint(ic, mode, 0);
398 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) {
399 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr);
400 ht_rateprint(ic, mode, 1);
401 }
402 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
403 ic_printf(ic, "%s MCS 40MHz:\n", modestr);
404 ht_rateprint(ic, mode, 2);
405 }
406 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
407 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) {
408 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr);
409 ht_rateprint(ic, mode, 3);
410 }
411 }
412
413 void
ieee80211_ht_announce(struct ieee80211com * ic)414 ieee80211_ht_announce(struct ieee80211com *ic)
415 {
416
417 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
418 isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
419 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream);
420 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
421 ht_announce(ic, IEEE80211_MODE_11NA);
422 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
423 ht_announce(ic, IEEE80211_MODE_11NG);
424 }
425
426 void
ieee80211_init_suphtrates(struct ieee80211com * ic)427 ieee80211_init_suphtrates(struct ieee80211com *ic)
428 {
429 #define ADDRATE(x) do { \
430 htrateset->rs_rates[htrateset->rs_nrates] = x; \
431 htrateset->rs_nrates++; \
432 } while (0)
433 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates;
434 int i;
435
436 memset(htrateset, 0, sizeof(struct ieee80211_htrateset));
437 for (i = 0; i < ic->ic_txstream * 8; i++)
438 ADDRATE(i);
439 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
440 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32))
441 ADDRATE(32);
442 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
443 if (ic->ic_txstream >= 2) {
444 for (i = 33; i <= 38; i++)
445 ADDRATE(i);
446 }
447 if (ic->ic_txstream >= 3) {
448 for (i = 39; i <= 52; i++)
449 ADDRATE(i);
450 }
451 if (ic->ic_txstream == 4) {
452 for (i = 53; i <= 76; i++)
453 ADDRATE(i);
454 }
455 }
456 #undef ADDRATE
457 }
458
459 /*
460 * Receive processing.
461 */
462
463 /*
464 * Decap the encapsulated A-MSDU frames and dispatch all but
465 * the last for delivery. The last frame is returned for
466 * delivery via the normal path.
467 */
468 struct mbuf *
ieee80211_decap_amsdu(struct ieee80211_node * ni,struct mbuf * m)469 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
470 {
471 struct ieee80211vap *vap = ni->ni_vap;
472 int framelen;
473 struct mbuf *n;
474
475 /* discard 802.3 header inserted by ieee80211_decap */
476 m_adj(m, sizeof(struct ether_header));
477
478 vap->iv_stats.is_amsdu_decap++;
479
480 for (;;) {
481 /*
482 * Decap the first frame, bust it apart from the
483 * remainder and deliver. We leave the last frame
484 * delivery to the caller (for consistency with other
485 * code paths, could also do it here).
486 */
487 m = ieee80211_decap1(m, &framelen);
488 if (m == NULL) {
489 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
490 ni->ni_macaddr, "a-msdu", "%s", "decap failed");
491 vap->iv_stats.is_amsdu_tooshort++;
492 return NULL;
493 }
494 if (m->m_pkthdr.len == framelen)
495 break;
496 n = m_split(m, framelen, IEEE80211_M_NOWAIT);
497 if (n == NULL) {
498 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
499 ni->ni_macaddr, "a-msdu",
500 "%s", "unable to split encapsulated frames");
501 vap->iv_stats.is_amsdu_split++;
502 m_freem(m); /* NB: must reclaim */
503 return NULL;
504 }
505 vap->iv_deliver_data(vap, ni, m);
506
507 /*
508 * Remove frame contents; each intermediate frame
509 * is required to be aligned to a 4-byte boundary.
510 */
511 m = n;
512 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */
513 }
514 return m; /* last delivered by caller */
515 }
516
517 static void
ampdu_rx_purge_slot(struct ieee80211_rx_ampdu * rap,int i)518 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i)
519 {
520 struct mbuf *m;
521
522 /* Walk the queue, removing frames as appropriate */
523 for (;;) {
524 m = mbufq_dequeue(&rap->rxa_mq[i]);
525 if (m == NULL)
526 break;
527 rap->rxa_qbytes -= m->m_pkthdr.len;
528 rap->rxa_qframes--;
529 m_freem(m);
530 }
531 }
532
533 /*
534 * Add the given frame to the current RX reorder slot.
535 *
536 * For future offloaded A-MSDU handling where multiple frames with
537 * the same sequence number show up here, this routine will append
538 * those frames as long as they're appropriately tagged.
539 */
540 static int
ampdu_rx_add_slot(struct ieee80211_rx_ampdu * rap,int off,int tid,ieee80211_seq rxseq,struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs)541 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid,
542 ieee80211_seq rxseq,
543 struct ieee80211_node *ni,
544 struct mbuf *m,
545 const struct ieee80211_rx_stats *rxs)
546 {
547 const struct ieee80211_rx_stats *rxs_final = NULL;
548 struct ieee80211vap *vap = ni->ni_vap;
549 int toss_dup;
550 #define PROCESS 0 /* caller should process frame */
551 #define CONSUMED 1 /* frame consumed, caller does nothing */
552
553 /*
554 * Figure out if this is a duplicate frame for the given slot.
555 *
556 * We're assuming that the driver will hand us all the frames
557 * for a given AMSDU decap pass and if we get /a/ frame
558 * for an AMSDU decap then we'll get all of them.
559 *
560 * The tricksy bit is that we don't know when the /end/ of
561 * the decap pass is, because we aren't tracking state here
562 * per-slot to know that we've finished receiving the frame list.
563 *
564 * The driver sets RX_F_AMSDU and RX_F_AMSDU_MORE to tell us
565 * what's going on; so ideally we'd just check the frame at the
566 * end of the reassembly slot to see if its F_AMSDU w/ no F_AMSDU_MORE -
567 * that means we've received the whole AMSDU decap pass.
568 */
569
570 /*
571 * Get the rxs of the final mbuf in the slot, if one exists.
572 */
573 if (!mbufq_empty(&rap->rxa_mq[off])) {
574 rxs_final = ieee80211_get_rx_params_ptr(mbufq_last(&rap->rxa_mq[off]));
575 }
576
577 /* Default to tossing the duplicate frame */
578 toss_dup = 1;
579
580 /*
581 * Check to see if the final frame has F_AMSDU and F_AMSDU set, AND
582 * this frame has F_AMSDU set (MORE or otherwise.) That's a sign
583 * that more can come.
584 */
585
586 if ((rxs != NULL) && (rxs_final != NULL) &&
587 ieee80211_check_rxseq_amsdu(rxs) &&
588 ieee80211_check_rxseq_amsdu(rxs_final)) {
589 if (! ieee80211_check_rxseq_amsdu_more(rxs_final)) {
590 /*
591 * amsdu_more() returning 0 means "it's not the
592 * final frame" so we can append more
593 * frames here.
594 */
595 toss_dup = 0;
596 }
597 }
598
599 /*
600 * If the list is empty OR we have determined we can put more
601 * driver decap'ed AMSDU frames in here, then insert.
602 */
603 if (mbufq_empty(&rap->rxa_mq[off]) || (toss_dup == 0)) {
604 if (mbufq_enqueue(&rap->rxa_mq[off], m) != 0) {
605 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
606 ni->ni_macaddr,
607 "a-mpdu queue fail",
608 "seqno %u tid %u BA win <%u:%u> off=%d, qlen=%d, maxqlen=%d",
609 rxseq, tid, rap->rxa_start,
610 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
611 off,
612 mbufq_len(&rap->rxa_mq[off]),
613 rap->rxa_mq[off].mq_maxlen);
614 /* XXX error count */
615 m_freem(m);
616 return CONSUMED;
617 }
618 rap->rxa_qframes++;
619 rap->rxa_qbytes += m->m_pkthdr.len;
620 vap->iv_stats.is_ampdu_rx_reorder++;
621 /*
622 * Statistics for AMSDU decap.
623 */
624 if (rxs != NULL && ieee80211_check_rxseq_amsdu(rxs)) {
625 if (ieee80211_check_rxseq_amsdu_more(rxs)) {
626 /* more=1, AMSDU, end of batch */
627 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
628 } else {
629 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
630 }
631 }
632 } else {
633 IEEE80211_DISCARD_MAC(vap,
634 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
635 ni->ni_macaddr, "a-mpdu duplicate",
636 "seqno %u tid %u BA win <%u:%u>",
637 rxseq, tid, rap->rxa_start,
638 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
639 if (rxs != NULL) {
640 IEEE80211_DISCARD_MAC(vap,
641 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
642 ni->ni_macaddr, "a-mpdu duplicate",
643 "seqno %d tid %u pktflags 0x%08x\n",
644 rxseq, tid, rxs->c_pktflags);
645 }
646 if (rxs_final != NULL) {
647 IEEE80211_DISCARD_MAC(vap,
648 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
649 ni->ni_macaddr, "a-mpdu duplicate",
650 "final: pktflags 0x%08x\n",
651 rxs_final->c_pktflags);
652 }
653 vap->iv_stats.is_rx_dup++;
654 IEEE80211_NODE_STAT(ni, rx_dup);
655 m_freem(m);
656 }
657 return CONSUMED;
658 #undef CONSUMED
659 #undef PROCESS
660 }
661
662 /*
663 * Purge all frames in the A-MPDU re-order queue.
664 */
665 static void
ampdu_rx_purge(struct ieee80211_rx_ampdu * rap)666 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
667 {
668 int i;
669
670 for (i = 0; i < rap->rxa_wnd; i++) {
671 ampdu_rx_purge_slot(rap, i);
672 if (rap->rxa_qframes == 0)
673 break;
674 }
675 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
676 ("lost %u data, %u frames on ampdu rx q",
677 rap->rxa_qbytes, rap->rxa_qframes));
678 }
679
680 static void
ieee80211_ampdu_rx_init_rap(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap)681 ieee80211_ampdu_rx_init_rap(struct ieee80211_node *ni,
682 struct ieee80211_rx_ampdu *rap)
683 {
684 int i;
685
686 /* XXX TODO: ensure the queues are empty */
687 memset(rap, 0, sizeof(*rap));
688 for (i = 0; i < IEEE80211_AGGR_BAWMAX; i++)
689 mbufq_init(&rap->rxa_mq[i], 256);
690 }
691
692 /*
693 * Start A-MPDU rx/re-order processing for the specified TID.
694 */
695 static int
ampdu_rx_start(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap,int baparamset,int batimeout,int baseqctl)696 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
697 int baparamset, int batimeout, int baseqctl)
698 {
699 struct ieee80211vap *vap = ni->ni_vap;
700 int bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
701
702 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
703 /*
704 * AMPDU previously setup and not terminated with a DELBA,
705 * flush the reorder q's in case anything remains.
706 */
707 ampdu_rx_purge(rap);
708 }
709 ieee80211_ampdu_rx_init_rap(ni, rap);
710 rap->rxa_wnd = (bufsiz == 0) ?
711 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
712 rap->rxa_start = _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START);
713 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
714
715 /* XXX this should be a configuration flag */
716 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
717 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
718 rap->rxa_flags |= IEEE80211_AGGR_AMSDU;
719 else
720 rap->rxa_flags &= ~IEEE80211_AGGR_AMSDU;
721
722 return 0;
723 }
724
725 /*
726 * Public function; manually setup the RX ampdu state.
727 */
728 int
ieee80211_ampdu_rx_start_ext(struct ieee80211_node * ni,int tid,int seq,int baw)729 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw)
730 {
731 struct ieee80211_rx_ampdu *rap;
732
733 /* XXX TODO: sanity check tid, seq, baw */
734
735 rap = &ni->ni_rx_ampdu[tid];
736
737 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) {
738 /*
739 * AMPDU previously setup and not terminated with a DELBA,
740 * flush the reorder q's in case anything remains.
741 */
742 ampdu_rx_purge(rap);
743 }
744
745 ieee80211_ampdu_rx_init_rap(ni, rap);
746
747 rap->rxa_wnd = (baw== 0) ?
748 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX);
749 if (seq == -1) {
750 /* Wait for the first RX frame, use that as BAW */
751 rap->rxa_start = 0;
752 rap->rxa_flags |= IEEE80211_AGGR_WAITRX;
753 } else {
754 rap->rxa_start = seq;
755 }
756 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND;
757
758 /* XXX TODO: no amsdu flag */
759
760 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
761 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x",
762 __func__,
763 tid,
764 seq,
765 rap->rxa_wnd,
766 rap->rxa_flags);
767
768 return 0;
769 }
770
771 /*
772 * Public function; manually stop the RX AMPDU state.
773 */
774 void
ieee80211_ampdu_rx_stop_ext(struct ieee80211_node * ni,int tid)775 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid)
776 {
777 struct ieee80211_rx_ampdu *rap;
778
779 /* XXX TODO: sanity check tid, seq, baw */
780 rap = &ni->ni_rx_ampdu[tid];
781 ampdu_rx_stop(ni, rap);
782 }
783
784 /*
785 * Stop A-MPDU rx processing for the specified TID.
786 */
787 static void
ampdu_rx_stop(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap)788 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
789 {
790
791 ampdu_rx_purge(rap);
792 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING
793 | IEEE80211_AGGR_XCHGPEND
794 | IEEE80211_AGGR_WAITRX);
795 }
796
797 /*
798 * Dispatch a frame from the A-MPDU reorder queue. The
799 * frame is fed back into ieee80211_input marked with an
800 * M_AMPDU_MPDU flag so it doesn't come back to us (it also
801 * permits ieee80211_input to optimize re-processing).
802 */
803 static __inline void
ampdu_dispatch(struct ieee80211_node * ni,struct mbuf * m)804 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
805 {
806 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */
807 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */
808 (void) ieee80211_input(ni, m, 0, 0);
809 }
810
811 static int
ampdu_dispatch_slot(struct ieee80211_rx_ampdu * rap,struct ieee80211_node * ni,int i)812 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
813 int i)
814 {
815 struct mbuf *m;
816 int n = 0;
817
818 for (;;) {
819 m = mbufq_dequeue(&rap->rxa_mq[i]);
820 if (m == NULL)
821 break;
822 n++;
823
824 rap->rxa_qbytes -= m->m_pkthdr.len;
825 rap->rxa_qframes--;
826
827 ampdu_dispatch(ni, m);
828 }
829 return (n);
830 }
831
832 static void
ampdu_rx_moveup(struct ieee80211_rx_ampdu * rap,struct ieee80211_node * ni,int i,int winstart)833 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni,
834 int i, int winstart)
835 {
836 struct ieee80211vap *vap = ni->ni_vap;
837
838 /*
839 * If frames remain, copy the mbuf pointers down so
840 * they correspond to the offsets in the new window.
841 */
842 if (rap->rxa_qframes != 0) {
843 int n = rap->rxa_qframes, j;
844 for (j = i+1; j < rap->rxa_wnd; j++) {
845 /*
846 * Concat the list contents over, which will
847 * blank the source list for us.
848 */
849 if (mbufq_len(&rap->rxa_mq[j]) != 0) {
850 n = n - mbufq_len(&rap->rxa_mq[j]);
851 mbufq_concat(&rap->rxa_mq[j-i], &rap->rxa_mq[j]);
852 KASSERT(n >= 0, ("%s: n < 0 (%d)", __func__, n));
853 if (n == 0)
854 break;
855 }
856 }
857 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
858 "BA win <%d:%d> winstart %d",
859 __func__, n, rap->rxa_qframes, i, rap->rxa_start,
860 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
861 winstart));
862 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes;
863 }
864 }
865
866 /*
867 * Dispatch as many frames as possible from the re-order queue.
868 * Frames will always be "at the front"; we process all frames
869 * up to the first empty slot in the window. On completion we
870 * cleanup state if there are still pending frames in the current
871 * BA window. We assume the frame at slot 0 is already handled
872 * by the caller; we always start at slot 1.
873 */
874 static void
ampdu_rx_dispatch(struct ieee80211_rx_ampdu * rap,struct ieee80211_node * ni)875 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
876 {
877 struct ieee80211vap *vap = ni->ni_vap;
878 int i, r, r2;
879
880 /* flush run of frames */
881 r2 = 0;
882 for (i = 1; i < rap->rxa_wnd; i++) {
883 r = ampdu_dispatch_slot(rap, ni, i);
884 if (r == 0)
885 break;
886 r2 += r;
887 }
888
889 /* move up frames */
890 ampdu_rx_moveup(rap, ni, i, -1);
891
892 /*
893 * Adjust the start of the BA window to
894 * reflect the frames just dispatched.
895 */
896 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
897 vap->iv_stats.is_ampdu_rx_oor += r2;
898
899 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
900 "%s: moved slot up %d slots to start at %d (%d frames)",
901 __func__,
902 i,
903 rap->rxa_start,
904 r2);
905 }
906
907 /*
908 * Dispatch all frames in the A-MPDU re-order queue.
909 */
910 static void
ampdu_rx_flush(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap)911 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
912 {
913 int i, r;
914
915 for (i = 0; i < rap->rxa_wnd; i++) {
916 r = ampdu_dispatch_slot(rap, ni, i);
917 if (r == 0)
918 continue;
919 ni->ni_vap->iv_stats.is_ampdu_rx_oor += r;
920
921 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
922 "%s: moved slot up %d slots to start at %d (%d frames)",
923 __func__,
924 1,
925 rap->rxa_start,
926 r);
927
928 if (rap->rxa_qframes == 0)
929 break;
930 }
931 }
932
933 /*
934 * Dispatch all frames in the A-MPDU re-order queue
935 * preceding the specified sequence number. This logic
936 * handles window moves due to a received MSDU or BAR.
937 */
938 static void
ampdu_rx_flush_upto(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap,ieee80211_seq winstart)939 ampdu_rx_flush_upto(struct ieee80211_node *ni,
940 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
941 {
942 struct ieee80211vap *vap = ni->ni_vap;
943 ieee80211_seq seqno;
944 int i, r;
945
946 /*
947 * Flush any complete MSDU's with a sequence number lower
948 * than winstart. Gaps may exist. Note that we may actually
949 * dispatch frames past winstart if a run continues; this is
950 * an optimization that avoids having to do a separate pass
951 * to dispatch frames after moving the BA window start.
952 */
953 seqno = rap->rxa_start;
954 for (i = 0; i < rap->rxa_wnd; i++) {
955 if ((r = mbufq_len(&rap->rxa_mq[i])) != 0) {
956 (void) ampdu_dispatch_slot(rap, ni, i);
957 } else {
958 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
959 break;
960 }
961 vap->iv_stats.is_ampdu_rx_oor += r;
962 seqno = IEEE80211_SEQ_INC(seqno);
963
964 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
965 "%s: moved slot up %d slots to start at %d (%d frames)",
966 __func__,
967 1,
968 seqno,
969 r);
970 }
971
972 /*
973 * If frames remain, copy the mbuf pointers down so
974 * they correspond to the offsets in the new window.
975 */
976 ampdu_rx_moveup(rap, ni, i, winstart);
977
978 /*
979 * Move the start of the BA window; we use the
980 * sequence number of the last MSDU that was
981 * passed up the stack+1 or winstart if stopped on
982 * a gap in the reorder buffer.
983 */
984 rap->rxa_start = seqno;
985 }
986
987 /*
988 * Process a received QoS data frame for an HT station. Handle
989 * A-MPDU reordering: if this frame is received out of order
990 * and falls within the BA window hold onto it. Otherwise if
991 * this frame completes a run, flush any pending frames. We
992 * return 1 if the frame is consumed. A 0 is returned if
993 * the frame should be processed normally by the caller.
994 *
995 * A-MSDU: handle hardware decap'ed A-MSDU frames that are
996 * pretending to be MPDU's. They're dispatched directly if
997 * able; or attempted to put into the receive reordering slot.
998 */
999 int
ieee80211_ampdu_reorder(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs)1000 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m,
1001 const struct ieee80211_rx_stats *rxs)
1002 {
1003 #define PROCESS 0 /* caller should process frame */
1004 #define CONSUMED 1 /* frame consumed, caller does nothing */
1005 struct ieee80211vap *vap = ni->ni_vap;
1006 struct ieee80211_qosframe *wh;
1007 struct ieee80211_rx_ampdu *rap;
1008 ieee80211_seq rxseq;
1009 uint8_t tid;
1010 int off;
1011 int amsdu = ieee80211_check_rxseq_amsdu(rxs);
1012 int amsdu_end = ieee80211_check_rxseq_amsdu_more(rxs);
1013
1014 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU,
1015 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags));
1016 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1017
1018 /* NB: m_len known to be sufficient */
1019 wh = mtod(m, struct ieee80211_qosframe *);
1020 if (!IEEE80211_IS_QOSDATA(wh)) {
1021 /*
1022 * Not QoS data, shouldn't get here but just
1023 * return it to the caller for processing.
1024 */
1025 return PROCESS;
1026 }
1027
1028 /*
1029 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery.
1030 *
1031 * Multicast QoS data frames are checked against a different
1032 * counter, not the per-TID counter.
1033 */
1034 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1035 return PROCESS;
1036
1037 tid = ieee80211_getqos(wh)[0];
1038 tid &= IEEE80211_QOS_TID;
1039 rap = &ni->ni_rx_ampdu[tid];
1040 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1041 /*
1042 * No ADDBA request yet, don't touch.
1043 */
1044 return PROCESS;
1045 }
1046 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1047 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) {
1048 /*
1049 * Fragments are not allowed; toss.
1050 */
1051 IEEE80211_DISCARD_MAC(vap,
1052 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1053 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid,
1054 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1055 vap->iv_stats.is_ampdu_rx_drop++;
1056 IEEE80211_NODE_STAT(ni, rx_drop);
1057 m_freem(m);
1058 return CONSUMED;
1059 }
1060 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT;
1061 rap->rxa_nframes++;
1062
1063 /*
1064 * Handle waiting for the first frame to define the BAW.
1065 * Some firmware doesn't provide the RX of the starting point
1066 * of the BAW and we have to cope.
1067 */
1068 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) {
1069 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX;
1070 rap->rxa_start = rxseq;
1071 }
1072 again:
1073 if (rxseq == rap->rxa_start) {
1074 /*
1075 * First frame in window.
1076 */
1077 if (rap->rxa_qframes != 0) {
1078 /*
1079 * Dispatch as many packets as we can.
1080 */
1081 KASSERT(mbufq_empty(&rap->rxa_mq[0]), ("unexpected dup"));
1082 ampdu_dispatch(ni, m);
1083 ampdu_rx_dispatch(rap, ni);
1084 return CONSUMED;
1085 } else {
1086 /*
1087 * In order; advance window if needed and notify
1088 * caller to dispatch directly.
1089 */
1090 if (amsdu) {
1091 if (amsdu_end) {
1092 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1093 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end);
1094 } else {
1095 IEEE80211_NODE_STAT(ni, rx_amsdu_more);
1096 }
1097 } else {
1098 rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
1099 }
1100 return PROCESS;
1101 }
1102 }
1103 /*
1104 * Frame is out of order; store if in the BA window.
1105 */
1106 /* calculate offset in BA window */
1107 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1108 if (off < rap->rxa_wnd) {
1109 /*
1110 * Common case (hopefully): in the BA window.
1111 * Sec 9.10.7.6.2 a) (p.137)
1112 */
1113
1114 /*
1115 * Check for frames sitting too long in the reorder queue.
1116 * This should only ever happen if frames are not delivered
1117 * without the sender otherwise notifying us (e.g. with a
1118 * BAR to move the window). Typically this happens because
1119 * of vendor bugs that cause the sequence number to jump.
1120 * When this happens we get a gap in the reorder queue that
1121 * leaves frame sitting on the queue until they get pushed
1122 * out due to window moves. When the vendor does not send
1123 * BAR this move only happens due to explicit packet sends
1124 *
1125 * NB: we only track the time of the oldest frame in the
1126 * reorder q; this means that if we flush we might push
1127 * frames that still "new"; if this happens then subsequent
1128 * frames will result in BA window moves which cost something
1129 * but is still better than a big throughput dip.
1130 */
1131 if (rap->rxa_qframes != 0) {
1132 /* XXX honor batimeout? */
1133 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1134 /*
1135 * Too long since we received the first
1136 * frame; flush the reorder buffer.
1137 */
1138 if (rap->rxa_qframes != 0) {
1139 vap->iv_stats.is_ampdu_rx_age +=
1140 rap->rxa_qframes;
1141 ampdu_rx_flush(ni, rap);
1142 }
1143 /*
1144 * Advance the window if needed and notify
1145 * the caller to dispatch directly.
1146 */
1147 if (amsdu) {
1148 if (amsdu_end) {
1149 rap->rxa_start =
1150 IEEE80211_SEQ_INC(rxseq);
1151 IEEE80211_NODE_STAT(ni,
1152 rx_amsdu_more_end);
1153 } else {
1154 IEEE80211_NODE_STAT(ni,
1155 rx_amsdu_more);
1156 }
1157 } else {
1158 rap->rxa_start =
1159 IEEE80211_SEQ_INC(rxseq);
1160 }
1161 return PROCESS;
1162 }
1163 } else {
1164 /*
1165 * First frame, start aging timer.
1166 */
1167 rap->rxa_age = ticks;
1168 }
1169
1170 /* save packet - this consumes, no matter what */
1171 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m, rxs);
1172 return CONSUMED;
1173 }
1174 if (off < IEEE80211_SEQ_BA_RANGE) {
1175 /*
1176 * Outside the BA window, but within range;
1177 * flush the reorder q and move the window.
1178 * Sec 9.10.7.6.2 b) (p.138)
1179 */
1180 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1181 "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
1182 rap->rxa_start,
1183 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1184 rap->rxa_qframes, rxseq, tid);
1185 vap->iv_stats.is_ampdu_rx_move++;
1186
1187 /*
1188 * The spec says to flush frames up to but not including:
1189 * WinStart_B = rxseq - rap->rxa_wnd + 1
1190 * Then insert the frame or notify the caller to process
1191 * it immediately. We can safely do this by just starting
1192 * over again because we know the frame will now be within
1193 * the BA window.
1194 */
1195 /* NB: rxa_wnd known to be >0 */
1196 ampdu_rx_flush_upto(ni, rap,
1197 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
1198 goto again;
1199 } else {
1200 /*
1201 * Outside the BA window and out of range; toss.
1202 * Sec 9.10.7.6.2 c) (p.138)
1203 */
1204 IEEE80211_DISCARD_MAC(vap,
1205 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1206 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1207 rap->rxa_start,
1208 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1209 rap->rxa_qframes, rxseq, tid,
1210 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1211 vap->iv_stats.is_ampdu_rx_drop++;
1212 IEEE80211_NODE_STAT(ni, rx_drop);
1213 m_freem(m);
1214 return CONSUMED;
1215 }
1216 #undef CONSUMED
1217 #undef PROCESS
1218 }
1219
1220 /*
1221 * Process a BAR ctl frame. Dispatch all frames up to
1222 * the sequence number of the frame. If this frame is
1223 * out of range it's discarded.
1224 */
1225 void
ieee80211_recv_bar(struct ieee80211_node * ni,struct mbuf * m0)1226 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
1227 {
1228 struct ieee80211vap *vap = ni->ni_vap;
1229 struct ieee80211_frame_bar *wh;
1230 struct ieee80211_rx_ampdu *rap;
1231 ieee80211_seq rxseq;
1232 int tid, off;
1233
1234 if (!ieee80211_recv_bar_ena) {
1235 #if 0
1236 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N,
1237 ni->ni_macaddr, "BAR", "%s", "processing disabled");
1238 #endif
1239 vap->iv_stats.is_ampdu_bar_bad++;
1240 return;
1241 }
1242 wh = mtod(m0, struct ieee80211_frame_bar *);
1243 /* XXX check basic BAR */
1244 tid = _IEEE80211_MASKSHIFT(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
1245 rap = &ni->ni_rx_ampdu[tid];
1246 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
1247 /*
1248 * No ADDBA request yet, don't touch.
1249 */
1250 IEEE80211_DISCARD_MAC(vap,
1251 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
1252 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
1253 vap->iv_stats.is_ampdu_bar_bad++;
1254 return;
1255 }
1256 vap->iv_stats.is_ampdu_bar_rx++;
1257 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1258 if (rxseq == rap->rxa_start)
1259 return;
1260 /* calculate offset in BA window */
1261 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
1262 if (off < IEEE80211_SEQ_BA_RANGE) {
1263 /*
1264 * Flush the reorder q up to rxseq and move the window.
1265 * Sec 9.10.7.6.3 a) (p.138)
1266 */
1267 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
1268 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
1269 rap->rxa_start,
1270 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1271 rap->rxa_qframes, rxseq, tid);
1272 vap->iv_stats.is_ampdu_bar_move++;
1273
1274 ampdu_rx_flush_upto(ni, rap, rxseq);
1275 if (off >= rap->rxa_wnd) {
1276 /*
1277 * BAR specifies a window start to the right of BA
1278 * window; we must move it explicitly since
1279 * ampdu_rx_flush_upto will not.
1280 */
1281 rap->rxa_start = rxseq;
1282 }
1283 } else {
1284 /*
1285 * Out of range; toss.
1286 * Sec 9.10.7.6.3 b) (p.138)
1287 */
1288 IEEE80211_DISCARD_MAC(vap,
1289 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
1290 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
1291 rap->rxa_start,
1292 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
1293 rap->rxa_qframes, rxseq, tid,
1294 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
1295 vap->iv_stats.is_ampdu_bar_oow++;
1296 IEEE80211_NODE_STAT(ni, rx_drop);
1297 }
1298 }
1299
1300 /*
1301 * Setup HT-specific state in a node. Called only
1302 * when HT use is negotiated so we don't do extra
1303 * work for temporary and/or legacy sta's.
1304 */
1305 void
ieee80211_ht_node_init(struct ieee80211_node * ni)1306 ieee80211_ht_node_init(struct ieee80211_node *ni)
1307 {
1308 struct ieee80211_tx_ampdu *tap;
1309 int tid;
1310
1311 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1312 ni,
1313 "%s: called (%p)",
1314 __func__,
1315 ni);
1316
1317 if (ni->ni_flags & IEEE80211_NODE_HT) {
1318 /*
1319 * Clean AMPDU state on re-associate. This handles the case
1320 * where a station leaves w/o notifying us and then returns
1321 * before node is reaped for inactivity.
1322 */
1323 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1324 ni,
1325 "%s: calling cleanup (%p)",
1326 __func__, ni);
1327 ieee80211_ht_node_cleanup(ni);
1328 }
1329 for (tid = 0; tid < WME_NUM_TID; tid++) {
1330 tap = &ni->ni_tx_ampdu[tid];
1331 tap->txa_tid = tid;
1332 tap->txa_ni = ni;
1333 ieee80211_txampdu_init_pps(tap);
1334 /* NB: further initialization deferred */
1335 ieee80211_ampdu_rx_init_rap(ni, &ni->ni_rx_ampdu[tid]);
1336 }
1337 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1338 IEEE80211_NODE_AMSDU;
1339 }
1340
1341 /*
1342 * Cleanup HT-specific state in a node. Called only
1343 * when HT use has been marked.
1344 */
1345 void
ieee80211_ht_node_cleanup(struct ieee80211_node * ni)1346 ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
1347 {
1348 struct ieee80211com *ic = ni->ni_ic;
1349 int i;
1350
1351 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
1352 ni,
1353 "%s: called (%p)",
1354 __func__, ni);
1355
1356 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
1357
1358 /* XXX optimize this */
1359 for (i = 0; i < WME_NUM_TID; i++) {
1360 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
1361 if (tap->txa_flags & IEEE80211_AGGR_SETUP)
1362 ampdu_tx_stop(tap);
1363 }
1364 for (i = 0; i < WME_NUM_TID; i++)
1365 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]);
1366
1367 ni->ni_htcap = 0;
1368 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL;
1369 }
1370
1371 /*
1372 * Age out HT resources for a station.
1373 */
1374 void
ieee80211_ht_node_age(struct ieee80211_node * ni)1375 ieee80211_ht_node_age(struct ieee80211_node *ni)
1376 {
1377 struct ieee80211vap *vap = ni->ni_vap;
1378 uint8_t tid;
1379
1380 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
1381
1382 for (tid = 0; tid < WME_NUM_TID; tid++) {
1383 struct ieee80211_rx_ampdu *rap;
1384
1385 rap = &ni->ni_rx_ampdu[tid];
1386 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0)
1387 continue;
1388 if (rap->rxa_qframes == 0)
1389 continue;
1390 /*
1391 * Check for frames sitting too long in the reorder queue.
1392 * See above for more details on what's happening here.
1393 */
1394 /* XXX honor batimeout? */
1395 if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
1396 /*
1397 * Too long since we received the first
1398 * frame; flush the reorder buffer.
1399 */
1400 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes;
1401 ampdu_rx_flush(ni, rap);
1402 }
1403 }
1404 }
1405
1406 static struct ieee80211_channel *
findhtchan(struct ieee80211com * ic,struct ieee80211_channel * c,int htflags)1407 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
1408 {
1409 return ieee80211_find_channel(ic, c->ic_freq,
1410 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
1411 }
1412
1413 /*
1414 * Adjust a channel to be HT/non-HT according to the vap's configuration.
1415 */
1416 struct ieee80211_channel *
ieee80211_ht_adjust_channel(struct ieee80211com * ic,struct ieee80211_channel * chan,int flags)1417 ieee80211_ht_adjust_channel(struct ieee80211com *ic,
1418 struct ieee80211_channel *chan, int flags)
1419 {
1420 struct ieee80211_channel *c;
1421
1422 if (flags & IEEE80211_FHT_HT) {
1423 /* promote to HT if possible */
1424 if (flags & IEEE80211_FHT_USEHT40) {
1425 if (!IEEE80211_IS_CHAN_HT40(chan)) {
1426 /* NB: arbitrarily pick ht40+ over ht40- */
1427 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
1428 if (c == NULL)
1429 c = findhtchan(ic, chan,
1430 IEEE80211_CHAN_HT40D);
1431 if (c == NULL)
1432 c = findhtchan(ic, chan,
1433 IEEE80211_CHAN_HT20);
1434 if (c != NULL)
1435 chan = c;
1436 }
1437 } else if (!IEEE80211_IS_CHAN_HT20(chan)) {
1438 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
1439 if (c != NULL)
1440 chan = c;
1441 }
1442 } else if (IEEE80211_IS_CHAN_HT(chan)) {
1443 /* demote to legacy, HT use is disabled */
1444 c = ieee80211_find_channel(ic, chan->ic_freq,
1445 chan->ic_flags &~ IEEE80211_CHAN_HT);
1446 if (c != NULL)
1447 chan = c;
1448 }
1449 return chan;
1450 }
1451
1452 /*
1453 * Setup HT-specific state for a legacy WDS peer.
1454 */
1455 void
ieee80211_ht_wds_init(struct ieee80211_node * ni)1456 ieee80211_ht_wds_init(struct ieee80211_node *ni)
1457 {
1458 struct ieee80211vap *vap = ni->ni_vap;
1459 struct ieee80211_tx_ampdu *tap;
1460 int tid;
1461
1462 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested"));
1463
1464 /* XXX check scan cache in case peer has an ap and we have info */
1465 /*
1466 * If setup with a legacy channel; locate an HT channel.
1467 * Otherwise if the inherited channel (from a companion
1468 * AP) is suitable use it so we use the same location
1469 * for the extension channel).
1470 */
1471 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic,
1472 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan));
1473
1474 ni->ni_htcap = 0;
1475 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
1476 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
1477 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1478 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
1479 ni->ni_chw = IEEE80211_STA_RX_BW_40;
1480 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1481 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
1482 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1483 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
1484 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
1485 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
1486 } else {
1487 ni->ni_chw = IEEE80211_STA_RX_BW_20;
1488 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
1489 }
1490 ni->ni_htctlchan = ni->ni_chan->ic_ieee;
1491 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
1492 ni->ni_flags |= IEEE80211_NODE_RIFS;
1493 /* XXX does it make sense to enable SMPS? */
1494
1495 ni->ni_htopmode = 0; /* XXX need protection state */
1496 ni->ni_htstbc = 0; /* XXX need info */
1497
1498 for (tid = 0; tid < WME_NUM_TID; tid++) {
1499 tap = &ni->ni_tx_ampdu[tid];
1500 tap->txa_tid = tid;
1501 ieee80211_txampdu_init_pps(tap);
1502 }
1503 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */
1504 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU |
1505 IEEE80211_NODE_AMSDU;
1506 }
1507
1508 /*
1509 * Notify a VAP of a change in the HTINFO ie if it's a hostap VAP.
1510 *
1511 * This is to be called from the deferred HT protection update
1512 * task once the flags are updated.
1513 */
1514 void
ieee80211_htinfo_notify(struct ieee80211vap * vap)1515 ieee80211_htinfo_notify(struct ieee80211vap *vap)
1516 {
1517
1518 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1519
1520 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
1521 return;
1522 if (vap->iv_state != IEEE80211_S_RUN ||
1523 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan))
1524 return;
1525
1526 IEEE80211_NOTE(vap,
1527 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1528 vap->iv_bss,
1529 "HT bss occupancy change: %d sta, %d ht, "
1530 "%d ht40%s, HT protmode now 0x%x"
1531 , vap->iv_sta_assoc
1532 , vap->iv_ht_sta_assoc
1533 , vap->iv_ht40_sta_assoc
1534 , (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) ?
1535 ", non-HT sta present" : ""
1536 , vap->iv_curhtprotmode);
1537
1538 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO);
1539 }
1540
1541 /*
1542 * Calculate HT protection mode from current
1543 * state and handle updates.
1544 */
1545 static void
htinfo_update(struct ieee80211vap * vap)1546 htinfo_update(struct ieee80211vap *vap)
1547 {
1548 struct ieee80211com *ic = vap->iv_ic;
1549 uint8_t protmode;
1550
1551 if (vap->iv_sta_assoc != vap->iv_ht_sta_assoc) {
1552 protmode = IEEE80211_HTINFO_OPMODE_MIXED
1553 | IEEE80211_HTINFO_NONHT_PRESENT;
1554 } else if (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) {
1555 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
1556 | IEEE80211_HTINFO_NONHT_PRESENT;
1557 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
1558 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1559 vap->iv_sta_assoc != vap->iv_ht40_sta_assoc) {
1560 protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
1561 } else {
1562 protmode = IEEE80211_HTINFO_OPMODE_PURE;
1563 }
1564 if (protmode != vap->iv_curhtprotmode) {
1565 vap->iv_curhtprotmode = protmode;
1566 /* Update VAP with new protection mode */
1567 ieee80211_vap_update_ht_protmode(vap);
1568 }
1569 }
1570
1571 /*
1572 * Handle an HT station joining a BSS.
1573 */
1574 void
ieee80211_ht_node_join(struct ieee80211_node * ni)1575 ieee80211_ht_node_join(struct ieee80211_node *ni)
1576 {
1577 struct ieee80211vap *vap = ni->ni_vap;
1578
1579 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1580
1581 if (ni->ni_flags & IEEE80211_NODE_HT) {
1582 vap->iv_ht_sta_assoc++;
1583 if (ni->ni_chw == IEEE80211_STA_RX_BW_40)
1584 vap->iv_ht40_sta_assoc++;
1585 }
1586 htinfo_update(vap);
1587 }
1588
1589 /*
1590 * Handle an HT station leaving a BSS.
1591 */
1592 void
ieee80211_ht_node_leave(struct ieee80211_node * ni)1593 ieee80211_ht_node_leave(struct ieee80211_node *ni)
1594 {
1595 struct ieee80211vap *vap = ni->ni_vap;
1596
1597 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1598
1599 if (ni->ni_flags & IEEE80211_NODE_HT) {
1600 vap->iv_ht_sta_assoc--;
1601 if (ni->ni_chw == IEEE80211_STA_RX_BW_40)
1602 vap->iv_ht40_sta_assoc--;
1603 }
1604 htinfo_update(vap);
1605 }
1606
1607 /*
1608 * Public version of htinfo_update; used for processing
1609 * beacon frames from overlapping bss.
1610 *
1611 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED
1612 * (on receipt of a beacon that advertises MIXED) or
1613 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon
1614 * from an overlapping legacy bss). We treat MIXED with
1615 * a higher precedence than PROTOPT (i.e. we will not change
1616 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This
1617 * corresponds to how we handle things in htinfo_update.
1618 *
1619 */
1620 void
ieee80211_htprot_update(struct ieee80211vap * vap,int protmode)1621 ieee80211_htprot_update(struct ieee80211vap *vap, int protmode)
1622 {
1623 struct ieee80211com *ic = vap->iv_ic;
1624 #define OPMODE(x) _IEEE80211_SHIFTMASK(x, IEEE80211_HTINFO_OPMODE)
1625 IEEE80211_LOCK(ic);
1626
1627 /* track non-HT station presence */
1628 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT,
1629 ("protmode 0x%x", protmode));
1630 vap->iv_flags_ht |= IEEE80211_FHT_NONHT_PR;
1631 vap->iv_lastnonht = ticks;
1632
1633 if (protmode != vap->iv_curhtprotmode &&
1634 (OPMODE(vap->iv_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED ||
1635 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) {
1636 vap->iv_curhtprotmode = protmode;
1637 /* Update VAP with new protection mode */
1638 ieee80211_vap_update_ht_protmode(vap);
1639 }
1640 IEEE80211_UNLOCK(ic);
1641 #undef OPMODE
1642 }
1643
1644 /*
1645 * Time out presence of an overlapping bss with non-HT
1646 * stations. When operating in hostap mode we listen for
1647 * beacons from other stations and if we identify a non-HT
1648 * station is present we update the opmode field of the
1649 * HTINFO ie. To identify when all non-HT stations are
1650 * gone we time out this condition.
1651 */
1652 void
ieee80211_ht_timeout(struct ieee80211vap * vap)1653 ieee80211_ht_timeout(struct ieee80211vap *vap)
1654 {
1655
1656 IEEE80211_LOCK_ASSERT(vap->iv_ic);
1657
1658 if ((vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) &&
1659 ieee80211_time_after(ticks, vap->iv_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
1660 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
1661 "%s", "time out non-HT STA present on channel");
1662 vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
1663 htinfo_update(vap);
1664 }
1665 }
1666
1667 /*
1668 * Process an 802.11n HT capabilities ie.
1669 */
1670 void
ieee80211_parse_htcap(struct ieee80211_node * ni,const uint8_t * ie)1671 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1672 {
1673 if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1674 /*
1675 * Station used Vendor OUI ie to associate;
1676 * mark the node so when we respond we'll use
1677 * the Vendor OUI's and not the standard ie's.
1678 */
1679 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1680 ie += 4;
1681 } else
1682 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1683
1684 ni->ni_htcap = le16dec(ie +
1685 __offsetof(struct ieee80211_ie_htcap, hc_cap));
1686 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1687 }
1688
1689 static void
htinfo_parse(struct ieee80211_node * ni,const struct ieee80211_ie_htinfo * htinfo)1690 htinfo_parse(struct ieee80211_node *ni,
1691 const struct ieee80211_ie_htinfo *htinfo)
1692 {
1693 uint16_t w;
1694
1695 ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1696 ni->ni_ht2ndchan = _IEEE80211_SHIFTMASK(htinfo->hi_byte1,
1697 IEEE80211_HTINFO_2NDCHAN);
1698 w = le16dec(&htinfo->hi_byte2);
1699 ni->ni_htopmode = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_OPMODE);
1700 w = le16dec(&htinfo->hi_byte45);
1701 ni->ni_htstbc = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1702 }
1703
1704 /*
1705 * Parse an 802.11n HT info ie and save useful information
1706 * to the node state. Note this does not effect any state
1707 * changes such as for channel width change.
1708 */
1709 void
ieee80211_parse_htinfo(struct ieee80211_node * ni,const uint8_t * ie)1710 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1711 {
1712 if (ie[0] == IEEE80211_ELEMID_VENDOR)
1713 ie += 4;
1714 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie);
1715 }
1716
1717 /*
1718 * Handle 11n/11ac channel switch.
1719 *
1720 * Use the received HT/VHT ie's to identify the right channel to use.
1721 * If we cannot locate it in the channel table then fallback to
1722 * legacy operation.
1723 *
1724 * Note that we use this information to identify the node's
1725 * channel only; the caller is responsible for insuring any
1726 * required channel change is done (e.g. in sta mode when
1727 * parsing the contents of a beacon frame).
1728 */
1729 static int
htinfo_update_chw(struct ieee80211_node * ni,int htflags,int vhtflags)1730 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags)
1731 {
1732 struct ieee80211com *ic = ni->ni_ic;
1733 struct ieee80211_channel *c;
1734 int chanflags;
1735 int ret = 0;
1736
1737 /*
1738 * First step - do HT/VHT only channel lookup based on operating mode
1739 * flags. This involves masking out the VHT flags as well.
1740 * Otherwise we end up doing the full channel walk each time
1741 * we trigger this, which is expensive.
1742 */
1743 chanflags = (ni->ni_chan->ic_flags &~
1744 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags;
1745
1746 if (chanflags == ni->ni_chan->ic_flags)
1747 goto done;
1748
1749 /*
1750 * If HT /or/ VHT flags have changed then check both.
1751 * We need to start by picking a HT channel anyway.
1752 */
1753
1754 c = NULL;
1755 chanflags = (ni->ni_chan->ic_flags &~
1756 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags;
1757 /* XXX not right for ht40- */
1758 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1759 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) {
1760 /*
1761 * No HT40 channel entry in our table; fall back
1762 * to HT20 operation. This should not happen.
1763 */
1764 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1765 #if 0
1766 IEEE80211_NOTE(ni->ni_vap,
1767 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1768 "no HT40 channel (freq %u), falling back to HT20",
1769 ni->ni_chan->ic_freq);
1770 #endif
1771 /* XXX stat */
1772 }
1773
1774 /* Nothing found - leave it alone; move onto VHT */
1775 if (c == NULL)
1776 c = ni->ni_chan;
1777
1778 /*
1779 * If it's non-HT, then bail out now.
1780 */
1781 if (! IEEE80211_IS_CHAN_HT(c)) {
1782 IEEE80211_NOTE(ni->ni_vap,
1783 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1784 "not HT; skipping VHT check (%u/0x%x)",
1785 c->ic_freq, c->ic_flags);
1786 goto done;
1787 }
1788
1789 /*
1790 * Next step - look at the current VHT flags and determine
1791 * if we need to upgrade. Mask out the VHT and HT flags since
1792 * the vhtflags field will already have the correct HT
1793 * flags to use.
1794 */
1795 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) {
1796 chanflags = (c->ic_flags
1797 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT))
1798 | vhtflags;
1799 IEEE80211_NOTE(ni->ni_vap,
1800 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1801 ni,
1802 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x",
1803 __func__, ni->ni_vht_chanwidth, vhtflags);
1804
1805 IEEE80211_NOTE(ni->ni_vap,
1806 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
1807 ni,
1808 "%s: VHT; trying lookup for %d/0x%08x",
1809 __func__, c->ic_freq, chanflags);
1810 c = ieee80211_find_channel(ic, c->ic_freq, chanflags);
1811 }
1812
1813 /* Finally, if it's changed */
1814 if (c != NULL && c != ni->ni_chan) {
1815 IEEE80211_NOTE(ni->ni_vap,
1816 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1817 "switch station to %s%d channel %u/0x%x",
1818 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT",
1819 IEEE80211_IS_CHAN_VHT80(c) ? 80 :
1820 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20),
1821 c->ic_freq, c->ic_flags);
1822 ni->ni_chan = c;
1823 ret = 1;
1824 }
1825 /* NB: caller responsible for forcing any channel change */
1826
1827 done:
1828 /* update node's (11n) tx channel width */
1829 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
1830 IEEE80211_STA_RX_BW_40 : IEEE80211_STA_RX_BW_20;
1831 return (ret);
1832 }
1833
1834 /*
1835 * Update 11n MIMO PS state according to received htcap.
1836 */
1837 static __inline int
htcap_update_mimo_ps(struct ieee80211_node * ni)1838 htcap_update_mimo_ps(struct ieee80211_node *ni)
1839 {
1840 uint16_t oflags = ni->ni_flags;
1841
1842 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) {
1843 case IEEE80211_HTCAP_SMPS_DYNAMIC:
1844 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1845 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
1846 break;
1847 case IEEE80211_HTCAP_SMPS_ENA:
1848 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
1849 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1850 break;
1851 case IEEE80211_HTCAP_SMPS_OFF:
1852 default: /* disable on rx of reserved value */
1853 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
1854 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
1855 break;
1856 }
1857 return (oflags ^ ni->ni_flags);
1858 }
1859
1860 /*
1861 * Update short GI state according to received htcap
1862 * and local settings.
1863 */
1864 static __inline void
htcap_update_shortgi(struct ieee80211_node * ni)1865 htcap_update_shortgi(struct ieee80211_node *ni)
1866 {
1867 struct ieee80211vap *vap = ni->ni_vap;
1868
1869 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40);
1870 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
1871 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20))
1872 ni->ni_flags |= IEEE80211_NODE_SGI20;
1873 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
1874 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40))
1875 ni->ni_flags |= IEEE80211_NODE_SGI40;
1876 }
1877
1878 /*
1879 * Update LDPC state according to received htcap
1880 * and local settings.
1881 */
1882 static __inline void
htcap_update_ldpc(struct ieee80211_node * ni)1883 htcap_update_ldpc(struct ieee80211_node *ni)
1884 {
1885 struct ieee80211vap *vap = ni->ni_vap;
1886
1887 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) &&
1888 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX))
1889 ni->ni_flags |= IEEE80211_NODE_LDPC;
1890 }
1891
1892 /*
1893 * Parse and update HT-related state extracted from
1894 * the HT cap and info ie's.
1895 *
1896 * This is called from the STA management path and
1897 * the ieee80211_node_join() path. It will take into
1898 * account the IEs discovered during scanning and
1899 * adjust things accordingly.
1900 */
1901 void
ieee80211_ht_updateparams(struct ieee80211_node * ni,const uint8_t * htcapie,const uint8_t * htinfoie)1902 ieee80211_ht_updateparams(struct ieee80211_node *ni,
1903 const uint8_t *htcapie, const uint8_t *htinfoie)
1904 {
1905 struct ieee80211vap *vap = ni->ni_vap;
1906 const struct ieee80211_ie_htinfo *htinfo;
1907
1908 ieee80211_parse_htcap(ni, htcapie);
1909 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
1910 htcap_update_mimo_ps(ni);
1911 htcap_update_shortgi(ni);
1912 htcap_update_ldpc(ni);
1913
1914 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR)
1915 htinfoie += 4;
1916 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
1917 htinfo_parse(ni, htinfo);
1918
1919 /*
1920 * Defer the node channel change; we need to now
1921 * update VHT parameters before we do it.
1922 */
1923
1924 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) &&
1925 (vap->iv_flags_ht & IEEE80211_FHT_RIFS))
1926 ni->ni_flags |= IEEE80211_NODE_RIFS;
1927 else
1928 ni->ni_flags &= ~IEEE80211_NODE_RIFS;
1929 }
1930
1931 static uint32_t
ieee80211_vht_get_vhtflags(struct ieee80211_node * ni,uint32_t htflags)1932 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags)
1933 {
1934 #define _RETURN_CHAN_BITS(_cb) \
1935 do { \
1936 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, \
1937 "%s:%d: selected %b", __func__, __LINE__, \
1938 (_cb), IEEE80211_CHAN_BITS); \
1939 return (_cb); \
1940 } while(0)
1941 struct ieee80211vap *vap;
1942 const struct ieee80211_ie_htinfo *htinfo;
1943 uint32_t vhtflags;
1944 bool can_vht160, can_vht80p80, can_vht80;
1945 bool ht40;
1946
1947 vap = ni->ni_vap;
1948
1949 /* If we do not support VHT or VHT is disabled just return. */
1950 if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
1951 (vap->iv_vht_flags & IEEE80211_FVHT_VHT) == 0)
1952 _RETURN_CHAN_BITS(0);
1953
1954 /*
1955 * The original code was based on
1956 * 802.11ac-2013, Table 8-183x-VHT Operation Information subfields.
1957 * 802.11-2020, Table 9-274-VHT Operation Information subfields
1958 * has IEEE80211_VHT_CHANWIDTH_160MHZ and
1959 * IEEE80211_VHT_CHANWIDTH_80P80MHZ deprecated.
1960 * For current logic see
1961 * 802.11-2020, 11.38.1 Basic VHT BSS functionality.
1962 */
1963
1964 htinfo = (const struct ieee80211_ie_htinfo *)ni->ni_ies.htinfo_ie;
1965 ht40 = ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH) ==
1966 IEEE80211_HTINFO_TXWIDTH_2040);
1967 can_vht160 = can_vht80p80 = can_vht80 = false;
1968
1969 /* 20 Mhz */
1970 if (!ht40) {
1971 /* Check for the full valid combination -- other fields be 0. */
1972 if (ni->ni_vht_chanwidth != IEEE80211_VHT_CHANWIDTH_USE_HT ||
1973 ni->ni_vht_chan2 != 0)
1974 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
1975 "%s: invalid VHT BSS bandwidth 0/%d/%d/%d",
1976 __func__, ni->ni_vht_chanwidth,
1977 ni->ni_vht_chan1, ni->ni_vht_chan2);
1978
1979 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT20 | IEEE80211_CHAN_HT20);
1980 }
1981
1982 vhtflags = 0;
1983
1984 /* We know we can at least do 40Mhz, so mirror the HT40 flags. */
1985 if (htflags == IEEE80211_CHAN_HT40U)
1986 vhtflags |= IEEE80211_CHAN_HT40U;
1987 else if (htflags == IEEE80211_CHAN_HT40D)
1988 vhtflags |= IEEE80211_CHAN_HT40D;
1989
1990 /* 40 MHz */
1991 if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) {
1992 if (ni->ni_vht_chan2 != 0)
1993 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
1994 "%s: invalid VHT BSS bandwidth 1/%d/%d/%d",
1995 __func__, ni->ni_vht_chanwidth,
1996 ni->ni_vht_chan1, ni->ni_vht_chan2);
1997
1998 if ((vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40) != 0) {
1999 if (htflags == IEEE80211_CHAN_HT40U)
2000 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT40U | vhtflags);
2001 if (htflags == IEEE80211_CHAN_HT40D)
2002 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT40D | vhtflags);
2003 }
2004
2005 /* If we get here VHT40 is not supported or disabled. */
2006 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT20 | IEEE80211_CHAN_HT20);
2007 }
2008
2009 /* Deprecated check for 160. */
2010 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) &&
2011 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vht_cap.vht_cap_info) &&
2012 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) != 0)
2013 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT160 | vhtflags);
2014
2015 /* Deprecated check for 80P80. */
2016 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) &&
2017 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vht_cap.vht_cap_info) &&
2018 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) != 0)
2019 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT80P80 | vhtflags);
2020
2021 if (ni->ni_vht_chanwidth != IEEE80211_VHT_CHANWIDTH_80MHZ) {
2022 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni,
2023 "%s: invalid VHT BSS bandwidth %d/%d/%d", __func__,
2024 ni->ni_vht_chanwidth, ni->ni_vht_chan2);
2025
2026 _RETURN_CHAN_BITS(0);
2027 }
2028
2029 /* CCFS1 > 0 and | CCFS1 - CCFS0 | = 8 */
2030 if (ni->ni_vht_chan2 > 0 && (ni->ni_vht_chan2 - ni->ni_vht_chan1) == 8)
2031 can_vht160 = can_vht80 = true;
2032
2033 /* CCFS1 > 0 and | CCFS1 - CCFS0 | > 16 */
2034 if (ni->ni_vht_chan2 > 0 && (ni->ni_vht_chan2 - ni->ni_vht_chan1) > 16)
2035 can_vht80p80 = can_vht80 = true;
2036
2037 /* CFFS1 == 0 */
2038 if (ni->ni_vht_chan2 == 0)
2039 can_vht80 = true;
2040
2041 if (can_vht160 && (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160) != 0)
2042 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT160 | vhtflags);
2043
2044 if (can_vht80p80 && (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80) != 0)
2045 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT80P80 | vhtflags);
2046
2047 if (can_vht80 && (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80) != 0)
2048 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT80 | vhtflags);
2049
2050 if (ht40 && (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40) != 0) {
2051 if (htflags == IEEE80211_CHAN_HT40U)
2052 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT40U | vhtflags);
2053 if (htflags == IEEE80211_CHAN_HT40D)
2054 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT40D | vhtflags);
2055 }
2056
2057 /* Either we disabled support or got an invalid setting. */
2058 _RETURN_CHAN_BITS(IEEE80211_CHAN_VHT20 | IEEE80211_CHAN_HT20);
2059 #undef _RETURN_CHAN_BITS
2060 }
2061
2062 /*
2063 * Final part of updating the HT parameters.
2064 *
2065 * This is called from the STA management path and
2066 * the ieee80211_node_join() path. It will take into
2067 * account the IEs discovered during scanning and
2068 * adjust things accordingly.
2069 *
2070 * This is done after a call to ieee80211_ht_updateparams()
2071 * because it (and the upcoming VHT version of updateparams)
2072 * needs to ensure everything is parsed before htinfo_update_chw()
2073 * is called - which will change the channel config for the
2074 * node for us.
2075 */
2076 int
ieee80211_ht_updateparams_final(struct ieee80211_node * ni,const uint8_t * htcapie,const uint8_t * htinfoie)2077 ieee80211_ht_updateparams_final(struct ieee80211_node *ni,
2078 const uint8_t *htcapie, const uint8_t *htinfoie)
2079 {
2080 struct ieee80211vap *vap = ni->ni_vap;
2081 const struct ieee80211_ie_htinfo *htinfo;
2082 int htflags, vhtflags;
2083 int ret = 0;
2084
2085 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie;
2086
2087 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2088 IEEE80211_CHAN_HT20 : 0;
2089
2090 /* NB: honor operating mode constraint */
2091 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
2092 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2093 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
2094 htflags = IEEE80211_CHAN_HT40U;
2095 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
2096 htflags = IEEE80211_CHAN_HT40D;
2097 }
2098
2099 /*
2100 * VHT flags - do much the same; check whether VHT is available
2101 * and if so, what our ideal channel use would be based on our
2102 * capabilities and the (pre-parsed) VHT info IE.
2103 */
2104 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2105
2106 if (htinfo_update_chw(ni, htflags, vhtflags))
2107 ret = 1;
2108
2109 return (ret);
2110 }
2111
2112 /*
2113 * Parse and update HT-related state extracted from the HT cap ie
2114 * for a station joining an HT BSS.
2115 *
2116 * This is called from the hostap path for each station.
2117 */
2118 void
ieee80211_ht_updatehtcap(struct ieee80211_node * ni,const uint8_t * htcapie)2119 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie)
2120 {
2121 struct ieee80211vap *vap = ni->ni_vap;
2122
2123 ieee80211_parse_htcap(ni, htcapie);
2124 if (vap->iv_htcaps & IEEE80211_HTC_SMPS)
2125 htcap_update_mimo_ps(ni);
2126 htcap_update_shortgi(ni);
2127 htcap_update_ldpc(ni);
2128 }
2129
2130 /*
2131 * Called once HT and VHT capabilities are parsed in hostap mode -
2132 * this will adjust the channel configuration of the given node
2133 * based on the configuration and capabilities.
2134 */
2135 void
ieee80211_ht_updatehtcap_final(struct ieee80211_node * ni)2136 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni)
2137 {
2138 struct ieee80211vap *vap = ni->ni_vap;
2139 int htflags;
2140 int vhtflags;
2141
2142 /* NB: honor operating mode constraint */
2143 /* XXX 40 MHz intolerant */
2144 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ?
2145 IEEE80211_CHAN_HT20 : 0;
2146 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
2147 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) {
2148 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan))
2149 htflags = IEEE80211_CHAN_HT40U;
2150 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan))
2151 htflags = IEEE80211_CHAN_HT40D;
2152 }
2153 /*
2154 * VHT flags - do much the same; check whether VHT is available
2155 * and if so, what our ideal channel use would be based on our
2156 * capabilities and the (pre-parsed) VHT info IE.
2157 */
2158 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags);
2159
2160 (void) htinfo_update_chw(ni, htflags, vhtflags);
2161 }
2162
2163 /*
2164 * Install received HT rate set by parsing the HT cap ie.
2165 */
2166 int
ieee80211_setup_htrates(struct ieee80211_node * ni,const uint8_t * ie,int flags)2167 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
2168 {
2169 struct ieee80211com *ic = ni->ni_ic;
2170 struct ieee80211vap *vap = ni->ni_vap;
2171 const struct ieee80211_ie_htcap *htcap;
2172 struct ieee80211_htrateset *rs;
2173 int i, maxequalmcs, maxunequalmcs;
2174
2175 maxequalmcs = ic->ic_txstream * 8 - 1;
2176 maxunequalmcs = 0;
2177 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) {
2178 if (ic->ic_txstream >= 2)
2179 maxunequalmcs = 38;
2180 if (ic->ic_txstream >= 3)
2181 maxunequalmcs = 52;
2182 if (ic->ic_txstream >= 4)
2183 maxunequalmcs = 76;
2184 }
2185
2186 rs = &ni->ni_htrates;
2187 memset(rs, 0, sizeof(*rs));
2188 if (ie != NULL) {
2189 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2190 ie += 4;
2191 htcap = (const struct ieee80211_ie_htcap *) ie;
2192 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2193 if (isclr(htcap->hc_mcsset, i))
2194 continue;
2195 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
2196 IEEE80211_NOTE(vap,
2197 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2198 "WARNING, HT rate set too large; only "
2199 "using %u rates", IEEE80211_HTRATE_MAXSIZE);
2200 vap->iv_stats.is_rx_rstoobig++;
2201 break;
2202 }
2203 if (i <= 31 && i > maxequalmcs)
2204 continue;
2205 if (i == 32 &&
2206 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
2207 continue;
2208 if (i > 32 && i > maxunequalmcs)
2209 continue;
2210 rs->rs_rates[rs->rs_nrates++] = i;
2211 }
2212 }
2213 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
2214 }
2215
2216 /*
2217 * Mark rates in a node's HT rate set as basic according
2218 * to the information in the supplied HT info ie.
2219 */
2220 void
ieee80211_setup_basic_htrates(struct ieee80211_node * ni,const uint8_t * ie)2221 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
2222 {
2223 const struct ieee80211_ie_htinfo *htinfo;
2224 struct ieee80211_htrateset *rs;
2225 int i, j;
2226
2227 if (ie[0] == IEEE80211_ELEMID_VENDOR)
2228 ie += 4;
2229 htinfo = (const struct ieee80211_ie_htinfo *) ie;
2230 rs = &ni->ni_htrates;
2231 if (rs->rs_nrates == 0) {
2232 IEEE80211_NOTE(ni->ni_vap,
2233 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
2234 "%s", "WARNING, empty HT rate set");
2235 return;
2236 }
2237 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
2238 if (isclr(htinfo->hi_basicmcsset, i))
2239 continue;
2240 for (j = 0; j < rs->rs_nrates; j++)
2241 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
2242 rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
2243 }
2244 }
2245
2246 static void
ampdu_tx_setup(struct ieee80211_tx_ampdu * tap)2247 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap)
2248 {
2249 callout_init(&tap->txa_timer, 1);
2250 tap->txa_flags |= IEEE80211_AGGR_SETUP;
2251 tap->txa_lastsample = ticks;
2252 }
2253
2254 static void
ampdu_tx_stop(struct ieee80211_tx_ampdu * tap)2255 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap)
2256 {
2257 struct ieee80211_node *ni = tap->txa_ni;
2258 struct ieee80211com *ic = ni->ni_ic;
2259
2260 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2261 tap->txa_ni,
2262 "%s: called",
2263 __func__);
2264
2265 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP,
2266 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid,
2267 TID_TO_WME_AC(tap->txa_tid)));
2268
2269 /*
2270 * Stop BA stream if setup so driver has a chance
2271 * to reclaim any resources it might have allocated.
2272 */
2273 ic->ic_addba_stop(ni, tap);
2274 /*
2275 * Stop any pending BAR transmit.
2276 */
2277 bar_stop_timer(tap);
2278
2279 /*
2280 * Reset packet estimate.
2281 */
2282 ieee80211_txampdu_init_pps(tap);
2283
2284 /* NB: clearing NAK means we may re-send ADDBA */
2285 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
2286 }
2287
2288 /*
2289 * ADDBA response timeout.
2290 *
2291 * If software aggregation and per-TID queue management was done here,
2292 * that queue would be unpaused after the ADDBA timeout occurs.
2293 */
2294 static void
addba_timeout(void * arg)2295 addba_timeout(void *arg)
2296 {
2297 struct ieee80211_tx_ampdu *tap = arg;
2298 struct ieee80211_node *ni = tap->txa_ni;
2299 struct ieee80211com *ic = ni->ni_ic;
2300
2301 /* XXX ? */
2302 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2303 tap->txa_attempts++;
2304 ic->ic_addba_response_timeout(ni, tap);
2305 }
2306
2307 static void
addba_start_timeout(struct ieee80211_tx_ampdu * tap)2308 addba_start_timeout(struct ieee80211_tx_ampdu *tap)
2309 {
2310 /* XXX use CALLOUT_PENDING instead? */
2311 callout_reset(&tap->txa_timer, ieee80211_addba_timeout,
2312 addba_timeout, tap);
2313 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
2314 tap->txa_nextrequest = ticks + ieee80211_addba_timeout;
2315 }
2316
2317 static void
addba_stop_timeout(struct ieee80211_tx_ampdu * tap)2318 addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
2319 {
2320 /* XXX use CALLOUT_PENDING instead? */
2321 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
2322 callout_stop(&tap->txa_timer);
2323 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
2324 }
2325 }
2326
2327 static void
null_addba_response_timeout(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)2328 null_addba_response_timeout(struct ieee80211_node *ni,
2329 struct ieee80211_tx_ampdu *tap)
2330 {
2331 }
2332
2333 /*
2334 * Default method for requesting A-MPDU tx aggregation.
2335 * We setup the specified state block and start a timer
2336 * to wait for an ADDBA response frame.
2337 */
2338 static int
ieee80211_addba_request(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int dialogtoken,int baparamset,int batimeout)2339 ieee80211_addba_request(struct ieee80211_node *ni,
2340 struct ieee80211_tx_ampdu *tap,
2341 int dialogtoken, int baparamset, int batimeout)
2342 {
2343 int bufsiz;
2344
2345 /* XXX locking */
2346 tap->txa_token = dialogtoken;
2347 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
2348 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2349 tap->txa_wnd = (bufsiz == 0) ?
2350 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2351 addba_start_timeout(tap);
2352 return 1;
2353 }
2354
2355 /*
2356 * Called by drivers that wish to request an ADDBA session be
2357 * setup. This brings it up and starts the request timer.
2358 */
2359 int
ieee80211_ampdu_tx_request_ext(struct ieee80211_node * ni,int tid)2360 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid)
2361 {
2362 struct ieee80211_tx_ampdu *tap;
2363
2364 if (tid < 0 || tid > 15)
2365 return (0);
2366 tap = &ni->ni_tx_ampdu[tid];
2367
2368 /* XXX locking */
2369 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2370 /* do deferred setup of state */
2371 ampdu_tx_setup(tap);
2372 }
2373 /* XXX hack for not doing proper locking */
2374 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2375 addba_start_timeout(tap);
2376 return (1);
2377 }
2378
2379 /*
2380 * Called by drivers that have marked a session as active.
2381 */
2382 int
ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node * ni,int tid,int status)2383 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid,
2384 int status)
2385 {
2386 struct ieee80211_tx_ampdu *tap;
2387
2388 if (tid < 0 || tid > 15)
2389 return (0);
2390 tap = &ni->ni_tx_ampdu[tid];
2391
2392 /* XXX locking */
2393 addba_stop_timeout(tap);
2394 if (status == 1) {
2395 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2396 tap->txa_attempts = 0;
2397 } else {
2398 /* mark tid so we don't try again */
2399 tap->txa_flags |= IEEE80211_AGGR_NAK;
2400 }
2401 return (1);
2402 }
2403
2404 /*
2405 * Default method for processing an A-MPDU tx aggregation
2406 * response. We shutdown any pending timer and update the
2407 * state block according to the reply.
2408 */
2409 static int
ieee80211_addba_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status,int baparamset,int batimeout)2410 ieee80211_addba_response(struct ieee80211_node *ni,
2411 struct ieee80211_tx_ampdu *tap,
2412 int status, int baparamset, int batimeout)
2413 {
2414 struct ieee80211vap *vap = ni->ni_vap;
2415 int bufsiz;
2416
2417 /* XXX locking */
2418 addba_stop_timeout(tap);
2419 if (status == IEEE80211_STATUS_SUCCESS) {
2420 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2421 /* XXX override our request? */
2422 tap->txa_wnd = (bufsiz == 0) ?
2423 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
2424 #ifdef __notyet__
2425 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2426 #endif
2427 tap->txa_flags |= IEEE80211_AGGR_RUNNING;
2428 tap->txa_attempts = 0;
2429 /* TODO: this should be a vap flag */
2430 if ((vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU) &&
2431 (ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2432 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
2433 tap->txa_flags |= IEEE80211_AGGR_AMSDU;
2434 else
2435 tap->txa_flags &= ~IEEE80211_AGGR_AMSDU;
2436 } else {
2437 /* mark tid so we don't try again */
2438 tap->txa_flags |= IEEE80211_AGGR_NAK;
2439 }
2440 return 1;
2441 }
2442
2443 /*
2444 * Default method for stopping A-MPDU tx aggregation.
2445 * Any timer is cleared and we drain any pending frames.
2446 */
2447 static void
ieee80211_addba_stop(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)2448 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
2449 {
2450 /* XXX locking */
2451 addba_stop_timeout(tap);
2452 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
2453 /* XXX clear aggregation queue */
2454 tap->txa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_AMSDU);
2455 }
2456 tap->txa_attempts = 0;
2457 }
2458
2459 /*
2460 * Process a received action frame using the default aggregation
2461 * policy. We intercept ADDBA-related frames and use them to
2462 * update our aggregation state. All other frames are passed up
2463 * for processing by ieee80211_recv_action.
2464 */
2465 static int
ht_recv_action_ba_addba_request(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2466 ht_recv_action_ba_addba_request(struct ieee80211_node *ni,
2467 const struct ieee80211_frame *wh,
2468 const uint8_t *frm, const uint8_t *efrm)
2469 {
2470 struct ieee80211com *ic = ni->ni_ic;
2471 struct ieee80211vap *vap = ni->ni_vap;
2472 struct ieee80211_rx_ampdu *rap;
2473 uint8_t dialogtoken;
2474 uint16_t baparamset, batimeout, baseqctl;
2475 uint16_t args[5];
2476 int tid;
2477
2478 dialogtoken = frm[2];
2479 baparamset = le16dec(frm+3);
2480 batimeout = le16dec(frm+5);
2481 baseqctl = le16dec(frm+7);
2482
2483 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2484
2485 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2486 "recv ADDBA request: dialogtoken %u baparamset 0x%x "
2487 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d amsdu %d",
2488 dialogtoken, baparamset,
2489 tid, _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ),
2490 batimeout,
2491 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START),
2492 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_FRAG),
2493 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU));
2494
2495 rap = &ni->ni_rx_ampdu[tid];
2496
2497 /* Send ADDBA response */
2498 args[0] = dialogtoken;
2499 /*
2500 * NB: We ack only if the sta associated with HT and
2501 * the ap is configured to do AMPDU rx (the latter
2502 * violates the 11n spec and is mostly for testing).
2503 */
2504 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
2505 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) {
2506 /* XXX TODO: handle ampdu_rx_start failure */
2507 ic->ic_ampdu_rx_start(ni, rap,
2508 baparamset, batimeout, baseqctl);
2509
2510 args[1] = IEEE80211_STATUS_SUCCESS;
2511 } else {
2512 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2513 ni, "reject ADDBA request: %s",
2514 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
2515 "administratively disabled" :
2516 "not negotiated for station");
2517 vap->iv_stats.is_addba_reject++;
2518 args[1] = IEEE80211_STATUS_UNSPECIFIED;
2519 }
2520 /* XXX honor rap flags? */
2521 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2522 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2523 | _IEEE80211_SHIFTMASK(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
2524 ;
2525
2526 /*
2527 * TODO: we're out of iv_flags_ht fields; once
2528 * this is extended we should make this configurable.
2529 */
2530 if ((baparamset & IEEE80211_BAPS_AMSDU) &&
2531 (ni->ni_flags & IEEE80211_NODE_AMSDU_RX) &&
2532 (vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU))
2533 args[2] |= IEEE80211_BAPS_AMSDU;
2534
2535 args[3] = 0;
2536 args[4] = 0;
2537 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2538 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
2539 return 0;
2540 }
2541
2542 static int
ht_recv_action_ba_addba_response(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2543 ht_recv_action_ba_addba_response(struct ieee80211_node *ni,
2544 const struct ieee80211_frame *wh,
2545 const uint8_t *frm, const uint8_t *efrm)
2546 {
2547 struct ieee80211com *ic = ni->ni_ic;
2548 struct ieee80211vap *vap = ni->ni_vap;
2549 struct ieee80211_tx_ampdu *tap;
2550 uint8_t dialogtoken, policy;
2551 uint16_t baparamset, batimeout, code;
2552 int tid;
2553 #ifdef IEEE80211_DEBUG
2554 int amsdu, bufsiz;
2555 #endif
2556
2557 dialogtoken = frm[2];
2558 code = le16dec(frm+3);
2559 baparamset = le16dec(frm+5);
2560 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID);
2561 #ifdef IEEE80211_DEBUG
2562 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ);
2563 amsdu = !! _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU);
2564 #endif
2565 policy = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_POLICY);
2566 batimeout = le16dec(frm+7);
2567
2568 tap = &ni->ni_tx_ampdu[tid];
2569 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
2570 IEEE80211_DISCARD_MAC(vap,
2571 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2572 ni->ni_macaddr, "ADDBA response",
2573 "no pending ADDBA, tid %d dialogtoken %u "
2574 "code %d", tid, dialogtoken, code);
2575 vap->iv_stats.is_addba_norequest++;
2576 return 0;
2577 }
2578 if (dialogtoken != tap->txa_token) {
2579 IEEE80211_DISCARD_MAC(vap,
2580 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2581 ni->ni_macaddr, "ADDBA response",
2582 "dialogtoken mismatch: waiting for %d, "
2583 "received %d, tid %d code %d",
2584 tap->txa_token, dialogtoken, tid, code);
2585 vap->iv_stats.is_addba_badtoken++;
2586 return 0;
2587 }
2588 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */
2589 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) {
2590 IEEE80211_DISCARD_MAC(vap,
2591 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2592 ni->ni_macaddr, "ADDBA response",
2593 "policy mismatch: expecting %s, "
2594 "received %s, tid %d code %d",
2595 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE,
2596 policy, tid, code);
2597 vap->iv_stats.is_addba_badpolicy++;
2598 return 0;
2599 }
2600 #if 0
2601 /* XXX we take MIN in ieee80211_addba_response */
2602 if (bufsiz > IEEE80211_AGGR_BAWMAX) {
2603 IEEE80211_DISCARD_MAC(vap,
2604 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2605 ni->ni_macaddr, "ADDBA response",
2606 "BA window too large: max %d, "
2607 "received %d, tid %d code %d",
2608 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code);
2609 vap->iv_stats.is_addba_badbawinsize++;
2610 return 0;
2611 }
2612 #endif
2613
2614 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2615 "recv ADDBA response: dialogtoken %u code %d "
2616 "baparamset 0x%x (tid %d bufsiz %d amsdu %d) batimeout %d",
2617 dialogtoken, code, baparamset, tid,
2618 bufsiz,
2619 amsdu,
2620 batimeout);
2621 ic->ic_addba_response(ni, tap, code, baparamset, batimeout);
2622 return 0;
2623 }
2624
2625 static int
ht_recv_action_ba_delba(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2626 ht_recv_action_ba_delba(struct ieee80211_node *ni,
2627 const struct ieee80211_frame *wh,
2628 const uint8_t *frm, const uint8_t *efrm)
2629 {
2630 struct ieee80211com *ic = ni->ni_ic;
2631 struct ieee80211_rx_ampdu *rap;
2632 struct ieee80211_tx_ampdu *tap;
2633 uint16_t baparamset;
2634 #ifdef IEEE80211_DEBUG
2635 uint16_t code;
2636 #endif
2637 int tid;
2638
2639 baparamset = le16dec(frm+2);
2640 #ifdef IEEE80211_DEBUG
2641 code = le16dec(frm+4);
2642 #endif
2643
2644 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_TID);
2645
2646 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2647 "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
2648 "code %d", baparamset, tid,
2649 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_INIT), code);
2650
2651 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
2652 tap = &ni->ni_tx_ampdu[tid];
2653 ic->ic_addba_stop(ni, tap);
2654 } else {
2655 rap = &ni->ni_rx_ampdu[tid];
2656 ic->ic_ampdu_rx_stop(ni, rap);
2657 }
2658 return 0;
2659 }
2660
2661 /*
2662 * Handle the HT channel width action frame.
2663 *
2664 * 802.11-2020 9.6.11.2 (Notify Channel Width frame format).
2665 */
2666 static int
ht_recv_action_ht_txchwidth(struct ieee80211_node * ni,const struct ieee80211_frame * wh __unused,const uint8_t * frm,const uint8_t * efrm __unused)2667 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni,
2668 const struct ieee80211_frame *wh __unused,
2669 const uint8_t *frm, const uint8_t *efrm __unused)
2670 {
2671 int chw;
2672
2673 /* If 20/40 is not supported the chw cannot change. */
2674 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) == 0)
2675 return (0);
2676
2677 /*
2678 * The supported values are either 0 (any supported width)
2679 * or 1 (HT20). 80, 160, etc MHz widths are not represented
2680 * here.
2681 */
2682 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ?
2683 IEEE80211_STA_RX_BW_40 : IEEE80211_STA_RX_BW_20;
2684
2685 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2686 "%s: HT txchwidth, width %d%s (%s)", __func__,
2687 chw, ni->ni_chw != chw ? "*" : "", ieee80211_ni_chw_to_str(chw));
2688 if (chw != ni->ni_chw) {
2689 /* XXX does this need to change the ht40 station count? */
2690 ni->ni_chw = chw;
2691 /* XXX notify on change */
2692 }
2693 return 0;
2694 }
2695
2696 static int
ht_recv_action_ht_mimopwrsave(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2697 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni,
2698 const struct ieee80211_frame *wh,
2699 const uint8_t *frm, const uint8_t *efrm)
2700 {
2701 const struct ieee80211_action_ht_mimopowersave *mps =
2702 (const struct ieee80211_action_ht_mimopowersave *) frm;
2703
2704 /* XXX check iv_htcaps */
2705 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA)
2706 ni->ni_flags |= IEEE80211_NODE_MIMO_PS;
2707 else
2708 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS;
2709 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE)
2710 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS;
2711 else
2712 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS;
2713 /* XXX notify on change */
2714 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
2715 "%s: HT MIMO PS (%s%s)", __func__,
2716 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off",
2717 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : ""
2718 );
2719 return 0;
2720 }
2721
2722 /*
2723 * Transmit processing.
2724 */
2725
2726 /*
2727 * Check if A-MPDU should be requested/enabled for a stream.
2728 * We require a traffic rate above a per-AC threshold and we
2729 * also handle backoff from previous failed attempts.
2730 *
2731 * Drivers may override this method to bring in information
2732 * such as link state conditions in making the decision.
2733 */
2734 static int
ieee80211_ampdu_enable(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)2735 ieee80211_ampdu_enable(struct ieee80211_node *ni,
2736 struct ieee80211_tx_ampdu *tap)
2737 {
2738 struct ieee80211vap *vap = ni->ni_vap;
2739
2740 if (tap->txa_avgpps <
2741 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)])
2742 return 0;
2743 /* XXX check rssi? */
2744 if (tap->txa_attempts >= ieee80211_addba_maxtries &&
2745 ieee80211_time_after(ticks, tap->txa_nextrequest)) {
2746 /*
2747 * Don't retry too often; txa_nextrequest is set
2748 * to the minimum interval we'll retry after
2749 * ieee80211_addba_maxtries failed attempts are made.
2750 */
2751 return 0;
2752 }
2753 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni,
2754 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d",
2755 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)],
2756 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts);
2757 return 1;
2758 }
2759
2760 /*
2761 * Request A-MPDU tx aggregation. Setup local state and
2762 * issue an ADDBA request. BA use will only happen after
2763 * the other end replies with ADDBA response.
2764 */
2765 int
ieee80211_ampdu_request(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)2766 ieee80211_ampdu_request(struct ieee80211_node *ni,
2767 struct ieee80211_tx_ampdu *tap)
2768 {
2769 struct ieee80211com *ic = ni->ni_ic;
2770 uint16_t args[5];
2771 int tid, dialogtoken;
2772 static int tokens = 0; /* XXX */
2773
2774 /* XXX locking */
2775 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
2776 /* do deferred setup of state */
2777 ampdu_tx_setup(tap);
2778 }
2779 /* XXX hack for not doing proper locking */
2780 tap->txa_flags &= ~IEEE80211_AGGR_NAK;
2781
2782 dialogtoken = (tokens+1) % 63; /* XXX */
2783 tid = tap->txa_tid;
2784
2785 /*
2786 * XXX TODO: This is racy with any other parallel TX going on. :(
2787 */
2788 tap->txa_start = ni->ni_txseqs[tid];
2789
2790 args[0] = dialogtoken;
2791 args[1] = 0; /* NB: status code not used */
2792 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
2793 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID)
2794 | _IEEE80211_SHIFTMASK(IEEE80211_AGGR_BAWMAX,
2795 IEEE80211_BAPS_BUFSIZ)
2796 ;
2797
2798 /* XXX TODO: this should be a flag, not iv_htcaps */
2799 if ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
2800 (ni->ni_vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU))
2801 args[2] |= IEEE80211_BAPS_AMSDU;
2802
2803 args[3] = 0; /* batimeout */
2804 /* NB: do first so there's no race against reply */
2805 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) {
2806 /* unable to setup state, don't make request */
2807 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2808 ni, "%s: could not setup BA stream for TID %d AC %d",
2809 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid));
2810 /* defer next try so we don't slam the driver with requests */
2811 tap->txa_attempts = ieee80211_addba_maxtries;
2812 /* NB: check in case driver wants to override */
2813 if (tap->txa_nextrequest <= ticks)
2814 tap->txa_nextrequest = ticks + ieee80211_addba_backoff;
2815 return 0;
2816 }
2817 tokens = dialogtoken; /* allocate token */
2818 /* NB: after calling ic_addba_request so driver can set txa_start */
2819 args[4] = _IEEE80211_SHIFTMASK(tap->txa_start, IEEE80211_BASEQ_START)
2820 | _IEEE80211_SHIFTMASK(0, IEEE80211_BASEQ_FRAG)
2821 ;
2822 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2823 IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
2824 }
2825
2826 /*
2827 * Terminate an AMPDU tx stream. State is reclaimed
2828 * and the peer notified with a DelBA Action frame.
2829 */
2830 void
ieee80211_ampdu_stop(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int reason)2831 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
2832 int reason)
2833 {
2834 struct ieee80211com *ic = ni->ni_ic;
2835 struct ieee80211vap *vap = ni->ni_vap;
2836 uint16_t args[4];
2837
2838 /* XXX locking */
2839 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2840 if (IEEE80211_AMPDU_RUNNING(tap)) {
2841 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2842 ni, "%s: stop BA stream for TID %d (reason: %d (%s))",
2843 __func__, tap->txa_tid, reason,
2844 ieee80211_reason_to_string(reason));
2845 vap->iv_stats.is_ampdu_stop++;
2846
2847 ic->ic_addba_stop(ni, tap);
2848 args[0] = tap->txa_tid;
2849 args[1] = IEEE80211_DELBAPS_INIT;
2850 args[2] = reason; /* XXX reason code */
2851 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
2852 IEEE80211_ACTION_BA_DELBA, args);
2853 } else {
2854 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
2855 ni, "%s: BA stream for TID %d not running "
2856 "(reason: %d (%s))", __func__, tap->txa_tid, reason,
2857 ieee80211_reason_to_string(reason));
2858 vap->iv_stats.is_ampdu_stop_failed++;
2859 }
2860 }
2861
2862 /* XXX */
2863 static void bar_start_timer(struct ieee80211_tx_ampdu *tap);
2864
2865 static void
bar_timeout(void * arg)2866 bar_timeout(void *arg)
2867 {
2868 struct ieee80211_tx_ampdu *tap = arg;
2869 struct ieee80211_node *ni = tap->txa_ni;
2870
2871 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0,
2872 ("bar/addba collision, flags 0x%x", tap->txa_flags));
2873
2874 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2875 ni, "%s: tid %u flags 0x%x attempts %d", __func__,
2876 tap->txa_tid, tap->txa_flags, tap->txa_attempts);
2877
2878 /* guard against race with bar_tx_complete */
2879 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
2880 return;
2881 /* XXX ? */
2882 if (tap->txa_attempts >= ieee80211_bar_maxtries) {
2883 struct ieee80211com *ic = ni->ni_ic;
2884
2885 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++;
2886 /*
2887 * If (at least) the last BAR TX timeout was due to
2888 * an ieee80211_send_bar() failures, then we need
2889 * to make sure we notify the driver that a BAR
2890 * TX did occur and fail. This gives the driver
2891 * a chance to undo any queue pause that may
2892 * have occurred.
2893 */
2894 ic->ic_bar_response(ni, tap, 1);
2895 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT);
2896 } else {
2897 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++;
2898 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) {
2899 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2900 ni, "%s: failed to TX, starting timer\n",
2901 __func__);
2902 /*
2903 * If ieee80211_send_bar() fails here, the
2904 * timer may have stopped and/or the pending
2905 * flag may be clear. Because of this,
2906 * fake the BARPEND and reset the timer.
2907 * A retransmission attempt will then occur
2908 * during the next timeout.
2909 */
2910 /* XXX locking */
2911 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
2912 bar_start_timer(tap);
2913 }
2914 }
2915 }
2916
2917 static void
bar_start_timer(struct ieee80211_tx_ampdu * tap)2918 bar_start_timer(struct ieee80211_tx_ampdu *tap)
2919 {
2920 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2921 tap->txa_ni,
2922 "%s: called",
2923 __func__);
2924 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap);
2925 }
2926
2927 static void
bar_stop_timer(struct ieee80211_tx_ampdu * tap)2928 bar_stop_timer(struct ieee80211_tx_ampdu *tap)
2929 {
2930 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2931 tap->txa_ni,
2932 "%s: called",
2933 __func__);
2934 callout_stop(&tap->txa_timer);
2935 }
2936
2937 static void
bar_tx_complete(struct ieee80211_node * ni,void * arg,int status)2938 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status)
2939 {
2940 struct ieee80211_tx_ampdu *tap = arg;
2941
2942 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2943 ni, "%s: tid %u flags 0x%x pending %d status %d",
2944 __func__, tap->txa_tid, tap->txa_flags,
2945 callout_pending(&tap->txa_timer), status);
2946
2947 ni->ni_vap->iv_stats.is_ampdu_bar_tx++;
2948 /* XXX locking */
2949 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) &&
2950 callout_pending(&tap->txa_timer)) {
2951 struct ieee80211com *ic = ni->ni_ic;
2952
2953 if (status == 0) /* ACK'd */
2954 bar_stop_timer(tap);
2955 ic->ic_bar_response(ni, tap, status);
2956 /* NB: just let timer expire so we pace requests */
2957 }
2958 }
2959
2960 static void
ieee80211_bar_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status)2961 ieee80211_bar_response(struct ieee80211_node *ni,
2962 struct ieee80211_tx_ampdu *tap, int status)
2963 {
2964
2965 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
2966 tap->txa_ni,
2967 "%s: called",
2968 __func__);
2969 if (status == 0) { /* got ACK */
2970 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N,
2971 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u",
2972 tap->txa_start,
2973 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1),
2974 tap->txa_qframes, tap->txa_seqpending,
2975 tap->txa_tid);
2976
2977 /* NB: timer already stopped in bar_tx_complete */
2978 tap->txa_start = tap->txa_seqpending;
2979 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
2980 }
2981 }
2982
2983 /*
2984 * Transmit a BAR frame to the specified node. The
2985 * BAR contents are drawn from the supplied aggregation
2986 * state associated with the node.
2987 *
2988 * NB: we only handle immediate ACK w/ compressed bitmap.
2989 */
2990 int
ieee80211_send_bar(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,ieee80211_seq seq)2991 ieee80211_send_bar(struct ieee80211_node *ni,
2992 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq)
2993 {
2994 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2995 struct ieee80211vap *vap = ni->ni_vap;
2996 struct ieee80211com *ic = ni->ni_ic;
2997 struct ieee80211_frame_bar *bar;
2998 struct mbuf *m;
2999 uint16_t barctl, barseqctl;
3000 uint8_t *frm;
3001 int tid, ret;
3002
3003 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
3004 tap->txa_ni,
3005 "%s: called",
3006 __func__);
3007
3008 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) {
3009 /* no ADDBA response, should not happen */
3010 /* XXX stat+msg */
3011 return EINVAL;
3012 }
3013 /* XXX locking */
3014 bar_stop_timer(tap);
3015
3016 ieee80211_ref_node(ni);
3017
3018 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar));
3019 if (m == NULL)
3020 senderr(ENOMEM, is_tx_nobuf);
3021
3022 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) {
3023 m_freem(m);
3024 senderr(ENOMEM, is_tx_nobuf); /* XXX */
3025 /* NOTREACHED */
3026 }
3027
3028 bar = mtod(m, struct ieee80211_frame_bar *);
3029 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3030 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
3031 bar->i_fc[1] = 0;
3032 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
3033 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
3034
3035 tid = tap->txa_tid;
3036 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
3037 0 : IEEE80211_BAR_NOACK)
3038 | IEEE80211_BAR_COMP
3039 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAR_TID)
3040 ;
3041 barseqctl = _IEEE80211_SHIFTMASK(seq, IEEE80211_BAR_SEQ_START);
3042 /* NB: known to have proper alignment */
3043 bar->i_ctl = htole16(barctl);
3044 bar->i_seq = htole16(barseqctl);
3045 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
3046
3047 M_WME_SETAC(m, WME_AC_VO);
3048
3049 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */
3050
3051 /* XXX locking */
3052 /* init/bump attempts counter */
3053 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0)
3054 tap->txa_attempts = 1;
3055 else
3056 tap->txa_attempts++;
3057 tap->txa_seqpending = seq;
3058 tap->txa_flags |= IEEE80211_AGGR_BARPEND;
3059
3060 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
3061 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)",
3062 tid, barctl, seq, tap->txa_attempts);
3063
3064 /*
3065 * ic_raw_xmit will free the node reference
3066 * regardless of queue/TX success or failure.
3067 */
3068 IEEE80211_TX_LOCK(ic);
3069 ret = ieee80211_raw_output(vap, ni, m, NULL);
3070 IEEE80211_TX_UNLOCK(ic);
3071 if (ret != 0) {
3072 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N,
3073 ni, "send BAR: failed: (ret = %d)\n",
3074 ret);
3075 /* xmit failed, clear state flag */
3076 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND;
3077 vap->iv_stats.is_ampdu_bar_tx_fail++;
3078 return ret;
3079 }
3080 /* XXX hack against tx complete happening before timer is started */
3081 if (tap->txa_flags & IEEE80211_AGGR_BARPEND)
3082 bar_start_timer(tap);
3083 return 0;
3084 bad:
3085 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N,
3086 tap->txa_ni,
3087 "%s: bad! ret=%d",
3088 __func__, ret);
3089 vap->iv_stats.is_ampdu_bar_tx_fail++;
3090 ieee80211_free_node(ni);
3091 return ret;
3092 #undef senderr
3093 }
3094
3095 static int
ht_action_output(struct ieee80211_node * ni,struct mbuf * m)3096 ht_action_output(struct ieee80211_node *ni, struct mbuf *m)
3097 {
3098 struct ieee80211_bpf_params params;
3099
3100 memset(¶ms, 0, sizeof(params));
3101 params.ibp_pri = WME_AC_VO;
3102 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
3103 /* NB: we know all frames are unicast */
3104 params.ibp_try0 = ni->ni_txparms->maxretry;
3105 params.ibp_power = ni->ni_txpower;
3106 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
3107 ¶ms);
3108 }
3109
3110 #define ADDSHORT(frm, v) do { \
3111 frm[0] = (v) & 0xff; \
3112 frm[1] = (v) >> 8; \
3113 frm += 2; \
3114 } while (0)
3115
3116 /*
3117 * Send an action management frame. The arguments are stuff
3118 * into a frame without inspection; the caller is assumed to
3119 * prepare them carefully (e.g. based on the aggregation state).
3120 */
3121 static int
ht_send_action_ba_addba(struct ieee80211_node * ni,int category,int action,void * arg0)3122 ht_send_action_ba_addba(struct ieee80211_node *ni,
3123 int category, int action, void *arg0)
3124 {
3125 struct ieee80211vap *vap = ni->ni_vap;
3126 struct ieee80211com *ic = ni->ni_ic;
3127 uint16_t *args = arg0;
3128 struct mbuf *m;
3129 uint8_t *frm;
3130
3131 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3132 "send ADDBA %s: dialogtoken %d status %d "
3133 "baparamset 0x%x (tid %d amsdu %d) batimeout 0x%x baseqctl 0x%x",
3134 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ?
3135 "request" : "response", args[0], args[1], args[2],
3136 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_TID),
3137 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_AMSDU),
3138 args[3], args[4]);
3139
3140 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3141 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3142 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3143 ieee80211_ref_node(ni);
3144
3145 m = ieee80211_getmgtframe(&frm,
3146 ic->ic_headroom + sizeof(struct ieee80211_frame),
3147 sizeof(uint16_t) /* action+category */
3148 /* XXX may action payload */
3149 + sizeof(struct ieee80211_action_ba_addbaresponse)
3150 );
3151 if (m != NULL) {
3152 *frm++ = category;
3153 *frm++ = action;
3154 *frm++ = args[0]; /* dialog token */
3155 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE)
3156 ADDSHORT(frm, args[1]); /* status code */
3157 ADDSHORT(frm, args[2]); /* baparamset */
3158 ADDSHORT(frm, args[3]); /* batimeout */
3159 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST)
3160 ADDSHORT(frm, args[4]); /* baseqctl */
3161 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3162 return ht_action_output(ni, m);
3163 } else {
3164 vap->iv_stats.is_tx_nobuf++;
3165 ieee80211_free_node(ni);
3166 return ENOMEM;
3167 }
3168 }
3169
3170 static int
ht_send_action_ba_delba(struct ieee80211_node * ni,int category,int action,void * arg0)3171 ht_send_action_ba_delba(struct ieee80211_node *ni,
3172 int category, int action, void *arg0)
3173 {
3174 struct ieee80211vap *vap = ni->ni_vap;
3175 struct ieee80211com *ic = ni->ni_ic;
3176 uint16_t *args = arg0;
3177 struct mbuf *m;
3178 uint16_t baparamset;
3179 uint8_t *frm;
3180
3181 baparamset = _IEEE80211_SHIFTMASK(args[0], IEEE80211_DELBAPS_TID)
3182 | args[1]
3183 ;
3184 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3185 "send DELBA action: tid %d, initiator %d reason %d (%s)",
3186 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
3187
3188 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3189 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3190 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3191 ieee80211_ref_node(ni);
3192
3193 m = ieee80211_getmgtframe(&frm,
3194 ic->ic_headroom + sizeof(struct ieee80211_frame),
3195 sizeof(uint16_t) /* action+category */
3196 /* XXX may action payload */
3197 + sizeof(struct ieee80211_action_ba_addbaresponse)
3198 );
3199 if (m != NULL) {
3200 *frm++ = category;
3201 *frm++ = action;
3202 ADDSHORT(frm, baparamset);
3203 ADDSHORT(frm, args[2]); /* reason code */
3204 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3205 return ht_action_output(ni, m);
3206 } else {
3207 vap->iv_stats.is_tx_nobuf++;
3208 ieee80211_free_node(ni);
3209 return ENOMEM;
3210 }
3211 }
3212
3213 static int
ht_send_action_ht_txchwidth(struct ieee80211_node * ni,int category,int action,void * arg0)3214 ht_send_action_ht_txchwidth(struct ieee80211_node *ni,
3215 int category, int action, void *arg0)
3216 {
3217 struct ieee80211vap *vap = ni->ni_vap;
3218 struct ieee80211com *ic = ni->ni_ic;
3219 struct mbuf *m;
3220 uint8_t *frm;
3221
3222 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
3223 "send HT txchwidth: width %d",
3224 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20);
3225
3226 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3227 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3228 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3229 ieee80211_ref_node(ni);
3230
3231 m = ieee80211_getmgtframe(&frm,
3232 ic->ic_headroom + sizeof(struct ieee80211_frame),
3233 sizeof(uint16_t) /* action+category */
3234 /* XXX may action payload */
3235 + sizeof(struct ieee80211_action_ba_addbaresponse)
3236 );
3237 if (m != NULL) {
3238 *frm++ = category;
3239 *frm++ = action;
3240 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ?
3241 IEEE80211_A_HT_TXCHWIDTH_2040 :
3242 IEEE80211_A_HT_TXCHWIDTH_20;
3243 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3244 return ht_action_output(ni, m);
3245 } else {
3246 vap->iv_stats.is_tx_nobuf++;
3247 ieee80211_free_node(ni);
3248 return ENOMEM;
3249 }
3250 }
3251 #undef ADDSHORT
3252
3253 /*
3254 * Construct the MCS bit mask for inclusion in an HT capabilities
3255 * information element.
3256 */
3257 static void
ieee80211_set_mcsset(struct ieee80211com * ic,uint8_t * frm)3258 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm)
3259 {
3260 int i;
3261 uint8_t txparams;
3262
3263 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4),
3264 ("ic_rxstream %d out of range", ic->ic_rxstream));
3265 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4),
3266 ("ic_txstream %d out of range", ic->ic_txstream));
3267
3268 for (i = 0; i < ic->ic_rxstream * 8; i++)
3269 setbit(frm, i);
3270 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
3271 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32))
3272 setbit(frm, 32);
3273 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) {
3274 if (ic->ic_rxstream >= 2) {
3275 for (i = 33; i <= 38; i++)
3276 setbit(frm, i);
3277 }
3278 if (ic->ic_rxstream >= 3) {
3279 for (i = 39; i <= 52; i++)
3280 setbit(frm, i);
3281 }
3282 if (ic->ic_rxstream >= 4) {
3283 for (i = 53; i <= 76; i++)
3284 setbit(frm, i);
3285 }
3286 }
3287
3288 txparams = 0x1; /* TX MCS set defined */
3289 if (ic->ic_rxstream != ic->ic_txstream) {
3290 txparams |= 0x2; /* TX RX MCS not equal */
3291 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */
3292 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL)
3293 txparams |= 0x16; /* TX unequal modulation sup */
3294 }
3295
3296 frm[12] = txparams;
3297 }
3298
3299 /*
3300 * Add body of an HTCAP information element.
3301 */
3302 static uint8_t *
ieee80211_add_htcap_body(uint8_t * frm,struct ieee80211_node * ni)3303 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
3304 {
3305 #define ADDSHORT(frm, v) do { \
3306 frm[0] = (v) & 0xff; \
3307 frm[1] = (v) >> 8; \
3308 frm += 2; \
3309 } while (0)
3310 struct ieee80211com *ic = ni->ni_ic;
3311 struct ieee80211vap *vap = ni->ni_vap;
3312 uint16_t caps, extcaps;
3313 int rxmax, density;
3314
3315 /* HT capabilities */
3316 caps = vap->iv_htcaps & 0xffff;
3317 /*
3318 * Note channel width depends on whether we are operating as
3319 * a sta or not. When operating as a sta we are generating
3320 * a request based on our desired configuration. Otherwise
3321 * we are operational and the channel attributes identify
3322 * how we've been setup (which might be different if a fixed
3323 * channel is specified).
3324 */
3325 if (vap->iv_opmode == IEEE80211_M_STA) {
3326 /* override 20/40 use based on config */
3327 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
3328 caps |= IEEE80211_HTCAP_CHWIDTH40;
3329 else
3330 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3331
3332 /* Start by using the advertised settings */
3333 rxmax = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3334 IEEE80211_HTCAP_MAXRXAMPDU);
3335 density = _IEEE80211_MASKSHIFT(ni->ni_htparam,
3336 IEEE80211_HTCAP_MPDUDENSITY);
3337
3338 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N,
3339 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n",
3340 __func__,
3341 rxmax,
3342 density,
3343 vap->iv_ampdu_rxmax,
3344 vap->iv_ampdu_density);
3345
3346 /* Cap at VAP rxmax */
3347 if (rxmax > vap->iv_ampdu_rxmax)
3348 rxmax = vap->iv_ampdu_rxmax;
3349
3350 /*
3351 * If the VAP ampdu density value greater, use that.
3352 *
3353 * (Larger density value == larger minimum gap between A-MPDU
3354 * subframes.)
3355 */
3356 if (vap->iv_ampdu_density > density)
3357 density = vap->iv_ampdu_density;
3358
3359 /*
3360 * NB: Hardware might support HT40 on some but not all
3361 * channels. We can't determine this earlier because only
3362 * after association the channel is upgraded to HT based
3363 * on the negotiated capabilities.
3364 */
3365 if (ni->ni_chan != IEEE80211_CHAN_ANYC &&
3366 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL &&
3367 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL)
3368 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3369 } else {
3370 /* override 20/40 use based on current channel */
3371 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3372 caps |= IEEE80211_HTCAP_CHWIDTH40;
3373 else
3374 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3375
3376 /* XXX TODO should it start by using advertised settings? */
3377 rxmax = vap->iv_ampdu_rxmax;
3378 density = vap->iv_ampdu_density;
3379 }
3380
3381 /* adjust short GI based on channel and config */
3382 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3383 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3384 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3385 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3386 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3387
3388 /* adjust STBC based on receive capabilities */
3389 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0)
3390 caps &= ~IEEE80211_HTCAP_RXSTBC;
3391
3392 /* adjust LDPC based on receive capabilites */
3393 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0)
3394 caps &= ~IEEE80211_HTCAP_LDPC;
3395
3396 ADDSHORT(frm, caps);
3397
3398 /* HT parameters */
3399 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3400 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3401 ;
3402 frm++;
3403
3404 /* pre-zero remainder of ie */
3405 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3406 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3407
3408 /* supported MCS set */
3409 /*
3410 * XXX: For sta mode the rate set should be restricted based
3411 * on the AP's capabilities, but ni_htrates isn't setup when
3412 * we're called to form an AssocReq frame so for now we're
3413 * restricted to the device capabilities.
3414 */
3415 ieee80211_set_mcsset(ni->ni_ic, frm);
3416
3417 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3418 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3419
3420 /* HT extended capabilities */
3421 extcaps = vap->iv_htextcaps & 0xffff;
3422
3423 ADDSHORT(frm, extcaps);
3424
3425 frm += sizeof(struct ieee80211_ie_htcap) -
3426 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3427
3428 return frm;
3429 #undef ADDSHORT
3430 }
3431
3432 /*
3433 * Add 802.11n HT capabilities information element
3434 */
3435 uint8_t *
ieee80211_add_htcap(uint8_t * frm,struct ieee80211_node * ni)3436 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
3437 {
3438 frm[0] = IEEE80211_ELEMID_HTCAP;
3439 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3440 return ieee80211_add_htcap_body(frm + 2, ni);
3441 }
3442
3443 /*
3444 * Non-associated probe request - add HT capabilities based on
3445 * the current channel configuration.
3446 */
3447 static uint8_t *
ieee80211_add_htcap_body_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)3448 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap,
3449 struct ieee80211_channel *c)
3450 {
3451 #define ADDSHORT(frm, v) do { \
3452 frm[0] = (v) & 0xff; \
3453 frm[1] = (v) >> 8; \
3454 frm += 2; \
3455 } while (0)
3456 struct ieee80211com *ic = vap->iv_ic;
3457 uint16_t caps, extcaps;
3458 int rxmax, density;
3459
3460 /* HT capabilities */
3461 caps = vap->iv_htcaps & 0xffff;
3462
3463 /*
3464 * We don't use this in STA mode; only in IBSS mode.
3465 * So in IBSS mode we base our HTCAP flags on the
3466 * given channel.
3467 */
3468
3469 /* override 20/40 use based on current channel */
3470 if (IEEE80211_IS_CHAN_HT40(c))
3471 caps |= IEEE80211_HTCAP_CHWIDTH40;
3472 else
3473 caps &= ~IEEE80211_HTCAP_CHWIDTH40;
3474
3475 /* Use the currently configured values */
3476 rxmax = vap->iv_ampdu_rxmax;
3477 density = vap->iv_ampdu_density;
3478
3479 /* adjust short GI based on channel and config */
3480 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0)
3481 caps &= ~IEEE80211_HTCAP_SHORTGI20;
3482 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 ||
3483 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
3484 caps &= ~IEEE80211_HTCAP_SHORTGI40;
3485 ADDSHORT(frm, caps);
3486
3487 /* HT parameters */
3488 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
3489 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY)
3490 ;
3491 frm++;
3492
3493 /* pre-zero remainder of ie */
3494 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
3495 __offsetof(struct ieee80211_ie_htcap, hc_mcsset));
3496
3497 /* supported MCS set */
3498 /*
3499 * XXX: For sta mode the rate set should be restricted based
3500 * on the AP's capabilities, but ni_htrates isn't setup when
3501 * we're called to form an AssocReq frame so for now we're
3502 * restricted to the device capabilities.
3503 */
3504 ieee80211_set_mcsset(ic, frm);
3505
3506 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) -
3507 __offsetof(struct ieee80211_ie_htcap, hc_mcsset);
3508
3509 /* HT extended capabilities */
3510 extcaps = vap->iv_htextcaps & 0xffff;
3511
3512 ADDSHORT(frm, extcaps);
3513
3514 frm += sizeof(struct ieee80211_ie_htcap) -
3515 __offsetof(struct ieee80211_ie_htcap, hc_txbf);
3516
3517 return frm;
3518 #undef ADDSHORT
3519 }
3520
3521 /*
3522 * Add 802.11n HT capabilities information element
3523 */
3524 uint8_t *
ieee80211_add_htcap_ch(uint8_t * frm,struct ieee80211vap * vap,struct ieee80211_channel * c)3525 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap,
3526 struct ieee80211_channel *c)
3527 {
3528 frm[0] = IEEE80211_ELEMID_HTCAP;
3529 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
3530 return ieee80211_add_htcap_body_ch(frm + 2, vap, c);
3531 }
3532
3533 /*
3534 * Add Broadcom OUI wrapped standard HTCAP ie; this is
3535 * used for compatibility w/ pre-draft implementations.
3536 */
3537 uint8_t *
ieee80211_add_htcap_vendor(uint8_t * frm,struct ieee80211_node * ni)3538 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
3539 {
3540 frm[0] = IEEE80211_ELEMID_VENDOR;
3541 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
3542 frm[2] = (BCM_OUI >> 0) & 0xff;
3543 frm[3] = (BCM_OUI >> 8) & 0xff;
3544 frm[4] = (BCM_OUI >> 16) & 0xff;
3545 frm[5] = BCM_OUI_HTCAP;
3546 return ieee80211_add_htcap_body(frm + 6, ni);
3547 }
3548
3549 /*
3550 * Construct the MCS bit mask of basic rates
3551 * for inclusion in an HT information element.
3552 */
3553 static void
ieee80211_set_basic_htrates(uint8_t * frm,const struct ieee80211_htrateset * rs)3554 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
3555 {
3556 int i;
3557
3558 for (i = 0; i < rs->rs_nrates; i++) {
3559 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
3560 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
3561 r < IEEE80211_HTRATE_MAXSIZE) {
3562 /* NB: this assumes a particular implementation */
3563 setbit(frm, r);
3564 }
3565 }
3566 }
3567
3568 /*
3569 * Update the HTINFO ie for a beacon frame.
3570 */
3571 void
ieee80211_ht_update_beacon(struct ieee80211vap * vap,struct ieee80211_beacon_offsets * bo)3572 ieee80211_ht_update_beacon(struct ieee80211vap *vap,
3573 struct ieee80211_beacon_offsets *bo)
3574 {
3575 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
3576 struct ieee80211_node *ni;
3577 const struct ieee80211_channel *bsschan;
3578 struct ieee80211com *ic = vap->iv_ic;
3579 struct ieee80211_ie_htinfo *ht =
3580 (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
3581
3582 ni = ieee80211_ref_node(vap->iv_bss);
3583 bsschan = ni->ni_chan;
3584
3585 /* XXX only update on channel change */
3586 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan);
3587 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3588 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM;
3589 else
3590 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
3591 if (IEEE80211_IS_CHAN_HT40U(bsschan))
3592 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3593 else if (IEEE80211_IS_CHAN_HT40D(bsschan))
3594 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3595 else
3596 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
3597 if (IEEE80211_IS_CHAN_HT40(bsschan))
3598 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
3599
3600 /* protection mode */
3601 /*
3602 * XXX TODO: this uses the global flag, not the per-VAP flag.
3603 * Eventually (once the protection modes are done per-channel
3604 * rather than per-VAP) we can flip this over to be per-VAP but
3605 * using the channel protection mode.
3606 */
3607 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
3608
3609 ieee80211_free_node(ni);
3610
3611 /* XXX propagate to vendor ie's */
3612 #undef PROTMODE
3613 }
3614
3615 /*
3616 * Add body of an HTINFO information element.
3617 *
3618 * NB: We don't use struct ieee80211_ie_htinfo because we can
3619 * be called to fillin both a standard ie and a compat ie that
3620 * has a vendor OUI at the front.
3621 */
3622 static uint8_t *
ieee80211_add_htinfo_body(uint8_t * frm,struct ieee80211_node * ni)3623 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
3624 {
3625 struct ieee80211vap *vap = ni->ni_vap;
3626 struct ieee80211com *ic = ni->ni_ic;
3627
3628 /* pre-zero remainder of ie */
3629 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
3630
3631 /* primary/control channel center */
3632 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3633
3634 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS)
3635 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM;
3636 else
3637 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
3638 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
3639 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
3640 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
3641 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
3642 else
3643 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
3644 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
3645 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
3646
3647 /*
3648 * Add current protection mode. Unlike for beacons,
3649 * this will respect the per-VAP flags.
3650 */
3651 frm[1] = vap->iv_curhtprotmode;
3652
3653 frm += 5;
3654
3655 /* basic MCS set */
3656 ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
3657 frm += sizeof(struct ieee80211_ie_htinfo) -
3658 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
3659 return frm;
3660 }
3661
3662 /*
3663 * Add 802.11n HT information element.
3664 */
3665 uint8_t *
ieee80211_add_htinfo(uint8_t * frm,struct ieee80211_node * ni)3666 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
3667 {
3668 frm[0] = IEEE80211_ELEMID_HTINFO;
3669 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
3670 return ieee80211_add_htinfo_body(frm + 2, ni);
3671 }
3672
3673 /*
3674 * Add Broadcom OUI wrapped standard HTINFO ie; this is
3675 * used for compatibility w/ pre-draft implementations.
3676 */
3677 uint8_t *
ieee80211_add_htinfo_vendor(uint8_t * frm,struct ieee80211_node * ni)3678 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
3679 {
3680 frm[0] = IEEE80211_ELEMID_VENDOR;
3681 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
3682 frm[2] = (BCM_OUI >> 0) & 0xff;
3683 frm[3] = (BCM_OUI >> 8) & 0xff;
3684 frm[4] = (BCM_OUI >> 16) & 0xff;
3685 frm[5] = BCM_OUI_HTINFO;
3686 return ieee80211_add_htinfo_body(frm + 6, ni);
3687 }
3688
3689 /*
3690 * Get the HT density for the given 802.11n node.
3691 *
3692 * Take into account the density advertised from the peer.
3693 * Larger values are longer A-MPDU density spacing values, and
3694 * we want to obey them per station if we get them.
3695 */
3696 int
ieee80211_ht_get_node_ampdu_density(const struct ieee80211_node * ni)3697 ieee80211_ht_get_node_ampdu_density(const struct ieee80211_node *ni)
3698 {
3699 struct ieee80211vap *vap;
3700 int peer_mpdudensity;
3701
3702 vap = ni->ni_vap;
3703 peer_mpdudensity =
3704 _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3705 if (vap->iv_ampdu_density > peer_mpdudensity)
3706 peer_mpdudensity = vap->iv_ampdu_density;
3707 return (peer_mpdudensity);
3708 }
3709
3710 /*
3711 * Get the transmit A-MPDU limit for the given 802.11n node.
3712 *
3713 * Take into account the limit advertised from the peer.
3714 * Smaller values indicate smaller maximum A-MPDU sizes, and
3715 * should be used when forming an A-MPDU to the given peer.
3716 */
3717 int
ieee80211_ht_get_node_ampdu_limit(const struct ieee80211_node * ni)3718 ieee80211_ht_get_node_ampdu_limit(const struct ieee80211_node *ni)
3719 {
3720 struct ieee80211vap *vap;
3721 int peer_mpdulimit;
3722
3723 vap = ni->ni_vap;
3724 peer_mpdulimit =
3725 _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
3726
3727 return (MIN(vap->iv_ampdu_limit, peer_mpdulimit));
3728 }
3729
3730 /*
3731 * Return true if short-GI is available when transmitting to
3732 * the given node at 20MHz.
3733 *
3734 * Ensure it's configured and available in the VAP / driver as
3735 * well as the node.
3736 */
3737 bool
ieee80211_ht_check_tx_shortgi_20(const struct ieee80211_node * ni)3738 ieee80211_ht_check_tx_shortgi_20(const struct ieee80211_node *ni)
3739 {
3740 const struct ieee80211vap *vap;
3741 const struct ieee80211com *ic;
3742
3743 if (! ieee80211_ht_check_tx_ht(ni))
3744 return (false);
3745
3746 vap = ni->ni_vap;
3747 ic = ni->ni_ic;
3748
3749 return ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) &&
3750 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) &&
3751 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20));
3752 }
3753
3754 /*
3755 * Return true if short-GI is available when transmitting to
3756 * the given node at 40MHz.
3757 *
3758 * Ensure it's configured and available in the VAP / driver as
3759 * well as the node and BSS.
3760 */
3761 bool
ieee80211_ht_check_tx_shortgi_40(const struct ieee80211_node * ni)3762 ieee80211_ht_check_tx_shortgi_40(const struct ieee80211_node *ni)
3763 {
3764 const struct ieee80211vap *vap;
3765 const struct ieee80211com *ic;
3766
3767 if (! ieee80211_ht_check_tx_ht40(ni))
3768 return (false);
3769
3770 vap = ni->ni_vap;
3771 ic = ni->ni_ic;
3772
3773 return ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40) &&
3774 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) &&
3775 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40));
3776 }
3777
3778 /*
3779 * Return true if HT rates can be used for the given node.
3780 *
3781 * There are some situations seen in the wild, wild past where
3782 * HT APs would announce HT but no HT rates.
3783 */
3784 bool
ieee80211_ht_check_tx_ht(const struct ieee80211_node * ni)3785 ieee80211_ht_check_tx_ht(const struct ieee80211_node *ni)
3786 {
3787 const struct ieee80211vap *vap;
3788 const struct ieee80211_channel *bss_chan;
3789
3790 if (ni == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC ||
3791 ni->ni_vap == NULL || ni->ni_vap->iv_bss == NULL)
3792 return (false);
3793
3794 vap = ni->ni_vap;
3795 bss_chan = vap->iv_bss->ni_chan;
3796
3797 if (bss_chan == IEEE80211_CHAN_ANYC)
3798 return (false);
3799
3800 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3801 ni->ni_htrates.rs_nrates == 0)
3802 return (false);
3803 return (IEEE80211_IS_CHAN_HT(ni->ni_chan));
3804 }
3805
3806 /*
3807 * Return true if HT40 rates can be transmitted to the given node.
3808 *
3809 * This verifies that the BSS is HT40 capable and the current
3810 * node channel width is 40MHz.
3811 */
3812 bool
ieee80211_ht_check_tx_ht40(const struct ieee80211_node * ni)3813 ieee80211_ht_check_tx_ht40(const struct ieee80211_node *ni)
3814 {
3815 struct ieee80211vap *vap;
3816 struct ieee80211_channel *bss_chan;
3817
3818 if (! ieee80211_ht_check_tx_ht(ni))
3819 return (false);
3820
3821 vap = ni->ni_vap;
3822 bss_chan = vap->iv_bss->ni_chan;
3823
3824 return (IEEE80211_IS_CHAN_HT40(bss_chan) &&
3825 IEEE80211_IS_CHAN_HT40(ni->ni_chan) &&
3826 (ni->ni_chw == IEEE80211_STA_RX_BW_40));
3827 }
3828