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