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