1 /*-
2 * Copyright (c) 2020-2025 The FreeBSD Foundation
3 * Copyright (c) 2020-2025 Bjoern A. Zeeb
4 *
5 * This software was developed by Björn Zeeb under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Public functions are called linuxkpi_*().
32 * Internal (static) functions are called lkpi_*().
33 *
34 * The internal structures holding metadata over public structures are also
35 * called lkpi_xxx (usually with a member at the end called xxx).
36 * Note: we do not replicate the structure names but the general variable names
37 * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
38 * There are macros to access one from the other.
39 * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
40 */
41
42 /*
43 * TODO:
44 * - lots :)
45 * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
46 */
47
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/sbuf.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/queue.h>
59 #include <sys/taskqueue.h>
60 #include <sys/libkern.h>
61
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/if_media.h>
65 #include <net/ethernet.h>
66
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_proto.h>
69 #include <net80211/ieee80211_ratectl.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_vht.h>
72
73 #define LINUXKPI_NET80211
74 #include <net/mac80211.h>
75
76 #include <linux/workqueue.h>
77 #include <linux/rculist.h>
78 #include "linux_80211.h"
79
80 /* #define LKPI_80211_USE_SCANLIST */
81 /* #define LKPI_80211_BGSCAN */
82 #define LKPI_80211_WME
83 #define LKPI_80211_HW_CRYPTO
84 #define LKPI_80211_HT
85 #define LKPI_80211_VHT
86
87 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
88 #define LKPI_80211_HT
89 #endif
90 #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
91 #define LKPI_80211_HW_CRYPTO
92 #endif
93
94 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
95
96 /* XXX-BZ really want this and others in queue.h */
97 #define TAILQ_ELEM_INIT(elm, field) do { \
98 (elm)->field.tqe_next = NULL; \
99 (elm)->field.tqe_prev = NULL; \
100 } while (0)
101
102 /* -------------------------------------------------------------------------- */
103
104 SYSCTL_DECL(_compat_linuxkpi);
105 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
106 "LinuxKPI 802.11 compatibility layer");
107
108 static bool lkpi_order_scanlist = false;
109 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, order_scanlist, CTLFLAG_RW,
110 &lkpi_order_scanlist, 0, "Enable LinuxKPI 802.11 scan list shuffeling");
111
112 #if defined(LKPI_80211_HW_CRYPTO)
113 static bool lkpi_hwcrypto = false;
114 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
115 &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
116
117 static bool lkpi_hwcrypto_tkip = false;
118 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, tkip, CTLFLAG_RDTUN,
119 &lkpi_hwcrypto_tkip, 0, "Enable LinuxKPI 802.11 TKIP crypto offload");
120 #endif
121
122 /* Keep public for as long as header files are using it too. */
123 int linuxkpi_debug_80211;
124
125 #ifdef LINUXKPI_DEBUG_80211
126 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
127 &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
128
129 #define UNIMPLEMENTED if (linuxkpi_debug_80211 & D80211_TODO) \
130 printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
131 #define TRACEOK() if (linuxkpi_debug_80211 & D80211_TRACEOK) \
132 printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
133 #else
134 #define UNIMPLEMENTED do { } while (0)
135 #define TRACEOK() do { } while (0)
136 #endif
137
138 /* #define PREP_TX_INFO_DURATION (IEEE80211_TRANS_WAIT * 1000) */
139 #ifndef PREP_TX_INFO_DURATION
140 #define PREP_TX_INFO_DURATION 0 /* Let the driver do its thing. */
141 #endif
142
143 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
144 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
145
146 /* IEEE 802.11-05/0257r1 */
147 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
148
149 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
150 static const uint8_t ieee80211e_up_to_ac[] = {
151 IEEE80211_AC_BE,
152 IEEE80211_AC_BK,
153 IEEE80211_AC_BK,
154 IEEE80211_AC_BE,
155 IEEE80211_AC_VI,
156 IEEE80211_AC_VI,
157 IEEE80211_AC_VO,
158 IEEE80211_AC_VO,
159 #if 0
160 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
161 #endif
162 };
163
164 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
165 /*
166 * XXX TODO need a "glue layer" to link cfg80211 ops to
167 * mac80211 and to the driver or net80211.
168 * Can we pass some on 1:1? Need to compare the (*f)().
169 */
170 };
171
172 #if 0
173 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
174 struct ieee80211_node *);
175 #endif
176 static void lkpi_sw_scan_task(void *, int);
177 static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
178 static void lkpi_80211_txq_task(void *, int);
179 static void lkpi_80211_lhw_rxq_task(void *, int);
180 static void lkpi_ieee80211_free_skb_mbuf(void *);
181 #ifdef LKPI_80211_WME
182 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
183 #endif
184 static void lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *);
185
186 static const char *
lkpi_rate_info_bw_to_str(enum rate_info_bw bw)187 lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
188 {
189
190 switch (bw) {
191
192 case RATE_INFO_BW_20:
193 return ("20");
194 break;
195 case RATE_INFO_BW_5:
196 return ("5");
197 break;
198 case RATE_INFO_BW_10:
199 return ("10");
200 break;
201 case RATE_INFO_BW_40:
202 return ("40");
203 break;
204 case RATE_INFO_BW_80:
205 return ("80");
206 break;
207 case RATE_INFO_BW_160:
208 return ("160");
209 break;
210 case RATE_INFO_BW_HE_RU:
211 IMPROVE("nl80211_he_ru_alloc");
212 return ("HE_RU");
213 break;
214 case RATE_INFO_BW_320:
215 return ("320");
216 break;
217 case RATE_INFO_BW_EHT_RU:
218 IMPROVE("nl80211_eht_ru_alloc");
219 return ("EHT_RU");
220 break;
221 default:
222 return ("?");
223 break;
224 }
225 }
226
227 static void
lkpi_nl80211_sta_info_to_str(struct sbuf * s,const char * prefix,const uint64_t flags)228 lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
229 const uint64_t flags)
230 {
231 int bit, i;
232
233 sbuf_printf(s, "%s %#010jx", prefix, flags);
234
235 i = 0;
236 for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
237
238 if ((flags & BIT_ULL(bit)) == 0)
239 continue;
240
241 #define EXPAND_CASE(_flag) \
242 case NL80211_STA_INFO_ ## _flag: \
243 sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag); \
244 i++; \
245 break;
246
247 switch (bit) {
248 EXPAND_CASE(BEACON_RX)
249 EXPAND_CASE(BEACON_SIGNAL_AVG)
250 EXPAND_CASE(BSS_PARAM)
251 EXPAND_CASE(CHAIN_SIGNAL)
252 EXPAND_CASE(CHAIN_SIGNAL_AVG)
253 EXPAND_CASE(CONNECTED_TIME)
254 EXPAND_CASE(INACTIVE_TIME)
255 EXPAND_CASE(SIGNAL)
256 EXPAND_CASE(SIGNAL_AVG)
257 EXPAND_CASE(STA_FLAGS)
258 EXPAND_CASE(RX_BITRATE)
259 EXPAND_CASE(RX_PACKETS)
260 EXPAND_CASE(RX_BYTES)
261 EXPAND_CASE(RX_DROP_MISC)
262 EXPAND_CASE(TX_BITRATE)
263 EXPAND_CASE(TX_PACKETS)
264 EXPAND_CASE(TX_BYTES)
265 EXPAND_CASE(TX_BYTES64)
266 EXPAND_CASE(RX_BYTES64)
267 EXPAND_CASE(TX_FAILED)
268 EXPAND_CASE(TX_RETRIES)
269 EXPAND_CASE(RX_DURATION)
270 EXPAND_CASE(TX_DURATION)
271 EXPAND_CASE(ACK_SIGNAL)
272 EXPAND_CASE(ACK_SIGNAL_AVG)
273 default:
274 sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
275 break;
276 }
277 }
278 #undef EXPAND_CASE
279 if (i > 0)
280 sbuf_printf(s, ">");
281 sbuf_printf(s, "\n");
282 }
283
284 static void
lkpi_80211_dump_lvif_stas(struct lkpi_vif * lvif,struct sbuf * s)285 lkpi_80211_dump_lvif_stas(struct lkpi_vif *lvif, struct sbuf *s)
286 {
287 struct lkpi_hw *lhw;
288 struct ieee80211_hw *hw;
289 struct ieee80211vap *vap;
290 struct ieee80211_vif *vif;
291 struct lkpi_sta *lsta;
292 struct ieee80211_sta *sta;
293 struct station_info sinfo;
294 int error;
295
296 vif = LVIF_TO_VIF(lvif);
297 vap = LVIF_TO_VAP(lvif);
298 lhw = vap->iv_ic->ic_softc;
299 hw = LHW_TO_HW(lhw);
300
301 wiphy_lock(hw->wiphy);
302 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
303 sta = LSTA_TO_STA(lsta);
304
305 sbuf_putc(s, '\n');
306 sbuf_printf(s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
307
308 memset(&sinfo, 0, sizeof(sinfo));
309 error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
310 if (error == EEXIST) /* Not added to driver. */
311 continue;
312 if (error == ENOTSUPP) {
313 sbuf_printf(s, " sta_statistics not supported\n");
314 continue;
315 }
316 if (error != 0) {
317 sbuf_printf(s, " sta_statistics failed: %d\n", error);
318 continue;
319 }
320
321 /* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
322 if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
323 (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
324 memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
325 sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
326 }
327 /* If no CHAIN_SIGNAL is reported, try to fill it in from the lsta sinfo. */
328 if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) == 0 &&
329 (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) != 0) {
330 sinfo.chains = lsta->sinfo.chains;
331 memcpy(sinfo.chain_signal, lsta->sinfo.chain_signal,
332 sizeof(sinfo.chain_signal));
333 sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
334 }
335
336 lkpi_nl80211_sta_info_to_str(s, " nl80211_sta_info (valid fields)", sinfo.filled);
337 sbuf_printf(s, " connected_time %u inactive_time %u\n",
338 sinfo.connected_time, sinfo.inactive_time);
339 sbuf_printf(s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
340 (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
341 sbuf_printf(s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
342 (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
343
344 sbuf_printf(s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
345 (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
346 sbuf_printf(s, " tx_duration %ju tx_retries %u\n",
347 (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
348
349 sbuf_printf(s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
350 sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
351 sbuf_printf(s, " generation %d assoc_req_ies_len %zu chains %#04x\n",
352 sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
353
354 for (int i = 0; i < nitems(sinfo.chain_signal) && i < IEEE80211_MAX_CHAINS; i++) {
355 if (!(sinfo.chains & BIT(i)))
356 continue;
357 sbuf_printf(s, " chain[%d] signal %d signal_avg %d\n",
358 i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
359 }
360
361 /* assoc_req_ies, bss_param, sta_flags */
362
363 sbuf_printf(s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
364 sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
365 sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
366 sinfo.rxrate.legacy * 100,
367 sinfo.rxrate.mcs, sinfo.rxrate.nss);
368 sbuf_printf(s, " he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
369 sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
370 sinfo.rxrate.eht_gi);
371 sbuf_printf(s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
372 sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
373 sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
374 sinfo.txrate.legacy * 100,
375 sinfo.txrate.mcs, sinfo.txrate.nss);
376 sbuf_printf(s, " he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
377 sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
378 sinfo.txrate.eht_gi);
379 }
380 wiphy_unlock(hw->wiphy);
381 }
382
383 static int
lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)384 lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
385 {
386 struct lkpi_vif *lvif;
387 struct sbuf s;
388
389 if (req->newptr)
390 return (EPERM);
391
392 lvif = (struct lkpi_vif *)arg1;
393
394 sbuf_new_for_sysctl(&s, NULL, 1024, req);
395
396 lkpi_80211_dump_lvif_stas(lvif, &s);
397
398 sbuf_finish(&s);
399 sbuf_delete(&s);
400
401 return (0);
402 }
403
404 static enum ieee80211_sta_rx_bandwidth
lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)405 lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
406 {
407 switch (cw) {
408 case NL80211_CHAN_WIDTH_320:
409 return (IEEE80211_STA_RX_BW_320);
410 case NL80211_CHAN_WIDTH_160:
411 case NL80211_CHAN_WIDTH_80P80:
412 return (IEEE80211_STA_RX_BW_160);
413 case NL80211_CHAN_WIDTH_80:
414 return (IEEE80211_STA_RX_BW_80);
415 case NL80211_CHAN_WIDTH_40:
416 return (IEEE80211_STA_RX_BW_40);
417 case NL80211_CHAN_WIDTH_20:
418 case NL80211_CHAN_WIDTH_20_NOHT:
419 return (IEEE80211_STA_RX_BW_20);
420 case NL80211_CHAN_WIDTH_5:
421 case NL80211_CHAN_WIDTH_10:
422 /* Unsupported input. */
423 return (IEEE80211_STA_RX_BW_20);
424 }
425 }
426
427 static enum nl80211_chan_width
lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bandwidth rx_bw)428 lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bandwidth rx_bw)
429 {
430 switch (rx_bw) {
431 case IEEE80211_STA_RX_BW_20:
432 return (NL80211_CHAN_WIDTH_20); /* _NOHT */
433 case IEEE80211_STA_RX_BW_40:
434 return (NL80211_CHAN_WIDTH_40);
435 case IEEE80211_STA_RX_BW_80:
436 return (NL80211_CHAN_WIDTH_80);
437 case IEEE80211_STA_RX_BW_160:
438 return (NL80211_CHAN_WIDTH_160); /* 80P80 */
439 case IEEE80211_STA_RX_BW_320:
440 return (NL80211_CHAN_WIDTH_320);
441 }
442 }
443
444 static void
lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)445 lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
446 struct ieee80211_vif *vif, struct ieee80211_sta *sta)
447 {
448 struct ieee80211_chanctx_conf *chanctx_conf;
449 enum ieee80211_sta_rx_bandwidth old_bw;
450 uint32_t changed;
451
452 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
453 lockdep_is_held(&hw->wiphy->mtx));
454 if (chanctx_conf == NULL)
455 return;
456
457 old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
458 if (old_bw == sta->deflink.bandwidth)
459 return;
460
461 chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
462 if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
463 !sta->deflink.ht_cap.ht_supported)
464 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
465
466 chanctx_conf->min_def = chanctx_conf->def;
467
468 vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
469
470 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
471 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
472 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
473 }
474
475 #if defined(LKPI_80211_HT)
476 static void
lkpi_sta_sync_ht_from_ni(struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni)477 lkpi_sta_sync_ht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
478 struct ieee80211_node *ni)
479 {
480 struct ieee80211vap *vap;
481 uint8_t *ie;
482 struct ieee80211_ht_cap *htcap;
483 int i, rx_nss;
484
485 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
486 sta->deflink.ht_cap.ht_supported = false;
487 return;
488 }
489
490 sta->deflink.ht_cap.ht_supported = true;
491
492 /* htcap->ampdu_params_info */
493 vap = ni->ni_vap;
494 sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
495 if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
496 sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
497 sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
498 if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
499 sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
500
501 ie = ni->ni_ies.htcap_ie;
502 KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
503 if (ie[0] == IEEE80211_ELEMID_VENDOR)
504 ie += 4;
505 ie += 2;
506 htcap = (struct ieee80211_ht_cap *)ie;
507 sta->deflink.ht_cap.cap = htcap->cap_info;
508 sta->deflink.ht_cap.mcs = htcap->mcs;
509
510 /*
511 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
512 * optional MCS for Nss=1..4. We need to check the first four
513 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
514 * MCS33.. is UEQM.
515 */
516 rx_nss = 0;
517 for (i = 0; i < 4; i++) {
518 if (htcap->mcs.rx_mask[i] != 0)
519 rx_nss++;
520 }
521 if (rx_nss > 0) {
522 sta->deflink.rx_nss = rx_nss;
523 } else {
524 sta->deflink.ht_cap.ht_supported = false;
525 return;
526 }
527
528 if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
529 IEEE80211_IS_CHAN_HT40(ni->ni_chan))
530 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
531 else
532 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
533
534 IMPROVE("sta->wme");
535
536 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
537 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
538 else
539 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
540 sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
541 #ifdef __handled_by_driver__ /* iwlwifi only? actually unused? */
542 for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
543 sta->deflink.agg.max_tid_amsdu_len[j] = ;
544 }
545 #endif
546 }
547 #endif
548
549 #if defined(LKPI_80211_VHT)
550 static void
lkpi_sta_sync_vht_from_ni(struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni)551 lkpi_sta_sync_vht_from_ni(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
552 struct ieee80211_node *ni)
553 {
554 enum ieee80211_sta_rx_bandwidth bw;
555 uint32_t width;
556 int rx_nss;
557 uint16_t rx_mcs_map;
558 uint8_t mcs;
559
560 if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
561 !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
562 sta->deflink.vht_cap.vht_supported = false;
563 return;
564 }
565
566 sta->deflink.vht_cap.vht_supported = true;
567
568 sta->deflink.vht_cap.cap = ni->ni_vhtcap;
569 sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
570
571 /*
572 * If VHT20/40 are selected do not update the bandwidth
573 * from HT but stya on VHT.
574 */
575 if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
576 goto skip_bw;
577
578 bw = sta->deflink.bandwidth;
579 width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
580 switch (width) {
581 /* Deprecated. */
582 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
583 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
584 bw = IEEE80211_STA_RX_BW_160;
585 break;
586 default:
587 /* Check if we do support 160Mhz somehow after all. */
588 if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
589 bw = IEEE80211_STA_RX_BW_160;
590 else
591 bw = IEEE80211_STA_RX_BW_80;
592 }
593 /*
594 * While we can set what is possibly supported we also need to be
595 * on a channel which supports that bandwidth; e.g., we can support
596 * VHT160 but the AP only does VHT80.
597 * Further ni_chan will also have filtered out what we disabled
598 * by configuration.
599 * Once net80211 channel selection is fixed for 802.11-2020 and
600 * VHT160 we can possibly spare ourselves the above.
601 */
602 if (bw == IEEE80211_STA_RX_BW_160 &&
603 !IEEE80211_IS_CHAN_VHT160(ni->ni_chan) &&
604 !IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
605 bw = IEEE80211_STA_RX_BW_80;
606 if (bw == IEEE80211_STA_RX_BW_80 &&
607 !IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
608 bw = sta->deflink.bandwidth;
609 sta->deflink.bandwidth = bw;
610 skip_bw:
611
612 rx_nss = 0;
613 rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
614 for (int i = 7; i >= 0; i--) {
615 mcs = rx_mcs_map >> (2 * i);
616 mcs &= 0x3;
617 if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
618 rx_nss = i + 1;
619 break;
620 }
621 }
622 if (rx_nss > 0)
623 sta->deflink.rx_nss = rx_nss;
624
625 switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
626 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
627 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
628 break;
629 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
630 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
631 break;
632 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
633 default:
634 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
635 break;
636 }
637 }
638 #endif
639
640 static void
lkpi_sta_sync_from_ni(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni,bool updchnctx)641 lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
642 struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
643 {
644
645 #if defined(LKPI_80211_HT)
646 lkpi_sta_sync_ht_from_ni(vif, sta, ni);
647 #endif
648 #if defined(LKPI_80211_VHT)
649 lkpi_sta_sync_vht_from_ni(vif, sta, ni);
650 #endif
651
652 /*
653 * Ensure rx_nss is at least 1 as otherwise drivers run into
654 * unexpected problems.
655 */
656 sta->deflink.rx_nss = MAX(1, sta->deflink.rx_nss);
657
658 /*
659 * We are also called from node allocation which net80211
660 * can do even on `ifconfig down`; in that case the chanctx
661 * may still be valid and we get a discrepancy between
662 * sta and chanctx. Thus do not try to update the chanctx
663 * when called from lkpi_lsta_alloc().
664 */
665 if (updchnctx)
666 lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
667 }
668
669 static uint8_t
lkpi_get_max_rx_chains(struct ieee80211_node * ni)670 lkpi_get_max_rx_chains(struct ieee80211_node *ni)
671 {
672 uint8_t chains;
673 #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
674 struct lkpi_sta *lsta;
675 struct ieee80211_sta *sta;
676
677 lsta = ni->ni_drv_data;
678 sta = LSTA_TO_STA(lsta);
679 #endif
680
681 chains = 1;
682 #if defined(LKPI_80211_HT)
683 IMPROVE("We should factor counting MCS/NSS out for sync and here");
684 if (sta->deflink.ht_cap.ht_supported)
685 chains = MAX(chains, sta->deflink.rx_nss);
686 #endif
687
688 #if defined(LKPI_80211_VHT)
689 if (sta->deflink.vht_cap.vht_supported)
690 chains = MAX(chains, sta->deflink.rx_nss);
691 #endif
692
693 return (chains);
694 }
695
696 static void
lkpi_lsta_dump(struct lkpi_sta * lsta,struct ieee80211_node * ni,const char * _f,int _l)697 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
698 const char *_f, int _l)
699 {
700
701 #ifdef LINUXKPI_DEBUG_80211
702 if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
703 return;
704 if (lsta == NULL)
705 return;
706
707 printf("%s:%d lsta %p ni %p sta %p\n",
708 _f, _l, lsta, ni, &lsta->sta);
709 if (ni != NULL)
710 ieee80211_dump_node(NULL, ni);
711 printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
712 printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
713 &lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
714 #endif
715 }
716
717 static void
lkpi_lsta_remove(struct lkpi_sta * lsta,struct lkpi_vif * lvif)718 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
719 {
720
721 lockdep_assert_wiphy(lsta->hw->wiphy);
722
723 KASSERT(!list_empty(&lsta->lsta_list),
724 ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
725 list_del_init(&lsta->lsta_list);
726 }
727
728 static struct lkpi_sta *
lkpi_lsta_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN],struct ieee80211_hw * hw,struct ieee80211_node * ni)729 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
730 struct ieee80211_hw *hw, struct ieee80211_node *ni)
731 {
732 struct lkpi_sta *lsta;
733 struct lkpi_vif *lvif;
734 struct ieee80211_vif *vif;
735 struct ieee80211_sta *sta;
736 int band, i, tid;
737
738 lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
739 M_NOWAIT | M_ZERO);
740 if (lsta == NULL)
741 return (NULL);
742
743 lsta->hw = hw;
744 lsta->added_to_drv = false;
745 lsta->state = IEEE80211_STA_NOTEXIST;
746 /*
747 * Link the ni to the lsta here without taking a reference.
748 * For one we would have to take the reference in node_init()
749 * as ieee80211_alloc_node() will initialise the refcount after us.
750 * For the other a ni and an lsta are 1:1 mapped and always together
751 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
752 * using the ni references for the lsta as well despite it being
753 * two separate allocations.
754 */
755 lsta->ni = ni;
756 /* The back-pointer "drv_data" to net80211_node let's us get lsta. */
757 ni->ni_drv_data = lsta;
758
759 lvif = VAP_TO_LVIF(vap);
760 vif = LVIF_TO_VIF(lvif);
761 sta = LSTA_TO_STA(lsta);
762
763 IEEE80211_ADDR_COPY(sta->addr, mac);
764
765 /* TXQ */
766 for (tid = 0; tid < nitems(sta->txq); tid++) {
767 struct lkpi_txq *ltxq;
768
769 /* We are not limiting ourselves to hw.queues here. */
770 ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
771 M_LKPI80211, M_NOWAIT | M_ZERO);
772 if (ltxq == NULL)
773 goto cleanup;
774 /* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
775 if (tid == IEEE80211_NUM_TIDS) {
776 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
777 free(ltxq, M_LKPI80211);
778 continue;
779 }
780 IMPROVE("AP/if we support non-STA here too");
781 ltxq->txq.ac = IEEE80211_AC_VO;
782 } else {
783 ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
784 }
785 ltxq->seen_dequeue = false;
786 ltxq->stopped = false;
787 ltxq->txq.vif = vif;
788 ltxq->txq.tid = tid;
789 ltxq->txq.sta = sta;
790 TAILQ_ELEM_INIT(ltxq, txq_entry);
791 skb_queue_head_init(<xq->skbq);
792 LKPI_80211_LTXQ_LOCK_INIT(ltxq);
793 sta->txq[tid] = <xq->txq;
794 }
795
796 /* Deflink information. */
797 for (band = 0; band < NUM_NL80211_BANDS; band++) {
798 struct ieee80211_supported_band *supband;
799
800 supband = hw->wiphy->bands[band];
801 if (supband == NULL)
802 continue;
803
804 for (i = 0; i < supband->n_bitrates; i++) {
805 switch (band) {
806 case NL80211_BAND_2GHZ:
807 switch (supband->bitrates[i].bitrate) {
808 case 240: /* 11g only */
809 case 120: /* 11g only */
810 case 110:
811 case 60: /* 11g only */
812 case 55:
813 case 20:
814 case 10:
815 sta->deflink.supp_rates[band] |= BIT(i);
816 break;
817 }
818 break;
819 case NL80211_BAND_5GHZ:
820 switch (supband->bitrates[i].bitrate) {
821 case 240:
822 case 120:
823 case 60:
824 sta->deflink.supp_rates[band] |= BIT(i);
825 break;
826 }
827 break;
828 }
829 }
830 }
831
832 sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
833 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
834 sta->deflink.rx_nss = 1;
835
836 lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
837
838 IMPROVE("he, eht, bw_320, ... smps_mode, ..");
839
840 /* Link configuration. */
841 IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
842 sta->link[0] = &sta->deflink;
843 for (i = 1; i < nitems(sta->link); i++) {
844 IMPROVE("more links; only link[0] = deflink currently.");
845 }
846 IMPROVE("11be");
847 sta->mlo = false;
848
849 /* Deferred TX path. */
850 LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
851 TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
852 mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
853 lsta->txq_ready = true;
854
855 return (lsta);
856
857 cleanup:
858 for (; tid >= 0; tid--) {
859 struct lkpi_txq *ltxq;
860
861 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
862 LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
863 free(sta->txq[tid], M_LKPI80211);
864 }
865 free(lsta, M_LKPI80211);
866 return (NULL);
867 }
868
869 static void
lkpi_lsta_free(struct lkpi_sta * lsta,struct ieee80211_node * ni)870 lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
871 {
872 struct mbuf *m;
873
874 if (lsta->added_to_drv)
875 panic("%s: Trying to free an lsta still known to firmware: "
876 "lsta %p ni %p added_to_drv %d\n",
877 __func__, lsta, ni, lsta->added_to_drv);
878
879 /* XXX-BZ free resources, ... */
880 IMPROVE();
881
882 /* Drain sta->txq[] */
883
884 LKPI_80211_LSTA_TXQ_LOCK(lsta);
885 lsta->txq_ready = false;
886 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
887
888 /* Drain taskq, won't be restarted until added_to_drv is set again. */
889 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
890 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
891
892 /* Flush mbufq (make sure to release ni refs!). */
893 m = mbufq_dequeue(&lsta->txq);
894 while (m != NULL) {
895 struct ieee80211_node *nim;
896
897 nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
898 if (nim != NULL)
899 ieee80211_free_node(nim);
900 m_freem(m);
901 m = mbufq_dequeue(&lsta->txq);
902 }
903 KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
904 __func__, lsta, mbufq_len(&lsta->txq)));
905 LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
906
907 /* Remove lsta from vif; that is done by the state machine. Should assert it? */
908
909 IMPROVE("Make sure everything is cleaned up.");
910
911 /* Free lsta. */
912 lsta->ni = NULL;
913 ni->ni_drv_data = NULL;
914 free(lsta, M_LKPI80211);
915 }
916
917
918 static enum nl80211_band
lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel * c)919 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
920 {
921
922 if (IEEE80211_IS_CHAN_2GHZ(c))
923 return (NL80211_BAND_2GHZ);
924 else if (IEEE80211_IS_CHAN_5GHZ(c))
925 return (NL80211_BAND_5GHZ);
926 #ifdef __notyet__
927 else if ()
928 return (NL80211_BAND_6GHZ);
929 else if ()
930 return (NL80211_BAND_60GHZ);
931 else if (IEEE80211_IS_CHAN_GSM(c))
932 return (NL80211_BAND_XXX);
933 #endif
934 else
935 panic("%s: unsupported band. c %p flags %#x\n",
936 __func__, c, c->ic_flags);
937 }
938
939 static uint32_t
lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)940 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
941 {
942
943 /* XXX-BZ this is just silly; net80211 is too convoluted. */
944 /* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
945 switch (band) {
946 case NL80211_BAND_2GHZ:
947 return (IEEE80211_CHAN_2GHZ);
948 break;
949 case NL80211_BAND_5GHZ:
950 return (IEEE80211_CHAN_5GHZ);
951 break;
952 case NL80211_BAND_60GHZ:
953 break;
954 case NL80211_BAND_6GHZ:
955 break;
956 default:
957 panic("%s: unsupported band %u\n", __func__, band);
958 break;
959 }
960
961 IMPROVE();
962 return (0x00);
963 }
964
965 #ifdef LINUXKPI_DEBUG_80211
966 static const char *
lkpi_nl80211_band_name(enum nl80211_band band)967 lkpi_nl80211_band_name(enum nl80211_band band)
968 {
969 switch (band) {
970 case NL80211_BAND_2GHZ:
971 return "2Ghz";
972 break;
973 case NL80211_BAND_5GHZ:
974 return "5Ghz";
975 break;
976 case NL80211_BAND_60GHZ:
977 return "60Ghz";
978 break;
979 case NL80211_BAND_6GHZ:
980 return "6Ghz";
981 break;
982 default:
983 panic("%s: unsupported band %u\n", __func__, band);
984 break;
985 }
986 }
987 #endif
988
989 #if 0
990 static enum ieee80211_ac_numbers
991 lkpi_ac_net_to_l80211(int ac)
992 {
993
994 switch (ac) {
995 case WME_AC_VO:
996 return (IEEE80211_AC_VO);
997 case WME_AC_VI:
998 return (IEEE80211_AC_VI);
999 case WME_AC_BE:
1000 return (IEEE80211_AC_BE);
1001 case WME_AC_BK:
1002 return (IEEE80211_AC_BK);
1003 default:
1004 printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
1005 return (IEEE80211_AC_BE);
1006 }
1007 }
1008 #endif
1009
1010 static enum nl80211_iftype
lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)1011 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
1012 {
1013
1014 switch (opmode) {
1015 case IEEE80211_M_IBSS:
1016 return (NL80211_IFTYPE_ADHOC);
1017 break;
1018 case IEEE80211_M_STA:
1019 return (NL80211_IFTYPE_STATION);
1020 break;
1021 case IEEE80211_M_WDS:
1022 return (NL80211_IFTYPE_WDS);
1023 break;
1024 case IEEE80211_M_HOSTAP:
1025 return (NL80211_IFTYPE_AP);
1026 break;
1027 case IEEE80211_M_MONITOR:
1028 return (NL80211_IFTYPE_MONITOR);
1029 break;
1030 case IEEE80211_M_MBSS:
1031 return (NL80211_IFTYPE_MESH_POINT);
1032 break;
1033 case IEEE80211_M_AHDEMO:
1034 /* FALLTHROUGH */
1035 default:
1036 printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
1037 /* FALLTHROUGH */
1038 }
1039 return (NL80211_IFTYPE_UNSPECIFIED);
1040 }
1041
1042 #ifdef LKPI_80211_HW_CRYPTO
1043 static const char *
lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)1044 lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
1045 {
1046 switch (wlan_cipher_suite) {
1047 case WLAN_CIPHER_SUITE_WEP40:
1048 return ("WEP40");
1049 case WLAN_CIPHER_SUITE_WEP104:
1050 return ("WEP104");
1051 case WLAN_CIPHER_SUITE_TKIP:
1052 return ("TKIP");
1053 case WLAN_CIPHER_SUITE_CCMP:
1054 return ("CCMP");
1055 case WLAN_CIPHER_SUITE_CCMP_256:
1056 return ("CCMP_256");
1057 case WLAN_CIPHER_SUITE_GCMP:
1058 return ("GCMP");
1059 case WLAN_CIPHER_SUITE_GCMP_256:
1060 return ("GCMP_256");
1061 case WLAN_CIPHER_SUITE_AES_CMAC:
1062 return ("AES_CMAC");
1063 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1064 return ("BIP_CMAC_256");
1065 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1066 return ("BIP_GMAC_128");
1067 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1068 return ("BIP_GMAC_256");
1069 default:
1070 return ("??");
1071 }
1072 }
1073
1074 static uint32_t
lkpi_l80211_to_net80211_cyphers(struct ieee80211com * ic,uint32_t wlan_cipher_suite)1075 lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
1076 uint32_t wlan_cipher_suite)
1077 {
1078 switch (wlan_cipher_suite) {
1079 case WLAN_CIPHER_SUITE_WEP40:
1080 return (IEEE80211_CRYPTO_WEP);
1081 case WLAN_CIPHER_SUITE_WEP104:
1082 return (IEEE80211_CRYPTO_WEP);
1083 case WLAN_CIPHER_SUITE_TKIP:
1084 return (IEEE80211_CRYPTO_TKIP);
1085 case WLAN_CIPHER_SUITE_CCMP:
1086 return (IEEE80211_CRYPTO_AES_CCM);
1087 case WLAN_CIPHER_SUITE_CCMP_256:
1088 return (IEEE80211_CRYPTO_AES_CCM_256);
1089 case WLAN_CIPHER_SUITE_GCMP:
1090 return (IEEE80211_CRYPTO_AES_GCM_128);
1091 case WLAN_CIPHER_SUITE_GCMP_256:
1092 return (IEEE80211_CRYPTO_AES_GCM_256);
1093 case WLAN_CIPHER_SUITE_AES_CMAC:
1094 return (IEEE80211_CRYPTO_BIP_CMAC_128);
1095 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1096 return (IEEE80211_CRYPTO_BIP_CMAC_256);
1097 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1098 return (IEEE80211_CRYPTO_BIP_GMAC_128);
1099 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1100 return (IEEE80211_CRYPTO_BIP_GMAC_256);
1101 default:
1102 ic_printf(ic, "%s: unknown WLAN Cipher Suite %#08x | %u (%s)\n",
1103 __func__,
1104 wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
1105 lkpi_cipher_suite_to_name(wlan_cipher_suite));
1106 return (0);
1107 }
1108 }
1109
1110 static uint32_t
lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher,uint8_t keylen)1111 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
1112 {
1113
1114 switch (cipher) {
1115 case IEEE80211_CIPHER_WEP:
1116 if (keylen == (40/NBBY))
1117 return (WLAN_CIPHER_SUITE_WEP40);
1118 else if (keylen == (104/NBBY))
1119 return (WLAN_CIPHER_SUITE_WEP104);
1120 else {
1121 printf("%s: WEP with unsupported keylen %d\n",
1122 __func__, keylen * NBBY);
1123 return (0);
1124 }
1125 break;
1126 case IEEE80211_CIPHER_TKIP:
1127 return (WLAN_CIPHER_SUITE_TKIP);
1128 case IEEE80211_CIPHER_AES_CCM:
1129 return (WLAN_CIPHER_SUITE_CCMP);
1130 case IEEE80211_CIPHER_AES_CCM_256:
1131 return (WLAN_CIPHER_SUITE_CCMP_256);
1132 case IEEE80211_CIPHER_AES_GCM_128:
1133 return (WLAN_CIPHER_SUITE_GCMP);
1134 case IEEE80211_CIPHER_AES_GCM_256:
1135 return (WLAN_CIPHER_SUITE_GCMP_256);
1136 case IEEE80211_CIPHER_BIP_CMAC_128:
1137 return (WLAN_CIPHER_SUITE_AES_CMAC);
1138 case IEEE80211_CIPHER_BIP_CMAC_256:
1139 return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1140 case IEEE80211_CIPHER_BIP_GMAC_128:
1141 return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1142 case IEEE80211_CIPHER_BIP_GMAC_256:
1143 return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1144
1145 case IEEE80211_CIPHER_AES_OCB:
1146 case IEEE80211_CIPHER_TKIPMIC:
1147 /*
1148 * TKIP w/ hw MIC support
1149 * (gone wrong; should really be a crypto flag in net80211).
1150 */
1151 case IEEE80211_CIPHER_CKIP:
1152 case IEEE80211_CIPHER_NONE:
1153 printf("%s: unsupported cipher %#010x\n", __func__, cipher);
1154 break;
1155 default:
1156 printf("%s: unknown cipher %#010x\n", __func__, cipher);
1157 };
1158 return (0);
1159 }
1160 #endif
1161
1162 #ifdef __notyet__
1163 static enum ieee80211_sta_state
lkpi_net80211_state_to_sta_state(enum ieee80211_state state)1164 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
1165 {
1166
1167 /*
1168 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
1169 * "done". Also ASSOC/AUTHORIZED are both "RUN" then?
1170 */
1171 switch (state) {
1172 case IEEE80211_S_INIT:
1173 return (IEEE80211_STA_NOTEXIST);
1174 case IEEE80211_S_SCAN:
1175 return (IEEE80211_STA_NONE);
1176 case IEEE80211_S_AUTH:
1177 return (IEEE80211_STA_AUTH);
1178 case IEEE80211_S_ASSOC:
1179 return (IEEE80211_STA_ASSOC);
1180 case IEEE80211_S_RUN:
1181 return (IEEE80211_STA_AUTHORIZED);
1182 case IEEE80211_S_CAC:
1183 case IEEE80211_S_CSA:
1184 case IEEE80211_S_SLEEP:
1185 default:
1186 UNIMPLEMENTED;
1187 };
1188
1189 return (IEEE80211_STA_NOTEXIST);
1190 }
1191 #endif
1192
1193 static struct linuxkpi_ieee80211_channel *
lkpi_find_lkpi80211_chan(struct lkpi_hw * lhw,struct ieee80211_channel * c)1194 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
1195 struct ieee80211_channel *c)
1196 {
1197 struct ieee80211_hw *hw;
1198 struct linuxkpi_ieee80211_channel *channels;
1199 enum nl80211_band band;
1200 int i, nchans;
1201
1202 hw = LHW_TO_HW(lhw);
1203 band = lkpi_net80211_chan_to_nl80211_band(c);
1204 if (hw->wiphy->bands[band] == NULL)
1205 return (NULL);
1206
1207 nchans = hw->wiphy->bands[band]->n_channels;
1208 if (nchans <= 0)
1209 return (NULL);
1210
1211 channels = hw->wiphy->bands[band]->channels;
1212 for (i = 0; i < nchans; i++) {
1213 if (channels[i].center_freq == c->ic_freq)
1214 return (&channels[i]);
1215 }
1216
1217 return (NULL);
1218 }
1219
1220 #if 0
1221 static struct linuxkpi_ieee80211_channel *
1222 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
1223 {
1224 struct linuxkpi_ieee80211_channel *chan;
1225 struct ieee80211_channel *c;
1226 struct lkpi_hw *lhw;
1227
1228 chan = NULL;
1229 if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
1230 c = ni->ni_chan;
1231 else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1232 c = ic->ic_bsschan;
1233 else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
1234 c = ic->ic_curchan;
1235 else
1236 c = NULL;
1237
1238 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
1239 lhw = ic->ic_softc;
1240 chan = lkpi_find_lkpi80211_chan(lhw, c);
1241 }
1242
1243 return (chan);
1244 }
1245 #endif
1246
1247 struct linuxkpi_ieee80211_channel *
linuxkpi_ieee80211_get_channel(struct wiphy * wiphy,uint32_t freq)1248 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
1249 {
1250 enum nl80211_band band;
1251
1252 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1253 struct ieee80211_supported_band *supband;
1254 struct linuxkpi_ieee80211_channel *channels;
1255 int i;
1256
1257 supband = wiphy->bands[band];
1258 if (supband == NULL || supband->n_channels == 0)
1259 continue;
1260
1261 channels = supband->channels;
1262 for (i = 0; i < supband->n_channels; i++) {
1263 if (channels[i].center_freq == freq)
1264 return (&channels[i]);
1265 }
1266 }
1267
1268 return (NULL);
1269 }
1270
1271 #ifdef LKPI_80211_HW_CRYPTO
1272 static int
lkpi_sta_del_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct lkpi_sta * lsta)1273 lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1274 struct lkpi_sta *lsta)
1275 {
1276 int error;
1277
1278 if (!lkpi_hwcrypto)
1279 return (0);
1280
1281 lockdep_assert_wiphy(hw->wiphy);
1282 ieee80211_ref_node(lsta->ni);
1283
1284 error = 0;
1285 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
1286 struct ieee80211_key_conf *kc;
1287 int err;
1288
1289 if (lsta->kc[keyix] == NULL)
1290 continue;
1291 kc = lsta->kc[keyix];
1292
1293 #ifdef LINUXKPI_DEBUG_80211
1294 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1295 ic_printf(lsta->ni->ni_ic, "%d %lu %s: running set_key cmd %d(%s) for "
1296 "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1297 curthread->td_tid, jiffies, __func__,
1298 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1299 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1300 #endif
1301
1302 err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
1303 LSTA_TO_STA(lsta), kc);
1304 if (err != 0) {
1305 ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1306 "sta %6D failed: %d\n", curthread->td_tid, jiffies, __func__,
1307 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":", err);
1308 error++;
1309
1310 /*
1311 * If we free the key here we will never be able to get it
1312 * removed from the driver/fw which will likely make us
1313 * crash (firmware).
1314 */
1315 continue;
1316 }
1317 #ifdef LINUXKPI_DEBUG_80211
1318 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1319 ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1320 "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
1321 curthread->td_tid, jiffies, __func__,
1322 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1323 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1324 #endif
1325
1326 lsta->kc[keyix] = NULL;
1327 free(kc, M_LKPI80211);
1328 }
1329 ieee80211_free_node(lsta->ni);
1330 return (error);
1331 }
1332
1333 /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
1334 /* See also lkpi_sta_del_keys() these days. */
1335 static int
lkpi_iv_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)1336 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1337 {
1338 struct ieee80211com *ic;
1339 struct lkpi_hw *lhw;
1340 struct ieee80211_hw *hw;
1341 struct lkpi_vif *lvif;
1342 struct lkpi_sta *lsta;
1343 struct ieee80211_vif *vif;
1344 struct ieee80211_sta *sta;
1345 struct ieee80211_node *ni;
1346 struct ieee80211_key_conf *kc;
1347 int error;
1348
1349 ic = vap->iv_ic;
1350 lhw = ic->ic_softc;
1351 hw = LHW_TO_HW(lhw);
1352 lvif = VAP_TO_LVIF(vap);
1353 vif = LVIF_TO_VIF(lvif);
1354
1355 /*
1356 * Make sure we do not make it here without going through
1357 * lkpi_iv_key_update_begin() first.
1358 */
1359 lockdep_assert_wiphy(hw->wiphy);
1360
1361 /*
1362 * While we are assoc we may still send packets. We cannot delete the
1363 * keys as otherwise packets could go out unencrypted. Some firmware
1364 * does not like this and will fire an assert.
1365 * net80211 needs to drive this better but given we want the disassoc
1366 * frame out and have to unlock we are open to a race currently.
1367 * This check should prevent problems.
1368 * How to test: run 800Mbit/s UDP traffic and during that restart your
1369 * supplicant. You want to survive that.
1370 */
1371 if (vif->cfg.assoc) {
1372 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1373 ic_printf(ic, "%d %lu %s: vif still assoc; not deleting keys\n",
1374 curthread->td_tid, jiffies, __func__);
1375 return (0);
1376 }
1377
1378 if (IEEE80211_KEY_UNDEFINED(k)) {
1379 ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1380 __func__, vap, k, k->wk_cipher, k->wk_keyix);
1381 return (0);
1382 }
1383
1384 if (vap->iv_bss == NULL) {
1385 ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1386 __func__, vap->iv_bss, vap);
1387 return (0);
1388 }
1389
1390 ni = ieee80211_ref_node(vap->iv_bss);
1391 lsta = ni->ni_drv_data;
1392 if (lsta == NULL) {
1393 ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1394 __func__, ni, ni->ni_bssid, ":");
1395 ieee80211_free_node(ni);
1396 return (0);
1397 }
1398 sta = LSTA_TO_STA(lsta);
1399
1400 if (lsta->kc[k->wk_keyix] == NULL) {
1401 #ifdef LINUXKPI_DEBUG_80211
1402 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1403 ic_printf(ic, "%d %lu %s: sta %6D and no key information, "
1404 "keyidx %u wk_macaddr %6D; returning success\n",
1405 curthread->td_tid, jiffies, __func__, sta->addr, ":",
1406 k->wk_keyix, k->wk_macaddr, ":");
1407 #endif
1408 ieee80211_free_node(ni);
1409 return (1);
1410 }
1411 kc = lsta->kc[k->wk_keyix];
1412
1413 #ifdef LINUXKPI_DEBUG_80211
1414 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1415 ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1416 "keyidx %u hw_key_idx %u flags %b\n",
1417 curthread->td_tid, jiffies, __func__,
1418 DISABLE_KEY, "DISABLE", sta->addr, ":",
1419 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1420 #endif
1421
1422 error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
1423 if (error != 0) {
1424 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1425 curthread->td_tid, jiffies, __func__,
1426 DISABLE_KEY, "DISABLE", sta->addr, ":", error);
1427 error = 0;
1428 goto out;
1429 }
1430
1431 #ifdef LINUXKPI_DEBUG_80211
1432 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1433 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1434 "keyidx %u hw_key_idx %u flags %b\n",
1435 curthread->td_tid, jiffies, __func__,
1436 DISABLE_KEY, "DISABLE", sta->addr, ":",
1437 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1438 #endif
1439 lsta->kc[k->wk_keyix] = NULL;
1440 free(kc, M_LKPI80211);
1441 error = 1;
1442 out:
1443 ieee80211_free_node(ni);
1444 return (error);
1445 }
1446
1447 static int
lkpi_iv_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)1448 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1449 {
1450 struct ieee80211com *ic;
1451 struct lkpi_hw *lhw;
1452 struct ieee80211_hw *hw;
1453 struct lkpi_vif *lvif;
1454 struct lkpi_sta *lsta;
1455 struct ieee80211_vif *vif;
1456 struct ieee80211_sta *sta;
1457 struct ieee80211_node *ni;
1458 struct ieee80211_key_conf *kc;
1459 uint32_t lcipher;
1460 uint16_t exp_flags;
1461 uint8_t keylen;
1462 int error;
1463
1464 ic = vap->iv_ic;
1465 lhw = ic->ic_softc;
1466 hw = LHW_TO_HW(lhw);
1467
1468 /*
1469 * Make sure we do not make it here without going through
1470 * lkpi_iv_key_update_begin() first.
1471 */
1472 lockdep_assert_wiphy(hw->wiphy);
1473
1474 if (IEEE80211_KEY_UNDEFINED(k)) {
1475 ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1476 __func__, vap, k, k->wk_cipher, k->wk_keyix);
1477 return (0);
1478 }
1479
1480 if (vap->iv_bss == NULL) {
1481 ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1482 __func__, vap->iv_bss, vap);
1483 return (0);
1484 }
1485 ni = ieee80211_ref_node(vap->iv_bss);
1486 lsta = ni->ni_drv_data;
1487 if (lsta == NULL) {
1488 ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1489 __func__, ni, ni->ni_bssid, ":");
1490 ieee80211_free_node(ni);
1491 return (0);
1492 }
1493 sta = LSTA_TO_STA(lsta);
1494
1495 keylen = k->wk_keylen;
1496 lcipher = lkpi_net80211_to_l80211_cipher_suite(
1497 k->wk_cipher->ic_cipher, k->wk_keylen);
1498 switch (lcipher) {
1499 case WLAN_CIPHER_SUITE_TKIP:
1500 keylen += 2 * k->wk_cipher->ic_miclen;
1501 break;
1502 case WLAN_CIPHER_SUITE_CCMP:
1503 case WLAN_CIPHER_SUITE_GCMP:
1504 break;
1505 default:
1506 ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
1507 __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
1508 IMPROVE();
1509 ieee80211_free_node(ni);
1510 return (0);
1511 }
1512
1513 if (lsta->kc[k->wk_keyix] != NULL) {
1514 IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
1515 ic_printf(ic, "%s: sta %6D found with key information\n",
1516 __func__, sta->addr, ":");
1517 kc = lsta->kc[k->wk_keyix];
1518 lsta->kc[k->wk_keyix] = NULL;
1519 free(kc, M_LKPI80211);
1520 kc = NULL; /* safeguard */
1521 }
1522
1523 kc = malloc(sizeof(*kc) + keylen, M_LKPI80211, M_WAITOK | M_ZERO);
1524 kc->_k = k; /* Save the pointer to net80211. */
1525 kc->cipher = lcipher;
1526 kc->keyidx = k->wk_keyix;
1527 #if 0
1528 kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
1529 #endif
1530 atomic64_set(&kc->tx_pn, k->wk_keytsc);
1531 kc->keylen = k->wk_keylen;
1532 memcpy(kc->key, k->wk_key, k->wk_keylen);
1533
1534 if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1535 kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
1536 if (k->wk_flags & IEEE80211_KEY_GROUP)
1537 kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
1538
1539 kc->iv_len = k->wk_cipher->ic_header;
1540 kc->icv_len = k->wk_cipher->ic_trailer;
1541
1542 switch (kc->cipher) {
1543 case WLAN_CIPHER_SUITE_TKIP:
1544 memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, k->wk_txmic, k->wk_cipher->ic_miclen);
1545 memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, k->wk_rxmic, k->wk_cipher->ic_miclen);
1546 break;
1547 case WLAN_CIPHER_SUITE_CCMP:
1548 case WLAN_CIPHER_SUITE_GCMP:
1549 break;
1550 default:
1551 /* currently UNREACH */
1552 IMPROVE();
1553 break;
1554 };
1555 lsta->kc[k->wk_keyix] = kc;
1556
1557 #ifdef LINUXKPI_DEBUG_80211
1558 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1559 ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1560 "kc %p keyidx %u hw_key_idx %u keylen %u flags %b\n",
1561 curthread->td_tid, jiffies, __func__,
1562 SET_KEY, "SET", sta->addr, ":", kc, kc->keyidx, kc->hw_key_idx,
1563 kc->keylen, kc->flags, IEEE80211_KEY_FLAG_BITS);
1564 #endif
1565
1566 lvif = VAP_TO_LVIF(vap);
1567 vif = LVIF_TO_VIF(lvif);
1568 error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
1569 if (error != 0) {
1570 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1571 curthread->td_tid, jiffies, __func__,
1572 SET_KEY, "SET", sta->addr, ":", error);
1573 lsta->kc[k->wk_keyix] = NULL;
1574 free(kc, M_LKPI80211);
1575 ieee80211_free_node(ni);
1576 return (0);
1577 }
1578
1579 #ifdef LINUXKPI_DEBUG_80211
1580 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1581 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1582 "kc %p keyidx %u hw_key_idx %u flags %b\n",
1583 curthread->td_tid, jiffies, __func__,
1584 SET_KEY, "SET", sta->addr, ":",
1585 kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1586 #endif
1587
1588 exp_flags = 0;
1589 switch (kc->cipher) {
1590 case WLAN_CIPHER_SUITE_TKIP:
1591 exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1592 IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1593 IEEE80211_KEY_FLAG_GENERATE_MMIC |
1594 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
1595 #define TKIP_INVAL_COMBINATION \
1596 (IEEE80211_KEY_FLAG_PUT_MIC_SPACE|IEEE80211_KEY_FLAG_GENERATE_MMIC)
1597 if ((kc->flags & TKIP_INVAL_COMBINATION) == TKIP_INVAL_COMBINATION) {
1598 ic_printf(ic, "%s: SET_KEY for %s returned invalid "
1599 "combination %b\n", __func__,
1600 lkpi_cipher_suite_to_name(kc->cipher),
1601 kc->flags, IEEE80211_KEY_FLAG_BITS);
1602 }
1603 #undef TKIP_INVAL_COMBINATION
1604 #ifdef __notyet__
1605 /* Do flags surgery; special see linuxkpi_ieee80211_ifattach(). */
1606 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) != 0) {
1607 k->wk_flags &= ~(IEEE80211_KEY_NOMICMGT|IEEE80211_KEY_NOMIC);
1608 k->wk_flags |= IEEE80211_KEY_SWMIC;
1609 ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC
1610 }
1611 #endif
1612 break;
1613 case WLAN_CIPHER_SUITE_CCMP:
1614 case WLAN_CIPHER_SUITE_GCMP:
1615 exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1616 IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1617 IEEE80211_KEY_FLAG_GENERATE_IV |
1618 IEEE80211_KEY_FLAG_GENERATE_IV_MGMT | /* Only needs IV geeration for MGMT frames. */
1619 IEEE80211_KEY_FLAG_SW_MGMT_TX); /* MFP in software */
1620 break;
1621 }
1622 if ((kc->flags & ~exp_flags) != 0)
1623 ic_printf(ic, "%s: SET_KEY for %s returned unexpected key flags: "
1624 " %#06x & ~%#06x = %b\n", __func__,
1625 lkpi_cipher_suite_to_name(kc->cipher), kc->flags, exp_flags,
1626 (kc->flags & ~exp_flags), IEEE80211_KEY_FLAG_BITS);
1627
1628 #ifdef __notyet__
1629 /* Do flags surgery. */
1630 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) == 0)
1631 k->wk_flags |= IEEE80211_KEY_NOIVMGT;
1632 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
1633 k->wk_flags |= IEEE80211_KEY_NOIV;
1634 #endif
1635
1636 ieee80211_free_node(ni);
1637 return (1);
1638 }
1639
1640 static void
lkpi_iv_key_update_begin(struct ieee80211vap * vap)1641 lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1642 {
1643 struct ieee80211_node_table *nt;
1644 struct ieee80211com *ic;
1645 struct lkpi_hw *lhw;
1646 struct ieee80211_hw *hw;
1647 struct lkpi_vif *lvif;
1648 struct ieee80211_node *ni;
1649 bool icislocked, ntislocked;
1650
1651 ic = vap->iv_ic;
1652 lhw = ic->ic_softc;
1653 hw = LHW_TO_HW(lhw);
1654 lvif = VAP_TO_LVIF(vap);
1655 nt = &ic->ic_sta;
1656
1657 icislocked = IEEE80211_IS_LOCKED(ic);
1658 ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1659
1660 #ifdef LINUXKPI_DEBUG_80211
1661 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1662 ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
1663 "lvif ic_unlocked %d nt_unlocked %d\n",
1664 curthread->td_tid, jiffies, __func__, vap,
1665 ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1666 lvif->ic_unlocked, lvif->nt_unlocked);
1667 #endif
1668
1669 /*
1670 * This is inconsistent net80211 locking to be fixed one day.
1671 */
1672 /* Try to make sure the node does not go away while possibly unlocked. */
1673 ni = NULL;
1674 if (icislocked || ntislocked) {
1675 if (vap->iv_bss != NULL)
1676 ni = ieee80211_ref_node(vap->iv_bss);
1677 }
1678
1679 if (icislocked)
1680 IEEE80211_UNLOCK(ic);
1681 if (ntislocked)
1682 IEEE80211_NODE_UNLOCK(nt);
1683
1684 wiphy_lock(hw->wiphy);
1685
1686 KASSERT(lvif->key_update_iv_bss == NULL, ("%s: key_update_iv_bss not NULL %p",
1687 __func__, lvif->key_update_iv_bss));
1688 lvif->key_update_iv_bss = ni;
1689
1690 /*
1691 * ic/nt_unlocked could be a bool given we are under the lock and there
1692 * must only be a single thread.
1693 * In case anything in the future disturbs the order the refcnt will
1694 * help us catching problems a lot easier.
1695 */
1696 if (icislocked)
1697 refcount_acquire(&lvif->ic_unlocked);
1698 if (ntislocked)
1699 refcount_acquire(&lvif->nt_unlocked);
1700
1701 /*
1702 * Stop the queues while doing key updates.
1703 */
1704 ieee80211_stop_queues(hw);
1705 }
1706
1707 static void
lkpi_iv_key_update_end(struct ieee80211vap * vap)1708 lkpi_iv_key_update_end(struct ieee80211vap *vap)
1709 {
1710 struct ieee80211_node_table *nt;
1711 struct ieee80211com *ic;
1712 struct lkpi_hw *lhw;
1713 struct ieee80211_hw *hw;
1714 struct lkpi_vif *lvif;
1715 bool icislocked, ntislocked;
1716
1717 ic = vap->iv_ic;
1718 lhw = ic->ic_softc;
1719 hw = LHW_TO_HW(lhw);
1720 lvif = VAP_TO_LVIF(vap);
1721 nt = &ic->ic_sta;
1722
1723 /*
1724 * Re-enabled the queues after the key update.
1725 */
1726 lkpi_ieee80211_wake_queues_locked(hw);
1727
1728 icislocked = IEEE80211_IS_LOCKED(ic);
1729 MPASS(!icislocked);
1730 ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1731 MPASS(!ntislocked);
1732
1733 #ifdef LINUXKPI_DEBUG_80211
1734 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1735 ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
1736 "lvif ic_unlocked %d nt_unlocked %d\n",
1737 curthread->td_tid, jiffies, __func__, vap,
1738 ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1739 lvif->ic_unlocked, lvif->nt_unlocked);
1740 #endif
1741
1742 /*
1743 * Check under lock; see comment in lkpi_iv_key_update_begin().
1744 * In case the refcnt gets out of sync locking in net80211 will
1745 * quickly barf as well (trying to unlock a lock not held).
1746 */
1747 icislocked = refcount_release_if_last(&lvif->ic_unlocked);
1748 ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
1749
1750 if (lvif->key_update_iv_bss != NULL) {
1751 ieee80211_free_node(lvif->key_update_iv_bss);
1752 lvif->key_update_iv_bss = NULL;
1753 }
1754
1755 wiphy_unlock(hw->wiphy);
1756
1757 /*
1758 * This is inconsistent net80211 locking to be fixed one day.
1759 * ic before nt to avoid a LOR.
1760 */
1761 if (icislocked)
1762 IEEE80211_LOCK(ic);
1763 if (ntislocked)
1764 IEEE80211_NODE_LOCK(nt);
1765 }
1766 #endif
1767
1768 static void
lkpi_cleanup_mcast_list_locked(struct lkpi_hw * lhw)1769 lkpi_cleanup_mcast_list_locked(struct lkpi_hw *lhw)
1770 {
1771 struct list_head *le, *next;
1772 struct netdev_hw_addr *addr;
1773
1774 if (lhw->mc_list.count != 0) {
1775 list_for_each_safe(le, next, &lhw->mc_list.addr_list) {
1776 addr = list_entry(le, struct netdev_hw_addr, addr_list);
1777 list_del(le);
1778 lhw->mc_list.count--;
1779 free(addr, M_LKPI80211);
1780 }
1781 }
1782 KASSERT(lhw->mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
1783 __func__, &lhw->mc_list, lhw->mc_list.count));
1784 }
1785
1786 static u_int
lkpi_ic_update_mcast_copy(void * arg,struct sockaddr_dl * sdl,u_int cnt)1787 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
1788 {
1789 struct netdev_hw_addr_list *mc_list;
1790 struct netdev_hw_addr *addr;
1791
1792 KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
1793 __func__, arg, sdl, cnt));
1794
1795 mc_list = arg;
1796 /* If it is on the list already skip it. */
1797 netdev_hw_addr_list_for_each(addr, mc_list) {
1798 if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
1799 return (0);
1800 }
1801
1802 addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
1803 if (addr == NULL)
1804 return (0);
1805
1806 INIT_LIST_HEAD(&addr->addr_list);
1807 memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
1808 /* XXX this should be a netdev function? */
1809 list_add(&addr->addr_list, &mc_list->addr_list);
1810 mc_list->count++;
1811
1812 #ifdef LINUXKPI_DEBUG_80211
1813 if (linuxkpi_debug_80211 & D80211_TRACE)
1814 printf("%s:%d: mc_list count %d: added %6D\n",
1815 __func__, __LINE__, mc_list->count, addr->addr, ":");
1816 #endif
1817
1818 return (1);
1819 }
1820
1821 static void
lkpi_update_mcast_filter(struct ieee80211com * ic)1822 lkpi_update_mcast_filter(struct ieee80211com *ic)
1823 {
1824 struct lkpi_hw *lhw;
1825 struct ieee80211_hw *hw;
1826 u64 mc;
1827 unsigned int changed_flags, flags;
1828 bool scanning;
1829
1830 lhw = ic->ic_softc;
1831
1832 if (lhw->ops->prepare_multicast == NULL ||
1833 lhw->ops->configure_filter == NULL)
1834 return;
1835
1836 LKPI_80211_LHW_SCAN_LOCK(lhw);
1837 scanning = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1838 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1839
1840 LKPI_80211_LHW_MC_LOCK(lhw);
1841
1842 flags = 0;
1843 if (scanning)
1844 flags |= FIF_BCN_PRBRESP_PROMISC;
1845 if (lhw->mc_all_multi)
1846 flags |= FIF_ALLMULTI;
1847
1848 hw = LHW_TO_HW(lhw);
1849 mc = lkpi_80211_mo_prepare_multicast(hw, &lhw->mc_list);
1850
1851 changed_flags = (lhw->mc_flags ^ flags) & FIF_FLAGS_MASK;
1852 lkpi_80211_mo_configure_filter(hw, changed_flags, &flags, mc);
1853 lhw->mc_flags = flags;
1854
1855 #ifdef LINUXKPI_DEBUG_80211
1856 if (linuxkpi_debug_80211 & D80211_TRACE)
1857 printf("%s: changed_flags %#06x count %d mc_flags %#010x\n",
1858 __func__, changed_flags, lhw->mc_list.count, lhw->mc_flags);
1859 #endif
1860
1861 LKPI_80211_LHW_MC_UNLOCK(lhw);
1862 }
1863
1864 static enum ieee80211_bss_changed
lkpi_update_dtim_tsf(struct ieee80211_vif * vif,struct ieee80211_node * ni,struct ieee80211vap * vap,const char * _f,int _l)1865 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
1866 struct ieee80211vap *vap, const char *_f, int _l)
1867 {
1868 enum ieee80211_bss_changed bss_changed;
1869
1870 bss_changed = 0;
1871
1872 #ifdef LINUXKPI_DEBUG_80211
1873 if (linuxkpi_debug_80211 & D80211_TRACE)
1874 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1875 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1876 "sync_device_ts %u bss_changed %#010jx\n",
1877 __func__, __LINE__, _f, _l,
1878 vif->cfg.assoc, vif->cfg.aid,
1879 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1880 vif->bss_conf.sync_dtim_count,
1881 (uintmax_t)vif->bss_conf.sync_tsf,
1882 vif->bss_conf.sync_device_ts,
1883 (uintmax_t)bss_changed);
1884 #endif
1885
1886 if (vif->bss_conf.beacon_int != ni->ni_intval) {
1887 vif->bss_conf.beacon_int = ni->ni_intval;
1888 /* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
1889 if (vif->bss_conf.beacon_int < 16)
1890 vif->bss_conf.beacon_int = 16;
1891 bss_changed |= BSS_CHANGED_BEACON_INT;
1892 }
1893
1894 /*
1895 * lkpi_iv_sta_recv_mgmt() will directly call into this function.
1896 * iwlwifi(4) in iwl_mvm_bss_info_changed_station_common() will
1897 * stop seesion protection the moment it sees
1898 * BSS_CHANGED_BEACON_INFO (with the expectations that it was
1899 * "a beacon from the associated AP"). It will also update
1900 * the beacon filter in that case. This is the only place
1901 * we set the BSS_CHANGED_BEACON_INFO on the non-teardown
1902 * path so make sure we only do run this check once we are
1903 * assoc. (*iv_recv_mgmt)() will be called before we enter
1904 * here so the ni will be updates with information from the
1905 * beacon via net80211::sta_recv_mgmt(). We also need to
1906 * make sure we do not do it on every beacon we still may
1907 * get so only do if something changed. vif->bss_conf.dtim_period
1908 * should be 0 as we start up (we also reset it on teardown).
1909 */
1910 if (vif->cfg.assoc &&
1911 vif->bss_conf.dtim_period != ni->ni_dtim_period &&
1912 ni->ni_dtim_period > 0) {
1913 vif->bss_conf.dtim_period = ni->ni_dtim_period;
1914 bss_changed |= BSS_CHANGED_BEACON_INFO;
1915 }
1916
1917 vif->bss_conf.sync_dtim_count = ni->ni_dtim_count;
1918 vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1919 /* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1920
1921 #ifdef LINUXKPI_DEBUG_80211
1922 if (linuxkpi_debug_80211 & D80211_TRACE)
1923 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1924 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1925 "sync_device_ts %u bss_changed %#010jx\n",
1926 __func__, __LINE__, _f, _l,
1927 vif->cfg.assoc, vif->cfg.aid,
1928 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1929 vif->bss_conf.sync_dtim_count,
1930 (uintmax_t)vif->bss_conf.sync_tsf,
1931 vif->bss_conf.sync_device_ts,
1932 (uintmax_t)bss_changed);
1933 #endif
1934
1935 return (bss_changed);
1936 }
1937
1938 static void
lkpi_stop_hw_scan(struct lkpi_hw * lhw,struct ieee80211_vif * vif)1939 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
1940 {
1941 struct ieee80211_hw *hw;
1942 int error;
1943 bool cancel;
1944
1945 TRACE_SCAN(lhw->ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
1946
1947 LKPI_80211_LHW_SCAN_LOCK(lhw);
1948 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1949 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1950 if (!cancel)
1951 return;
1952
1953 hw = LHW_TO_HW(lhw);
1954
1955 IEEE80211_UNLOCK(lhw->ic);
1956 wiphy_lock(hw->wiphy);
1957 /* Need to cancel the scan. */
1958 lkpi_80211_mo_cancel_hw_scan(hw, vif);
1959 wiphy_unlock(hw->wiphy);
1960
1961 /* Need to make sure we see ieee80211_scan_completed. */
1962 LKPI_80211_LHW_SCAN_LOCK(lhw);
1963 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
1964 error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
1965 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1966 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1967
1968 IEEE80211_LOCK(lhw->ic);
1969
1970 if (cancel)
1971 ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
1972 __func__, error, lhw, vif);
1973 }
1974
1975 static void
lkpi_hw_conf_idle(struct ieee80211_hw * hw,bool new)1976 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1977 {
1978 struct lkpi_hw *lhw;
1979 int error;
1980 bool old;
1981
1982 old = hw->conf.flags & IEEE80211_CONF_IDLE;
1983 if (old == new)
1984 return;
1985
1986 hw->conf.flags ^= IEEE80211_CONF_IDLE;
1987 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1988 if (error != 0 && error != EOPNOTSUPP) {
1989 lhw = HW_TO_LHW(hw);
1990 ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1991 __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1992 }
1993 }
1994
1995 static enum ieee80211_bss_changed
lkpi_disassoc(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct lkpi_hw * lhw)1996 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1997 struct lkpi_hw *lhw)
1998 {
1999 enum ieee80211_bss_changed changed;
2000 struct lkpi_vif *lvif;
2001
2002 changed = 0;
2003 sta->aid = 0;
2004 if (vif->cfg.assoc) {
2005
2006 vif->cfg.assoc = false;
2007 vif->cfg.aid = 0;
2008 changed |= BSS_CHANGED_ASSOC;
2009 IMPROVE();
2010
2011 lkpi_update_mcast_filter(lhw->ic);
2012
2013 /*
2014 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
2015 * assoc = false right away here will remove the sta from
2016 * firmware for iwlwifi.
2017 * We no longer do this but only return the BSS_CHNAGED value.
2018 * The caller is responsible for removing the sta gong to
2019 * IEEE80211_STA_NOTEXIST and then executing the
2020 * bss_info_changed() update.
2021 * See lkpi_sta_run_to_init() for more detailed comment.
2022 */
2023
2024 lvif = VIF_TO_LVIF(vif);
2025 lvif->beacons = 0;
2026 }
2027
2028 return (changed);
2029 }
2030
2031 static void
lkpi_wake_tx_queues(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool dequeue_seen,bool no_emptyq)2032 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
2033 bool dequeue_seen, bool no_emptyq)
2034 {
2035 struct lkpi_txq *ltxq;
2036 int tid;
2037 bool ltxq_empty;
2038
2039 /* Wake up all queues to know they are allocated in the driver. */
2040 for (tid = 0; tid < nitems(sta->txq); tid++) {
2041
2042 if (tid == IEEE80211_NUM_TIDS) {
2043 IMPROVE("station specific?");
2044 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
2045 continue;
2046 } else if (tid >= hw->queues)
2047 continue;
2048
2049 if (sta->txq[tid] == NULL)
2050 continue;
2051
2052 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
2053 if (dequeue_seen && !ltxq->seen_dequeue)
2054 continue;
2055
2056 LKPI_80211_LTXQ_LOCK(ltxq);
2057 ltxq_empty = skb_queue_empty(<xq->skbq);
2058 LKPI_80211_LTXQ_UNLOCK(ltxq);
2059 if (no_emptyq && ltxq_empty)
2060 continue;
2061
2062 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
2063 }
2064 }
2065
2066 /*
2067 * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
2068 * packet. The problem is that the state machine functions tend to hold the
2069 * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
2070 * We call this after dropping the ic lock and before acquiring the LHW lock.
2071 * we make sure no further packets are queued and if they are queued the task
2072 * will finish or be cancelled. At the end if a packet is left we manually
2073 * send it. scan_to_auth() would re-enable sending if the lsta would be
2074 * re-used.
2075 */
2076 static void
lkpi_80211_flush_tx(struct lkpi_hw * lhw,struct lkpi_sta * lsta)2077 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
2078 {
2079 struct ieee80211_hw *hw;
2080 struct mbufq mq;
2081 struct mbuf *m;
2082 int len;
2083
2084 /* There is no lockdep_assert_not_held_wiphy(). */
2085 hw = LHW_TO_HW(lhw);
2086 lockdep_assert_not_held(&hw->wiphy->mtx);
2087
2088 /* Do not accept any new packets until scan_to_auth or lsta_free(). */
2089 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2090 lsta->txq_ready = false;
2091 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2092
2093 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
2094 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
2095
2096 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2097 len = mbufq_len(&lsta->txq);
2098 if (len <= 0) {
2099 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2100 return;
2101 }
2102
2103 mbufq_init(&mq, IFQ_MAXLEN);
2104 mbufq_concat(&mq, &lsta->txq);
2105 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2106
2107 m = mbufq_dequeue(&mq);
2108 while (m != NULL) {
2109 lkpi_80211_txq_tx_one(lsta, m);
2110 m = mbufq_dequeue(&mq);
2111 }
2112 }
2113
2114
2115 static void
lkpi_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2116 lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2117 {
2118 struct ieee80211_chanctx_conf *chanctx_conf;
2119 struct lkpi_chanctx *lchanctx;
2120
2121 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2122 lockdep_is_held(&hw->wiphy->mtx));
2123
2124 if (chanctx_conf == NULL)
2125 return;
2126
2127 /* Remove vif context. */
2128 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
2129
2130 lkpi_hw_conf_idle(hw, true);
2131
2132 /* Remove chan ctx. */
2133 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2134
2135 /* Cleanup. */
2136 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2137 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2138 list_del(&lchanctx->entry);
2139 free(lchanctx, M_LKPI80211);
2140 }
2141
2142
2143 /* -------------------------------------------------------------------------- */
2144
2145 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2146 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2147 {
2148
2149 return (0);
2150 }
2151
2152 /* lkpi_iv_newstate() handles the stop scan case generally. */
2153 #define lkpi_sta_scan_to_init(_v, _n, _a) lkpi_sta_state_do_nada(_v, _n, _a)
2154
2155 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2156 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2157 {
2158 struct linuxkpi_ieee80211_channel *chan;
2159 struct lkpi_chanctx *lchanctx;
2160 struct ieee80211_chanctx_conf *chanctx_conf;
2161 struct lkpi_hw *lhw;
2162 struct ieee80211_hw *hw;
2163 struct lkpi_vif *lvif;
2164 struct ieee80211_vif *vif;
2165 struct ieee80211_node *ni;
2166 struct lkpi_sta *lsta;
2167 enum ieee80211_bss_changed bss_changed;
2168 struct ieee80211_prep_tx_info prep_tx_info;
2169 uint32_t changed;
2170 int error;
2171 bool synched;
2172
2173 /*
2174 * In here we use vap->iv_bss until lvif->lvif_bss is set.
2175 * For all later (STATE >= AUTH) functions we need to use the lvif
2176 * cache which will be tracked even through (*iv_update_bss)().
2177 */
2178
2179 if (vap->iv_bss == NULL) {
2180 ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
2181 return (EINVAL);
2182 }
2183 /*
2184 * Keep the ni alive locally. In theory (and practice) iv_bss can change
2185 * once we unlock here. This is due to net80211 allowing state changes
2186 * and new join1() despite having an active node as well as due to
2187 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
2188 */
2189 ni = ieee80211_ref_node(vap->iv_bss);
2190 if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
2191 ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
2192 "on vap %p\n", __func__, ni, vap);
2193 ieee80211_free_node(ni); /* Error handling for the local ni. */
2194 return (EINVAL);
2195 }
2196
2197 lhw = vap->iv_ic->ic_softc;
2198 chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
2199 if (chan == NULL) {
2200 ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
2201 "iv_bss ni %p on vap %p\n", __func__, ni, vap);
2202 ieee80211_free_node(ni); /* Error handling for the local ni. */
2203 return (ESRCH);
2204 }
2205
2206 hw = LHW_TO_HW(lhw);
2207 lvif = VAP_TO_LVIF(vap);
2208 vif = LVIF_TO_VIF(lvif);
2209
2210 LKPI_80211_LVIF_LOCK(lvif);
2211 /* XXX-BZ KASSERT later? */
2212 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
2213 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2214 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2215 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2216 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2217 lvif->lvif_bss_synched);
2218 LKPI_80211_LVIF_UNLOCK(lvif);
2219 ieee80211_free_node(ni); /* Error handling for the local ni. */
2220 return (EBUSY);
2221 }
2222 LKPI_80211_LVIF_UNLOCK(lvif);
2223
2224 IEEE80211_UNLOCK(vap->iv_ic);
2225 wiphy_lock(hw->wiphy);
2226
2227 /* Add chanctx (or if exists, change it). */
2228 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2229 lockdep_is_held(&hw->wiphy->mtx));
2230 if (chanctx_conf != NULL) {
2231 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2232 IMPROVE("diff changes for changed, working on live copy, rcu");
2233 } else {
2234 /* Keep separate alloc as in Linux this is rcu managed? */
2235 lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
2236 M_LKPI80211, M_WAITOK | M_ZERO);
2237 chanctx_conf = &lchanctx->chanctx_conf;
2238 }
2239
2240 chanctx_conf->rx_chains_static = 1;
2241 chanctx_conf->rx_chains_dynamic = 1;
2242 chanctx_conf->radar_enabled =
2243 (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2244 chanctx_conf->def.chan = chan;
2245 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
2246 chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
2247 chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
2248 IMPROVE("Check vht_cap from band not just chan?");
2249 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
2250 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
2251
2252 #ifdef LKPI_80211_HT
2253 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2254 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2255 chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
2256 else
2257 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
2258 }
2259 #endif
2260 #ifdef LKPI_80211_VHT
2261 if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
2262 if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2263 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
2264 else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2265 chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
2266 else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2267 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
2268 }
2269 #endif
2270 chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
2271 /* Responder ... */
2272 #if 0
2273 chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2274 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
2275 #ifdef LKPI_80211_HT
2276 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
2277 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
2278 #endif
2279 chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
2280 chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
2281 #else
2282 chanctx_conf->min_def = chanctx_conf->def;
2283 #endif
2284
2285 /* Set bss info (bss_info_changed). */
2286 bss_changed = 0;
2287 vif->bss_conf.bssid = ni->ni_bssid;
2288 bss_changed |= BSS_CHANGED_BSSID;
2289 vif->bss_conf.txpower = ni->ni_txpower;
2290 bss_changed |= BSS_CHANGED_TXPOWER;
2291 vif->cfg.idle = false;
2292 bss_changed |= BSS_CHANGED_IDLE;
2293
2294 /* vif->bss_conf.basic_rates ? Where exactly? */
2295
2296 lvif->beacons = 0;
2297 /* Should almost assert it is this. */
2298 vif->cfg.assoc = false;
2299 vif->cfg.aid = 0;
2300
2301 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2302
2303 error = 0;
2304 if (vif->bss_conf.chanctx_conf == chanctx_conf) {
2305 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
2306 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
2307 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
2308 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2309 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
2310 } else {
2311 error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
2312 if (error == 0 || error == EOPNOTSUPP) {
2313 vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
2314 vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
2315 vif->bss_conf.chanreq.oper.center_freq1 =
2316 chanctx_conf->def.center_freq1;
2317 vif->bss_conf.chanreq.oper.center_freq2 =
2318 chanctx_conf->def.center_freq2;
2319 } else {
2320 ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2321 "failed: %d\n", __func__, __LINE__, error);
2322 goto out;
2323 }
2324
2325 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2326 rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
2327
2328 /* Assign vif chanctx. */
2329 if (error == 0)
2330 error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2331 &vif->bss_conf, chanctx_conf);
2332 if (error == EOPNOTSUPP)
2333 error = 0;
2334 if (error != 0) {
2335 ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2336 "failed: %d\n", __func__, __LINE__, error);
2337 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2338 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2339 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2340 list_del(&lchanctx->entry);
2341 free(lchanctx, M_LKPI80211);
2342 goto out;
2343 }
2344 }
2345 IMPROVE("update radiotap chan fields too");
2346
2347 /* RATES */
2348 IMPROVE("bss info: not all needs to come now and rates are missing");
2349 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2350
2351 /*
2352 * Given ni and lsta are 1:1 from alloc to free we can assert that
2353 * ni always has lsta data attach despite net80211 node swapping
2354 * under the hoods.
2355 */
2356 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
2357 __func__, ni, ni->ni_drv_data));
2358 lsta = ni->ni_drv_data;
2359
2360 /* Insert the [l]sta into the list of known stations. */
2361 list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2362
2363 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
2364 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2365 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
2366 "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2367 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2368 if (error != 0) {
2369 IMPROVE("do we need to undo the chan ctx?");
2370 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2371 "failed: %d\n", __func__, __LINE__, error);
2372 goto out;
2373 }
2374 #if 0
2375 lsta->added_to_drv = true; /* mo manages. */
2376 #endif
2377
2378 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2379
2380 #if 0
2381 /*
2382 * Wakeup all queues now that sta is there so we have as much time to
2383 * possibly prepare the queue in the driver to be ready for the 1st
2384 * packet; lkpi_80211_txq_tx_one() still has a workaround as there
2385 * is no guarantee or way to check.
2386 * XXX-BZ and by now we know that this does not work on all drivers
2387 * for all queues.
2388 */
2389 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
2390 #endif
2391
2392 /* Start mgd_prepare_tx. */
2393 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2394 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2395 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2396 lsta->in_mgd = true;
2397
2398 /*
2399 * What is going to happen next:
2400 * - <twiddle> .. we should end up in "auth_to_assoc"
2401 * - event_callback
2402 * - update sta_state (NONE to AUTH)
2403 * - mgd_complete_tx
2404 * (ideally we'd do that on a callback for something else ...)
2405 */
2406
2407 wiphy_unlock(hw->wiphy);
2408 IEEE80211_LOCK(vap->iv_ic);
2409
2410 LKPI_80211_LVIF_LOCK(lvif);
2411 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2412 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2413 lsta->ni != vap->iv_bss)
2414 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2415 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2416 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2417 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2418 lvif->lvif_bss_synched, ni, lsta);
2419
2420 /*
2421 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2422 * Given we cache lsta we use lsta->ni instead of ni here (even though
2423 * lsta->ni == ni) to be distinct from the rest of the code where we do
2424 * assume that ni == vap->iv_bss which it may or may not be.
2425 * So do NOT use iv_bss here anymore as that may have diverged from our
2426 * function local ni already while ic was unlocked and would lead to
2427 * inconsistencies. Go and see if we lost a race and do not update
2428 * lvif_bss_synched in that case.
2429 */
2430 ieee80211_ref_node(lsta->ni);
2431 lvif->lvif_bss = lsta;
2432 if (lsta->ni == vap->iv_bss) {
2433 lvif->lvif_bss_synched = synched = true;
2434 } else {
2435 /* Set to un-synched no matter what. */
2436 lvif->lvif_bss_synched = synched = false;
2437 /*
2438 * We do not error as someone has to take us down.
2439 * If we are followed by a 2nd, new net80211::join1() going to
2440 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2441 * will take the lvif->lvif_bss node down eventually.
2442 * What happens with the vap->iv_bss node will entirely be up
2443 * to net80211 as we never used the node beyond alloc()/free()
2444 * and we do not hold an extra reference for that anymore given
2445 * ni : lsta == 1:1.
2446 * Problem is if we do not error a MGMT/AUTH frame will be
2447 * sent from net80211::sta_newstate(); disable lsta queue below.
2448 */
2449 }
2450 LKPI_80211_LVIF_UNLOCK(lvif);
2451 /*
2452 * Make sure in case the sta did not change and we re-added it,
2453 * that we can tx again but only if the vif/iv_bss are in sync.
2454 * Otherwise this should prevent the MGMT/AUTH frame from being
2455 * sent triggering a warning in iwlwifi.
2456 */
2457 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2458 lsta->txq_ready = synched;
2459 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2460 goto out_relocked;
2461
2462 out:
2463 wiphy_unlock(hw->wiphy);
2464 IEEE80211_LOCK(vap->iv_ic);
2465 out_relocked:
2466 /*
2467 * Release the reference that kept the ni stable locally
2468 * during the work of this function.
2469 */
2470 if (ni != NULL)
2471 ieee80211_free_node(ni);
2472 return (error);
2473 }
2474
2475 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2476 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2477 {
2478 struct lkpi_hw *lhw;
2479 struct ieee80211_hw *hw;
2480 struct lkpi_vif *lvif;
2481 struct ieee80211_vif *vif;
2482 struct ieee80211_node *ni;
2483 struct lkpi_sta *lsta;
2484 struct ieee80211_sta *sta;
2485 struct ieee80211_prep_tx_info prep_tx_info;
2486 enum ieee80211_bss_changed bss_changed;
2487 int error;
2488
2489 lhw = vap->iv_ic->ic_softc;
2490 hw = LHW_TO_HW(lhw);
2491 lvif = VAP_TO_LVIF(vap);
2492 vif = LVIF_TO_VIF(lvif);
2493
2494 LKPI_80211_LVIF_LOCK(lvif);
2495 #ifdef LINUXKPI_DEBUG_80211
2496 /* XXX-BZ KASSERT later; state going down so no action. */
2497 if (lvif->lvif_bss == NULL)
2498 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2499 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2500 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2501 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2502 lvif->lvif_bss_synched);
2503 #endif
2504
2505 lsta = lvif->lvif_bss;
2506 LKPI_80211_LVIF_UNLOCK(lvif);
2507 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2508 "lvif %p vap %p\n", __func__,
2509 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2510 ni = lsta->ni; /* Reference held for lvif_bss. */
2511 sta = LSTA_TO_STA(lsta);
2512
2513 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2514
2515 IEEE80211_UNLOCK(vap->iv_ic);
2516 wiphy_lock(hw->wiphy);
2517
2518 /* flush, drop. */
2519 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2520
2521 /* Wake tx queues to get packet(s) out. */
2522 lkpi_wake_tx_queues(hw, sta, false, true);
2523
2524 /* flush, no drop */
2525 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2526
2527 /* End mgd_complete_tx. */
2528 if (lsta->in_mgd) {
2529 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2530 prep_tx_info.success = false;
2531 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2532 lsta->in_mgd = false;
2533 }
2534
2535 /* sync_rx_queues */
2536 lkpi_80211_mo_sync_rx_queues(hw);
2537
2538 /* sta_pre_rcu_remove */
2539 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2540
2541 /* Take the station down. */
2542
2543 /* Adjust sta and change state (from NONE) to NOTEXIST. */
2544 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2545 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2546 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2547 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2548 if (error != 0) {
2549 IMPROVE("do we need to undo the chan ctx?");
2550 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2551 "failed: %d\n", __func__, __LINE__, error);
2552 goto out;
2553 }
2554 #if 0
2555 lsta->added_to_drv = false; /* mo manages. */
2556 #endif
2557
2558 bss_changed = 0;
2559 vif->bss_conf.dtim_period = 0; /* go back to 0. */
2560 bss_changed |= BSS_CHANGED_BEACON_INFO;
2561 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2562
2563 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2564
2565 LKPI_80211_LVIF_LOCK(lvif);
2566 /* Remove ni reference for this cache of lsta. */
2567 lvif->lvif_bss = NULL;
2568 lvif->lvif_bss_synched = false;
2569 LKPI_80211_LVIF_UNLOCK(lvif);
2570 lkpi_lsta_remove(lsta, lvif);
2571 /*
2572 * The very last release the reference on the ni for the ni/lsta on
2573 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
2574 * and potentially freed.
2575 */
2576 ieee80211_free_node(ni);
2577
2578 /* conf_tx */
2579
2580 lkpi_remove_chanctx(hw, vif);
2581
2582 out:
2583 wiphy_unlock(hw->wiphy);
2584 IEEE80211_LOCK(vap->iv_ic);
2585 return (error);
2586 }
2587
2588 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2589 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2590 {
2591 int error;
2592
2593 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
2594 if (error == 0)
2595 error = lkpi_sta_scan_to_init(vap, nstate, arg);
2596 return (error);
2597 }
2598
2599 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2600 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2601 {
2602 struct lkpi_hw *lhw;
2603 struct ieee80211_hw *hw;
2604 struct lkpi_vif *lvif;
2605 struct ieee80211_vif *vif;
2606 struct lkpi_sta *lsta;
2607 struct ieee80211_prep_tx_info prep_tx_info;
2608 int error;
2609
2610 lhw = vap->iv_ic->ic_softc;
2611 hw = LHW_TO_HW(lhw);
2612 lvif = VAP_TO_LVIF(vap);
2613 vif = LVIF_TO_VIF(lvif);
2614
2615 IEEE80211_UNLOCK(vap->iv_ic);
2616 wiphy_lock(hw->wiphy);
2617
2618 LKPI_80211_LVIF_LOCK(lvif);
2619 /* XXX-BZ KASSERT later? */
2620 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2621 #ifdef LINUXKPI_DEBUG_80211
2622 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2623 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2624 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2625 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2626 lvif->lvif_bss_synched);
2627 #endif
2628 error = ENOTRECOVERABLE;
2629 LKPI_80211_LVIF_UNLOCK(lvif);
2630 goto out;
2631 }
2632 lsta = lvif->lvif_bss;
2633 LKPI_80211_LVIF_UNLOCK(lvif);
2634
2635 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
2636
2637 /* Finish auth. */
2638 IMPROVE("event callback");
2639
2640 /* Update sta_state (NONE to AUTH). */
2641 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2642 "NONE: %#x\n", __func__, lsta, lsta->state));
2643 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2644 if (error != 0) {
2645 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2646 "failed: %d\n", __func__, __LINE__, error);
2647 goto out;
2648 }
2649
2650 /* End mgd_complete_tx. */
2651 if (lsta->in_mgd) {
2652 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2653 prep_tx_info.success = true;
2654 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2655 lsta->in_mgd = false;
2656 }
2657
2658 /* Now start assoc. */
2659
2660 /* Start mgd_prepare_tx. */
2661 if (!lsta->in_mgd) {
2662 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2663 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2664 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2665 lsta->in_mgd = true;
2666 }
2667
2668 /* Wake tx queue to get packet out. */
2669 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
2670
2671 /*
2672 * <twiddle> .. we end up in "assoc_to_run"
2673 * - update sta_state (AUTH to ASSOC)
2674 * - conf_tx [all]
2675 * - bss_info_changed (assoc, aid, ssid, ..)
2676 * - change_chanctx (if needed)
2677 * - event_callback
2678 * - mgd_complete_tx
2679 */
2680
2681 out:
2682 wiphy_unlock(hw->wiphy);
2683 IEEE80211_LOCK(vap->iv_ic);
2684 return (error);
2685 }
2686
2687 /* auth_to_auth, assoc_to_assoc. */
2688 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2689 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2690 {
2691 struct lkpi_hw *lhw;
2692 struct ieee80211_hw *hw;
2693 struct lkpi_vif *lvif;
2694 struct ieee80211_vif *vif;
2695 struct lkpi_sta *lsta;
2696 struct ieee80211_prep_tx_info prep_tx_info;
2697 int error;
2698
2699 lhw = vap->iv_ic->ic_softc;
2700 hw = LHW_TO_HW(lhw);
2701 lvif = VAP_TO_LVIF(vap);
2702 vif = LVIF_TO_VIF(lvif);
2703
2704 IEEE80211_UNLOCK(vap->iv_ic);
2705 wiphy_lock(hw->wiphy);
2706
2707 LKPI_80211_LVIF_LOCK(lvif);
2708 /* XXX-BZ KASSERT later? */
2709 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2710 #ifdef LINUXKPI_DEBUG_80211
2711 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2712 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2713 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2714 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2715 lvif->lvif_bss_synched);
2716 #endif
2717 LKPI_80211_LVIF_UNLOCK(lvif);
2718 error = ENOTRECOVERABLE;
2719 goto out;
2720 }
2721 lsta = lvif->lvif_bss;
2722 LKPI_80211_LVIF_UNLOCK(lvif);
2723
2724 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
2725 lsta, lvif, vap));
2726
2727 IMPROVE("event callback?");
2728
2729 /* End mgd_complete_tx. */
2730 if (lsta->in_mgd) {
2731 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2732 prep_tx_info.success = false;
2733 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2734 lsta->in_mgd = false;
2735 }
2736
2737 /* Now start assoc. */
2738
2739 /* Start mgd_prepare_tx. */
2740 if (!lsta->in_mgd) {
2741 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2742 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2743 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2744 lsta->in_mgd = true;
2745 }
2746
2747 error = 0;
2748 out:
2749 wiphy_unlock(hw->wiphy);
2750 IEEE80211_LOCK(vap->iv_ic);
2751
2752 return (error);
2753 }
2754
2755 static int
_lkpi_sta_assoc_to_down(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2756 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2757 {
2758 struct lkpi_hw *lhw;
2759 struct ieee80211_hw *hw;
2760 struct lkpi_vif *lvif;
2761 struct ieee80211_vif *vif;
2762 struct ieee80211_node *ni;
2763 struct lkpi_sta *lsta;
2764 struct ieee80211_sta *sta;
2765 struct ieee80211_prep_tx_info prep_tx_info;
2766 enum ieee80211_bss_changed bss_changed;
2767 int error;
2768
2769 lhw = vap->iv_ic->ic_softc;
2770 hw = LHW_TO_HW(lhw);
2771 lvif = VAP_TO_LVIF(vap);
2772 vif = LVIF_TO_VIF(lvif);
2773
2774 IEEE80211_UNLOCK(vap->iv_ic);
2775 wiphy_lock(hw->wiphy);
2776
2777 LKPI_80211_LVIF_LOCK(lvif);
2778 #ifdef LINUXKPI_DEBUG_80211
2779 /* XXX-BZ KASSERT later; state going down so no action. */
2780 if (lvif->lvif_bss == NULL)
2781 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2782 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2783 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2784 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2785 lvif->lvif_bss_synched);
2786 #endif
2787 lsta = lvif->lvif_bss;
2788 LKPI_80211_LVIF_UNLOCK(lvif);
2789 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2790 "lvif %p vap %p\n", __func__,
2791 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2792
2793 ni = lsta->ni; /* Reference held for lvif_bss. */
2794 sta = LSTA_TO_STA(lsta);
2795
2796 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2797
2798 /* flush, drop. */
2799 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2800
2801 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2802 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2803 !lsta->in_mgd) {
2804 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2805 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2806 prep_tx_info.was_assoc = true;
2807 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2808 lsta->in_mgd = true;
2809 }
2810
2811 wiphy_unlock(hw->wiphy);
2812 IEEE80211_LOCK(vap->iv_ic);
2813
2814 /* Call iv_newstate first so we get potential DEAUTH packet out. */
2815 error = lvif->iv_newstate(vap, nstate, arg);
2816 if (error != 0) {
2817 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2818 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2819 goto outni;
2820 }
2821
2822 IEEE80211_UNLOCK(vap->iv_ic);
2823
2824 /* Ensure the packets get out. */
2825 lkpi_80211_flush_tx(lhw, lsta);
2826
2827 wiphy_lock(hw->wiphy);
2828
2829 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2830
2831 /* Wake tx queues to get packet(s) out. */
2832 lkpi_wake_tx_queues(hw, sta, false, true);
2833
2834 /* flush, no drop */
2835 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2836
2837 /* End mgd_complete_tx. */
2838 if (lsta->in_mgd) {
2839 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2840 prep_tx_info.success = false;
2841 prep_tx_info.was_assoc = true;
2842 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2843 lsta->in_mgd = false;
2844 }
2845
2846 /* sync_rx_queues */
2847 lkpi_80211_mo_sync_rx_queues(hw);
2848
2849 /* sta_pre_rcu_remove */
2850 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2851
2852 /* Take the station down. */
2853
2854 /* Update sta and change state (from AUTH) to NONE. */
2855 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2856 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2857 "AUTH: %#x\n", __func__, lsta, lsta->state));
2858 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2859 if (error != 0) {
2860 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2861 "failed: %d\n", __func__, __LINE__, error);
2862 goto out;
2863 }
2864
2865 /* See comment in lkpi_sta_run_to_init(). */
2866 bss_changed = 0;
2867 bss_changed |= lkpi_disassoc(sta, vif, lhw);
2868
2869 #ifdef LKPI_80211_HW_CRYPTO
2870 /*
2871 * In theory we remove keys here but there must not exist any for this
2872 * state change until we clean them up again into small steps and no
2873 * code duplication.
2874 */
2875 #endif
2876
2877 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2878
2879 /* Adjust sta and change state (from NONE) to NOTEXIST. */
2880 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2881 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2882 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2883 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2884 if (error != 0) {
2885 IMPROVE("do we need to undo the chan ctx?");
2886 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2887 "failed: %d\n", __func__, __LINE__, error);
2888 goto out;
2889 }
2890
2891 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
2892
2893 IMPROVE("Any bss_info changes to announce?");
2894 vif->bss_conf.qos = 0;
2895 bss_changed |= BSS_CHANGED_QOS;
2896 vif->cfg.ssid_len = 0;
2897 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2898 bss_changed |= BSS_CHANGED_BSSID;
2899 vif->bss_conf.dtim_period = 0; /* go back to 0. */
2900 bss_changed |= BSS_CHANGED_BEACON_INFO;
2901 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2902
2903 LKPI_80211_LVIF_LOCK(lvif);
2904 /* Remove ni reference for this cache of lsta. */
2905 lvif->lvif_bss = NULL;
2906 lvif->lvif_bss_synched = false;
2907 LKPI_80211_LVIF_UNLOCK(lvif);
2908 lkpi_lsta_remove(lsta, lvif);
2909 /*
2910 * The very last release the reference on the ni for the ni/lsta on
2911 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
2912 * and potentially freed.
2913 */
2914 ieee80211_free_node(ni);
2915
2916 /* conf_tx */
2917
2918 lkpi_remove_chanctx(hw, vif);
2919
2920 error = EALREADY;
2921 out:
2922 wiphy_unlock(hw->wiphy);
2923 IEEE80211_LOCK(vap->iv_ic);
2924 outni:
2925 return (error);
2926 }
2927
2928 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2929 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2930 {
2931 int error;
2932
2933 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2934 if (error != 0 && error != EALREADY)
2935 return (error);
2936
2937 /* At this point iv_bss is long a new node! */
2938
2939 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2940 return (error);
2941 }
2942
2943 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2944 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2945 {
2946 int error;
2947
2948 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2949 return (error);
2950 }
2951
2952 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2953 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2954 {
2955 int error;
2956
2957 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2958 return (error);
2959 }
2960
2961 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2962 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2963 {
2964 struct lkpi_hw *lhw;
2965 struct ieee80211_hw *hw;
2966 struct lkpi_vif *lvif;
2967 struct ieee80211_vif *vif;
2968 struct ieee80211_node *ni;
2969 struct lkpi_sta *lsta;
2970 struct ieee80211_sta *sta;
2971 struct ieee80211_prep_tx_info prep_tx_info;
2972 enum ieee80211_bss_changed bss_changed;
2973 int error;
2974
2975 lhw = vap->iv_ic->ic_softc;
2976 hw = LHW_TO_HW(lhw);
2977 lvif = VAP_TO_LVIF(vap);
2978 vif = LVIF_TO_VIF(lvif);
2979
2980 IEEE80211_UNLOCK(vap->iv_ic);
2981 wiphy_lock(hw->wiphy);
2982
2983 LKPI_80211_LVIF_LOCK(lvif);
2984 /* XXX-BZ KASSERT later? */
2985 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2986 #ifdef LINUXKPI_DEBUG_80211
2987 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2988 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2989 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2990 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2991 lvif->lvif_bss_synched);
2992 #endif
2993 LKPI_80211_LVIF_UNLOCK(lvif);
2994 error = ENOTRECOVERABLE;
2995 goto out;
2996 }
2997 lsta = lvif->lvif_bss;
2998 LKPI_80211_LVIF_UNLOCK(lvif);
2999 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3000 "lvif %p vap %p\n", __func__,
3001 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3002
3003 ni = lsta->ni; /* Reference held for lvif_bss. */
3004
3005 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
3006 "and to lesser extend ieee80211_notify_node_join");
3007
3008 /* Finish assoc. */
3009 /* Update sta_state (AUTH to ASSOC) and set aid. */
3010 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3011 "AUTH: %#x\n", __func__, lsta, lsta->state));
3012 sta = LSTA_TO_STA(lsta);
3013 sta->aid = IEEE80211_NODE_AID(ni);
3014 #ifdef LKPI_80211_WME
3015 if (vap->iv_flags & IEEE80211_F_WME)
3016 sta->wme = true;
3017 #endif
3018 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3019 if (error != 0) {
3020 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3021 "failed: %d\n", __func__, __LINE__, error);
3022 goto out;
3023 }
3024
3025 IMPROVE("wme / conf_tx [all]");
3026
3027 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3028 bss_changed = 0;
3029 #ifdef LKPI_80211_WME
3030 bss_changed |= lkpi_wme_update(lhw, vap, true);
3031 #endif
3032 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
3033 lvif->beacons = 0;
3034 vif->cfg.assoc = true;
3035 vif->cfg.aid = IEEE80211_NODE_AID(ni);
3036 bss_changed |= BSS_CHANGED_ASSOC;
3037 }
3038 /* We set SSID but this is not BSSID! */
3039 vif->cfg.ssid_len = ni->ni_esslen;
3040 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
3041 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
3042 vif->bss_conf.use_short_preamble) {
3043 vif->bss_conf.use_short_preamble ^= 1;
3044 /* bss_changed |= BSS_CHANGED_??? */
3045 }
3046 if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
3047 vif->bss_conf.use_short_slot) {
3048 vif->bss_conf.use_short_slot ^= 1;
3049 /* bss_changed |= BSS_CHANGED_??? */
3050 }
3051 if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
3052 vif->bss_conf.qos) {
3053 vif->bss_conf.qos ^= 1;
3054 bss_changed |= BSS_CHANGED_QOS;
3055 }
3056
3057 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3058 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3059
3060 /* - change_chanctx (if needed)
3061 * - event_callback
3062 */
3063
3064 /* End mgd_complete_tx. */
3065 if (lsta->in_mgd) {
3066 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3067 prep_tx_info.success = true;
3068 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3069 lsta->in_mgd = false;
3070 }
3071
3072 lkpi_hw_conf_idle(hw, false);
3073
3074 /*
3075 * And then:
3076 * - (more packets)?
3077 * - set_key
3078 * - set_default_unicast_key
3079 * - set_key (?)
3080 * - ipv6_addr_change (?)
3081 */
3082
3083 if (!ieee80211_node_is_authorized(ni)) {
3084 IMPROVE("net80211 does not consider node authorized");
3085 }
3086
3087 IMPROVE("Is this the right spot, has net80211 done all updates already?");
3088 lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
3089
3090 /* Update thresholds. */
3091 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
3092 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
3093 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
3094 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
3095
3096 /* Update sta_state (ASSOC to AUTHORIZED). */
3097 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3098 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3099 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3100 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
3101 if (error != 0) {
3102 IMPROVE("undo some changes?");
3103 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
3104 "failed: %d\n", __func__, __LINE__, error);
3105 goto out;
3106 }
3107
3108 /* - drv_config (?)
3109 * - bss_info_changed
3110 * - set_rekey_data (?)
3111 *
3112 * And now we should be passing packets.
3113 */
3114 IMPROVE("Need that bssid setting, and the keys");
3115
3116 bss_changed = 0;
3117 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3118 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3119
3120 /* Prepare_multicast && configure_filter. */
3121 lkpi_update_mcast_filter(vap->iv_ic);
3122
3123 out:
3124 wiphy_unlock(hw->wiphy);
3125 IEEE80211_LOCK(vap->iv_ic);
3126 return (error);
3127 }
3128
3129 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3130 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3131 {
3132 int error;
3133
3134 error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
3135 if (error == 0)
3136 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
3137 return (error);
3138 }
3139
3140 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3141 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3142 {
3143 struct lkpi_hw *lhw;
3144 struct ieee80211_hw *hw;
3145 struct lkpi_vif *lvif;
3146 struct ieee80211_vif *vif;
3147 struct ieee80211_node *ni;
3148 struct lkpi_sta *lsta;
3149 struct ieee80211_sta *sta;
3150 struct ieee80211_prep_tx_info prep_tx_info;
3151 #if 0
3152 enum ieee80211_bss_changed bss_changed;
3153 #endif
3154 int error;
3155
3156 lhw = vap->iv_ic->ic_softc;
3157 hw = LHW_TO_HW(lhw);
3158 lvif = VAP_TO_LVIF(vap);
3159 vif = LVIF_TO_VIF(lvif);
3160
3161 LKPI_80211_LVIF_LOCK(lvif);
3162 #ifdef LINUXKPI_DEBUG_80211
3163 /* XXX-BZ KASSERT later; state going down so no action. */
3164 if (lvif->lvif_bss == NULL)
3165 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3166 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3167 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3168 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3169 lvif->lvif_bss_synched);
3170 #endif
3171 lsta = lvif->lvif_bss;
3172 LKPI_80211_LVIF_UNLOCK(lvif);
3173 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3174 "lvif %p vap %p\n", __func__,
3175 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3176
3177 ni = lsta->ni; /* Reference held for lvif_bss. */
3178 sta = LSTA_TO_STA(lsta);
3179
3180 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3181
3182 IEEE80211_UNLOCK(vap->iv_ic);
3183 wiphy_lock(hw->wiphy);
3184
3185 /* flush, drop. */
3186 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3187
3188 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3189 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3190 !lsta->in_mgd) {
3191 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3192 prep_tx_info.duration = PREP_TX_INFO_DURATION;
3193 prep_tx_info.was_assoc = true;
3194 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3195 lsta->in_mgd = true;
3196 }
3197
3198 wiphy_unlock(hw->wiphy);
3199 IEEE80211_LOCK(vap->iv_ic);
3200
3201 /* Call iv_newstate first so we get potential DISASSOC packet out. */
3202 error = lvif->iv_newstate(vap, nstate, arg);
3203 if (error != 0) {
3204 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3205 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3206 goto outni;
3207 }
3208
3209 IEEE80211_UNLOCK(vap->iv_ic);
3210
3211 /* Ensure the packets get out. */
3212 lkpi_80211_flush_tx(lhw, lsta);
3213
3214 wiphy_lock(hw->wiphy);
3215
3216 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3217
3218 /* Wake tx queues to get packet(s) out. */
3219 lkpi_wake_tx_queues(hw, sta, false, true);
3220
3221 /* flush, no drop */
3222 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3223
3224 /* End mgd_complete_tx. */
3225 if (lsta->in_mgd) {
3226 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3227 prep_tx_info.success = false;
3228 prep_tx_info.was_assoc = true;
3229 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3230 lsta->in_mgd = false;
3231 }
3232
3233 #if 0
3234 /* sync_rx_queues */
3235 lkpi_80211_mo_sync_rx_queues(hw);
3236
3237 /* sta_pre_rcu_remove */
3238 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3239 #endif
3240
3241 /* Take the station down. */
3242
3243 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3244 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3245 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3246 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3247 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3248 if (error != 0) {
3249 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3250 "failed: %d\n", __func__, __LINE__, error);
3251 goto out;
3252 }
3253
3254 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3255
3256 #ifdef LKPI_80211_HW_CRYPTO
3257 if (lkpi_hwcrypto) {
3258 error = lkpi_sta_del_keys(hw, vif, lsta);
3259 if (error != 0) {
3260 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3261 "failed: %d\n", __func__, __LINE__, error);
3262 /*
3263 * Either drv/fw will crash or cleanup itself,
3264 * otherwise net80211 will delete the keys (at a
3265 * less appropriate time).
3266 */
3267 /* goto out; */
3268 }
3269 }
3270 #endif
3271
3272 /* Update sta_state (ASSOC to AUTH). */
3273 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3274 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3275 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3276 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3277 if (error != 0) {
3278 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3279 "failed: %d\n", __func__, __LINE__, error);
3280 goto out;
3281 }
3282
3283 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3284
3285 #if 0
3286 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3287 lkpi_disassoc(sta, vif, lhw);
3288 #endif
3289
3290 error = EALREADY;
3291 out:
3292 wiphy_unlock(hw->wiphy);
3293 IEEE80211_LOCK(vap->iv_ic);
3294 outni:
3295 return (error);
3296 }
3297
3298 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3299 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3300 {
3301 struct lkpi_hw *lhw;
3302 struct ieee80211_hw *hw;
3303 struct lkpi_vif *lvif;
3304 struct ieee80211_vif *vif;
3305 struct ieee80211_node *ni;
3306 struct lkpi_sta *lsta;
3307 struct ieee80211_sta *sta;
3308 struct ieee80211_prep_tx_info prep_tx_info;
3309 enum ieee80211_bss_changed bss_changed;
3310 int error;
3311
3312 lhw = vap->iv_ic->ic_softc;
3313 hw = LHW_TO_HW(lhw);
3314 lvif = VAP_TO_LVIF(vap);
3315 vif = LVIF_TO_VIF(lvif);
3316
3317 IEEE80211_UNLOCK(vap->iv_ic);
3318 wiphy_lock(hw->wiphy);
3319
3320 LKPI_80211_LVIF_LOCK(lvif);
3321 #ifdef LINUXKPI_DEBUG_80211
3322 /* XXX-BZ KASSERT later; state going down so no action. */
3323 if (lvif->lvif_bss == NULL)
3324 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3325 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3326 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3327 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3328 lvif->lvif_bss_synched);
3329 #endif
3330 lsta = lvif->lvif_bss;
3331 LKPI_80211_LVIF_UNLOCK(lvif);
3332 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3333 "lvif %p vap %p\n", __func__,
3334 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3335
3336 ni = lsta->ni; /* Reference held for lvif_bss. */
3337 sta = LSTA_TO_STA(lsta);
3338
3339 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3340
3341 /* flush, drop. */
3342 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3343
3344 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3345 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3346 !lsta->in_mgd) {
3347 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3348 prep_tx_info.duration = PREP_TX_INFO_DURATION;
3349 prep_tx_info.was_assoc = true;
3350 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3351 lsta->in_mgd = true;
3352 }
3353
3354 wiphy_unlock(hw->wiphy);
3355 IEEE80211_LOCK(vap->iv_ic);
3356
3357 /* Call iv_newstate first so we get potential DISASSOC packet out. */
3358 error = lvif->iv_newstate(vap, nstate, arg);
3359 if (error != 0) {
3360 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3361 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3362 goto outni;
3363 }
3364
3365 IEEE80211_UNLOCK(vap->iv_ic);
3366
3367 /* Ensure the packets get out. */
3368 lkpi_80211_flush_tx(lhw, lsta);
3369
3370 wiphy_lock(hw->wiphy);
3371
3372 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3373
3374 /* Wake tx queues to get packet(s) out. */
3375 lkpi_wake_tx_queues(hw, sta, false, true);
3376
3377 /* flush, no drop */
3378 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3379
3380 /* End mgd_complete_tx. */
3381 if (lsta->in_mgd) {
3382 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3383 prep_tx_info.success = false;
3384 prep_tx_info.was_assoc = true;
3385 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3386 lsta->in_mgd = false;
3387 }
3388
3389 /* sync_rx_queues */
3390 lkpi_80211_mo_sync_rx_queues(hw);
3391
3392 /* sta_pre_rcu_remove */
3393 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3394
3395 /* Take the station down. */
3396
3397 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3398 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3399 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3400 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3401 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3402 if (error != 0) {
3403 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3404 "failed: %d\n", __func__, __LINE__, error);
3405 goto out;
3406 }
3407
3408 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3409
3410 #ifdef LKPI_80211_HW_CRYPTO
3411 if (lkpi_hwcrypto) {
3412 /*
3413 * In theory we only need to do this if we changed assoc.
3414 * If we were not assoc, there should be no keys and we
3415 * should not be here.
3416 */
3417 #ifdef notyet
3418 KASSERT((bss_changed & BSS_CHANGED_ASSOC) != 0, ("%s: "
3419 "trying to remove keys but were not assoc: %#010jx, lvif %p\n",
3420 __func__, (uintmax_t)bss_changed, lvif));
3421 #endif
3422 error = lkpi_sta_del_keys(hw, vif, lsta);
3423 if (error != 0) {
3424 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3425 "failed: %d\n", __func__, __LINE__, error);
3426 /*
3427 * Either drv/fw will crash or cleanup itself,
3428 * otherwise net80211 will delete the keys (at a
3429 * less appropriate time).
3430 */
3431 /* goto out; */
3432 }
3433 }
3434 #endif
3435
3436 /* Update sta_state (ASSOC to AUTH). */
3437 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3438 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3439 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3440 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3441 if (error != 0) {
3442 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3443 "failed: %d\n", __func__, __LINE__, error);
3444 goto out;
3445 }
3446
3447 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3448
3449 /* Update sta and change state (from AUTH) to NONE. */
3450 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3451 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3452 "AUTH: %#x\n", __func__, lsta, lsta->state));
3453 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3454 if (error != 0) {
3455 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3456 "failed: %d\n", __func__, __LINE__, error);
3457 goto out;
3458 }
3459
3460 bss_changed = 0;
3461 /*
3462 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
3463 *
3464 * One would expect this to happen when going off AUTHORIZED.
3465 * See comment there; removes the sta from fw if not careful
3466 * (bss_info_changed() change is executed right away).
3467 *
3468 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
3469 * as otherwise drivers (iwlwifi at least) will silently not remove
3470 * the sta from the firmware and when we will add a new one trigger
3471 * a fw assert.
3472 *
3473 * The order which works best so far avoiding early removal or silent
3474 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
3475 * the iwlwifi:mac80211.c case still to be tested):
3476 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
3477 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
3478 * (removes the sta given assoc is false)
3479 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
3480 * 4) call unassign_vif_chanctx
3481 * 5) call lkpi_hw_conf_idle
3482 * 6) call remove_chanctx
3483 *
3484 * Note: vif->driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC
3485 * might change this.
3486 */
3487 bss_changed |= lkpi_disassoc(sta, vif, lhw);
3488
3489 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3490
3491 /* Adjust sta and change state (from NONE) to NOTEXIST. */
3492 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3493 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3494 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3495 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3496 if (error != 0) {
3497 IMPROVE("do we need to undo the chan ctx?");
3498 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3499 "failed: %d\n", __func__, __LINE__, error);
3500 goto out;
3501 }
3502
3503 lkpi_lsta_remove(lsta, lvif);
3504
3505 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
3506
3507 IMPROVE("Any bss_info changes to announce?");
3508 vif->bss_conf.qos = 0;
3509 bss_changed |= BSS_CHANGED_QOS;
3510 vif->cfg.ssid_len = 0;
3511 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3512 bss_changed |= BSS_CHANGED_BSSID;
3513 vif->bss_conf.use_short_preamble = false;
3514 vif->bss_conf.qos = false;
3515 /* XXX BSS_CHANGED_???? */
3516 vif->bss_conf.dtim_period = 0; /* go back to 0. */
3517 bss_changed |= BSS_CHANGED_BEACON_INFO;
3518 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3519
3520 LKPI_80211_LVIF_LOCK(lvif);
3521 /* Remove ni reference for this cache of lsta. */
3522 lvif->lvif_bss = NULL;
3523 lvif->lvif_bss_synched = false;
3524 LKPI_80211_LVIF_UNLOCK(lvif);
3525 /*
3526 * The very last release the reference on the ni for the ni/lsta on
3527 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
3528 * and potentially freed.
3529 */
3530 ieee80211_free_node(ni);
3531
3532 /* conf_tx */
3533
3534 lkpi_remove_chanctx(hw, vif);
3535
3536 error = EALREADY;
3537 out:
3538 wiphy_unlock(hw->wiphy);
3539 IEEE80211_LOCK(vap->iv_ic);
3540 outni:
3541 return (error);
3542 }
3543
3544 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3545 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3546 {
3547
3548 return (lkpi_sta_run_to_init(vap, nstate, arg));
3549 }
3550
3551 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3552 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3553 {
3554 int error;
3555
3556 error = lkpi_sta_run_to_init(vap, nstate, arg);
3557 if (error != 0 && error != EALREADY)
3558 return (error);
3559
3560 /* At this point iv_bss is long a new node! */
3561
3562 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
3563 return (error);
3564 }
3565
3566 /* -------------------------------------------------------------------------- */
3567
3568 /*
3569 * The matches the documented state changes in net80211::sta_newstate().
3570 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
3571 * there are "invalid" (so there is a room for failure here).
3572 */
3573 struct fsm_state {
3574 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
3575 enum ieee80211_state ostate;
3576 enum ieee80211_state nstate;
3577 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
3578 } sta_state_fsm[] = {
3579 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada },
3580 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* scan_to_init */
3581 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */
3582 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */
3583 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */
3584
3585 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3586 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3587 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
3588 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3589 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */
3590
3591 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3592 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3593 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */
3594 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* Send ?AUTH. */
3595 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */
3596
3597 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* Send ASSOCREQ. */
3598 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */
3599 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* Send ASSOCREQ/REASSOCREQ. */
3600
3601 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run },
3602 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
3603 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada },
3604
3605 /* Dummy at the end without handler. */
3606 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL },
3607 };
3608
3609 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3610 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3611 {
3612 struct ieee80211com *ic;
3613 struct lkpi_hw *lhw;
3614 struct lkpi_vif *lvif;
3615 struct ieee80211_vif *vif;
3616 struct fsm_state *s;
3617 enum ieee80211_state ostate;
3618 int error;
3619
3620 ic = vap->iv_ic;
3621 IEEE80211_LOCK_ASSERT(ic);
3622 ostate = vap->iv_state;
3623
3624 #ifdef LINUXKPI_DEBUG_80211
3625 if (linuxkpi_debug_80211 & D80211_TRACE)
3626 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
3627 __func__, __LINE__, vap, nstate, arg);
3628 #endif
3629
3630 if (vap->iv_opmode == IEEE80211_M_STA) {
3631
3632 lhw = ic->ic_softc;
3633 lvif = VAP_TO_LVIF(vap);
3634 vif = LVIF_TO_VIF(lvif);
3635
3636 /* No need to replicate this in most state handlers. */
3637 if (nstate > IEEE80211_S_SCAN)
3638 lkpi_stop_hw_scan(lhw, vif);
3639
3640 s = sta_state_fsm;
3641
3642 } else {
3643 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
3644 "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
3645 return (ENOSYS);
3646 }
3647
3648 error = 0;
3649 for (; s->handler != NULL; s++) {
3650 if (ostate == s->ostate && nstate == s->nstate) {
3651 #ifdef LINUXKPI_DEBUG_80211
3652 if (linuxkpi_debug_80211 & D80211_TRACE)
3653 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3654 " %d (%s): arg %d.\n", __func__,
3655 ostate, ieee80211_state_name[ostate],
3656 nstate, ieee80211_state_name[nstate], arg);
3657 #endif
3658 error = s->handler(vap, nstate, arg);
3659 break;
3660 }
3661 }
3662 IEEE80211_LOCK_ASSERT(vap->iv_ic);
3663
3664 if (s->handler == NULL) {
3665 IMPROVE("turn this into a KASSERT\n");
3666 ic_printf(vap->iv_ic, "%s: unsupported state transition "
3667 "%d (%s) -> %d (%s)\n", __func__,
3668 ostate, ieee80211_state_name[ostate],
3669 nstate, ieee80211_state_name[nstate]);
3670 return (ENOSYS);
3671 }
3672
3673 if (error == EALREADY) {
3674 #ifdef LINUXKPI_DEBUG_80211
3675 if (linuxkpi_debug_80211 & D80211_TRACE)
3676 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3677 "%d (%s): iv_newstate already handled: %d.\n",
3678 __func__, ostate, ieee80211_state_name[ostate],
3679 nstate, ieee80211_state_name[nstate], error);
3680 #endif
3681 return (0);
3682 }
3683
3684 if (error != 0) {
3685 ic_printf(vap->iv_ic, "%s: error %d during state transition "
3686 "%d (%s) -> %d (%s)\n", __func__, error,
3687 ostate, ieee80211_state_name[ostate],
3688 nstate, ieee80211_state_name[nstate]);
3689 return (error);
3690 }
3691
3692 #ifdef LINUXKPI_DEBUG_80211
3693 if (linuxkpi_debug_80211 & D80211_TRACE)
3694 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3695 "calling net80211 parent\n",
3696 __func__, __LINE__, vap, nstate, arg);
3697 #endif
3698
3699 return (lvif->iv_newstate(vap, nstate, arg));
3700 }
3701
3702 /* -------------------------------------------------------------------------- */
3703
3704 /*
3705 * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3706 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3707 * new node without us knowing and thus our ni/lsta are out of sync.
3708 */
3709 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)3710 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3711 {
3712 struct lkpi_vif *lvif;
3713 struct ieee80211_node *rni;
3714
3715 IEEE80211_LOCK_ASSERT(vap->iv_ic);
3716
3717 lvif = VAP_TO_LVIF(vap);
3718
3719 LKPI_80211_LVIF_LOCK(lvif);
3720 lvif->lvif_bss_synched = false;
3721 LKPI_80211_LVIF_UNLOCK(lvif);
3722
3723 rni = lvif->iv_update_bss(vap, ni);
3724 return (rni);
3725 }
3726
3727 #ifdef LKPI_80211_WME
3728 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)3729 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
3730 {
3731 struct ieee80211com *ic;
3732 struct ieee80211_hw *hw;
3733 struct lkpi_vif *lvif;
3734 struct ieee80211_vif *vif;
3735 struct chanAccParams chp;
3736 struct wmeParams wmeparr[WME_NUM_AC];
3737 struct ieee80211_tx_queue_params txqp;
3738 enum ieee80211_bss_changed changed;
3739 int error;
3740 uint16_t ac;
3741
3742 hw = LHW_TO_HW(lhw);
3743 lockdep_assert_wiphy(hw->wiphy);
3744
3745 IMPROVE();
3746 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
3747 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
3748
3749 if (vap == NULL)
3750 return (0);
3751
3752 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
3753 return (0);
3754
3755 if (lhw->ops->conf_tx == NULL)
3756 return (0);
3757
3758 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
3759 lhw->update_wme = true;
3760 return (0);
3761 }
3762 lhw->update_wme = false;
3763
3764 ic = lhw->ic;
3765 ieee80211_wme_ic_getparams(ic, &chp);
3766 IEEE80211_LOCK(ic);
3767 for (ac = 0; ac < WME_NUM_AC; ac++)
3768 wmeparr[ac] = chp.cap_wmeParams[ac];
3769 IEEE80211_UNLOCK(ic);
3770
3771 lvif = VAP_TO_LVIF(vap);
3772 vif = LVIF_TO_VIF(lvif);
3773
3774 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
3775 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3776 struct wmeParams *wmep;
3777
3778 wmep = &wmeparr[ac];
3779 bzero(&txqp, sizeof(txqp));
3780 txqp.cw_min = wmep->wmep_logcwmin;
3781 txqp.cw_max = wmep->wmep_logcwmax;
3782 txqp.txop = wmep->wmep_txopLimit;
3783 txqp.aifs = wmep->wmep_aifsn;
3784 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3785 if (error != 0)
3786 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3787 __func__, ac, error);
3788 }
3789 changed = BSS_CHANGED_QOS;
3790 if (!planned)
3791 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
3792
3793 return (changed);
3794 }
3795 #endif
3796
3797 static int
lkpi_ic_wme_update(struct ieee80211com * ic)3798 lkpi_ic_wme_update(struct ieee80211com *ic)
3799 {
3800 #ifdef LKPI_80211_WME
3801 struct ieee80211vap *vap;
3802 struct lkpi_hw *lhw;
3803 struct ieee80211_hw *hw;
3804
3805 IMPROVE("Use the per-VAP callback in net80211.");
3806 vap = TAILQ_FIRST(&ic->ic_vaps);
3807 if (vap == NULL)
3808 return (0);
3809
3810 lhw = ic->ic_softc;
3811 hw = LHW_TO_HW(lhw);
3812
3813 wiphy_lock(hw->wiphy);
3814 lkpi_wme_update(lhw, vap, false);
3815 wiphy_unlock(hw->wiphy);
3816 #endif
3817 return (0); /* unused */
3818 }
3819
3820 static void
lkpi_iv_sta_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)3821 lkpi_iv_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
3822 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
3823 {
3824 struct lkpi_hw *lhw;
3825 struct ieee80211_hw *hw;
3826 struct lkpi_vif *lvif;
3827 struct ieee80211_vif *vif;
3828 enum ieee80211_bss_changed bss_changed;
3829
3830 lvif = VAP_TO_LVIF(ni->ni_vap);
3831 vif = LVIF_TO_VIF(lvif);
3832
3833 lvif->iv_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
3834
3835 switch (subtype) {
3836 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3837 break;
3838 case IEEE80211_FC0_SUBTYPE_BEACON:
3839 /*
3840 * Only count beacons when assoc. SCAN has its own logging.
3841 * This is for connection/beacon loss/session protection almost
3842 * over debugging when trying to get into a stable RUN state.
3843 */
3844 if (vif->cfg.assoc)
3845 lvif->beacons++;
3846 break;
3847 default:
3848 return;
3849 }
3850
3851 lhw = ni->ni_ic->ic_softc;
3852 hw = LHW_TO_HW(lhw);
3853
3854 /*
3855 * If this direct call to mo_bss_info_changed will not work due to
3856 * locking, see if queue_work() is fast enough.
3857 */
3858 bss_changed = lkpi_update_dtim_tsf(vif, ni, ni->ni_vap, __func__, __LINE__);
3859 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3860 }
3861
3862 /*
3863 * Change link-layer address on the vif (if the vap is not started/"UP").
3864 * This can happen if a user changes 'ether' using ifconfig.
3865 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
3866 * we do use a per-[l]vif event handler to be sure we exist as we
3867 * cannot assume that from every vap derives a vif and we have a hard
3868 * time checking based on net80211 information.
3869 * Should this ever become a real problem we could add a callback function
3870 * to wlan_iflladdr() to be set optionally but that would be for a
3871 * single-consumer (or needs a list) -- was just too complicated for an
3872 * otherwise perfect mechanism FreeBSD already provides.
3873 */
3874 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)3875 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
3876 {
3877 struct epoch_tracker et;
3878 struct ieee80211_vif *vif;
3879
3880 NET_EPOCH_ENTER(et);
3881 /* NB: identify vap's by if_transmit; left as an extra check. */
3882 if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3883 (if_getflags(ifp) & IFF_UP) != 0) {
3884 NET_EPOCH_EXIT(et);
3885 return;
3886 }
3887
3888 vif = arg;
3889 IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
3890 NET_EPOCH_EXIT(et);
3891 }
3892
3893 static struct ieee80211vap *
lkpi_ic_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])3894 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
3895 int unit, enum ieee80211_opmode opmode, int flags,
3896 const uint8_t bssid[IEEE80211_ADDR_LEN],
3897 const uint8_t mac[IEEE80211_ADDR_LEN])
3898 {
3899 struct lkpi_hw *lhw;
3900 struct ieee80211_hw *hw;
3901 struct lkpi_vif *lvif;
3902 struct ieee80211vap *vap;
3903 struct ieee80211_vif *vif;
3904 struct ieee80211_tx_queue_params txqp;
3905 enum ieee80211_bss_changed changed;
3906 struct sysctl_oid *node;
3907 size_t len;
3908 int error, i;
3909 uint16_t ac;
3910
3911 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
3912 return (NULL);
3913
3914 lhw = ic->ic_softc;
3915 hw = LHW_TO_HW(lhw);
3916
3917 len = sizeof(*lvif);
3918 len += hw->vif_data_size; /* vif->drv_priv */
3919
3920 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
3921 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3922 TASK_INIT(&lvif->sw_scan_task, 0, lkpi_sw_scan_task, lvif);
3923 INIT_LIST_HEAD(&lvif->lsta_list);
3924 lvif->lvif_bss = NULL;
3925 refcount_init(&lvif->nt_unlocked, 0);
3926 lvif->lvif_bss_synched = false;
3927 vap = LVIF_TO_VAP(lvif);
3928
3929 vif = LVIF_TO_VIF(lvif);
3930 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
3931 vif->p2p = false;
3932 vif->probe_req_reg = false;
3933 vif->type = lkpi_opmode_to_vif_type(opmode);
3934 lvif->wdev.iftype = vif->type;
3935 /* Need to fill in other fields as well. */
3936 IMPROVE();
3937
3938 /* XXX-BZ hardcoded for now! */
3939 #if 1
3940 RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3941 vif->bss_conf.vif = vif;
3942 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
3943 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
3944 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
3945 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
3946 vif->bss_conf.link_id = 0; /* Non-MLO operation. */
3947 vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
3948 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
3949 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */
3950 vif->bss_conf.qos = false;
3951 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */
3952 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3953 vif->cfg.aid = 0;
3954 vif->cfg.assoc = false;
3955 vif->cfg.idle = true;
3956 vif->cfg.ps = false;
3957 IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3958 /*
3959 * We need to initialize it to something as the bss_info_changed call
3960 * will try to copy from it in iwlwifi and NULL is a panic.
3961 * We will set the proper one in scan_to_auth() before being assoc.
3962 */
3963 vif->bss_conf.bssid = ieee80211broadcastaddr;
3964 #endif
3965 #if 0
3966 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
3967 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
3968 vif->bss_conf.beacon_int = ic->ic_bintval;
3969 /* iwlwifi bug. */
3970 if (vif->bss_conf.beacon_int < 16)
3971 vif->bss_conf.beacon_int = 16;
3972 #endif
3973
3974 /* Link Config */
3975 vif->link_conf[0] = &vif->bss_conf;
3976 for (i = 0; i < nitems(vif->link_conf); i++) {
3977 IMPROVE("more than 1 link one day");
3978 }
3979
3980 /* Setup queue defaults; driver may override in (*add_interface). */
3981 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3982 if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3983 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3984 else if (hw->queues >= IEEE80211_NUM_ACS)
3985 vif->hw_queue[i] = i;
3986 else
3987 vif->hw_queue[i] = 0;
3988
3989 /* Initialize the queue to running. Stopped? */
3990 lvif->hw_queue_stopped[i] = false;
3991 }
3992 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3993
3994 IMPROVE();
3995
3996 wiphy_lock(hw->wiphy);
3997 error = lkpi_80211_mo_start(hw);
3998 if (error != 0) {
3999 wiphy_unlock(hw->wiphy);
4000 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
4001 mtx_destroy(&lvif->mtx);
4002 free(lvif, M_80211_VAP);
4003 return (NULL);
4004 }
4005
4006 error = lkpi_80211_mo_add_interface(hw, vif);
4007 if (error != 0) {
4008 IMPROVE(); /* XXX-BZ mo_stop()? */
4009 wiphy_unlock(hw->wiphy);
4010 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
4011 mtx_destroy(&lvif->mtx);
4012 free(lvif, M_80211_VAP);
4013 return (NULL);
4014 }
4015 wiphy_unlock(hw->wiphy);
4016
4017 LKPI_80211_LHW_LVIF_LOCK(lhw);
4018 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
4019 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4020
4021 /* Set bss_info. */
4022 changed = 0;
4023 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
4024
4025 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
4026 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
4027 wiphy_lock(hw->wiphy);
4028 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4029
4030 bzero(&txqp, sizeof(txqp));
4031 txqp.cw_min = 15;
4032 txqp.cw_max = 1023;
4033 txqp.txop = 0;
4034 txqp.aifs = 2;
4035 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4036 if (error != 0)
4037 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4038 __func__, ac, error);
4039 }
4040 wiphy_unlock(hw->wiphy);
4041 changed = BSS_CHANGED_QOS;
4042 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
4043
4044 /* Force MC init. */
4045 lkpi_update_mcast_filter(ic);
4046
4047 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
4048
4049 /* Now we have a valid vap->iv_ifp. Any checksum offloading goes below. */
4050
4051 IMPROVE();
4052
4053 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
4054 lvif->iv_newstate = vap->iv_newstate;
4055 vap->iv_newstate = lkpi_iv_newstate;
4056 lvif->iv_update_bss = vap->iv_update_bss;
4057 vap->iv_update_bss = lkpi_iv_update_bss;
4058 lvif->iv_recv_mgmt = vap->iv_recv_mgmt;
4059 vap->iv_recv_mgmt = lkpi_iv_sta_recv_mgmt;
4060
4061 #ifdef LKPI_80211_HW_CRYPTO
4062 /* Key management. */
4063 if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
4064 vap->iv_key_set = lkpi_iv_key_set;
4065 vap->iv_key_delete = lkpi_iv_key_delete;
4066 vap->iv_key_update_begin = lkpi_iv_key_update_begin;
4067 vap->iv_key_update_end = lkpi_iv_key_update_end;
4068 }
4069 #endif
4070
4071 #ifdef LKPI_80211_HT
4072 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
4073 #endif
4074
4075 ieee80211_ratectl_init(vap);
4076
4077 /* Complete setup. */
4078 ieee80211_vap_attach(vap, ieee80211_media_change,
4079 ieee80211_media_status, mac);
4080
4081 #ifdef LKPI_80211_HT
4082 /*
4083 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
4084 * to do so if they cannot do the crypto too.
4085 */
4086 if (!lkpi_hwcrypto && IEEE80211_CONF_AMPDU_OFFLOAD(ic))
4087 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
4088 #endif
4089
4090 if (hw->max_listen_interval == 0)
4091 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
4092 hw->conf.listen_interval = hw->max_listen_interval;
4093 ic->ic_set_channel(ic);
4094
4095 /* XXX-BZ do we need to be able to update these? */
4096 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
4097 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
4098 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
4099 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
4100 /* any others? */
4101
4102 /* Add per-VIF/VAP sysctls. */
4103 sysctl_ctx_init(&lvif->sysctl_ctx);
4104
4105 node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
4106 SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
4107 OID_AUTO, if_name(vap->iv_ifp),
4108 CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
4109
4110 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4111 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
4112 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
4113 lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
4114
4115 IMPROVE();
4116
4117 return (vap);
4118 }
4119
4120 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)4121 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
4122 {
4123
4124 wiphy_unregister(hw->wiphy);
4125 linuxkpi_ieee80211_ifdetach(hw);
4126
4127 IMPROVE();
4128 }
4129
4130 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)4131 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
4132 {
4133
4134 TODO();
4135 }
4136
4137 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)4138 lkpi_ic_vap_delete(struct ieee80211vap *vap)
4139 {
4140 struct ieee80211com *ic;
4141 struct lkpi_hw *lhw;
4142 struct ieee80211_hw *hw;
4143 struct lkpi_vif *lvif;
4144 struct ieee80211_vif *vif;
4145
4146 lvif = VAP_TO_LVIF(vap);
4147 vif = LVIF_TO_VIF(lvif);
4148 ic = vap->iv_ic;
4149 lhw = ic->ic_softc;
4150 hw = LHW_TO_HW(lhw);
4151
4152 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
4153
4154 /* Clear up per-VIF/VAP sysctls. */
4155 sysctl_ctx_free(&lvif->sysctl_ctx);
4156
4157 ieee80211_draintask(ic, &lvif->sw_scan_task);
4158
4159 LKPI_80211_LHW_LVIF_LOCK(lhw);
4160 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
4161 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4162
4163 ieee80211_ratectl_deinit(vap);
4164 ieee80211_vap_detach(vap);
4165
4166 IMPROVE("clear up other bits in this state");
4167
4168 lkpi_80211_mo_remove_interface(hw, vif);
4169
4170 /* Single VAP, so we can do this here. */
4171 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4172
4173 mtx_destroy(&lvif->mtx);
4174 free(lvif, M_80211_VAP);
4175 }
4176
4177 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)4178 lkpi_ic_update_mcast(struct ieee80211com *ic)
4179 {
4180 struct ieee80211vap *vap;
4181 struct lkpi_hw *lhw;
4182
4183 lhw = ic->ic_softc;
4184 if (lhw->ops->prepare_multicast == NULL ||
4185 lhw->ops->configure_filter == NULL)
4186 return;
4187
4188 LKPI_80211_LHW_MC_LOCK(lhw);
4189 /* Cleanup anything on the current list. */
4190 lkpi_cleanup_mcast_list_locked(lhw);
4191
4192 /* Build up the new list (or allmulti). */
4193 if (ic->ic_allmulti == 0) {
4194 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
4195 if_foreach_llmaddr(vap->iv_ifp,
4196 lkpi_ic_update_mcast_copy, &lhw->mc_list);
4197 lhw->mc_all_multi = false;
4198 } else {
4199 lhw->mc_all_multi = true;
4200 }
4201 LKPI_80211_LHW_MC_UNLOCK(lhw);
4202
4203 lkpi_update_mcast_filter(ic);
4204 TRACEOK();
4205 }
4206
4207 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)4208 lkpi_ic_update_promisc(struct ieee80211com *ic)
4209 {
4210
4211 UNIMPLEMENTED;
4212 }
4213
4214 static void
lkpi_ic_update_chw(struct ieee80211com * ic)4215 lkpi_ic_update_chw(struct ieee80211com *ic)
4216 {
4217
4218 UNIMPLEMENTED;
4219 }
4220
4221 /* Start / stop device. */
4222 static void
lkpi_ic_parent(struct ieee80211com * ic)4223 lkpi_ic_parent(struct ieee80211com *ic)
4224 {
4225 struct lkpi_hw *lhw;
4226 struct ieee80211_hw *hw;
4227 #ifdef HW_START_STOP
4228 int error;
4229 #endif
4230 bool start_all;
4231
4232 IMPROVE();
4233
4234 lhw = ic->ic_softc;
4235 hw = LHW_TO_HW(lhw);
4236 start_all = false;
4237
4238 /* IEEE80211_UNLOCK(ic); */
4239 wiphy_lock(hw->wiphy);
4240 if (ic->ic_nrunning > 0) {
4241 #ifdef HW_START_STOP
4242 error = lkpi_80211_mo_start(hw);
4243 if (error == 0)
4244 #endif
4245 start_all = true;
4246 } else {
4247 #ifdef HW_START_STOP
4248 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4249 #endif
4250 }
4251 wiphy_unlock(hw->wiphy);
4252 /* IEEE80211_LOCK(ic); */
4253
4254 if (start_all)
4255 ieee80211_start_all(ic);
4256 }
4257
4258 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)4259 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
4260 size_t ie_ids_len)
4261 {
4262 int i;
4263
4264 for (i = 0; i < ie_ids_len; i++) {
4265 if (ie == *ie_ids)
4266 return (true);
4267 }
4268
4269 return (false);
4270 }
4271
4272 /* Return true if skipped; false if error. */
4273 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)4274 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
4275 {
4276 size_t x;
4277 uint8_t l;
4278
4279 x = *xp;
4280
4281 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
4282 __func__, x, ies_len, ies));
4283 l = ies[x + 1];
4284 x += 2 + l;
4285
4286 if (x > ies_len)
4287 return (false);
4288
4289 *xp = x;
4290 return (true);
4291 }
4292
4293 static uint8_t *
lkpi_scan_ies_add(uint8_t * p,struct ieee80211_scan_ies * scan_ies,uint32_t band_mask,struct ieee80211vap * vap,struct ieee80211_hw * hw)4294 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4295 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
4296 {
4297 struct ieee80211_supported_band *supband;
4298 struct linuxkpi_ieee80211_channel *channels;
4299 struct ieee80211com *ic;
4300 const struct ieee80211_channel *chan;
4301 const struct ieee80211_rateset *rs;
4302 uint8_t *pb;
4303 int band, i;
4304
4305 ic = vap->iv_ic;
4306 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4307 if ((band_mask & (1 << band)) == 0)
4308 continue;
4309
4310 supband = hw->wiphy->bands[band];
4311 /*
4312 * This should not happen;
4313 * band_mask is a bitmask of valid bands to scan on.
4314 */
4315 if (supband == NULL || supband->n_channels == 0)
4316 continue;
4317
4318 /* Find a first channel to get the mode and rates from. */
4319 channels = supband->channels;
4320 chan = NULL;
4321 for (i = 0; i < supband->n_channels; i++) {
4322 uint32_t flags;
4323
4324 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4325 continue;
4326
4327 flags = 0;
4328 switch (band) {
4329 case NL80211_BAND_2GHZ:
4330 flags |= IEEE80211_CHAN_G;
4331 break;
4332 case NL80211_BAND_5GHZ:
4333 flags |= IEEE80211_CHAN_A;
4334 break;
4335 default:
4336 panic("%s:%d: unupported band %d\n",
4337 __func__, __LINE__, band);
4338 }
4339
4340 chan = ieee80211_find_channel(ic,
4341 channels[i].center_freq, flags);
4342 if (chan != NULL)
4343 break;
4344 }
4345
4346 /* This really should not happen. */
4347 if (chan == NULL)
4348 continue;
4349
4350 pb = p;
4351 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */
4352 p = ieee80211_add_rates(p, rs);
4353 p = ieee80211_add_xrates(p, rs);
4354
4355 #if defined(LKPI_80211_HT)
4356 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
4357 struct ieee80211_channel *c;
4358
4359 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4360 vap->iv_flags_ht);
4361 p = ieee80211_add_htcap_ch(p, vap, c);
4362 }
4363 #endif
4364 #if defined(LKPI_80211_VHT)
4365 if (band == NL80211_BAND_5GHZ &&
4366 (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
4367 struct ieee80211_channel *c;
4368
4369 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4370 vap->iv_flags_ht);
4371 c = ieee80211_vht_adjust_channel(ic, c,
4372 vap->iv_vht_flags);
4373 p = ieee80211_add_vhtcap_ch(p, vap, c);
4374 }
4375 #endif
4376
4377 scan_ies->ies[band] = pb;
4378 scan_ies->len[band] = p - pb;
4379 }
4380
4381 /* Add common_ies */
4382 pb = p;
4383 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4384 vap->iv_wpa_ie != NULL) {
4385 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4386 p += 2 + vap->iv_wpa_ie[1];
4387 }
4388 if (vap->iv_appie_probereq != NULL) {
4389 memcpy(p, vap->iv_appie_probereq->ie_data,
4390 vap->iv_appie_probereq->ie_len);
4391 p += vap->iv_appie_probereq->ie_len;
4392 }
4393 scan_ies->common_ies = pb;
4394 scan_ies->common_ie_len = p - pb;
4395
4396 return (p);
4397 }
4398
4399 static void
lkpi_enable_hw_scan(struct lkpi_hw * lhw)4400 lkpi_enable_hw_scan(struct lkpi_hw *lhw)
4401 {
4402
4403 if (lhw->ops->hw_scan) {
4404 /*
4405 * Advertise full-offload scanning.
4406 *
4407 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4408 * we essentially disable hw_scan for all drivers not setting
4409 * the flag.
4410 */
4411 lhw->ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4412 lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4413 }
4414 }
4415
4416 #ifndef LKPI_80211_USE_SCANLIST
4417 static const uint32_t chan_pri[] = {
4418 5180, 5500, 5745,
4419 5260, 5580, 5660, 5825,
4420 5220, 5300, 5540, 5620, 5700, 5785, 5865,
4421 2437, 2412, 2422, 2462, 2472, 2432, 2452
4422 };
4423
4424 static int
lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel * lc)4425 lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel *lc)
4426 {
4427 int i;
4428
4429 for (i = 0; i < nitems(chan_pri); i++) {
4430 if (lc->center_freq == chan_pri[i])
4431 return (i);
4432 }
4433
4434 return (-1);
4435 }
4436
4437 static int
lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel * lc1,const struct linuxkpi_ieee80211_channel * lc2)4438 lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel *lc1,
4439 const struct linuxkpi_ieee80211_channel *lc2)
4440 {
4441 int idx1, idx2;
4442
4443 /* Find index in list. */
4444 idx1 = lkpi_scan_chan_list_idx(lc1);
4445 idx2 = lkpi_scan_chan_list_idx(lc2);
4446
4447 if (idx1 == -1 && idx2 != -1)
4448 return (1);
4449 if (idx1 != -1 && idx2 == -1)
4450 return (-1);
4451
4452 /* Neither on the list, use center_freq. */
4453 if (idx1 == -1 && idx2 == -1)
4454 return (lc1->center_freq - lc2->center_freq);
4455
4456 /* Whichever is first in the list. */
4457 return (idx1 - idx2);
4458 }
4459
4460 static void
lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel ** cpp,size_t nchan)4461 lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel **cpp, size_t nchan)
4462 {
4463 struct linuxkpi_ieee80211_channel *lc, *nc;
4464 size_t i, j;
4465 int rc;
4466
4467 for (i = (nchan - 1); i > 0; i--) {
4468 for (j = i; j > 0 ; j--) {
4469 lc = *(cpp + j);
4470 nc = *(cpp + j - 1);
4471 rc = lkpi_scan_chan_list_comp(lc, nc);
4472 if (rc < 0) {
4473 *(cpp + j) = nc;
4474 *(cpp + j - 1) = lc;
4475 }
4476 }
4477 }
4478 }
4479
4480 static bool
lkpi_scan_chan(struct linuxkpi_ieee80211_channel * c,struct ieee80211com * ic,bool log)4481 lkpi_scan_chan(struct linuxkpi_ieee80211_channel *c,
4482 struct ieee80211com *ic, bool log)
4483 {
4484
4485 if ((c->flags & IEEE80211_CHAN_DISABLED) != 0) {
4486 if (log)
4487 TRACE_SCAN(ic, "Skipping disabled chan "
4488 "on band %s [%#x/%u/%#x]",
4489 lkpi_nl80211_band_name(c->band), c->hw_value,
4490 c->center_freq, c->flags);
4491 return (false);
4492 }
4493 if (isclr(ic->ic_chan_active, ieee80211_mhz2ieee(c->center_freq,
4494 lkpi_nl80211_band_to_net80211_band(c->band)))) {
4495 if (log)
4496 TRACE_SCAN(ic, "Skipping !active chan "
4497 "on band %s [%#x/%u/%#x]",
4498 lkpi_nl80211_band_name(c->band), c->hw_value,
4499 c->center_freq, c->flags);
4500 return (false);
4501 }
4502 return (true);
4503 }
4504 #endif
4505
4506 static void
lkpi_ic_scan_start(struct ieee80211com * ic)4507 lkpi_ic_scan_start(struct ieee80211com *ic)
4508 {
4509 struct lkpi_hw *lhw;
4510 struct ieee80211_hw *hw;
4511 struct lkpi_vif *lvif;
4512 struct ieee80211_vif *vif;
4513 struct ieee80211_scan_state *ss;
4514 struct ieee80211vap *vap;
4515 int error;
4516 bool is_hw_scan;
4517
4518 lhw = ic->ic_softc;
4519 ss = ic->ic_scan;
4520 vap = ss->ss_vap;
4521 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4522
4523 LKPI_80211_LHW_SCAN_LOCK(lhw);
4524 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4525 /* A scan is still running. */
4526 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4527 TRACE_SCAN(ic, "Trying to start new scan while still running; "
4528 "cancelling new net80211 scan; scan_flags %b",
4529 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4530 ieee80211_cancel_scan(vap);
4531 return;
4532 }
4533 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4534 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4535
4536 #if 0
4537 if (vap->iv_state != IEEE80211_S_SCAN) {
4538 TODO("We need to be able to scan if not in S_SCAN");
4539 TRACE_SCAN(ic, "scan_flags %b iv_state %d",
4540 lhw->scan_flags, LKPI_LHW_SCAN_BITS, vap->iv_state);
4541 ieee80211_cancel_scan(vap);
4542 return;
4543 }
4544 #endif
4545
4546 hw = LHW_TO_HW(lhw);
4547 if (!is_hw_scan) {
4548 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4549 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4550
4551 lvif = VAP_TO_LVIF(vap);
4552 vif = LVIF_TO_VIF(lvif);
4553
4554 if (vap->iv_state == IEEE80211_S_SCAN)
4555 lkpi_hw_conf_idle(hw, false);
4556
4557 LKPI_80211_LHW_SCAN_LOCK(lhw);
4558 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
4559 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4560
4561 lkpi_update_mcast_filter(ic);
4562
4563 TRACE_SCAN(vap->iv_ic, "Starting SW_SCAN: scan_flags %b",
4564 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4565 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
4566 /* net80211::scan_start() handled PS for us. */
4567 IMPROVE();
4568 /* XXX Also means it is too late to flush queues?
4569 * need to check iv_sta_ps or overload? */
4570 /* XXX want to adjust ss end time/ maxdwell? */
4571
4572 } else {
4573 struct ieee80211_scan_request *hw_req;
4574 struct linuxkpi_ieee80211_channel *lc, **cpp;
4575 struct cfg80211_ssid *ssids;
4576 struct cfg80211_scan_6ghz_params *s6gp;
4577 size_t chan_len, nchan, ssids_len, s6ghzlen;
4578 int band, i, ssid_count, common_ie_len;
4579 #ifndef LKPI_80211_USE_SCANLIST
4580 int n;
4581 #endif
4582 uint32_t band_mask;
4583 uint8_t *ie, *ieend;
4584 bool running;
4585
4586 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4587 ssids_len = ssid_count * sizeof(*ssids);
4588 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */
4589
4590 band_mask = 0;
4591 nchan = 0;
4592 if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4593 #ifdef LKPI_80211_USE_SCANLIST
4594 /* Avoid net80211 scan lists until it has proper scan offload support. */
4595 for (i = ss->ss_next; i < ss->ss_last; i++) {
4596 nchan++;
4597 band = lkpi_net80211_chan_to_nl80211_band(
4598 ss->ss_chans[ss->ss_next + i]);
4599 band_mask |= (1 << band);
4600 }
4601 #else
4602 /* Instead we scan for all channels all the time. */
4603 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4604 switch (band) {
4605 case NL80211_BAND_2GHZ:
4606 case NL80211_BAND_5GHZ:
4607 break;
4608 default:
4609 continue;
4610 }
4611 if (hw->wiphy->bands[band] != NULL) {
4612 struct linuxkpi_ieee80211_channel *channels;
4613 int n;
4614
4615 band_mask |= (1 << band);
4616
4617 channels = hw->wiphy->bands[band]->channels;
4618 n = hw->wiphy->bands[band]->n_channels;
4619 for (i = 0; i < n; i++) {
4620 if (lkpi_scan_chan(&channels[i], ic, true))
4621 nchan++;
4622 }
4623 }
4624 }
4625 #endif
4626 } else {
4627 IMPROVE("individual band scans not yet supported, only scanning first band");
4628 /* In theory net80211 should drive this. */
4629 /* Probably we need to add local logic for now;
4630 * need to deal with scan_complete
4631 * and cancel_scan and keep local state.
4632 * Also cut the nchan down above.
4633 */
4634 /* XXX-BZ ath10k does not set this but still does it? &$%^ */
4635 }
4636
4637 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
4638
4639 common_ie_len = 0;
4640 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4641 vap->iv_wpa_ie != NULL)
4642 common_ie_len += vap->iv_wpa_ie[1];
4643 if (vap->iv_appie_probereq != NULL)
4644 common_ie_len += vap->iv_appie_probereq->ie_len;
4645
4646 /* We would love to check this at an earlier stage... */
4647 if (common_ie_len > hw->wiphy->max_scan_ie_len) {
4648 ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4649 "wiphy->max_scan_ie_len %d\n", __func__,
4650 common_ie_len, hw->wiphy->max_scan_ie_len);
4651 }
4652
4653 hw_req = malloc(sizeof(*hw_req) + ssids_len +
4654 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4655 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
4656
4657 hw_req->req.flags = 0; /* XXX ??? */
4658 /* hw_req->req.wdev */
4659 hw_req->req.wiphy = hw->wiphy;
4660 hw_req->req.no_cck = false; /* XXX */
4661
4662 /*
4663 * In general setting duration[_mandatory] seems to pessimise
4664 * default scanning behaviour. We only use it for BGSCANnig
4665 * to keep the dwell times small.
4666 * Setting duration_mandatory makes this the maximum dwell
4667 * time (otherwise may be shorter). Duration is in TU.
4668 */
4669 if ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) != 0) {
4670 unsigned long dwell;
4671
4672 if ((ic->ic_caps & IEEE80211_C_BGSCAN) == 0 ||
4673 (vap->iv_flags & IEEE80211_F_BGSCAN) == 0)
4674 ic_printf(ic, "BGSCAN despite off: %b, %b, %b\n",
4675 ic->ic_flags_ext, IEEE80211_FEXT_BITS,
4676 vap->iv_flags, IEEE80211_F_BITS,
4677 ic->ic_caps, IEEE80211_C_BITS);
4678
4679 dwell = ss->ss_mindwell;
4680 if (dwell == 0)
4681 dwell = msecs_to_ticks(20);
4682
4683 hw_req->req.duration_mandatory = true;
4684 hw_req->req.duration = TICKS_2_USEC(dwell) / 1024;
4685 }
4686
4687 #ifdef __notyet__
4688 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
4689 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
4690 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
4691 #endif
4692 eth_broadcast_addr(hw_req->req.bssid);
4693
4694 hw_req->req.n_channels = nchan;
4695 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
4696 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
4697 #ifdef LKPI_80211_USE_SCANLIST
4698 for (i = 0; i < nchan; i++) {
4699 *(cpp + i) =
4700 (struct linuxkpi_ieee80211_channel *)(lc + i);
4701 }
4702 /* Avoid net80211 scan lists until it has proper scan offload support. */
4703 for (i = 0; i < nchan; i++) {
4704 struct ieee80211_channel *c;
4705
4706 c = ss->ss_chans[ss->ss_next + i];
4707 lc->center_freq = c->ic_freq; /* XXX */
4708 /* lc->flags */
4709 lc->band = lkpi_net80211_chan_to_nl80211_band(c);
4710 lc->max_power = c->ic_maxpower;
4711 /* lc-> ... */
4712 lc++;
4713 }
4714 #else
4715 /* Add bands in reverse order for scanning. */
4716 n = 0;
4717 for (band = NUM_NL80211_BANDS - 1; band >= 0; band--) {
4718 struct ieee80211_supported_band *supband;
4719 struct linuxkpi_ieee80211_channel *channels;
4720
4721 /* Band disabled for scanning? */
4722 if ((band_mask & (1 << band)) == 0)
4723 continue;
4724
4725 /* Nothing to scan in band? */
4726 supband = hw->wiphy->bands[band];
4727 if (supband == NULL || supband->n_channels == 0)
4728 continue;
4729
4730 channels = supband->channels;
4731 for (i = 0; i < supband->n_channels; i++) {
4732 if (lkpi_scan_chan(&channels[i], ic, false))
4733 *(cpp + n++) = &channels[i];
4734 }
4735 }
4736 if (lkpi_order_scanlist)
4737 lkpi_scan_chan_list_resort(cpp, nchan);
4738
4739 if ((linuxkpi_debug_80211 & D80211_SCAN) != 0) {
4740 printf("%s:%d: %s SCAN Channel List (nchan=%zu): ",
4741 __func__, __LINE__, ic->ic_name, nchan);
4742 for (i = 0; i < nchan; i++) {
4743 struct linuxkpi_ieee80211_channel *xc;
4744
4745 xc = *(cpp + i);
4746 printf(" %d(%d)",
4747 ieee80211_mhz2ieee(xc->center_freq,
4748 lkpi_nl80211_band_to_net80211_band(
4749 xc->band)),
4750 xc->center_freq);
4751 }
4752 printf("\n");
4753 }
4754 #endif
4755
4756 hw_req->req.n_ssids = ssid_count;
4757 if (hw_req->req.n_ssids > 0) {
4758 ssids = (struct cfg80211_ssid *)lc;
4759 hw_req->req.ssids = ssids;
4760 for (i = 0; i < ssid_count; i++) {
4761 ssids->ssid_len = ss->ss_ssid[i].len;
4762 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
4763 ss->ss_ssid[i].len);
4764 ssids++;
4765 }
4766 s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
4767 } else {
4768 s6gp = (struct cfg80211_scan_6ghz_params *)lc;
4769 }
4770
4771 /* 6GHz one day. */
4772 hw_req->req.n_6ghz_params = 0;
4773 hw_req->req.scan_6ghz_params = NULL;
4774 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */
4775 /* s6gp->... */
4776
4777 ie = ieend = (uint8_t *)s6gp;
4778 /* Copy per-band IEs, copy common IEs */
4779 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4780 hw_req->req.ie = ie;
4781 hw_req->req.ie_len = ieend - ie;
4782 hw_req->req.scan_start = jiffies;
4783
4784 lvif = VAP_TO_LVIF(vap);
4785 vif = LVIF_TO_VIF(lvif);
4786
4787 LKPI_80211_LHW_SCAN_LOCK(lhw);
4788 /* Re-check under lock. */
4789 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
4790 if (!running) {
4791 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
4792 "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
4793
4794 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
4795 lhw->hw_req = hw_req;
4796 }
4797 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4798 if (running) {
4799 free(hw_req, M_LKPI80211);
4800 TRACE_SCAN(ic, "Trying to start new scan while still "
4801 "running (2); cancelling new net80211 scan; "
4802 "scan_flags %b",
4803 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4804 ieee80211_cancel_scan(vap);
4805 return;
4806 }
4807
4808 lkpi_update_mcast_filter(ic);
4809 TRACE_SCAN(ic, "Starting HW_SCAN: scan_flags %b, "
4810 "ie_len %d, n_ssids %d, n_chan %d, common_ie_len %d [%d, %d]",
4811 lhw->scan_flags, LKPI_LHW_SCAN_BITS, hw_req->req.ie_len,
4812 hw_req->req.n_ssids, hw_req->req.n_channels,
4813 hw_req->ies.common_ie_len,
4814 hw_req->ies.len[NL80211_BAND_2GHZ],
4815 hw_req->ies.len[NL80211_BAND_5GHZ]);
4816
4817 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
4818 if (error != 0) {
4819 bool scan_done;
4820 int e;
4821
4822 TRACE_SCAN(ic, "hw_scan failed; scan_flags %b, error %d",
4823 lhw->scan_flags, LKPI_LHW_SCAN_BITS, error);
4824 ieee80211_cancel_scan(vap);
4825
4826 /*
4827 * ieee80211_scan_completed must be called in either
4828 * case of error or none. So let the free happen there
4829 * and only there.
4830 * That would be fine in theory but in practice drivers
4831 * behave differently:
4832 * ath10k does not return hw_scan until after scan_complete
4833 * and can then still return an error.
4834 * rtw88 can return 1 or -EBUSY without scan_complete
4835 * iwlwifi can return various errors before scan starts
4836 * ...
4837 * So we cannot rely on that behaviour and have to check
4838 * and balance between both code paths.
4839 */
4840 e = 0;
4841 scan_done = true;
4842 LKPI_80211_LHW_SCAN_LOCK(lhw);
4843 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4844
4845 free(lhw->hw_req, M_LKPI80211);
4846 lhw->hw_req = NULL;
4847 /*
4848 * The ieee80211_cancel_scan() above runs in a
4849 * taskq and it may take ages for the previous
4850 * scan to clear; starting a new one right away
4851 * we run into the problem that the old one is
4852 * still active.
4853 */
4854 e = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz);
4855 scan_done = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
4856
4857 /*
4858 * Now we can clear running if no one else did.
4859 */
4860 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4861 }
4862 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4863 lkpi_update_mcast_filter(ic);
4864 if (!scan_done) {
4865 ic_printf(ic, "ERROR: %s: timeout/error to wait "
4866 "for ieee80211_cancel_scan: %d\n", __func__, e);
4867 return;
4868 }
4869
4870 /*
4871 * XXX-SIGH magic number.
4872 * rtw88 has a magic "return 1" if offloading scan is
4873 * not possible. Fall back to sw scan in that case.
4874 */
4875 if (error == 1) {
4876 /*
4877 * We need to put this into some defered context
4878 * the net80211 scan may not be done yet
4879 * (ic_flags & IEEE80211_F_SCAN) and we cannot
4880 * wait here; if we do scan_curchan_task always
4881 * runs after our timeout to finalize the scan.
4882 */
4883 ieee80211_runtask(ic, &lvif->sw_scan_task);
4884 return;
4885 }
4886
4887 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4888 __func__, error);
4889 }
4890 }
4891 }
4892
4893 static void
lkpi_sw_scan_task(void * arg,int pending __unused)4894 lkpi_sw_scan_task(void *arg, int pending __unused)
4895 {
4896 struct lkpi_hw *lhw;
4897 struct lkpi_vif *lvif;
4898 struct ieee80211vap *vap;
4899 struct ieee80211_scan_state *ss;
4900
4901 lvif = arg;
4902 vap = LVIF_TO_VAP(lvif);
4903 lhw = vap->iv_ic->ic_softc;
4904 ss = vap->iv_ic->ic_scan;
4905
4906 LKPI_80211_LHW_SCAN_LOCK(lhw);
4907 /*
4908 * We will re-enable this at scan_end calling lkpi_enable_hw_scan().
4909 * IEEE80211_FEXT_SCAN_OFFLOAD will be cleared by lkpi_ic_scan_start.
4910 */
4911 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
4912 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4913
4914 TRACE_SCAN(vap->iv_ic, "Triggering SW_SCAN: pending %d, scan_flags %b",
4915 pending, lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4916
4917 /*
4918 * This will call ic_scan_start() and we will get into the right path
4919 * unless other scans started in between.
4920 */
4921 ieee80211_start_scan(vap,
4922 IEEE80211_SCAN_ONCE,
4923 msecs_to_ticks(10000), /* 10000 ms (=~ 50 chan * 200 ms) */
4924 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4925 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4926 vap->iv_des_nssid, vap->iv_des_ssid);
4927 }
4928
4929 static void
lkpi_ic_scan_end(struct ieee80211com * ic)4930 lkpi_ic_scan_end(struct ieee80211com *ic)
4931 {
4932 struct lkpi_hw *lhw;
4933 bool is_hw_scan;
4934
4935 lhw = ic->ic_softc;
4936 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4937
4938 LKPI_80211_LHW_SCAN_LOCK(lhw);
4939 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
4940 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4941 return;
4942 }
4943 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4944 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4945
4946 if (!is_hw_scan) {
4947 struct ieee80211_scan_state *ss;
4948 struct ieee80211vap *vap;
4949 struct ieee80211_hw *hw;
4950 struct lkpi_vif *lvif;
4951 struct ieee80211_vif *vif;
4952
4953 ss = ic->ic_scan;
4954 vap = ss->ss_vap;
4955 hw = LHW_TO_HW(lhw);
4956 lvif = VAP_TO_LVIF(vap);
4957 vif = LVIF_TO_VIF(lvif);
4958
4959 lkpi_80211_mo_sw_scan_complete(hw, vif);
4960
4961 /* Send PS to stop buffering if n80211 does not for us? */
4962
4963 if (vap->iv_state == IEEE80211_S_SCAN)
4964 lkpi_hw_conf_idle(hw, true);
4965 }
4966
4967 /*
4968 * In case we disabled the hw_scan in lkpi_ic_scan_start() and
4969 * switched to swscan, re-enable hw_scan if available.
4970 */
4971 lkpi_enable_hw_scan(lhw);
4972
4973 LKPI_80211_LHW_SCAN_LOCK(lhw);
4974 wakeup(lhw);
4975 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4976 }
4977
4978 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)4979 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4980 unsigned long maxdwell)
4981 {
4982 struct lkpi_hw *lhw;
4983 bool is_hw_scan;
4984
4985 lhw = ss->ss_ic->ic_softc;
4986 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d maxdwell %lu",
4987 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
4988 ss->ss_ic->ic_curchan->ic_ieee, maxdwell);
4989
4990 LKPI_80211_LHW_SCAN_LOCK(lhw);
4991 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4992 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4993 if (!is_hw_scan)
4994 lhw->ic_scan_curchan(ss, maxdwell);
4995 }
4996
4997 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)4998 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4999 {
5000 struct lkpi_hw *lhw;
5001 bool is_hw_scan;
5002
5003 lhw = ss->ss_ic->ic_softc;
5004 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d mindwell %lu",
5005 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5006 ss->ss_ic->ic_curchan->ic_ieee, ss->ss_mindwell);
5007
5008 LKPI_80211_LHW_SCAN_LOCK(lhw);
5009 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5010 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5011 if (!is_hw_scan)
5012 lhw->ic_scan_mindwell(ss);
5013 }
5014
5015 static void
lkpi_ic_set_channel(struct ieee80211com * ic)5016 lkpi_ic_set_channel(struct ieee80211com *ic)
5017 {
5018 struct lkpi_hw *lhw;
5019 struct ieee80211_hw *hw;
5020 struct ieee80211_channel *c;
5021 struct linuxkpi_ieee80211_channel *chan;
5022 int error;
5023 bool hw_scan_running;
5024
5025 lhw = ic->ic_softc;
5026
5027 /* If we do not support (*config)() save us the work. */
5028 if (lhw->ops->config == NULL)
5029 return;
5030
5031 /* If we have a hw_scan running do not switch channels. */
5032 LKPI_80211_LHW_SCAN_LOCK(lhw);
5033 hw_scan_running =
5034 (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
5035 (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
5036 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5037 if (hw_scan_running)
5038 return;
5039
5040 c = ic->ic_curchan;
5041 if (c == NULL || c == IEEE80211_CHAN_ANYC) {
5042 ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
5043 c, lhw->ops->config);
5044 return;
5045 }
5046
5047 chan = lkpi_find_lkpi80211_chan(lhw, c);
5048 if (chan == NULL) {
5049 ic_printf(ic, "%s: c %p chan %p\n", __func__,
5050 c, chan);
5051 return;
5052 }
5053
5054 /* XXX max power for scanning? */
5055 IMPROVE();
5056
5057 hw = LHW_TO_HW(lhw);
5058 cfg80211_chandef_create(&hw->conf.chandef, chan,
5059 #ifdef LKPI_80211_HT
5060 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
5061 #endif
5062 NL80211_CHAN_NO_HT);
5063
5064 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
5065 if (error != 0 && error != EOPNOTSUPP) {
5066 ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
5067 __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
5068 /* XXX should we unroll to the previous chandef? */
5069 IMPROVE();
5070 } else {
5071 /* Update radiotap channels as well. */
5072 lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
5073 lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
5074 lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
5075 lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
5076 }
5077
5078 /* Currently PS is hard coded off! Not sure it belongs here. */
5079 IMPROVE();
5080 if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
5081 (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
5082 hw->conf.flags &= ~IEEE80211_CONF_PS;
5083 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
5084 if (error != 0 && error != EOPNOTSUPP)
5085 ic_printf(ic, "ERROR: %s: config %#0x returned "
5086 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
5087 error);
5088 }
5089 }
5090
5091 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])5092 lkpi_ic_node_alloc(struct ieee80211vap *vap,
5093 const uint8_t mac[IEEE80211_ADDR_LEN])
5094 {
5095 struct ieee80211com *ic;
5096 struct lkpi_hw *lhw;
5097 struct ieee80211_node *ni;
5098 struct ieee80211_hw *hw;
5099 struct lkpi_sta *lsta;
5100
5101 ic = vap->iv_ic;
5102 lhw = ic->ic_softc;
5103
5104 /* We keep allocations de-coupled so we can deal with the two worlds. */
5105 if (lhw->ic_node_alloc == NULL)
5106 return (NULL);
5107
5108 ni = lhw->ic_node_alloc(vap, mac);
5109 if (ni == NULL)
5110 return (NULL);
5111
5112 hw = LHW_TO_HW(lhw);
5113 lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
5114 if (lsta == NULL) {
5115 if (lhw->ic_node_free != NULL)
5116 lhw->ic_node_free(ni);
5117 return (NULL);
5118 }
5119
5120 return (ni);
5121 }
5122
5123 static int
lkpi_ic_node_init(struct ieee80211_node * ni)5124 lkpi_ic_node_init(struct ieee80211_node *ni)
5125 {
5126 struct ieee80211com *ic;
5127 struct lkpi_hw *lhw;
5128 int error;
5129
5130 ic = ni->ni_ic;
5131 lhw = ic->ic_softc;
5132
5133 if (lhw->ic_node_init != NULL) {
5134 error = lhw->ic_node_init(ni);
5135 if (error != 0)
5136 return (error);
5137 }
5138
5139 /* XXX-BZ Sync other state over. */
5140 IMPROVE();
5141
5142 return (0);
5143 }
5144
5145 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)5146 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
5147 {
5148 struct ieee80211com *ic;
5149 struct lkpi_hw *lhw;
5150
5151 ic = ni->ni_ic;
5152 lhw = ic->ic_softc;
5153
5154 /* XXX-BZ remove from driver, ... */
5155 IMPROVE();
5156
5157 if (lhw->ic_node_cleanup != NULL)
5158 lhw->ic_node_cleanup(ni);
5159 }
5160
5161 static void
lkpi_ic_node_free(struct ieee80211_node * ni)5162 lkpi_ic_node_free(struct ieee80211_node *ni)
5163 {
5164 struct ieee80211com *ic;
5165 struct lkpi_hw *lhw;
5166 struct lkpi_sta *lsta;
5167
5168 ic = ni->ni_ic;
5169 lhw = ic->ic_softc;
5170 lsta = ni->ni_drv_data;
5171
5172 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
5173
5174 /*
5175 * Pass in the original ni just in case of error we could check that
5176 * it is the same as lsta->ni.
5177 */
5178 lkpi_lsta_free(lsta, ni);
5179
5180 if (lhw->ic_node_free != NULL)
5181 lhw->ic_node_free(ni);
5182 }
5183
5184 /*
5185 * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
5186 * call path.
5187 * Unfortunately they have slightly different invariants. See
5188 * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
5189 * Both take care of the ni reference in case of error, and otherwise during
5190 * the callback after transmit.
5191 * The difference is that in case of error (*ic_raw_xmit) needs us to release
5192 * the mbuf, while (*ic_transmit) will free the mbuf itself.
5193 */
5194 static int
lkpi_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused,bool freem)5195 lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
5196 const struct ieee80211_bpf_params *params __unused,
5197 bool freem)
5198 {
5199 struct lkpi_sta *lsta;
5200 int error;
5201
5202 lsta = ni->ni_drv_data;
5203 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5204 #if 0
5205 if (!lsta->added_to_drv || !lsta->txq_ready) {
5206 #else
5207 /*
5208 * Backout this part of 886653492945f which breaks rtw88 or
5209 * in general drivers without (*sta_state)() but only the
5210 * legacy fallback to (*sta_add)().
5211 */
5212 if (!lsta->txq_ready) {
5213 #endif
5214 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5215 if (freem)
5216 m_free(m);
5217 return (ENETDOWN);
5218 }
5219
5220 /* Queue the packet and enqueue the task to handle it. */
5221 error = mbufq_enqueue(&lsta->txq, m);
5222 if (error != 0) {
5223 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5224 if (freem)
5225 m_free(m);
5226 #ifdef LINUXKPI_DEBUG_80211
5227 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5228 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
5229 __func__, error);
5230 #endif
5231 return (ENETDOWN);
5232 }
5233 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
5234 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5235
5236 #ifdef LINUXKPI_DEBUG_80211
5237 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5238 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
5239 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
5240 mbufq_len(&lsta->txq));
5241 #endif
5242
5243 return (0);
5244 }
5245
5246 static int
5247 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
5248 const struct ieee80211_bpf_params *params __unused)
5249 {
5250 return (lkpi_xmit(ni, m, NULL, true));
5251 }
5252
5253 #ifdef LKPI_80211_HW_CRYPTO
5254 /*
5255 * This is a bit of a hack given we know we are operating on a
5256 * single frame and we know that hardware will deal with it.
5257 * But otherwise the enmic bit and the encrypt bit need to be
5258 * decoupled.
5259 */
5260 static int
5261 lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
5262 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5263 {
5264 struct ieee80211_hdr *hdr;
5265 uint32_t hlen, hdrlen;
5266 uint8_t *p;
5267
5268 /*
5269 * TKIP only happens on data.
5270 */
5271 hdr = (void *)skb->data;
5272 if (!ieee80211_is_data_present(hdr->frame_control))
5273 return (0);
5274
5275 /*
5276 * "enmic" (though we do not do that).
5277 */
5278 /* any conditions to not apply this? */
5279 if (skb_tailroom(skb) < k->wk_cipher->ic_miclen)
5280 return (ENOBUFS);
5281
5282 p = skb_put(skb, k->wk_cipher->ic_miclen);
5283 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
5284 goto encrypt;
5285
5286 /*
5287 * (*enmic) which we hopefully do not have to do with hw accel.
5288 * That means if we make it here we have a problem.
5289 */
5290 TODO("(*enmic)");
5291 return (ENXIO);
5292
5293 encrypt:
5294 /*
5295 * "encrypt" (though we do not do that).
5296 */
5297 /*
5298 * Check if we have anything to do as requested by driver
5299 * or if we are done?
5300 */
5301 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5302 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
5303 return (0);
5304
5305 hlen = k->wk_cipher->ic_header;
5306 if (skb_headroom(skb) < hlen)
5307 return (ENOBUFS);
5308
5309 hdr = (void *)skb->data;
5310 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5311 p = skb_push(skb, hlen);
5312 memmove(p, p + hlen, hdrlen);
5313
5314 /* If driver request space only we are done. */
5315 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5316 return (0);
5317
5318 p += hdrlen;
5319 k->wk_cipher->ic_setiv(k, p);
5320
5321 /* If we make it hear we do sw encryption. */
5322 TODO("sw encrypt");
5323 return (ENXIO);
5324 }
5325
5326 static int
5327 lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
5328 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5329 {
5330 struct ieee80211_hdr *hdr;
5331 uint32_t hlen, hdrlen;
5332 uint8_t *p;
5333
5334 hdr = (void *)skb->data;
5335
5336 /*
5337 * Check if we have anythig to do as requested by driver
5338 * or if we are done?
5339 */
5340 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5341 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
5342 /* MFP */
5343 !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
5344 ieee80211_is_mgmt(hdr->frame_control)))
5345 return (0);
5346
5347 hlen = k->wk_cipher->ic_header;
5348 if (skb_headroom(skb) < hlen)
5349 return (ENOBUFS);
5350
5351 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5352 p = skb_push(skb, hlen);
5353 memmove(p, p + hlen, hdrlen);
5354
5355 /* If driver request space only we are done. */
5356 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5357 return (0);
5358
5359 p += hdrlen;
5360 k->wk_cipher->ic_setiv(k, p);
5361
5362 return (0);
5363 }
5364
5365 static int
5366 lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
5367 struct sk_buff *skb)
5368 {
5369 struct ieee80211_tx_info *info;
5370 struct ieee80211_key_conf *kc;
5371
5372 KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
5373 KASSERT(k != NULL, ("%s: key is NULL", __func__));
5374 KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
5375
5376 kc = lsta->kc[k->wk_keyix];
5377
5378 info = IEEE80211_SKB_CB(skb);
5379 info->control.hw_key = kc;
5380
5381 /* MUST NOT happen. KASSERT? */
5382 if (kc == NULL) {
5383 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
5384 "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
5385 return (ENXIO);
5386 }
5387
5388 switch (kc->cipher) {
5389 case WLAN_CIPHER_SUITE_TKIP:
5390 return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
5391 case WLAN_CIPHER_SUITE_CCMP:
5392 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5393 case WLAN_CIPHER_SUITE_GCMP:
5394 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5395 case WLAN_CIPHER_SUITE_WEP40:
5396 case WLAN_CIPHER_SUITE_WEP104:
5397 case WLAN_CIPHER_SUITE_CCMP_256:
5398 case WLAN_CIPHER_SUITE_GCMP_256:
5399 case WLAN_CIPHER_SUITE_AES_CMAC:
5400 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5401 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5402 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5403 default:
5404 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
5405 "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
5406 skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
5407 return (EOPNOTSUPP);
5408 }
5409 }
5410
5411 static uint8_t
5412 lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
5413 {
5414 struct ieee80211_key_conf *kc;
5415
5416 kc = lsta->kc[k->wk_keyix];
5417 if (kc == NULL)
5418 return (0);
5419
5420 IMPROVE("which other flags need tailroom?");
5421 if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
5422 return (32); /* Large enough to hold everything and pow2. */
5423
5424 return (0);
5425 }
5426 #endif
5427
5428 static void
5429 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
5430 {
5431 struct ieee80211_node *ni;
5432 struct ieee80211_frame *wh;
5433 struct ieee80211_key *k;
5434 struct sk_buff *skb;
5435 struct ieee80211com *ic;
5436 struct lkpi_hw *lhw;
5437 struct ieee80211_hw *hw;
5438 struct lkpi_vif *lvif;
5439 struct ieee80211_vif *vif;
5440 struct ieee80211_channel *c;
5441 struct ieee80211_tx_control control;
5442 struct ieee80211_tx_info *info;
5443 struct ieee80211_sta *sta;
5444 struct ieee80211_hdr *hdr;
5445 struct lkpi_txq *ltxq;
5446 void *buf;
5447 ieee80211_keyix keyix;
5448 uint8_t ac, tid, tailroom;
5449
5450 M_ASSERTPKTHDR(m);
5451 #ifdef LINUXKPI_DEBUG_80211
5452 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
5453 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
5454 #endif
5455
5456 ni = lsta->ni;
5457 k = NULL;
5458 keyix = IEEE80211_KEYIX_NONE;
5459 wh = mtod(m, struct ieee80211_frame *);
5460 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
5461
5462 #ifdef LKPI_80211_HW_CRYPTO
5463 if (lkpi_hwcrypto) {
5464 k = ieee80211_crypto_get_txkey(ni, m);
5465 if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
5466 keyix = k->wk_keyix;
5467 }
5468 #endif
5469
5470 /* Encrypt the frame if need be. */
5471 if (keyix == IEEE80211_KEYIX_NONE) {
5472 /* Retrieve key for TX && do software encryption. */
5473 k = ieee80211_crypto_encap(ni, m);
5474 if (k == NULL) {
5475 ieee80211_free_node(ni);
5476 m_freem(m);
5477 return;
5478 }
5479 }
5480 }
5481
5482 ic = ni->ni_ic;
5483 lhw = ic->ic_softc;
5484 hw = LHW_TO_HW(lhw);
5485 c = ni->ni_chan;
5486
5487 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
5488 struct lkpi_radiotap_tx_hdr *rtap;
5489
5490 rtap = &lhw->rtap_tx;
5491 rtap->wt_flags = 0;
5492 if (k != NULL)
5493 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
5494 if (m->m_flags & M_FRAG)
5495 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
5496 IMPROVE();
5497 rtap->wt_rate = 0;
5498 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
5499 rtap->wt_chan_freq = htole16(c->ic_freq);
5500 rtap->wt_chan_flags = htole16(c->ic_flags);
5501 }
5502
5503 ieee80211_radiotap_tx(ni->ni_vap, m);
5504 }
5505
5506 #ifdef LKPI_80211_HW_CRYPTO
5507 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
5508 tailroom = lkpi_hw_crypto_tailroom(lsta, k);
5509 else
5510 #endif
5511 tailroom = 0;
5512
5513 /*
5514 * net80211 should handle hw->extra_tx_headroom.
5515 * Though for as long as we are copying we don't mind.
5516 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
5517 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
5518 */
5519 skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
5520 if (skb == NULL) {
5521 static uint8_t skb_alloc_failures = 0;
5522
5523 if (skb_alloc_failures++ == 0) {
5524 int tid;
5525
5526 sta = LSTA_TO_STA(lsta);
5527 ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
5528 __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
5529 for (tid = 0; tid < nitems(sta->txq); tid++) {
5530 if (sta->txq[tid] == NULL)
5531 continue;
5532 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5533 ic_printf(ic, " tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
5534 tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(<xq->skbq));
5535 }
5536 }
5537 ieee80211_free_node(ni);
5538 m_freem(m);
5539 return;
5540 }
5541 skb_reserve(skb, hw->extra_tx_headroom);
5542
5543 /* XXX-BZ we need a SKB version understanding mbuf. */
5544 /* Save the mbuf for ieee80211_tx_complete(). */
5545 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
5546 skb->m = m;
5547 #if 0
5548 skb_put_data(skb, m->m_data, m->m_pkthdr.len);
5549 #else
5550 buf = skb_put(skb, m->m_pkthdr.len);
5551 m_copydata(m, 0, m->m_pkthdr.len, buf);
5552 #endif
5553 /* Save the ni. */
5554 m->m_pkthdr.PH_loc.ptr = ni;
5555
5556 lvif = VAP_TO_LVIF(ni->ni_vap);
5557 vif = LVIF_TO_VIF(lvif);
5558
5559 hdr = (void *)skb->data;
5560 tid = linuxkpi_ieee80211_get_tid(hdr, true);
5561 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
5562 if (!ieee80211_is_data(hdr->frame_control)) {
5563 /* MGMT and CTRL frames go on TID 7/VO. */
5564 skb->priority = 7;
5565 ac = IEEE80211_AC_VO;
5566 } else {
5567 /* Other non-QOS traffic goes to BE. */
5568 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */
5569 skb->priority = 0;
5570 ac = IEEE80211_AC_BE;
5571 }
5572 } else {
5573 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
5574 ac = ieee80211e_up_to_ac[tid & 7];
5575 }
5576 skb_set_queue_mapping(skb, ac);
5577
5578 info = IEEE80211_SKB_CB(skb);
5579 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5580 /* Slight delay; probably only happens on scanning so fine? */
5581 if (c == NULL || c == IEEE80211_CHAN_ANYC)
5582 c = ic->ic_curchan;
5583 info->band = lkpi_net80211_chan_to_nl80211_band(c);
5584 info->hw_queue = vif->hw_queue[ac];
5585 if (m->m_flags & M_EAPOL)
5586 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
5587 info->control.vif = vif;
5588 /* XXX-BZ info->control.rates */
5589 #ifdef __notyet__
5590 #ifdef LKPI_80211_HT
5591 info->control.rts_cts_rate_idx=
5592 info->control.use_rts= /* RTS */
5593 info->control.use_cts_prot= /* RTS/CTS*/
5594 #endif
5595 #endif
5596
5597 sta = LSTA_TO_STA(lsta);
5598 #ifdef LKPI_80211_HW_CRYPTO
5599 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
5600 int error;
5601
5602 error = lkpi_hw_crypto_prepare(lsta, k, skb);
5603 if (error != 0) {
5604 /*
5605 * We only have to free the skb which will free the
5606 * mbuf and release the reference on the ni.
5607 */
5608 dev_kfree_skb(skb);
5609 return;
5610 }
5611 }
5612 #endif
5613
5614 IMPROVE();
5615
5616 ltxq = NULL;
5617 if (!ieee80211_is_data_present(hdr->frame_control)) {
5618 if (vif->type == NL80211_IFTYPE_STATION &&
5619 lsta->added_to_drv &&
5620 sta->txq[IEEE80211_NUM_TIDS] != NULL)
5621 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
5622 } else if (lsta->added_to_drv &&
5623 sta->txq[skb->priority] != NULL) {
5624 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
5625 }
5626 if (ltxq == NULL)
5627 goto ops_tx;
5628
5629 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
5630 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
5631
5632 LKPI_80211_LTXQ_LOCK(ltxq);
5633 skb_queue_tail(<xq->skbq, skb);
5634 #ifdef LINUXKPI_DEBUG_80211
5635 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5636 printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
5637 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5638 "WAKE_TX_Q ac %d prio %u qmap %u\n",
5639 __func__, __LINE__,
5640 curthread->td_tid, jiffies,
5641 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5642 skb_queue_len(<xq->skbq), ltxq->txq.ac,
5643 ltxq->txq.tid, ac, skb->priority, skb->qmap);
5644 #endif
5645 LKPI_80211_LTXQ_UNLOCK(ltxq);
5646 wiphy_lock(hw->wiphy);
5647 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq);
5648 wiphy_unlock(hw->wiphy);
5649 return;
5650
5651 ops_tx:
5652 #ifdef LINUXKPI_DEBUG_80211
5653 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5654 printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5655 "TX ac %d prio %u qmap %u\n",
5656 __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
5657 skb, ac, skb->priority, skb->qmap);
5658 #endif
5659 memset(&control, 0, sizeof(control));
5660 control.sta = sta;
5661 wiphy_lock(hw->wiphy);
5662 lkpi_80211_mo_tx(hw, &control, skb);
5663 wiphy_unlock(hw->wiphy);
5664 }
5665
5666 static void
5667 lkpi_80211_txq_task(void *ctx, int pending)
5668 {
5669 struct lkpi_sta *lsta;
5670 struct mbufq mq;
5671 struct mbuf *m;
5672 bool shall_tx;
5673
5674 lsta = ctx;
5675
5676 #ifdef LINUXKPI_DEBUG_80211
5677 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5678 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
5679 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
5680 pending, mbufq_len(&lsta->txq));
5681 #endif
5682
5683 mbufq_init(&mq, IFQ_MAXLEN);
5684
5685 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5686 /*
5687 * Do not re-check lsta->txq_ready here; we may have a pending
5688 * disassoc/deauth frame still. On the contrary if txq_ready is
5689 * false we do not have a valid sta anymore in the firmware so no
5690 * point to try to TX.
5691 * We also use txq_ready as a semaphore and will drain the txq manually
5692 * if needed on our way towards SCAN/INIT in the state machine.
5693 */
5694 #if 0
5695 shall_tx = lsta->added_to_drv && lsta->txq_ready;
5696 #else
5697 /*
5698 * Backout this part of 886653492945f which breaks rtw88 or
5699 * in general drivers without (*sta_state)() but only the
5700 * legacy fallback to (*sta_add)().
5701 */
5702 shall_tx = lsta->txq_ready;
5703 #endif
5704 if (__predict_true(shall_tx))
5705 mbufq_concat(&mq, &lsta->txq);
5706 /*
5707 * else a state change will push the packets out manually or
5708 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
5709 */
5710 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5711
5712 m = mbufq_dequeue(&mq);
5713 while (m != NULL) {
5714 lkpi_80211_txq_tx_one(lsta, m);
5715 m = mbufq_dequeue(&mq);
5716 }
5717 }
5718
5719 static int
5720 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
5721 {
5722
5723 /* XXX TODO */
5724 IMPROVE();
5725
5726 /* Quick and dirty cheating hack. */
5727 struct ieee80211_node *ni;
5728
5729 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
5730 return (lkpi_xmit(ni, m, NULL, false));
5731 }
5732
5733 #ifdef LKPI_80211_HT
5734 static int
5735 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
5736 const uint8_t *frm, const uint8_t *efrm)
5737 {
5738 struct ieee80211com *ic;
5739 struct lkpi_hw *lhw;
5740
5741 ic = ni->ni_ic;
5742 lhw = ic->ic_softc;
5743
5744 IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
5745
5746 return (lhw->ic_recv_action(ni, wh, frm, efrm));
5747 }
5748
5749 static int
5750 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
5751 {
5752 struct ieee80211com *ic;
5753 struct lkpi_hw *lhw;
5754
5755 ic = ni->ni_ic;
5756 lhw = ic->ic_softc;
5757
5758 IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
5759
5760 return (lhw->ic_send_action(ni, category, action, sa));
5761 }
5762
5763
5764 static int
5765 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5766 {
5767 struct ieee80211com *ic;
5768 struct lkpi_hw *lhw;
5769
5770 ic = ni->ni_ic;
5771 lhw = ic->ic_softc;
5772
5773 IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
5774
5775 return (lhw->ic_ampdu_enable(ni, tap));
5776 }
5777
5778 /*
5779 * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5780 * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5781 *
5782 * NB: returns 0 on ERROR!
5783 */
5784 static int
5785 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5786 int dialogtoken, int baparamset, int batimeout)
5787 {
5788 struct ieee80211com *ic;
5789 struct lkpi_hw *lhw;
5790 struct ieee80211_hw *hw;
5791 struct ieee80211vap *vap;
5792 struct lkpi_vif *lvif;
5793 struct ieee80211_vif *vif;
5794 struct lkpi_sta *lsta;
5795 struct ieee80211_sta *sta;
5796 struct ieee80211_ampdu_params params = { };
5797 int error;
5798
5799 ic = ni->ni_ic;
5800 lhw = ic->ic_softc;
5801 hw = LHW_TO_HW(lhw);
5802 vap = ni->ni_vap;
5803 lvif = VAP_TO_LVIF(vap);
5804 vif = LVIF_TO_VIF(lvif);
5805 lsta = ni->ni_drv_data;
5806 sta = LSTA_TO_STA(lsta);
5807
5808 if (!lsta->added_to_drv) {
5809 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5810 __func__, lsta, ni, sta);
5811 return (0);
5812 }
5813
5814 params.sta = sta;
5815 params.action = IEEE80211_AMPDU_TX_START;
5816 /* Keep 0 here! */
5817 params.buf_size = 0;
5818 params.timeout = 0;
5819 params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5820 params.tid = tap->txa_tid;
5821 params.amsdu = false;
5822
5823 IEEE80211_UNLOCK(ic);
5824 wiphy_lock(hw->wiphy);
5825 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5826 wiphy_unlock(hw->wiphy);
5827 IEEE80211_LOCK(ic);
5828 if (error != 0) {
5829 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5830 __func__, error, ni, tap);
5831 return (0);
5832 }
5833
5834 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
5835 }
5836
5837 /*
5838 * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5839 * and calls the default ieee80211_addba_response() which always returns 1.
5840 *
5841 * NB: No error checking in net80211!
5842 * Staying with 0 is an error.
5843 */
5844 static int
5845 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5846 int status, int baparamset, int batimeout)
5847 {
5848 struct ieee80211com *ic;
5849 struct lkpi_hw *lhw;
5850 struct ieee80211_hw *hw;
5851 struct ieee80211vap *vap;
5852 struct lkpi_vif *lvif;
5853 struct ieee80211_vif *vif;
5854 struct lkpi_sta *lsta;
5855 struct ieee80211_sta *sta;
5856 struct ieee80211_ampdu_params params = { };
5857 int error;
5858
5859 ic = ni->ni_ic;
5860 lhw = ic->ic_softc;
5861 hw = LHW_TO_HW(lhw);
5862 vap = ni->ni_vap;
5863 lvif = VAP_TO_LVIF(vap);
5864 vif = LVIF_TO_VIF(lvif);
5865 lsta = ni->ni_drv_data;
5866 sta = LSTA_TO_STA(lsta);
5867
5868 if (!lsta->added_to_drv) {
5869 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5870 __func__, lsta, ni, sta);
5871 return (0);
5872 }
5873
5874 if (status == IEEE80211_STATUS_SUCCESS) {
5875 params.sta = sta;
5876 params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5877 params.buf_size = tap->txa_wnd;
5878 params.timeout = 0;
5879 params.ssn = 0;
5880 params.tid = tap->txa_tid;
5881 if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5882 params.amsdu = true;
5883 else
5884 params.amsdu = false;
5885 } else {
5886 /* We need to free the allocated resources. */
5887 params.sta = sta;
5888 switch (status) {
5889 /* params.action = FLUSH, FLUSH_CONT */
5890 default:
5891 params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5892 break;
5893 }
5894 params.buf_size = 0;
5895 params.timeout = 0;
5896 params.ssn = 0;
5897 params.tid = tap->txa_tid;
5898 params.amsdu = false;
5899 }
5900
5901 IEEE80211_UNLOCK(ic);
5902 wiphy_lock(hw->wiphy);
5903 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5904 wiphy_unlock(hw->wiphy);
5905 IEEE80211_LOCK(ic);
5906 if (error != 0) {
5907 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5908 __func__, error, ni, tap);
5909 return (0);
5910 }
5911
5912 IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
5913
5914 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
5915 }
5916
5917 /*
5918 * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5919 * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5920 */
5921 static void
5922 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5923 {
5924 struct ieee80211com *ic;
5925 struct lkpi_hw *lhw;
5926 struct ieee80211_hw *hw;
5927 struct ieee80211vap *vap;
5928 struct lkpi_vif *lvif;
5929 struct ieee80211_vif *vif;
5930 struct lkpi_sta *lsta;
5931 struct ieee80211_sta *sta;
5932 struct ieee80211_ampdu_params params = { };
5933 int error;
5934
5935 ic = ni->ni_ic;
5936 lhw = ic->ic_softc;
5937 hw = LHW_TO_HW(lhw);
5938 vap = ni->ni_vap;
5939 lvif = VAP_TO_LVIF(vap);
5940 vif = LVIF_TO_VIF(lvif);
5941 lsta = ni->ni_drv_data;
5942 sta = LSTA_TO_STA(lsta);
5943
5944 if (!lsta->added_to_drv) {
5945 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5946 __func__, lsta, ni, sta);
5947 goto n80211;
5948 }
5949
5950 /* We need to free the allocated resources. */
5951 params.sta = sta;
5952 IMPROVE("net80211 does not provide a reason to us");
5953 params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5954 params.buf_size = 0;
5955 params.timeout = 0;
5956 params.ssn = 0;
5957 params.tid = tap->txa_tid;
5958 params.amsdu = false;
5959
5960 IEEE80211_UNLOCK(ic);
5961 wiphy_lock(hw->wiphy);
5962 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5963 wiphy_unlock(hw->wiphy);
5964 IEEE80211_LOCK(ic);
5965 if (error != 0) {
5966 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5967 __func__, error, ni, tap);
5968 goto n80211;
5969 }
5970
5971 IMPROVE_HT("anyting else?");
5972
5973 n80211:
5974 lhw->ic_addba_stop(ni, tap);
5975 }
5976
5977 static void
5978 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5979 {
5980 struct ieee80211com *ic;
5981 struct lkpi_hw *lhw;
5982
5983 ic = ni->ni_ic;
5984 lhw = ic->ic_softc;
5985
5986 IMPROVE_HT();
5987
5988 lhw->ic_addba_response_timeout(ni, tap);
5989 }
5990
5991 static void
5992 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5993 int status)
5994 {
5995 struct ieee80211com *ic;
5996 struct lkpi_hw *lhw;
5997
5998 ic = ni->ni_ic;
5999 lhw = ic->ic_softc;
6000
6001 IMPROVE_HT();
6002
6003 lhw->ic_bar_response(ni, tap, status);
6004 }
6005
6006 static int
6007 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
6008 int baparamset, int batimeout, int baseqctl)
6009 {
6010 struct ieee80211com *ic;
6011 struct lkpi_hw *lhw;
6012 struct ieee80211_hw *hw;
6013 struct ieee80211vap *vap;
6014 struct lkpi_vif *lvif;
6015 struct ieee80211_vif *vif;
6016 struct lkpi_sta *lsta;
6017 struct ieee80211_sta *sta;
6018 struct ieee80211_ampdu_params params = { };
6019 int error;
6020
6021 ic = ni->ni_ic;
6022 lhw = ic->ic_softc;
6023 hw = LHW_TO_HW(lhw);
6024 vap = ni->ni_vap;
6025 lvif = VAP_TO_LVIF(vap);
6026 vif = LVIF_TO_VIF(lvif);
6027 lsta = ni->ni_drv_data;
6028 sta = LSTA_TO_STA(lsta);
6029
6030 IEEE80211_UNLOCK_ASSERT(ic);
6031
6032 if (!lsta->added_to_drv) {
6033 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6034 __func__, lsta, ni, vap, sta);
6035 return (-ENXIO);
6036 }
6037
6038 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6039 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6040 __func__, lsta, ni, vap, sta, lsta->state);
6041 return (-ENXIO);
6042 }
6043
6044 params.sta = sta;
6045 params.action = IEEE80211_AMPDU_RX_START;
6046 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
6047 if (params.buf_size == 0)
6048 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
6049 else
6050 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
6051 if (hw->max_rx_aggregation_subframes > 0 &&
6052 params.buf_size > hw->max_rx_aggregation_subframes)
6053 params.buf_size = hw->max_rx_aggregation_subframes;
6054 params.timeout = le16toh(batimeout);
6055 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
6056 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
6057
6058 /* Based on net80211::ampdu_rx_start(). */
6059 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
6060 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
6061 params.amsdu = true;
6062 else
6063 params.amsdu = false;
6064
6065 wiphy_lock(hw->wiphy);
6066 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6067 wiphy_unlock(hw->wiphy);
6068 if (error != 0) {
6069 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6070 __func__, error, ni, rap);
6071 return (error);
6072 }
6073
6074 if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
6075 IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
6076 }
6077
6078 IMPROVE_HT("net80211 is missing the error check on return and assumes success");
6079
6080 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
6081 return (error);
6082 }
6083
6084 static void
6085 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
6086 {
6087 struct ieee80211com *ic;
6088 struct lkpi_hw *lhw;
6089 struct ieee80211_hw *hw;
6090 struct ieee80211vap *vap;
6091 struct lkpi_vif *lvif;
6092 struct ieee80211_vif *vif;
6093 struct lkpi_sta *lsta;
6094 struct ieee80211_sta *sta;
6095 struct ieee80211_ampdu_params params = { };
6096 int error;
6097 uint8_t tid;
6098 bool ic_locked;
6099
6100 ic = ni->ni_ic;
6101 lhw = ic->ic_softc;
6102
6103 /*
6104 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
6105 * we did not START. Some drivers pass it down to firmware which will
6106 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
6107 * ieee80211_ht_node_init() amongst others which will iterate over all
6108 * tid and call ic_ampdu_rx_stop() unconditionally.
6109 * XXX net80211 should probably be more "gentle" in these cases and
6110 * track some state itself.
6111 */
6112 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
6113 goto net80211_only;
6114
6115 hw = LHW_TO_HW(lhw);
6116 vap = ni->ni_vap;
6117 lvif = VAP_TO_LVIF(vap);
6118 vif = LVIF_TO_VIF(lvif);
6119 lsta = ni->ni_drv_data;
6120 if (lsta == NULL) {
6121 ic_printf(ic, "%s: lsta %p ni %p vap %p, lsta is NULL\n",
6122 __func__, lsta, ni, vap);
6123 goto net80211_only;
6124 }
6125 sta = LSTA_TO_STA(lsta);
6126
6127 if (!lsta->added_to_drv) {
6128 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6129 __func__, lsta, ni, vap, sta);
6130 goto net80211_only;
6131 }
6132
6133 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6134 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6135 __func__, lsta, ni, vap, sta, lsta->state);
6136 goto net80211_only;
6137 }
6138
6139 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
6140 for (tid = 0; tid < WME_NUM_TID; tid++) {
6141 if (&ni->ni_rx_ampdu[tid] == rap)
6142 break;
6143 }
6144 if (tid == WME_NUM_TID) {
6145 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p TID not found\n",
6146 __func__, lsta, ni, vap, sta);
6147 goto net80211_only;
6148 }
6149
6150 params.sta = sta;
6151 params.action = IEEE80211_AMPDU_RX_STOP;
6152 params.buf_size = 0;
6153 params.timeout = 0;
6154 params.ssn = 0;
6155 params.tid = tid;
6156 params.amsdu = false;
6157
6158 ic_locked = IEEE80211_IS_LOCKED(ic);
6159 if (ic_locked)
6160 IEEE80211_UNLOCK(ic);
6161 wiphy_lock(hw->wiphy);
6162 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6163 wiphy_unlock(hw->wiphy);
6164 if (ic_locked)
6165 IEEE80211_LOCK(ic);
6166 if (error != 0)
6167 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6168 __func__, error, ni, rap);
6169
6170 net80211_only:
6171 lhw->ic_ampdu_rx_stop(ni, rap);
6172 }
6173 #endif
6174
6175 static void
6176 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
6177 uint8_t *bands, int *chan_flags, enum nl80211_band band)
6178 {
6179 #ifdef LKPI_80211_HT
6180 struct ieee80211_sta_ht_cap *ht_cap;
6181
6182 ht_cap = &hw->wiphy->bands[band]->ht_cap;
6183 if (!ht_cap->ht_supported)
6184 return;
6185
6186 switch (band) {
6187 case NL80211_BAND_2GHZ:
6188 setbit(bands, IEEE80211_MODE_11NG);
6189 break;
6190 case NL80211_BAND_5GHZ:
6191 setbit(bands, IEEE80211_MODE_11NA);
6192 break;
6193 default:
6194 IMPROVE("Unsupported band %d", band);
6195 return;
6196 }
6197
6198 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */
6199
6200 /*
6201 * Rather than manually checking each flag and
6202 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
6203 * simply copy the 16bits.
6204 */
6205 ic->ic_htcaps |= ht_cap->cap;
6206
6207 /* Then deal with the other flags. */
6208 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
6209 ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
6210 #ifdef __notyet__
6211 if (ieee80211_hw_check(hw, TX_AMSDU))
6212 ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
6213 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
6214 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
6215 IEEE80211_HTC_TX_AMSDU_AMPDU);
6216 #endif
6217
6218 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
6219 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
6220
6221 /* Only add HT40 channels if supported. */
6222 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
6223 chan_flags != NULL)
6224 *chan_flags |= NET80211_CBW_FLAG_HT40;
6225 #endif
6226 }
6227
6228 static void
6229 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
6230 int *n, struct ieee80211_channel *c)
6231 {
6232 struct lkpi_hw *lhw;
6233 struct ieee80211_hw *hw;
6234 struct linuxkpi_ieee80211_channel *channels;
6235 uint8_t bands[IEEE80211_MODE_BYTES];
6236 int chan_flags, error, i, nchans;
6237
6238 /* Channels */
6239 lhw = ic->ic_softc;
6240 hw = LHW_TO_HW(lhw);
6241
6242 /* NL80211_BAND_2GHZ */
6243 nchans = 0;
6244 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
6245 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
6246 if (nchans > 0) {
6247 memset(bands, 0, sizeof(bands));
6248 chan_flags = 0;
6249 setbit(bands, IEEE80211_MODE_11B);
6250 /* XXX-BZ unclear how to check for 11g. */
6251
6252 IMPROVE("the bitrates may have flags?");
6253 setbit(bands, IEEE80211_MODE_11G);
6254
6255 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
6256 NL80211_BAND_2GHZ);
6257
6258 channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
6259 for (i = 0; i < nchans && *n < maxchan; i++) {
6260 uint32_t nflags = 0;
6261 int cflags = chan_flags;
6262
6263 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
6264 ic_printf(ic, "%s: Skipping disabled chan "
6265 "[%u/%u/%#x]\n", __func__,
6266 channels[i].hw_value,
6267 channels[i].center_freq, channels[i].flags);
6268 continue;
6269 }
6270 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
6271 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
6272 if (channels[i].flags & IEEE80211_CHAN_RADAR)
6273 nflags |= IEEE80211_CHAN_DFS;
6274 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
6275 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
6276 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
6277 cflags &= ~NET80211_CBW_FLAG_VHT80;
6278 /* XXX how to map the remaining enum ieee80211_channel_flags? */
6279 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
6280 cflags &= ~NET80211_CBW_FLAG_HT40;
6281
6282 error = ieee80211_add_channel_cbw(c, maxchan, n,
6283 ieee80211_mhz2ieee(channels[i].center_freq,
6284 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
6285 channels[i].center_freq, channels[i].max_power,
6286 nflags, bands, cflags);
6287 /* net80211::ENOBUFS: *n >= maxchans */
6288 if (error != 0 && error != ENOBUFS)
6289 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
6290 "returned error %d\n",
6291 __func__, channels[i].hw_value,
6292 channels[i].center_freq, channels[i].flags,
6293 nflags, chan_flags, cflags, error);
6294 if (error != 0)
6295 break;
6296 }
6297 }
6298
6299 /* NL80211_BAND_5GHZ */
6300 nchans = 0;
6301 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
6302 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
6303 if (nchans > 0) {
6304 memset(bands, 0, sizeof(bands));
6305 chan_flags = 0;
6306 setbit(bands, IEEE80211_MODE_11A);
6307
6308 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
6309 NL80211_BAND_5GHZ);
6310
6311 #ifdef LKPI_80211_VHT
6312 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
6313
6314 ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
6315 ic->ic_vht_cap.vht_cap_info =
6316 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
6317 ic->ic_vht_cap.supp_mcs =
6318 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
6319
6320 setbit(bands, IEEE80211_MODE_VHT_5GHZ);
6321 chan_flags |= NET80211_CBW_FLAG_VHT80;
6322 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
6323 ic->ic_vht_cap.vht_cap_info))
6324 chan_flags |= NET80211_CBW_FLAG_VHT160;
6325 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
6326 ic->ic_vht_cap.vht_cap_info))
6327 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
6328 }
6329 #endif
6330
6331 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
6332 for (i = 0; i < nchans && *n < maxchan; i++) {
6333 uint32_t nflags = 0;
6334 int cflags = chan_flags;
6335
6336 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
6337 ic_printf(ic, "%s: Skipping disabled chan "
6338 "[%u/%u/%#x]\n", __func__,
6339 channels[i].hw_value,
6340 channels[i].center_freq, channels[i].flags);
6341 continue;
6342 }
6343 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
6344 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
6345 if (channels[i].flags & IEEE80211_CHAN_RADAR)
6346 nflags |= IEEE80211_CHAN_DFS;
6347 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
6348 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
6349 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
6350 cflags &= ~NET80211_CBW_FLAG_VHT80;
6351 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
6352 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
6353 cflags &= ~NET80211_CBW_FLAG_HT40;
6354
6355 error = ieee80211_add_channel_cbw(c, maxchan, n,
6356 ieee80211_mhz2ieee(channels[i].center_freq,
6357 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
6358 channels[i].center_freq, channels[i].max_power,
6359 nflags, bands, cflags);
6360 /* net80211::ENOBUFS: *n >= maxchans */
6361 if (error != 0 && error != ENOBUFS)
6362 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
6363 "returned error %d\n",
6364 __func__, channels[i].hw_value,
6365 channels[i].center_freq, channels[i].flags,
6366 nflags, chan_flags, cflags, error);
6367 if (error != 0)
6368 break;
6369 }
6370 }
6371 }
6372
6373 static void *
6374 lkpi_ieee80211_ifalloc(void)
6375 {
6376 struct ieee80211com *ic;
6377
6378 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
6379
6380 /* Setting these happens later when we have device information. */
6381 ic->ic_softc = NULL;
6382 ic->ic_name = "linuxkpi";
6383
6384 return (ic);
6385 }
6386
6387 struct ieee80211_hw *
6388 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
6389 {
6390 struct ieee80211_hw *hw;
6391 struct lkpi_hw *lhw;
6392 struct wiphy *wiphy;
6393 int ac;
6394
6395 /* Get us and the driver data also allocated. */
6396 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
6397 if (wiphy == NULL)
6398 return (NULL);
6399
6400 lhw = wiphy_priv(wiphy);
6401 lhw->ops = ops;
6402
6403 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
6404 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
6405 spin_lock_init(&lhw->txq_lock);
6406 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
6407 LKPI_80211_LHW_MC_LOCK_INIT(lhw);
6408 TAILQ_INIT(&lhw->lvif_head);
6409 __hw_addr_init(&lhw->mc_list);
6410 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
6411 lhw->txq_generation[ac] = 1;
6412 TAILQ_INIT(&lhw->scheduled_txqs[ac]);
6413 }
6414
6415 /* Chanctx_conf */
6416 INIT_LIST_HEAD(&lhw->lchanctx_list);
6417
6418 /* Deferred RX path. */
6419 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
6420 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
6421 mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
6422 lhw->rxq_stopped = false;
6423
6424 /*
6425 * XXX-BZ TODO make sure there is a "_null" function to all ops
6426 * not initialized.
6427 */
6428 hw = LHW_TO_HW(lhw);
6429 hw->wiphy = wiphy;
6430 hw->conf.flags |= IEEE80211_CONF_IDLE;
6431 hw->priv = (void *)(lhw + 1);
6432
6433 /* BSD Specific. */
6434 lhw->ic = lkpi_ieee80211_ifalloc();
6435
6436 IMPROVE();
6437
6438 return (hw);
6439 }
6440
6441 void
6442 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
6443 {
6444 struct lkpi_hw *lhw;
6445 struct mbuf *m;
6446
6447 lhw = HW_TO_LHW(hw);
6448 free(lhw->ic, M_LKPI80211);
6449 lhw->ic = NULL;
6450
6451 /*
6452 * Drain the deferred RX path.
6453 */
6454 LKPI_80211_LHW_RXQ_LOCK(lhw);
6455 lhw->rxq_stopped = true;
6456 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6457
6458 /* Drain taskq, won't be restarted due to rxq_stopped being set. */
6459 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
6460 taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
6461
6462 /* Flush mbufq (make sure to release ni refs!). */
6463 m = mbufq_dequeue(&lhw->rxq);
6464 while (m != NULL) {
6465 #ifdef LKPI_80211_USE_MTAG
6466 struct m_tag *mtag;
6467
6468 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6469 if (mtag != NULL) {
6470 struct lkpi_80211_tag_rxni *rxni;
6471
6472 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6473 ieee80211_free_node(rxni->ni);
6474 }
6475 #else
6476 if (m->m_pkthdr.PH_loc.ptr != NULL) {
6477 struct ieee80211_node *ni;
6478
6479 ni = m->m_pkthdr.PH_loc.ptr;
6480 ieee80211_free_node(ni);
6481 }
6482 #endif
6483 m_freem(m);
6484 m = mbufq_dequeue(&lhw->rxq);
6485 }
6486 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
6487 __func__, lhw, mbufq_len(&lhw->rxq)));
6488 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
6489
6490 /* Chanctx_conf. */
6491 if (!list_empty_careful(&lhw->lchanctx_list)) {
6492 struct lkpi_chanctx *lchanctx, *next;
6493 struct ieee80211_chanctx_conf *chanctx_conf;
6494
6495 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
6496 if (lchanctx->added_to_drv) {
6497 /* In reality we should panic? */
6498 chanctx_conf = &lchanctx->chanctx_conf;
6499 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
6500 }
6501 list_del(&lchanctx->entry);
6502 free(lchanctx, M_LKPI80211);
6503 }
6504 }
6505
6506 LKPI_80211_LHW_MC_LOCK(lhw);
6507 lkpi_cleanup_mcast_list_locked(lhw);
6508 LKPI_80211_LHW_MC_UNLOCK(lhw);
6509
6510 /* Cleanup more of lhw here or in wiphy_free()? */
6511 spin_lock_destroy(&lhw->txq_lock);
6512 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
6513 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
6514 sx_destroy(&lhw->lvif_sx);
6515 LKPI_80211_LHW_MC_LOCK_DESTROY(lhw)
6516 IMPROVE();
6517 }
6518
6519 void
6520 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw)
6521 {
6522 struct lkpi_hw *lhw;
6523 struct ieee80211com *ic;
6524 struct device *dev;
6525
6526 lhw = HW_TO_LHW(hw);
6527 ic = lhw->ic;
6528
6529 /* Save the backpointer from net80211 to LinuxKPI. */
6530 ic->ic_softc = lhw;
6531
6532 /*
6533 * Set a proper name before ieee80211_ifattach() if dev is set.
6534 * ath1xk also unset the dev so we need to check.
6535 */
6536 dev = wiphy_dev(hw->wiphy);
6537 if (dev != NULL) {
6538 ic->ic_name = dev_name(dev);
6539 } else {
6540 TODO("adjust arguments to still have the old dev or go through "
6541 "the hoops of getting the bsddev from hw and detach; "
6542 "or do in XXX; check ath1kx drivers");
6543 }
6544
6545 /* XXX-BZ do we also need to set wiphy name? */
6546 }
6547
6548 struct ieee80211_hw *
6549 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
6550 {
6551 struct lkpi_hw *lhw;
6552
6553 lhw = wiphy_priv(wiphy);
6554 return (LHW_TO_HW(lhw));
6555 }
6556
6557 static void
6558 lkpi_radiotap_attach(struct lkpi_hw *lhw)
6559 {
6560 struct ieee80211com *ic;
6561
6562 ic = lhw->ic;
6563 ieee80211_radiotap_attach(ic,
6564 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
6565 LKPI_RTAP_TX_FLAGS_PRESENT,
6566 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
6567 LKPI_RTAP_RX_FLAGS_PRESENT);
6568 }
6569
6570 int
6571 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
6572 {
6573 struct ieee80211com *ic;
6574 struct lkpi_hw *lhw;
6575 int band, i;
6576
6577 lhw = HW_TO_LHW(hw);
6578 ic = lhw->ic;
6579
6580 /* We do it this late as wiphy->dev should be set for the name. */
6581 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
6582 if (lhw->workq == NULL)
6583 return (-EAGAIN);
6584
6585 /* XXX-BZ figure this out how they count his... */
6586 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
6587 IEEE80211_ADDR_COPY(ic->ic_macaddr,
6588 hw->wiphy->perm_addr);
6589 } else if (hw->wiphy->n_addresses > 0) {
6590 /* We take the first one. */
6591 IEEE80211_ADDR_COPY(ic->ic_macaddr,
6592 hw->wiphy->addresses[0].addr);
6593 } else {
6594 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
6595 }
6596
6597 #ifdef __not_yet__
6598 /* See comment in lkpi_80211_txq_tx_one(). */
6599 ic->ic_headroom = hw->extra_tx_headroom;
6600 #endif
6601
6602 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
6603 ic->ic_opmode = IEEE80211_M_STA;
6604
6605 /* Set device capabilities. */
6606 /* XXX-BZ we need to get these from linux80211/drivers and convert. */
6607 ic->ic_caps =
6608 IEEE80211_C_STA |
6609 IEEE80211_C_MONITOR |
6610 IEEE80211_C_WPA | /* WPA/RSN */
6611 #ifdef LKPI_80211_WME
6612 IEEE80211_C_WME |
6613 #endif
6614 #if 0
6615 IEEE80211_C_PMGT |
6616 #endif
6617 IEEE80211_C_SHSLOT | /* short slot time supported */
6618 IEEE80211_C_SHPREAMBLE /* short preamble supported */
6619 ;
6620
6621 #ifdef LKPI_80211_BGSCAN
6622 if (lhw->ops->hw_scan)
6623 ic->ic_caps |= IEEE80211_C_BGSCAN;
6624 #endif
6625
6626 lkpi_enable_hw_scan(lhw);
6627
6628 /* Does HW support Fragmentation offload? */
6629 if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
6630 ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
6631
6632 /* Does HW support full AMPDU[-TX] offload? */
6633 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
6634 ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_OFFLOAD;
6635 #ifdef __notyet__
6636 if (ieee80211_hw_check(hw, TX_AMSDU))
6637 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
6638 #endif
6639
6640 /*
6641 * The wiphy variables report bitmasks of avail antennas.
6642 * (*get_antenna) get the current bitmask sets which can be
6643 * altered by (*set_antenna) for some drivers.
6644 * XXX-BZ will the count alone do us much good long-term in net80211?
6645 */
6646 if (hw->wiphy->available_antennas_rx ||
6647 hw->wiphy->available_antennas_tx) {
6648 uint32_t rxs, txs;
6649
6650 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
6651 ic->ic_rxstream = bitcount32(rxs);
6652 ic->ic_txstream = bitcount32(txs);
6653 }
6654 }
6655
6656 ic->ic_cryptocaps = 0;
6657 #ifdef LKPI_80211_HW_CRYPTO
6658 if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
6659 uint32_t hwciphers;
6660
6661 hwciphers = 0;
6662 for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
6663 uint32_t cs;
6664
6665 cs = lkpi_l80211_to_net80211_cyphers(
6666 ic, hw->wiphy->cipher_suites[i]);
6667 if (cs == IEEE80211_CRYPTO_TKIP) {
6668 /*
6669 * We do set this here. We will only find out
6670 * when doing a SET_KEY operation depending on
6671 * what the driver returns.
6672 * net80211::ieee80211_crypto_newkey()
6673 * checks this so we will have to do flags
6674 * surgery later.
6675 */
6676 cs |= IEEE80211_CRYPTO_TKIPMIC;
6677 }
6678 hwciphers |= cs;
6679 }
6680 /*
6681 * (20250415) nothing anywhere in the path checks we actually
6682 * support all these in net80211.
6683 * net80211 supports _256 variants but the ioctl does not.
6684 */
6685 IMPROVE("as net80211 grows more support, enable them");
6686 hwciphers &= (IEEE80211_CRYPTO_WEP |
6687 IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
6688 IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
6689 /*
6690 * We only support CCMP here, so further filter.
6691 * Also permit TKIP if turned on.
6692 */
6693 hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
6694 IEEE80211_CRYPTO_AES_GCM_128 |
6695 (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
6696 IEEE80211_CRYPTO_TKIPMIC) : 0));
6697 ieee80211_set_hardware_ciphers(ic, hwciphers);
6698 }
6699 #endif
6700
6701 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
6702 ic->ic_channels);
6703
6704 ieee80211_ifattach(ic);
6705
6706 ic->ic_update_mcast = lkpi_ic_update_mcast;
6707 ic->ic_update_promisc = lkpi_ic_update_promisc;
6708 ic->ic_update_chw = lkpi_ic_update_chw;
6709 ic->ic_parent = lkpi_ic_parent;
6710 ic->ic_scan_start = lkpi_ic_scan_start;
6711 ic->ic_scan_end = lkpi_ic_scan_end;
6712 ic->ic_set_channel = lkpi_ic_set_channel;
6713 ic->ic_transmit = lkpi_ic_transmit;
6714 ic->ic_raw_xmit = lkpi_ic_raw_xmit;
6715 ic->ic_vap_create = lkpi_ic_vap_create;
6716 ic->ic_vap_delete = lkpi_ic_vap_delete;
6717 ic->ic_getradiocaps = lkpi_ic_getradiocaps;
6718 ic->ic_wme.wme_update = lkpi_ic_wme_update;
6719
6720 lhw->ic_scan_curchan = ic->ic_scan_curchan;
6721 ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6722 lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6723 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6724
6725 lhw->ic_node_alloc = ic->ic_node_alloc;
6726 ic->ic_node_alloc = lkpi_ic_node_alloc;
6727 lhw->ic_node_init = ic->ic_node_init;
6728 ic->ic_node_init = lkpi_ic_node_init;
6729 lhw->ic_node_cleanup = ic->ic_node_cleanup;
6730 ic->ic_node_cleanup = lkpi_ic_node_cleanup;
6731 lhw->ic_node_free = ic->ic_node_free;
6732 ic->ic_node_free = lkpi_ic_node_free;
6733
6734 #ifdef LKPI_80211_HT
6735 /*
6736 * Only attach if the driver/firmware supports (*ampdu_action)().
6737 * Otherwise it is in the hands of net80211.
6738 */
6739 if (lhw->ops->ampdu_action != NULL) {
6740 lhw->ic_recv_action = ic->ic_recv_action;
6741 ic->ic_recv_action = lkpi_ic_recv_action;
6742 lhw->ic_send_action = ic->ic_send_action;
6743 ic->ic_send_action = lkpi_ic_send_action;
6744
6745 lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
6746 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
6747
6748 lhw->ic_addba_request = ic->ic_addba_request;
6749 ic->ic_addba_request = lkpi_ic_addba_request;
6750 lhw->ic_addba_response = ic->ic_addba_response;
6751 ic->ic_addba_response = lkpi_ic_addba_response;
6752 lhw->ic_addba_stop = ic->ic_addba_stop;
6753 ic->ic_addba_stop = lkpi_ic_addba_stop;
6754 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
6755 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
6756
6757 lhw->ic_bar_response = ic->ic_bar_response;
6758 ic->ic_bar_response = lkpi_ic_bar_response;
6759
6760 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
6761 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
6762 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
6763 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
6764 }
6765 #endif
6766
6767 lkpi_radiotap_attach(lhw);
6768
6769 /*
6770 * Assign the first possible channel for now; seems Realtek drivers
6771 * expect one.
6772 * Also remember the amount of bands we support and the most rates
6773 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6774 */
6775 lhw->supbands = lhw->max_rates = 0;
6776 for (band = 0; band < NUM_NL80211_BANDS; band++) {
6777 struct ieee80211_supported_band *supband;
6778 struct linuxkpi_ieee80211_channel *channels;
6779
6780 supband = hw->wiphy->bands[band];
6781 if (supband == NULL || supband->n_channels == 0)
6782 continue;
6783
6784 lhw->supbands++;
6785 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6786
6787 /* If we have a channel, we need to keep counting supbands. */
6788 if (hw->conf.chandef.chan != NULL)
6789 continue;
6790
6791 channels = supband->channels;
6792 for (i = 0; i < supband->n_channels; i++) {
6793
6794 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6795 continue;
6796
6797 cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
6798 #ifdef LKPI_80211_HT
6799 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
6800 #endif
6801 NL80211_CHAN_NO_HT);
6802 break;
6803 }
6804 }
6805
6806 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6807
6808 /* Make sure we do not support more than net80211 is willing to take. */
6809 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6810 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6811 lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6812 lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6813 }
6814
6815 /*
6816 * The maximum supported bitrates on any band + size for
6817 * DSSS Parameter Set give our per-band IE size.
6818 * SSID is the responsibility of the driver and goes on the side.
6819 * The user specified bits coming from the vap go into the
6820 * "common ies" fields.
6821 */
6822 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6823 if (lhw->max_rates > IEEE80211_RATE_SIZE)
6824 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
6825
6826 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6827 /*
6828 * net80211 does not seem to support the DSSS Parameter Set but
6829 * some of the drivers insert it so calculate the extra fixed
6830 * space in.
6831 */
6832 lhw->scan_ie_len += 2 + 1;
6833 }
6834
6835 #if defined(LKPI_80211_HT)
6836 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
6837 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
6838 #endif
6839 #if defined(LKPI_80211_VHT)
6840 if (IEEE80211_CONF_VHT(ic))
6841 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
6842 #endif
6843
6844 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */
6845 if (hw->wiphy->max_scan_ie_len > 0) {
6846 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
6847 goto err;
6848 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
6849 }
6850
6851 if (bootverbose) {
6852 ic_printf(ic, "netdev_features %b\n", hw->netdev_features, NETIF_F_BITS);
6853 ieee80211_announce(ic);
6854 }
6855
6856 return (0);
6857 err:
6858 IMPROVE("TODO FIXME CLEANUP");
6859 return (-EAGAIN);
6860 }
6861
6862 void
6863 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
6864 {
6865 struct lkpi_hw *lhw;
6866 struct ieee80211com *ic;
6867
6868 lhw = HW_TO_LHW(hw);
6869 ic = lhw->ic;
6870 ieee80211_ifdetach(ic);
6871 }
6872
6873 void
6874 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
6875 enum ieee80211_iface_iter flags,
6876 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
6877 void *arg)
6878 {
6879 struct lkpi_hw *lhw;
6880 struct lkpi_vif *lvif;
6881 struct ieee80211_vif *vif;
6882 bool active, atomic, nin_drv;
6883
6884 lhw = HW_TO_LHW(hw);
6885
6886 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
6887 IEEE80211_IFACE_ITER_RESUME_ALL|
6888 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6889 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
6890 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
6891 __func__, flags);
6892 }
6893
6894 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
6895 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
6896 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
6897
6898 if (atomic)
6899 LKPI_80211_LHW_LVIF_LOCK(lhw);
6900 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6901 struct ieee80211vap *vap;
6902
6903 vif = LVIF_TO_VIF(lvif);
6904
6905 /*
6906 * If we want "active" interfaces, we need to distinguish on
6907 * whether the driver knows about them or not to be able to
6908 * handle the "resume" case correctly. Skip the ones the
6909 * driver does not know about.
6910 */
6911 if (active && !lvif->added_to_drv &&
6912 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
6913 continue;
6914
6915 /*
6916 * If we shall skip interfaces not added to the driver do so
6917 * if we haven't yet.
6918 */
6919 if (nin_drv && !lvif->added_to_drv)
6920 continue;
6921
6922 /*
6923 * Run the iterator function if we are either not asking
6924 * asking for active only or if the VAP is "running".
6925 */
6926 /* XXX-BZ probably should have state in the lvif as well. */
6927 vap = LVIF_TO_VAP(lvif);
6928 if (!active || (vap->iv_state != IEEE80211_S_INIT))
6929 iterfunc(arg, vif->addr, vif);
6930 }
6931 if (atomic)
6932 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6933 }
6934
6935 static void
6936 lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6937 ieee80211_keyix keyix, struct lkpi_sta *lsta,
6938 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6939 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6940 void *arg)
6941 {
6942 if (!lsta->added_to_drv)
6943 return;
6944
6945 if (lsta->kc[keyix] == NULL)
6946 return;
6947
6948 iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
6949 }
6950
6951 void
6952 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
6953 struct ieee80211_vif *vif,
6954 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6955 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6956 void *arg, bool rcu)
6957 {
6958 struct lkpi_sta *lsta;
6959 struct lkpi_vif *lvif;
6960
6961 lvif = VIF_TO_LVIF(vif);
6962
6963 if (rcu) {
6964 rcu_read_lock_held(); /* XXX-BZ is this correct? */
6965
6966 if (vif == NULL) {
6967 TODO();
6968 } else {
6969 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
6970 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6971 keyix++)
6972 lkpi_ieee80211_iterate_keys(hw, vif,
6973 keyix, lsta, iterfunc, arg);
6974 }
6975 }
6976 } else {
6977 TODO("Used by suspend/resume; order of keys as installed to "
6978 "firmware is important; we'll need to rewrite some code for that");
6979 lockdep_assert_wiphy(hw->wiphy);
6980
6981 if (vif == NULL) {
6982 TODO();
6983 } else {
6984 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
6985 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6986 keyix++)
6987 lkpi_ieee80211_iterate_keys(hw, vif,
6988 keyix, lsta, iterfunc, arg);
6989 }
6990 }
6991 }
6992 }
6993
6994 void
6995 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
6996 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
6997 void *),
6998 void *arg)
6999 {
7000 struct lkpi_hw *lhw;
7001 struct lkpi_chanctx *lchanctx;
7002
7003 KASSERT(hw != NULL && iterfunc != NULL,
7004 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7005
7006 lhw = HW_TO_LHW(hw);
7007
7008 rcu_read_lock();
7009 list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
7010 if (!lchanctx->added_to_drv)
7011 continue;
7012 iterfunc(hw, &lchanctx->chanctx_conf, arg);
7013 }
7014 rcu_read_unlock();
7015 }
7016
7017 void
7018 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
7019 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
7020 {
7021 struct lkpi_hw *lhw;
7022 struct lkpi_vif *lvif;
7023 struct lkpi_sta *lsta;
7024 struct ieee80211_sta *sta;
7025
7026 KASSERT(hw != NULL && iterfunc != NULL,
7027 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7028
7029 lhw = HW_TO_LHW(hw);
7030
7031 LKPI_80211_LHW_LVIF_LOCK(lhw);
7032 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7033
7034 rcu_read_lock();
7035 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7036 if (!lsta->added_to_drv)
7037 continue;
7038 sta = LSTA_TO_STA(lsta);
7039 iterfunc(arg, sta);
7040 }
7041 rcu_read_unlock();
7042 }
7043 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7044 }
7045
7046 struct linuxkpi_ieee80211_regdomain *
7047 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
7048 {
7049 struct linuxkpi_ieee80211_regdomain *regd;
7050
7051 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
7052 GFP_KERNEL);
7053 return (regd);
7054 }
7055
7056 int
7057 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
7058 struct linuxkpi_ieee80211_regdomain *regd)
7059 {
7060 struct lkpi_hw *lhw;
7061 struct ieee80211com *ic;
7062 struct ieee80211_regdomain *rd;
7063
7064 lhw = wiphy_priv(wiphy);
7065 ic = lhw->ic;
7066
7067 rd = &ic->ic_regdomain;
7068 if (rd->isocc[0] == '\0') {
7069 rd->isocc[0] = regd->alpha2[0];
7070 rd->isocc[1] = regd->alpha2[1];
7071 }
7072
7073 TODO();
7074 /* XXX-BZ finish the rest. */
7075
7076 return (0);
7077 }
7078
7079 void
7080 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
7081 struct cfg80211_scan_info *info)
7082 {
7083 struct lkpi_hw *lhw;
7084 struct ieee80211com *ic;
7085 struct ieee80211_scan_state *ss;
7086
7087 lhw = wiphy_priv(hw->wiphy);
7088 ic = lhw->ic;
7089 ss = ic->ic_scan;
7090
7091 TRACE_SCAN(ic, "scan_flags %b info { %ju, %6D, aborted %d }",
7092 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7093 (uintmax_t)info->scan_start_tsf, info->tsf_bssid, ":",
7094 info->aborted);
7095
7096 ieee80211_scan_done(ss->ss_vap);
7097
7098 LKPI_80211_LHW_SCAN_LOCK(lhw);
7099 free(lhw->hw_req, M_LKPI80211);
7100 lhw->hw_req = NULL;
7101 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
7102 /* The wakeup(lhw) will be called from lkpi_ic_scan_end(). */
7103 /* wakeup(lhw); */
7104 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
7105
7106 return;
7107 }
7108
7109 static void
7110 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
7111 {
7112 struct ieee80211_node *ni;
7113 #ifdef LKPI_80211_USE_MTAG
7114 struct m_tag *mtag;
7115 #endif
7116 int ok;
7117
7118 ni = NULL;
7119 #ifdef LKPI_80211_USE_MTAG
7120 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7121 if (mtag != NULL) {
7122 struct lkpi_80211_tag_rxni *rxni;
7123
7124 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7125 ni = rxni->ni;
7126 }
7127 #else
7128 if (m->m_pkthdr.PH_loc.ptr != NULL) {
7129 ni = m->m_pkthdr.PH_loc.ptr;
7130 m->m_pkthdr.PH_loc.ptr = NULL;
7131 }
7132 #endif
7133
7134 if (ni != NULL) {
7135 ok = ieee80211_input_mimo(ni, m);
7136 ieee80211_free_node(ni); /* Release the reference. */
7137 if (ok < 0)
7138 m_freem(m);
7139 } else {
7140 ok = ieee80211_input_mimo_all(lhw->ic, m);
7141 /* mbuf got consumed. */
7142 }
7143
7144 #ifdef LINUXKPI_DEBUG_80211
7145 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7146 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
7147 #endif
7148 }
7149
7150 static void
7151 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
7152 {
7153 struct lkpi_hw *lhw;
7154 struct mbufq mq;
7155 struct mbuf *m;
7156
7157 lhw = ctx;
7158
7159 #ifdef LINUXKPI_DEBUG_80211
7160 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7161 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
7162 __func__, lhw, pending, mbufq_len(&lhw->rxq));
7163 #endif
7164
7165 mbufq_init(&mq, IFQ_MAXLEN);
7166
7167 LKPI_80211_LHW_RXQ_LOCK(lhw);
7168 mbufq_concat(&mq, &lhw->rxq);
7169 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7170
7171 m = mbufq_dequeue(&mq);
7172 while (m != NULL) {
7173 lkpi_80211_lhw_rxq_rx_one(lhw, m);
7174 m = mbufq_dequeue(&mq);
7175 }
7176 }
7177
7178 static void
7179 lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
7180 struct ieee80211_rx_status *rx_status,
7181 struct ieee80211_rx_stats *rx_stats,
7182 uint8_t *rssip)
7183 {
7184 struct ieee80211_supported_band *supband;
7185 struct rate_info rxrate;
7186 int i;
7187 uint8_t rssi;
7188
7189 memset(&rxrate, 0, sizeof(rxrate));
7190 memset(rx_stats, 0, sizeof(*rx_stats));
7191 rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
7192 /* XXX-BZ correct hardcoded noise floor, survey data? */
7193 rx_stats->c_nf = -96;
7194 if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
7195 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
7196 rssi = rx_status->signal;
7197 else
7198 rssi = rx_stats->c_nf;
7199 /*
7200 * net80211 signal strength data are in .5 dBm units relative to
7201 * the current noise floor (see comment in ieee80211_node.h).
7202 */
7203 rssi -= rx_stats->c_nf;
7204 if (rssip != NULL)
7205 *rssip = rssi;
7206 rx_stats->c_rssi = rssi * 2;
7207 rx_stats->r_flags |= IEEE80211_R_BAND;
7208 rx_stats->c_band =
7209 lkpi_nl80211_band_to_net80211_band(rx_status->band);
7210 rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
7211 rx_stats->c_freq = rx_status->freq;
7212 rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
7213
7214 rx_stats->c_rx_tsf = rx_status->mactime;
7215
7216 /* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
7217 if ((rx_status->flag & RX_FLAG_MACTIME) ==
7218 (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
7219 rx_stats->r_flags |= IEEE80211_R_TSF64;
7220 /* XXX RX_FLAG_MACTIME_PLCP_START ? */
7221 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
7222 rx_stats->r_flags |= IEEE80211_R_TSF_START;
7223 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
7224 rx_stats->r_flags |= IEEE80211_R_TSF_END;
7225 /* XXX-BZ if TSF_END will net80211 do the unwind of time? */
7226 }
7227
7228 if (rx_status->chains != 0) {
7229 int cc;
7230 int8_t crssi;
7231
7232 rx_stats->c_chain = rx_status->chains;
7233 rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
7234
7235 cc = 0;
7236 for (i = 0; i < nitems(rx_status->chain_signal); i++) {
7237 if (!(rx_status->chains & BIT(i)))
7238 continue;
7239 crssi = rx_status->chain_signal[i];
7240 crssi -= rx_stats->c_nf;
7241 rx_stats->c_rssi_ctl[i] = crssi * 2;
7242 rx_stats->c_rssi_ext[i] = crssi * 2; /* XXX _ext ??? ATH thing? */
7243 /* We currently only have the global noise floor value. */
7244 rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
7245 rx_stats->c_nf_ext[i] = rx_stats->c_nf;
7246 cc++;
7247 }
7248 if (cc > 0)
7249 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
7250 }
7251
7252 /* XXX-NET80211 We are not going to populate c_phytype! */
7253
7254 switch (rx_status->encoding) {
7255 case RX_ENC_LEGACY:
7256 {
7257 uint32_t legacy = 0;
7258
7259 supband = hw->wiphy->bands[rx_status->band];
7260 if (supband != NULL)
7261 legacy = supband->bitrates[rx_status->rate_idx].bitrate;
7262 rx_stats->c_rate = legacy;
7263 rxrate.legacy = legacy;
7264 /* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
7265 break;
7266 }
7267 case RX_ENC_HT:
7268 rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
7269 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
7270 rxrate.flags |= RATE_INFO_FLAGS_MCS;
7271 rxrate.mcs = rx_status->rate_idx;
7272 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
7273 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
7274 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
7275 }
7276 break;
7277 case RX_ENC_VHT:
7278 rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
7279 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
7280 rx_stats->c_vhtnss = rx_status->nss;
7281 rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
7282 rxrate.mcs = rx_status->rate_idx;
7283 rxrate.nss = rx_status->nss;
7284 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
7285 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
7286 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
7287 }
7288 break;
7289 case RX_ENC_HE:
7290 rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
7291 rxrate.mcs = rx_status->rate_idx;
7292 rxrate.nss = rx_status->nss;
7293 /* XXX TODO */
7294 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
7295 break;
7296 case RX_ENC_EHT:
7297 rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
7298 rxrate.mcs = rx_status->rate_idx;
7299 rxrate.nss = rx_status->nss;
7300 /* XXX TODO */
7301 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
7302 break;
7303 }
7304
7305 rxrate.bw = rx_status->bw;
7306 switch (rx_status->bw) {
7307 case RATE_INFO_BW_20:
7308 rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
7309 break;
7310 case RATE_INFO_BW_40:
7311 rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
7312 break;
7313 case RATE_INFO_BW_80:
7314 rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
7315 break;
7316 case RATE_INFO_BW_160:
7317 rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
7318 break;
7319 case RATE_INFO_BW_320:
7320 case RATE_INFO_BW_HE_RU:
7321 case RATE_INFO_BW_EHT_RU:
7322 case RATE_INFO_BW_5:
7323 case RATE_INFO_BW_10:
7324 TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
7325 break;
7326 }
7327
7328 if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
7329 rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
7330 if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
7331 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
7332
7333 /*
7334 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
7335 * case the hardware does something we do not expect always leave
7336 * these enabled. Leaving this commant as documentation for the || 1.
7337 */
7338 #if defined(LKPI_80211_HW_CRYPTO) || 1
7339 if (rx_status->flag & RX_FLAG_DECRYPTED) {
7340 rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
7341 /* Only valid if decrypted is set. */
7342 if (rx_status->flag & RX_FLAG_PN_VALIDATED)
7343 rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
7344 }
7345 if (rx_status->flag & RX_FLAG_IV_STRIPPED)
7346 rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
7347 if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
7348 rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
7349 if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
7350 rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
7351 if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
7352 rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
7353 if (rx_status->flag & RX_FLAG_MMIC_ERROR)
7354 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
7355 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
7356 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
7357 #endif
7358
7359 /* Fill in some sinfo bits to fill gaps not reported byt the driver. */
7360 if (lsta != NULL) {
7361 memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
7362 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
7363
7364 if (rx_status->chains != 0) {
7365 lsta->sinfo.chains = rx_status->chains;
7366 memcpy(lsta->sinfo.chain_signal, rx_status->chain_signal,
7367 sizeof(lsta->sinfo.chain_signal));
7368 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
7369 }
7370 }
7371 }
7372
7373 #ifdef LINUXKPI_DEBUG_80211
7374 static void
7375 lkpi_rx_log_beacon(struct mbuf *m, struct lkpi_hw *lhw,
7376 struct ieee80211_rx_status *rx_status)
7377 {
7378 struct ieee80211_mgmt *f;
7379 uint8_t *e;
7380 char ssid[IEEE80211_NWID_LEN * 4 + 1];
7381
7382 memset(ssid, '\0', sizeof(ssid));
7383
7384 f = mtod(m, struct ieee80211_mgmt *);
7385 e = f->u.beacon.variable;
7386 /*
7387 * Usually SSID is right after the fixed part and for debugging we will
7388 * be fine should we miss it if it is not.
7389 */
7390 while ((e - (uint8_t *)f) < m->m_len) {
7391 if (*e == IEEE80211_ELEMID_SSID)
7392 break;
7393 e += (2 + *(e + 1));
7394 }
7395 if (*e == IEEE80211_ELEMID_SSID) {
7396 int i, len;
7397 char *p;
7398
7399 p = ssid;
7400 len = m->m_len - ((e + 2) - (uint8_t *)f);
7401 if (len > *(e + 1))
7402 len = *(e + 1);
7403 e += 2;
7404 for (i = 0; i < len; i++) {
7405 /* Printable character? */
7406 if (*e >= 0x20 && *e < 0x7f) {
7407 *p++ = *e++;
7408 } else {
7409 snprintf(p, 5, "%#04x", *e++);
7410 p += 4;
7411 }
7412 }
7413 *p = '\0';
7414 }
7415
7416 /* We print skb, skb->data, m as we are seeing 'ghost beacons'. */
7417 TRACE_SCAN_BEACON(lhw->ic, "Beacon: scan_flags %b, band %s freq %u chan %-4d "
7418 "len %d { %#06x %#06x %6D %6D %6D %#06x %ju %u %#06x SSID '%s' }",
7419 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7420 lkpi_nl80211_band_name(rx_status->band), rx_status->freq,
7421 linuxkpi_ieee80211_frequency_to_channel(rx_status->freq, 0),
7422 m->m_pkthdr.len, f->frame_control, f->duration_id,
7423 f->da, ":", f->sa, ":", f->bssid, ":", f->seq_ctrl,
7424 (uintmax_t)le64_to_cpu(f->u.beacon.timestamp),
7425 le16_to_cpu(f->u.beacon.beacon_int),
7426 le16_to_cpu(f->u.beacon.capab_info), ssid);
7427 }
7428 #endif
7429
7430 /* For %list see comment towards the end of the function. */
7431 void
7432 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
7433 struct ieee80211_sta *sta, struct napi_struct *napi __unused,
7434 struct list_head *list __unused)
7435 {
7436 struct lkpi_hw *lhw;
7437 struct ieee80211com *ic;
7438 struct mbuf *m;
7439 struct skb_shared_info *shinfo;
7440 struct ieee80211_rx_status *rx_status;
7441 struct ieee80211_rx_stats rx_stats;
7442 struct ieee80211_node *ni;
7443 struct ieee80211vap *vap;
7444 struct ieee80211_hdr *hdr;
7445 struct lkpi_sta *lsta;
7446 int i, offset, ok, error;
7447 uint8_t rssi;
7448 bool is_beacon;
7449
7450 lhw = HW_TO_LHW(hw);
7451 ic = lhw->ic;
7452
7453 if (skb->len < 2) {
7454 /* Need 80211 stats here. */
7455 counter_u64_add(ic->ic_ierrors, 1);
7456 IMPROVE();
7457 goto err;
7458 }
7459
7460 /*
7461 * For now do the data copy; we can later improve things. Might even
7462 * have an mbuf backing the skb data then?
7463 */
7464 m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
7465 if (m == NULL) {
7466 counter_u64_add(ic->ic_ierrors, 1);
7467 goto err;
7468 }
7469 m_copyback(m, 0, skb->tail - skb->data, skb->data);
7470
7471 shinfo = skb_shinfo(skb);
7472 offset = m->m_len;
7473 for (i = 0; i < shinfo->nr_frags; i++) {
7474 m_copyback(m, offset, shinfo->frags[i].size,
7475 (uint8_t *)linux_page_address(shinfo->frags[i].page) +
7476 shinfo->frags[i].offset);
7477 offset += shinfo->frags[i].size;
7478 }
7479
7480 rx_status = IEEE80211_SKB_RXCB(skb);
7481
7482 hdr = (void *)skb->data;
7483 is_beacon = ieee80211_is_beacon(hdr->frame_control);
7484
7485 #ifdef LINUXKPI_DEBUG_80211
7486 /*
7487 * We use the mbuf here as otherwise the variable part might
7488 * be in skb frags.
7489 */
7490 if (is_beacon && ((linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0))
7491 lkpi_rx_log_beacon(m, lhw, rx_status);
7492
7493 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
7494 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) == 0)
7495 goto no_trace_beacons;
7496
7497 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7498 printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
7499 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
7500 __func__, skb, skb->len, skb->data_len,
7501 skb->truesize, skb->head, skb->data, skb->tail, skb->end,
7502 shinfo, shinfo->nr_frags,
7503 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
7504
7505 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
7506 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
7507
7508 /* Implement a dump_rxcb() !!! */
7509 if ((linuxkpi_debug_80211 & D80211_TRACE_RX) != 0 ||
7510 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0)
7511 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
7512 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
7513 __func__,
7514 (uintmax_t)rx_status->boottime_ns,
7515 (uintmax_t)rx_status->mactime,
7516 rx_status->device_timestamp,
7517 rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
7518 rx_status->freq,
7519 rx_status->bw,
7520 rx_status->encoding,
7521 rx_status->ampdu_reference,
7522 rx_status->band,
7523 rx_status->chains,
7524 rx_status->chain_signal[0],
7525 rx_status->chain_signal[1],
7526 rx_status->chain_signal[2],
7527 rx_status->chain_signal[3],
7528 rx_status->signal,
7529 rx_status->enc_flags,
7530 rx_status->he_dcm,
7531 rx_status->he_gi,
7532 rx_status->he_ru,
7533 rx_status->zero_length_psdu_type,
7534 rx_status->nss,
7535 rx_status->rate_idx);
7536 no_trace_beacons:
7537 #endif
7538
7539 lsta = NULL;
7540 if (sta != NULL) {
7541 lsta = STA_TO_LSTA(sta);
7542 ni = ieee80211_ref_node(lsta->ni);
7543 } else {
7544 struct ieee80211_frame_min *wh;
7545
7546 wh = mtod(m, struct ieee80211_frame_min *);
7547 ni = ieee80211_find_rxnode(ic, wh);
7548 if (ni != NULL)
7549 lsta = ni->ni_drv_data;
7550 }
7551
7552 rssi = 0;
7553 lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
7554
7555 ok = ieee80211_add_rx_params(m, &rx_stats);
7556 if (ok == 0) {
7557 m_freem(m);
7558 counter_u64_add(ic->ic_ierrors, 1);
7559 goto err;
7560 }
7561
7562 if (ni != NULL)
7563 vap = ni->ni_vap;
7564 else
7565 /*
7566 * XXX-BZ can we improve this by looking at the frame hdr
7567 * or other meta-data passed up?
7568 */
7569 vap = TAILQ_FIRST(&ic->ic_vaps);
7570
7571 #ifdef LINUXKPI_DEBUG_80211
7572 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7573 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
7574 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
7575 ni, vap, is_beacon ? " beacon" : "");
7576 #endif
7577
7578 if (ni != NULL && vap != NULL && is_beacon &&
7579 rx_status->device_timestamp > 0 &&
7580 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
7581 struct lkpi_vif *lvif;
7582 struct ieee80211_vif *vif;
7583 struct ieee80211_frame *wh;
7584
7585 wh = mtod(m, struct ieee80211_frame *);
7586 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
7587 goto skip_device_ts;
7588
7589 lvif = VAP_TO_LVIF(vap);
7590 vif = LVIF_TO_VIF(lvif);
7591
7592 IMPROVE("TIMING_BEACON_ONLY?");
7593 /* mac80211 specific (not net80211) so keep it here. */
7594 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
7595 /*
7596 * net80211 should take care of the other information (sync_tsf,
7597 * sync_dtim_count) as otherwise we need to parse the beacon.
7598 */
7599 skip_device_ts:
7600 ;
7601 }
7602
7603 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
7604 ieee80211_radiotap_active_vap(vap)) {
7605 struct lkpi_radiotap_rx_hdr *rtap;
7606
7607 rtap = &lhw->rtap_rx;
7608 rtap->wr_tsft = rx_status->device_timestamp;
7609 rtap->wr_flags = 0;
7610 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
7611 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7612 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
7613 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
7614 #if 0 /* .. or it does not given we strip it below. */
7615 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7616 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
7617 #endif
7618 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
7619 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
7620 rtap->wr_rate = 0;
7621 IMPROVE();
7622 /* XXX TODO status->encoding / rate_index / bw */
7623 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
7624 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
7625 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
7626 rtap->wr_dbm_antsignal = rssi;
7627 rtap->wr_dbm_antnoise = rx_stats.c_nf;
7628 }
7629
7630 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7631 m_adj(m, -IEEE80211_CRC_LEN);
7632
7633 #if 0
7634 if (list != NULL) {
7635 /*
7636 * Normally this would be queued up and delivered by
7637 * netif_receive_skb_list(), napi_gro_receive(), or the like.
7638 * See mt76::mac80211.c as only current possible consumer.
7639 */
7640 IMPROVE("we simply pass the packet to net80211 to deal with.");
7641 }
7642 #endif
7643
7644 /* Attach meta-information to the mbuf for the deferred RX path. */
7645 if (ni != NULL) {
7646 #ifdef LKPI_80211_USE_MTAG
7647 struct m_tag *mtag;
7648 struct lkpi_80211_tag_rxni *rxni;
7649
7650 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
7651 sizeof(*rxni), IEEE80211_M_NOWAIT);
7652 if (mtag == NULL) {
7653 m_freem(m);
7654 counter_u64_add(ic->ic_ierrors, 1);
7655 goto err;
7656 }
7657 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7658 rxni->ni = ni; /* We hold a reference. */
7659 m_tag_prepend(m, mtag);
7660 #else
7661 m->m_pkthdr.PH_loc.ptr = ni; /* We hold a reference. */
7662 #endif
7663 }
7664
7665 LKPI_80211_LHW_RXQ_LOCK(lhw);
7666 if (lhw->rxq_stopped) {
7667 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7668 m_freem(m);
7669 counter_u64_add(ic->ic_ierrors, 1);
7670 goto err;
7671 }
7672
7673 error = mbufq_enqueue(&lhw->rxq, m);
7674 if (error != 0) {
7675 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7676 m_freem(m);
7677 counter_u64_add(ic->ic_ierrors, 1);
7678 #ifdef LINUXKPI_DEBUG_80211
7679 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7680 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
7681 __func__, error);
7682 #endif
7683 goto err;
7684 }
7685 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
7686 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7687
7688 IMPROVE();
7689
7690 err:
7691 /* The skb is ours so we can free it :-) */
7692 kfree_skb(skb);
7693 }
7694
7695 uint8_t
7696 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
7697 {
7698 const struct ieee80211_frame *wh;
7699 uint8_t tid;
7700
7701 /* Linux seems to assume this is a QOS-Data-Frame */
7702 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
7703 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
7704 hdr->frame_control));
7705
7706 wh = (const struct ieee80211_frame *)hdr;
7707 tid = ieee80211_gettid(wh);
7708 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
7709 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
7710
7711 return (tid);
7712 }
7713
7714 /* -------------------------------------------------------------------------- */
7715
7716 static void
7717 lkpi_wiphy_work(struct work_struct *work)
7718 {
7719 struct lkpi_wiphy *lwiphy;
7720 struct wiphy *wiphy;
7721 struct wiphy_work *wk;
7722
7723 lwiphy = container_of(work, struct lkpi_wiphy, wwk);
7724 wiphy = LWIPHY_TO_WIPHY(lwiphy);
7725
7726 wiphy_lock(wiphy);
7727
7728 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7729 wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
7730 /* If there is nothing we do nothing. */
7731 if (wk == NULL) {
7732 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7733 wiphy_unlock(wiphy);
7734 return;
7735 }
7736 list_del_init(&wk->entry);
7737
7738 /* More work to do? */
7739 if (!list_empty(&lwiphy->wwk_list))
7740 schedule_work(work);
7741 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7742
7743 /* Finally call the (*wiphy_work_fn)() function. */
7744 wk->fn(wiphy, wk);
7745
7746 wiphy_unlock(wiphy);
7747 }
7748
7749 void
7750 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
7751 {
7752 struct lkpi_wiphy *lwiphy;
7753
7754 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7755
7756 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7757 /* Do not double-queue. */
7758 if (list_empty(&wwk->entry))
7759 list_add_tail(&wwk->entry, &lwiphy->wwk_list);
7760 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7761
7762 /*
7763 * See how ieee80211_queue_work() work continues in Linux or if things
7764 * migrate here over time?
7765 * Use a system queue from linux/workqueue.h for now.
7766 */
7767 queue_work(system_wq, &lwiphy->wwk);
7768 }
7769
7770 void
7771 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
7772 {
7773 struct lkpi_wiphy *lwiphy;
7774
7775 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7776
7777 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7778 /* Only cancel if queued. */
7779 if (!list_empty(&wwk->entry))
7780 list_del_init(&wwk->entry);
7781 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7782 }
7783
7784 void
7785 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
7786 {
7787 struct lkpi_wiphy *lwiphy;
7788 struct wiphy_work *wk;
7789
7790 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7791 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7792 /* If wwk is unset, flush everything; called when wiphy is shut down. */
7793 if (wwk != NULL && list_empty(&wwk->entry)) {
7794 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7795 return;
7796 }
7797
7798 while (!list_empty(&lwiphy->wwk_list)) {
7799
7800 wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7801 entry);
7802 list_del_init(&wk->entry);
7803 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7804 wk->fn(wiphy, wk);
7805 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7806 if (wk == wwk)
7807 break;
7808 }
7809 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7810 }
7811
7812 void
7813 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7814 {
7815 struct wiphy_delayed_work *wdwk;
7816
7817 wdwk = from_timer(wdwk, tl, timer);
7818 wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7819 }
7820
7821 void
7822 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7823 struct wiphy_delayed_work *wdwk, unsigned long delay)
7824 {
7825 if (delay == 0) {
7826 /* Run right away. */
7827 del_timer(&wdwk->timer);
7828 wiphy_work_queue(wiphy, &wdwk->work);
7829 } else {
7830 wdwk->wiphy = wiphy;
7831 mod_timer(&wdwk->timer, jiffies + delay);
7832 }
7833 }
7834
7835 void
7836 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7837 struct wiphy_delayed_work *wdwk)
7838 {
7839 del_timer_sync(&wdwk->timer);
7840 wiphy_work_cancel(wiphy, &wdwk->work);
7841 }
7842
7843 /* -------------------------------------------------------------------------- */
7844
7845 struct wiphy *
7846 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
7847 {
7848 struct lkpi_wiphy *lwiphy;
7849 struct wiphy *wiphy;
7850
7851 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
7852 if (lwiphy == NULL)
7853 return (NULL);
7854 lwiphy->ops = ops;
7855
7856 LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7857 INIT_LIST_HEAD(&lwiphy->wwk_list);
7858 INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7859
7860 wiphy = LWIPHY_TO_WIPHY(lwiphy);
7861
7862 mutex_init(&wiphy->mtx);
7863 TODO();
7864
7865 return (wiphy);
7866 }
7867
7868 void
7869 linuxkpi_wiphy_free(struct wiphy *wiphy)
7870 {
7871 struct lkpi_wiphy *lwiphy;
7872
7873 if (wiphy == NULL)
7874 return;
7875
7876 linuxkpi_wiphy_work_flush(wiphy, NULL);
7877 mutex_destroy(&wiphy->mtx);
7878
7879 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7880 LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7881
7882 kfree(lwiphy);
7883 }
7884
7885 static uint32_t
7886 lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7887 {
7888 TODO("cfg80211_calculate_bitrate_ht");
7889 return (rate->legacy);
7890 }
7891
7892 static uint32_t
7893 lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7894 {
7895 TODO("cfg80211_calculate_bitrate_vht");
7896 return (rate->legacy);
7897 }
7898
7899 uint32_t
7900 linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7901 {
7902
7903 /* Beware: order! */
7904 if (rate->flags & RATE_INFO_FLAGS_MCS)
7905 return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7906
7907 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7908 return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7909
7910 IMPROVE("HE/EHT/...");
7911
7912 return (rate->legacy);
7913 }
7914
7915 uint32_t
7916 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
7917 enum nl80211_band band)
7918 {
7919
7920 switch (band) {
7921 case NL80211_BAND_2GHZ:
7922 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
7923 break;
7924 case NL80211_BAND_5GHZ:
7925 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
7926 break;
7927 default:
7928 /* XXX abort, retry, error, panic? */
7929 break;
7930 }
7931
7932 return (0);
7933 }
7934
7935 uint32_t
7936 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
7937 {
7938
7939 return (ieee80211_mhz2ieee(freq, 0));
7940 }
7941
7942 #if 0
7943 static struct lkpi_sta *
7944 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
7945 {
7946 struct lkpi_sta *lsta, *temp;
7947
7948 rcu_read_lock();
7949 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7950 if (lsta->ni == ni) {
7951 rcu_read_unlock();
7952 return (lsta);
7953 }
7954 }
7955 rcu_read_unlock();
7956
7957 return (NULL);
7958 }
7959 #endif
7960
7961 struct ieee80211_sta *
7962 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
7963 {
7964 struct lkpi_vif *lvif;
7965 struct lkpi_sta *lsta;
7966 struct ieee80211_sta *sta;
7967
7968 lvif = VIF_TO_LVIF(vif);
7969
7970 rcu_read_lock();
7971 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7972 sta = LSTA_TO_STA(lsta);
7973 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7974 rcu_read_unlock();
7975 return (sta);
7976 }
7977 }
7978 rcu_read_unlock();
7979 return (NULL);
7980 }
7981
7982 struct ieee80211_sta *
7983 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7984 const uint8_t *addr, const uint8_t *ourvifaddr)
7985 {
7986 struct lkpi_hw *lhw;
7987 struct lkpi_vif *lvif;
7988 struct lkpi_sta *lsta;
7989 struct ieee80211_vif *vif;
7990 struct ieee80211_sta *sta;
7991
7992 lhw = wiphy_priv(hw->wiphy);
7993 sta = NULL;
7994
7995 LKPI_80211_LHW_LVIF_LOCK(lhw);
7996 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7997
7998 /* XXX-BZ check our address from the vif. */
7999
8000 vif = LVIF_TO_VIF(lvif);
8001 if (ourvifaddr != NULL &&
8002 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
8003 continue;
8004 sta = linuxkpi_ieee80211_find_sta(vif, addr);
8005 if (sta != NULL)
8006 break;
8007 }
8008 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8009
8010 if (sta != NULL) {
8011 lsta = STA_TO_LSTA(sta);
8012 if (!lsta->added_to_drv)
8013 return (NULL);
8014 }
8015
8016 return (sta);
8017 }
8018
8019 struct sk_buff *
8020 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
8021 struct ieee80211_txq *txq)
8022 {
8023 struct lkpi_txq *ltxq;
8024 struct lkpi_vif *lvif;
8025 struct sk_buff *skb;
8026
8027 IMPROVE("wiphy_lock? or assert?");
8028 skb = NULL;
8029 ltxq = TXQ_TO_LTXQ(txq);
8030 ltxq->seen_dequeue = true;
8031
8032 if (ltxq->stopped)
8033 goto stopped;
8034
8035 lvif = VIF_TO_LVIF(ltxq->txq.vif);
8036 if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
8037 ltxq->stopped = true;
8038 goto stopped;
8039 }
8040
8041 IMPROVE("hw(TX_FRAG_LIST)");
8042
8043 LKPI_80211_LTXQ_LOCK(ltxq);
8044 skb = skb_dequeue(<xq->skbq);
8045 LKPI_80211_LTXQ_UNLOCK(ltxq);
8046
8047 stopped:
8048 return (skb);
8049 }
8050
8051 void
8052 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
8053 unsigned long *frame_cnt, unsigned long *byte_cnt)
8054 {
8055 struct lkpi_txq *ltxq;
8056 struct sk_buff *skb;
8057 unsigned long fc, bc;
8058
8059 ltxq = TXQ_TO_LTXQ(txq);
8060
8061 fc = bc = 0;
8062 LKPI_80211_LTXQ_LOCK(ltxq);
8063 skb_queue_walk(<xq->skbq, skb) {
8064 fc++;
8065 bc += skb->len;
8066 }
8067 LKPI_80211_LTXQ_UNLOCK(ltxq);
8068 if (frame_cnt)
8069 *frame_cnt = fc;
8070 if (byte_cnt)
8071 *byte_cnt = bc;
8072
8073 /* Validate that this is doing the correct thing. */
8074 /* Should we keep track on en/dequeue? */
8075 IMPROVE();
8076 }
8077
8078 /*
8079 * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
8080 * The latter tries to derive the success status from the info flags
8081 * passed back from the driver. rawx_mit() saves the ni on the m and the
8082 * m on the skb for us to be able to give feedback to net80211.
8083 */
8084 static void
8085 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
8086 int status)
8087 {
8088 struct ieee80211_node *ni;
8089 struct mbuf *m;
8090
8091 m = skb->m;
8092 skb->m = NULL;
8093
8094 if (m != NULL) {
8095 ni = m->m_pkthdr.PH_loc.ptr;
8096 /* Status: 0 is ok, != 0 is error. */
8097 ieee80211_tx_complete(ni, m, status);
8098 /* ni & mbuf were consumed. */
8099 }
8100 }
8101
8102 void
8103 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
8104 int status)
8105 {
8106
8107 _lkpi_ieee80211_free_txskb(hw, skb, status);
8108 kfree_skb(skb);
8109 }
8110
8111 void
8112 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
8113 struct ieee80211_tx_status *txstat)
8114 {
8115 struct sk_buff *skb;
8116 struct ieee80211_tx_info *info;
8117 struct ieee80211_ratectl_tx_status txs;
8118 struct ieee80211_node *ni;
8119 int status;
8120
8121 skb = txstat->skb;
8122 if (skb->m != NULL) {
8123 struct mbuf *m;
8124
8125 m = skb->m;
8126 ni = m->m_pkthdr.PH_loc.ptr;
8127 memset(&txs, 0, sizeof(txs));
8128 } else {
8129 ni = NULL;
8130 }
8131
8132 info = txstat->info;
8133 if (info->flags & IEEE80211_TX_STAT_ACK) {
8134 status = 0; /* No error. */
8135 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
8136 } else {
8137 status = 1;
8138 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
8139 }
8140
8141 if (ni != NULL) {
8142 txs.pktlen = skb->len;
8143 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
8144 if (info->status.rates[0].count > 1) {
8145 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */
8146 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
8147 }
8148 #if 0 /* Unused in net80211 currently. */
8149 /* XXX-BZ convert check .flags for MCS/VHT/.. */
8150 txs.final_rate = info->status.rates[0].idx;
8151 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
8152 #endif
8153 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
8154 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */
8155 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
8156 }
8157
8158 IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
8159 ieee80211_ratectl_tx_complete(ni, &txs);
8160 ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
8161
8162 #ifdef LINUXKPI_DEBUG_80211
8163 if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
8164 printf("TX-RATE: %s: long_retries %d\n", __func__,
8165 txs.long_retries);
8166 }
8167 #endif
8168 }
8169
8170 #ifdef LINUXKPI_DEBUG_80211
8171 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
8172 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
8173 "band %u hw_queue %u tx_time_est %d : "
8174 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
8175 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
8176 "tx_time %u flags %#x "
8177 "status_driver_data [ %p %p ]\n",
8178 __func__, hw, skb, status, info->flags,
8179 info->band, info->hw_queue, info->tx_time_est,
8180 info->status.rates[0].idx, info->status.rates[0].count,
8181 info->status.rates[0].flags,
8182 info->status.rates[1].idx, info->status.rates[1].count,
8183 info->status.rates[1].flags,
8184 info->status.rates[2].idx, info->status.rates[2].count,
8185 info->status.rates[2].flags,
8186 info->status.rates[3].idx, info->status.rates[3].count,
8187 info->status.rates[3].flags,
8188 info->status.ack_signal, info->status.ampdu_ack_len,
8189 info->status.ampdu_len, info->status.antenna,
8190 info->status.tx_time, info->status.flags,
8191 info->status.status_driver_data[0],
8192 info->status.status_driver_data[1]);
8193 #endif
8194
8195 if (txstat->free_list) {
8196 _lkpi_ieee80211_free_txskb(hw, skb, status);
8197 list_add_tail(&skb->list, txstat->free_list);
8198 } else {
8199 linuxkpi_ieee80211_free_txskb(hw, skb, status);
8200 }
8201 }
8202
8203 void
8204 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
8205 {
8206 struct ieee80211_tx_status status;
8207
8208 memset(&status, 0, sizeof(status));
8209 status.info = IEEE80211_SKB_CB(skb);
8210 status.skb = skb;
8211 /* sta, n_rates, rates, free_list? */
8212
8213 ieee80211_tx_status_ext(hw, &status);
8214 }
8215
8216 /*
8217 * This is an internal bandaid for the moment for the way we glue
8218 * skbs and mbufs together for TX. Once we have skbs backed by
8219 * mbufs this should go away.
8220 * This is a public function but kept on the private KPI (lkpi_)
8221 * and is not exposed by a header file.
8222 */
8223 static void
8224 lkpi_ieee80211_free_skb_mbuf(void *p)
8225 {
8226 struct ieee80211_node *ni;
8227 struct mbuf *m;
8228
8229 if (p == NULL)
8230 return;
8231
8232 m = (struct mbuf *)p;
8233 M_ASSERTPKTHDR(m);
8234
8235 ni = m->m_pkthdr.PH_loc.ptr;
8236 m->m_pkthdr.PH_loc.ptr = NULL;
8237 if (ni != NULL)
8238 ieee80211_free_node(ni);
8239 m_freem(m);
8240 }
8241
8242 void
8243 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
8244 struct delayed_work *w, int delay)
8245 {
8246 struct lkpi_hw *lhw;
8247
8248 /* Need to make sure hw is in a stable (non-suspended) state. */
8249 IMPROVE();
8250
8251 lhw = HW_TO_LHW(hw);
8252 queue_delayed_work(lhw->workq, w, delay);
8253 }
8254
8255 void
8256 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
8257 struct work_struct *w)
8258 {
8259 struct lkpi_hw *lhw;
8260
8261 /* Need to make sure hw is in a stable (non-suspended) state. */
8262 IMPROVE();
8263
8264 lhw = HW_TO_LHW(hw);
8265 queue_work(lhw->workq, w);
8266 }
8267
8268 struct sk_buff *
8269 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, const uint8_t *addr,
8270 const uint8_t *ssid, size_t ssid_len, size_t tailroom)
8271 {
8272 struct sk_buff *skb;
8273 struct ieee80211_frame *wh;
8274 uint8_t *p;
8275 size_t len;
8276
8277 len = sizeof(*wh);
8278 len += 2 + ssid_len;
8279
8280 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
8281 if (skb == NULL)
8282 return (NULL);
8283
8284 skb_reserve(skb, hw->extra_tx_headroom);
8285
8286 wh = skb_put_zero(skb, sizeof(*wh));
8287 wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
8288 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
8289 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
8290 IEEE80211_ADDR_COPY(wh->i_addr2, addr);
8291 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
8292
8293 p = skb_put(skb, 2 + ssid_len);
8294 *p++ = IEEE80211_ELEMID_SSID;
8295 *p++ = ssid_len;
8296 if (ssid_len > 0)
8297 memcpy(p, ssid, ssid_len);
8298
8299 return (skb);
8300 }
8301
8302 struct sk_buff *
8303 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
8304 struct ieee80211_vif *vif)
8305 {
8306 struct lkpi_vif *lvif;
8307 struct ieee80211vap *vap;
8308 struct sk_buff *skb;
8309 struct ieee80211_frame_pspoll *psp;
8310 uint16_t v;
8311
8312 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
8313 if (skb == NULL)
8314 return (NULL);
8315
8316 skb_reserve(skb, hw->extra_tx_headroom);
8317
8318 lvif = VIF_TO_LVIF(vif);
8319 vap = LVIF_TO_VAP(lvif);
8320
8321 psp = skb_put_zero(skb, sizeof(*psp));
8322 psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
8323 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
8324 v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
8325 memcpy(&psp->i_aid, &v, sizeof(v));
8326 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
8327 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
8328
8329 return (skb);
8330 }
8331
8332 struct sk_buff *
8333 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
8334 struct ieee80211_vif *vif, int linkid, bool qos)
8335 {
8336 struct lkpi_vif *lvif;
8337 struct ieee80211vap *vap;
8338 struct sk_buff *skb;
8339 struct ieee80211_frame *nullf;
8340
8341 IMPROVE("linkid");
8342
8343 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
8344 if (skb == NULL)
8345 return (NULL);
8346
8347 skb_reserve(skb, hw->extra_tx_headroom);
8348
8349 lvif = VIF_TO_LVIF(vif);
8350 vap = LVIF_TO_VAP(lvif);
8351
8352 nullf = skb_put_zero(skb, sizeof(*nullf));
8353 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
8354 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
8355 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
8356
8357 IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
8358 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
8359 IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
8360
8361 return (skb);
8362 }
8363
8364 struct wireless_dev *
8365 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
8366 {
8367 struct lkpi_vif *lvif;
8368
8369 lvif = VIF_TO_LVIF(vif);
8370 return (&lvif->wdev);
8371 }
8372
8373 void
8374 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
8375 {
8376 struct lkpi_vif *lvif;
8377 struct ieee80211vap *vap;
8378 enum ieee80211_state nstate;
8379 int arg;
8380
8381 lvif = VIF_TO_LVIF(vif);
8382 vap = LVIF_TO_VAP(lvif);
8383
8384 /*
8385 * Go to init; otherwise we need to elaborately check state and
8386 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
8387 * Let the statemachine handle all neccessary changes.
8388 */
8389 nstate = IEEE80211_S_INIT;
8390 arg = 0; /* Not a valid reason. */
8391
8392 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
8393 "beacons %d dtim_period %d)\n", __func__, vif, vap,
8394 ieee80211_state_name[vap->iv_state],
8395 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
8396 vif->bss_conf.dtim_period);
8397 ieee80211_new_state(vap, nstate, arg);
8398 }
8399
8400 void
8401 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
8402 {
8403 struct lkpi_vif *lvif;
8404 struct ieee80211vap *vap;
8405
8406 lvif = VIF_TO_LVIF(vif);
8407 vap = LVIF_TO_VAP(lvif);
8408
8409 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
8410 "beacons %d dtim_period %d)\n", __func__, vif, vap,
8411 ieee80211_state_name[vap->iv_state],
8412 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
8413 vif->bss_conf.dtim_period);
8414 ieee80211_beacon_miss(vap->iv_ic);
8415 }
8416
8417 /* -------------------------------------------------------------------------- */
8418
8419 void
8420 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
8421 {
8422 struct lkpi_hw *lhw;
8423 struct lkpi_vif *lvif;
8424 struct ieee80211_vif *vif;
8425 int ac_count, ac;
8426
8427 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
8428 __func__, qnum, hw->queues, hw));
8429
8430 lhw = wiphy_priv(hw->wiphy);
8431
8432 /* See lkpi_ic_vap_create(). */
8433 if (hw->queues >= IEEE80211_NUM_ACS)
8434 ac_count = IEEE80211_NUM_ACS;
8435 else
8436 ac_count = 1;
8437
8438 LKPI_80211_LHW_LVIF_LOCK(lhw);
8439 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8440
8441 vif = LVIF_TO_VIF(lvif);
8442 for (ac = 0; ac < ac_count; ac++) {
8443 IMPROVE_TXQ("LOCKING");
8444 if (qnum == vif->hw_queue[ac]) {
8445 #ifdef LINUXKPI_DEBUG_80211
8446 /*
8447 * For now log this to better understand
8448 * how this is supposed to work.
8449 */
8450 if (lvif->hw_queue_stopped[ac] &&
8451 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8452 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8453 "lvif %p vif %p ac %d qnum %d already "
8454 "stopped\n", __func__, __LINE__,
8455 lhw, hw, lvif, vif, ac, qnum);
8456 #endif
8457 lvif->hw_queue_stopped[ac] = true;
8458 }
8459 }
8460 }
8461 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8462 }
8463
8464 void
8465 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
8466 {
8467 int i;
8468
8469 IMPROVE_TXQ("Locking; do we need further info?");
8470 for (i = 0; i < hw->queues; i++)
8471 linuxkpi_ieee80211_stop_queue(hw, i);
8472 }
8473
8474
8475 static void
8476 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
8477 {
8478 struct lkpi_hw *lhw;
8479 struct lkpi_vif *lvif;
8480 struct lkpi_sta *lsta;
8481 int ac_count, ac, tid;
8482
8483 /* See lkpi_ic_vap_create(). */
8484 if (hw->queues >= IEEE80211_NUM_ACS)
8485 ac_count = IEEE80211_NUM_ACS;
8486 else
8487 ac_count = 1;
8488
8489 lhw = wiphy_priv(hw->wiphy);
8490
8491 IMPROVE_TXQ("Locking");
8492 LKPI_80211_LHW_LVIF_LOCK(lhw);
8493 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8494 struct ieee80211_vif *vif;
8495
8496 vif = LVIF_TO_VIF(lvif);
8497 for (ac = 0; ac < ac_count; ac++) {
8498
8499 if (hwq == vif->hw_queue[ac]) {
8500
8501 /* XXX-BZ what about software scan? */
8502
8503 #ifdef LINUXKPI_DEBUG_80211
8504 /*
8505 * For now log this to better understand
8506 * how this is supposed to work.
8507 */
8508 if (!lvif->hw_queue_stopped[ac] &&
8509 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8510 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8511 "lvif %p vif %p ac %d hw_q not stopped\n",
8512 __func__, __LINE__,
8513 lhw, hw, lvif, vif, ac);
8514 #endif
8515 lvif->hw_queue_stopped[ac] = false;
8516
8517 rcu_read_lock();
8518 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
8519 struct ieee80211_sta *sta;
8520
8521 sta = LSTA_TO_STA(lsta);
8522 for (tid = 0; tid < nitems(sta->txq); tid++) {
8523 struct lkpi_txq *ltxq;
8524
8525 if (sta->txq[tid] == NULL)
8526 continue;
8527
8528 if (sta->txq[tid]->ac != ac)
8529 continue;
8530
8531 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8532 if (!ltxq->stopped)
8533 continue;
8534
8535 ltxq->stopped = false;
8536
8537 if (!skb_queue_empty(<xq->skbq))
8538 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
8539 }
8540 }
8541 rcu_read_unlock();
8542 }
8543 }
8544 }
8545 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8546 }
8547
8548 static void
8549 lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *hw)
8550 {
8551 int i;
8552
8553 IMPROVE_TXQ("Is this all/enough here?");
8554 for (i = 0; i < hw->queues; i++)
8555 lkpi_ieee80211_wake_queues(hw, i);
8556 }
8557
8558 void
8559 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
8560 {
8561 struct lkpi_hw *lhw;
8562 unsigned long flags;
8563
8564 lhw = HW_TO_LHW(hw);
8565
8566 spin_lock_irqsave(&lhw->txq_lock, flags);
8567 lkpi_ieee80211_wake_queues_locked(hw);
8568 spin_unlock_irqrestore(&lhw->txq_lock, flags);
8569 }
8570
8571 void
8572 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
8573 {
8574 struct lkpi_hw *lhw;
8575 unsigned long flags;
8576
8577 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
8578 __func__, qnum, hw->queues, hw));
8579
8580 lhw = HW_TO_LHW(hw);
8581
8582 spin_lock_irqsave(&lhw->txq_lock, flags);
8583 lkpi_ieee80211_wake_queues(hw, qnum);
8584 spin_unlock_irqrestore(&lhw->txq_lock, flags);
8585 }
8586
8587 /* This is just hardware queues. */
8588 void
8589 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
8590 {
8591 struct lkpi_hw *lhw;
8592
8593 lhw = HW_TO_LHW(hw);
8594
8595 IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
8596 IMPROVE_TXQ("LOCKING");
8597 if (++lhw->txq_generation[ac] == 0)
8598 lhw->txq_generation[ac]++;
8599 }
8600
8601 struct ieee80211_txq *
8602 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
8603 {
8604 struct lkpi_hw *lhw;
8605 struct ieee80211_txq *txq;
8606 struct lkpi_txq *ltxq;
8607
8608 lhw = HW_TO_LHW(hw);
8609 txq = NULL;
8610
8611 IMPROVE_TXQ("LOCKING");
8612
8613 /* Check that we are scheduled. */
8614 if (lhw->txq_generation[ac] == 0)
8615 goto out;
8616
8617 ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
8618 if (ltxq == NULL)
8619 goto out;
8620 if (ltxq->txq_generation == lhw->txq_generation[ac])
8621 goto out;
8622
8623 ltxq->txq_generation = lhw->txq_generation[ac];
8624 TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
8625 txq = <xq->txq;
8626 TAILQ_ELEM_INIT(ltxq, txq_entry);
8627
8628 out:
8629 return (txq);
8630 }
8631
8632 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
8633 struct ieee80211_txq *txq, bool withoutpkts)
8634 {
8635 struct lkpi_hw *lhw;
8636 struct lkpi_txq *ltxq;
8637 bool ltxq_empty;
8638
8639 ltxq = TXQ_TO_LTXQ(txq);
8640
8641 IMPROVE_TXQ("LOCKING");
8642
8643 /* Only schedule if work to do or asked to anyway. */
8644 LKPI_80211_LTXQ_LOCK(ltxq);
8645 ltxq_empty = skb_queue_empty(<xq->skbq);
8646 LKPI_80211_LTXQ_UNLOCK(ltxq);
8647 if (!withoutpkts && ltxq_empty)
8648 goto out;
8649
8650 /*
8651 * Make sure we do not double-schedule. We do this by checking tqe_prev,
8652 * the previous entry in our tailq. tqe_prev is always valid if this entry
8653 * is queued, tqe_next may be NULL if this is the only element in the list.
8654 */
8655 if (ltxq->txq_entry.tqe_prev != NULL)
8656 goto out;
8657
8658 lhw = HW_TO_LHW(hw);
8659 TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
8660 out:
8661 return;
8662 }
8663
8664 void
8665 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
8666 struct ieee80211_txq *txq)
8667 {
8668 struct lkpi_hw *lhw;
8669 struct ieee80211_txq *ntxq;
8670 struct ieee80211_tx_control control;
8671 struct sk_buff *skb;
8672
8673 lhw = HW_TO_LHW(hw);
8674
8675 LKPI_80211_LHW_TXQ_LOCK(lhw);
8676 ieee80211_txq_schedule_start(hw, txq->ac);
8677 do {
8678 ntxq = ieee80211_next_txq(hw, txq->ac);
8679 if (ntxq == NULL)
8680 break;
8681
8682 memset(&control, 0, sizeof(control));
8683 control.sta = ntxq->sta;
8684 do {
8685 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
8686 if (skb == NULL)
8687 break;
8688 lkpi_80211_mo_tx(hw, &control, skb);
8689 } while(1);
8690
8691 ieee80211_return_txq(hw, ntxq, false);
8692 } while (1);
8693 ieee80211_txq_schedule_end(hw, txq->ac);
8694 LKPI_80211_LHW_TXQ_UNLOCK(lhw);
8695 }
8696
8697 /* -------------------------------------------------------------------------- */
8698
8699 struct lkpi_cfg80211_bss {
8700 u_int refcnt;
8701 struct cfg80211_bss bss;
8702 };
8703
8704 struct lkpi_cfg80211_get_bss_iter_lookup {
8705 struct wiphy *wiphy;
8706 struct linuxkpi_ieee80211_channel *chan;
8707 const uint8_t *bssid;
8708 const uint8_t *ssid;
8709 size_t ssid_len;
8710 enum ieee80211_bss_type bss_type;
8711 enum ieee80211_privacy privacy;
8712
8713 /*
8714 * Something to store a copy of the result as the net80211 scan cache
8715 * is not refoucnted so a scan entry might go away any time.
8716 */
8717 bool match;
8718 struct cfg80211_bss *bss;
8719 };
8720
8721 static void
8722 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
8723 {
8724 struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
8725 size_t ielen;
8726
8727 lookup = arg;
8728
8729 /* Do not try to find another match. */
8730 if (lookup->match)
8731 return;
8732
8733 /* Nothing to store result. */
8734 if (lookup->bss == NULL)
8735 return;
8736
8737 if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
8738 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
8739 /* We have no idea what to compare to as the drivers only request ANY */
8740 return;
8741 }
8742
8743 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
8744 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
8745 /* We have no idea what to compare to as the drivers only request ANY */
8746 return;
8747 }
8748
8749 if (lookup->chan != NULL) {
8750 struct linuxkpi_ieee80211_channel *chan;
8751
8752 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
8753 se->se_chan->ic_freq);
8754 if (chan == NULL || chan != lookup->chan)
8755 return;
8756 }
8757
8758 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
8759 return;
8760
8761 if (lookup->ssid) {
8762 if (lookup->ssid_len != se->se_ssid[1] ||
8763 se->se_ssid[1] == 0)
8764 return;
8765 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
8766 return;
8767 }
8768
8769 ielen = se->se_ies.len;
8770
8771 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
8772 M_LKPI80211, M_NOWAIT | M_ZERO);
8773 if (lookup->bss->ies == NULL)
8774 return;
8775
8776 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
8777 lookup->bss->ies->len = ielen;
8778 if (ielen)
8779 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
8780
8781 lookup->match = true;
8782 }
8783
8784 struct cfg80211_bss *
8785 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
8786 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
8787 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
8788 {
8789 struct lkpi_cfg80211_bss *lbss;
8790 struct lkpi_cfg80211_get_bss_iter_lookup lookup;
8791 struct lkpi_hw *lhw;
8792 struct ieee80211vap *vap;
8793
8794 lhw = wiphy_priv(wiphy);
8795
8796 /* Let's hope we can alloc. */
8797 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
8798 if (lbss == NULL) {
8799 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
8800 return (NULL);
8801 }
8802
8803 lookup.wiphy = wiphy;
8804 lookup.chan = chan;
8805 lookup.bssid = bssid;
8806 lookup.ssid = ssid;
8807 lookup.ssid_len = ssid_len;
8808 lookup.bss_type = bss_type;
8809 lookup.privacy = privacy;
8810 lookup.match = false;
8811 lookup.bss = &lbss->bss;
8812
8813 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
8814 vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
8815 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
8816 if (!lookup.match) {
8817 free(lbss, M_LKPI80211);
8818 return (NULL);
8819 }
8820
8821 refcount_init(&lbss->refcnt, 1);
8822 return (&lbss->bss);
8823 }
8824
8825 void
8826 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
8827 {
8828 struct lkpi_cfg80211_bss *lbss;
8829
8830 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
8831
8832 /* Free everything again on refcount ... */
8833 if (refcount_release(&lbss->refcnt)) {
8834 free(lbss->bss.ies, M_LKPI80211);
8835 free(lbss, M_LKPI80211);
8836 }
8837 }
8838
8839 void
8840 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
8841 {
8842 struct lkpi_hw *lhw;
8843 struct ieee80211com *ic;
8844 struct ieee80211vap *vap;
8845
8846 lhw = wiphy_priv(wiphy);
8847 ic = lhw->ic;
8848
8849 /*
8850 * If we haven't called ieee80211_ifattach() yet
8851 * or there is no VAP, there are no scans to flush.
8852 */
8853 if (ic == NULL ||
8854 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
8855 return;
8856
8857 /* Should only happen on the current one? Not seen it late enough. */
8858 IEEE80211_LOCK(ic);
8859 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8860 ieee80211_scan_flush(vap);
8861 IEEE80211_UNLOCK(ic);
8862 }
8863
8864 /* -------------------------------------------------------------------------- */
8865
8866 /*
8867 * hw->conf get initialized/set in various places for us:
8868 * - linuxkpi_ieee80211_alloc_hw(): flags
8869 * - linuxkpi_ieee80211_ifattach(): chandef
8870 * - lkpi_ic_vap_create(): listen_interval
8871 * - lkpi_ic_set_channel(): chandef, flags
8872 */
8873
8874 int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8875 struct ieee80211_chanctx_conf *new)
8876 {
8877 struct cfg80211_chan_def *cd;
8878 uint32_t changed;
8879 int error;
8880
8881 changed = 0;
8882 if (new == NULL || new->def.chan == NULL)
8883 cd = NULL;
8884 else
8885 cd = &new->def;
8886
8887 if (cd && cd->chan != hw->conf.chandef.chan) {
8888 /* Copy; the chan pointer is fine and will stay valid. */
8889 hw->conf.chandef = *cd;
8890 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8891 }
8892 IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8893
8894 if (changed == 0)
8895 return (0);
8896
8897 error = lkpi_80211_mo_config(hw, changed);
8898 return (error);
8899 }
8900
8901 /* -------------------------------------------------------------------------- */
8902
8903 MODULE_VERSION(linuxkpi_wlan, 1);
8904 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
8905 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8906