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