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 if (vif->bss_conf.dtim_period != ni->ni_dtim_period &&
1894 ni->ni_dtim_period > 0) {
1895 vif->bss_conf.dtim_period = ni->ni_dtim_period;
1896 bss_changed |= BSS_CHANGED_BEACON_INFO;
1897 }
1898
1899 vif->bss_conf.sync_dtim_count = ni->ni_dtim_count;
1900 vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
1901 /* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
1902
1903 #ifdef LINUXKPI_DEBUG_80211
1904 if (linuxkpi_debug_80211 & D80211_TRACE)
1905 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
1906 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
1907 "sync_device_ts %u bss_changed %#010jx\n",
1908 __func__, __LINE__, _f, _l,
1909 vif->cfg.assoc, vif->cfg.aid,
1910 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
1911 vif->bss_conf.sync_dtim_count,
1912 (uintmax_t)vif->bss_conf.sync_tsf,
1913 vif->bss_conf.sync_device_ts,
1914 (uintmax_t)bss_changed);
1915 #endif
1916
1917 return (bss_changed);
1918 }
1919
1920 static void
lkpi_stop_hw_scan(struct lkpi_hw * lhw,struct ieee80211_vif * vif)1921 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
1922 {
1923 struct ieee80211_hw *hw;
1924 int error;
1925 bool cancel;
1926
1927 TRACE_SCAN(lhw->ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
1928
1929 LKPI_80211_LHW_SCAN_LOCK(lhw);
1930 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1931 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1932 if (!cancel)
1933 return;
1934
1935 hw = LHW_TO_HW(lhw);
1936
1937 IEEE80211_UNLOCK(lhw->ic);
1938 wiphy_lock(hw->wiphy);
1939 /* Need to cancel the scan. */
1940 lkpi_80211_mo_cancel_hw_scan(hw, vif);
1941 wiphy_unlock(hw->wiphy);
1942
1943 /* Need to make sure we see ieee80211_scan_completed. */
1944 LKPI_80211_LHW_SCAN_LOCK(lhw);
1945 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
1946 error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
1947 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
1948 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
1949
1950 IEEE80211_LOCK(lhw->ic);
1951
1952 if (cancel)
1953 ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
1954 __func__, error, lhw, vif);
1955 }
1956
1957 static void
lkpi_hw_conf_idle(struct ieee80211_hw * hw,bool new)1958 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
1959 {
1960 struct lkpi_hw *lhw;
1961 int error;
1962 bool old;
1963
1964 old = hw->conf.flags & IEEE80211_CONF_IDLE;
1965 if (old == new)
1966 return;
1967
1968 hw->conf.flags ^= IEEE80211_CONF_IDLE;
1969 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
1970 if (error != 0 && error != EOPNOTSUPP) {
1971 lhw = HW_TO_LHW(hw);
1972 ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
1973 __func__, IEEE80211_CONF_CHANGE_IDLE, error);
1974 }
1975 }
1976
1977 static enum ieee80211_bss_changed
lkpi_disassoc(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct lkpi_hw * lhw)1978 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1979 struct lkpi_hw *lhw)
1980 {
1981 enum ieee80211_bss_changed changed;
1982 struct lkpi_vif *lvif;
1983
1984 changed = 0;
1985 sta->aid = 0;
1986 if (vif->cfg.assoc) {
1987
1988 vif->cfg.assoc = false;
1989 vif->cfg.aid = 0;
1990 changed |= BSS_CHANGED_ASSOC;
1991 IMPROVE();
1992
1993 lkpi_update_mcast_filter(lhw->ic);
1994
1995 /*
1996 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
1997 * assoc = false right away here will remove the sta from
1998 * firmware for iwlwifi.
1999 * We no longer do this but only return the BSS_CHNAGED value.
2000 * The caller is responsible for removing the sta gong to
2001 * IEEE80211_STA_NOTEXIST and then executing the
2002 * bss_info_changed() update.
2003 * See lkpi_sta_run_to_init() for more detailed comment.
2004 */
2005
2006 lvif = VIF_TO_LVIF(vif);
2007 lvif->beacons = 0;
2008 }
2009
2010 return (changed);
2011 }
2012
2013 static void
lkpi_wake_tx_queues(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool dequeue_seen,bool no_emptyq)2014 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
2015 bool dequeue_seen, bool no_emptyq)
2016 {
2017 struct lkpi_txq *ltxq;
2018 int tid;
2019 bool ltxq_empty;
2020
2021 /* Wake up all queues to know they are allocated in the driver. */
2022 for (tid = 0; tid < nitems(sta->txq); tid++) {
2023
2024 if (tid == IEEE80211_NUM_TIDS) {
2025 IMPROVE("station specific?");
2026 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
2027 continue;
2028 } else if (tid >= hw->queues)
2029 continue;
2030
2031 if (sta->txq[tid] == NULL)
2032 continue;
2033
2034 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
2035 if (dequeue_seen && !ltxq->seen_dequeue)
2036 continue;
2037
2038 LKPI_80211_LTXQ_LOCK(ltxq);
2039 ltxq_empty = skb_queue_empty(<xq->skbq);
2040 LKPI_80211_LTXQ_UNLOCK(ltxq);
2041 if (no_emptyq && ltxq_empty)
2042 continue;
2043
2044 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
2045 }
2046 }
2047
2048 /*
2049 * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
2050 * packet. The problem is that the state machine functions tend to hold the
2051 * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
2052 * We call this after dropping the ic lock and before acquiring the LHW lock.
2053 * we make sure no further packets are queued and if they are queued the task
2054 * will finish or be cancelled. At the end if a packet is left we manually
2055 * send it. scan_to_auth() would re-enable sending if the lsta would be
2056 * re-used.
2057 */
2058 static void
lkpi_80211_flush_tx(struct lkpi_hw * lhw,struct lkpi_sta * lsta)2059 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
2060 {
2061 struct ieee80211_hw *hw;
2062 struct mbufq mq;
2063 struct mbuf *m;
2064 int len;
2065
2066 /* There is no lockdep_assert_not_held_wiphy(). */
2067 hw = LHW_TO_HW(lhw);
2068 lockdep_assert_not_held(&hw->wiphy->mtx);
2069
2070 /* Do not accept any new packets until scan_to_auth or lsta_free(). */
2071 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2072 lsta->txq_ready = false;
2073 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2074
2075 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
2076 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
2077
2078 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2079 len = mbufq_len(&lsta->txq);
2080 if (len <= 0) {
2081 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2082 return;
2083 }
2084
2085 mbufq_init(&mq, IFQ_MAXLEN);
2086 mbufq_concat(&mq, &lsta->txq);
2087 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2088
2089 m = mbufq_dequeue(&mq);
2090 while (m != NULL) {
2091 lkpi_80211_txq_tx_one(lsta, m);
2092 m = mbufq_dequeue(&mq);
2093 }
2094 }
2095
2096
2097 static void
lkpi_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2098 lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2099 {
2100 struct ieee80211_chanctx_conf *chanctx_conf;
2101 struct lkpi_chanctx *lchanctx;
2102
2103 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2104 lockdep_is_held(&hw->wiphy->mtx));
2105
2106 if (chanctx_conf == NULL)
2107 return;
2108
2109 /* Remove vif context. */
2110 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
2111
2112 lkpi_hw_conf_idle(hw, true);
2113
2114 /* Remove chan ctx. */
2115 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2116
2117 /* Cleanup. */
2118 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2119 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2120 list_del(&lchanctx->entry);
2121 free(lchanctx, M_LKPI80211);
2122 }
2123
2124
2125 /* -------------------------------------------------------------------------- */
2126
2127 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2128 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2129 {
2130
2131 return (0);
2132 }
2133
2134 /* lkpi_iv_newstate() handles the stop scan case generally. */
2135 #define lkpi_sta_scan_to_init(_v, _n, _a) lkpi_sta_state_do_nada(_v, _n, _a)
2136
2137 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2138 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2139 {
2140 struct linuxkpi_ieee80211_channel *chan;
2141 struct lkpi_chanctx *lchanctx;
2142 struct ieee80211_chanctx_conf *chanctx_conf;
2143 struct lkpi_hw *lhw;
2144 struct ieee80211_hw *hw;
2145 struct lkpi_vif *lvif;
2146 struct ieee80211_vif *vif;
2147 struct ieee80211_node *ni;
2148 struct lkpi_sta *lsta;
2149 enum ieee80211_bss_changed bss_changed;
2150 struct ieee80211_prep_tx_info prep_tx_info;
2151 uint32_t changed;
2152 int error;
2153 bool synched;
2154
2155 /*
2156 * In here we use vap->iv_bss until lvif->lvif_bss is set.
2157 * For all later (STATE >= AUTH) functions we need to use the lvif
2158 * cache which will be tracked even through (*iv_update_bss)().
2159 */
2160
2161 if (vap->iv_bss == NULL) {
2162 ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
2163 return (EINVAL);
2164 }
2165 /*
2166 * Keep the ni alive locally. In theory (and practice) iv_bss can change
2167 * once we unlock here. This is due to net80211 allowing state changes
2168 * and new join1() despite having an active node as well as due to
2169 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
2170 */
2171 ni = ieee80211_ref_node(vap->iv_bss);
2172 if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
2173 ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
2174 "on vap %p\n", __func__, ni, vap);
2175 ieee80211_free_node(ni); /* Error handling for the local ni. */
2176 return (EINVAL);
2177 }
2178
2179 lhw = vap->iv_ic->ic_softc;
2180 chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
2181 if (chan == NULL) {
2182 ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
2183 "iv_bss ni %p on vap %p\n", __func__, ni, vap);
2184 ieee80211_free_node(ni); /* Error handling for the local ni. */
2185 return (ESRCH);
2186 }
2187
2188 hw = LHW_TO_HW(lhw);
2189 lvif = VAP_TO_LVIF(vap);
2190 vif = LVIF_TO_VIF(lvif);
2191
2192 LKPI_80211_LVIF_LOCK(lvif);
2193 /* XXX-BZ KASSERT later? */
2194 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
2195 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2196 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2197 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2198 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2199 lvif->lvif_bss_synched);
2200 LKPI_80211_LVIF_UNLOCK(lvif);
2201 ieee80211_free_node(ni); /* Error handling for the local ni. */
2202 return (EBUSY);
2203 }
2204 LKPI_80211_LVIF_UNLOCK(lvif);
2205
2206 IEEE80211_UNLOCK(vap->iv_ic);
2207 wiphy_lock(hw->wiphy);
2208
2209 /* Add chanctx (or if exists, change it). */
2210 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2211 lockdep_is_held(&hw->wiphy->mtx));
2212 if (chanctx_conf != NULL) {
2213 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2214 IMPROVE("diff changes for changed, working on live copy, rcu");
2215 } else {
2216 /* Keep separate alloc as in Linux this is rcu managed? */
2217 lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
2218 M_LKPI80211, M_WAITOK | M_ZERO);
2219 chanctx_conf = &lchanctx->chanctx_conf;
2220 }
2221
2222 chanctx_conf->rx_chains_static = 1;
2223 chanctx_conf->rx_chains_dynamic = 1;
2224 chanctx_conf->radar_enabled =
2225 (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
2226 chanctx_conf->def.chan = chan;
2227 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
2228 chanctx_conf->def.center_freq1 = ieee80211_get_channel_center_freq1(ni->ni_chan);
2229 chanctx_conf->def.center_freq2 = ieee80211_get_channel_center_freq2(ni->ni_chan);
2230 IMPROVE("Check vht_cap from band not just chan?");
2231 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
2232 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
2233
2234 #ifdef LKPI_80211_HT
2235 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2236 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
2237 chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
2238 else
2239 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
2240 }
2241 #endif
2242 #ifdef LKPI_80211_VHT
2243 if (IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
2244 if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
2245 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
2246 else if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
2247 chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
2248 else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
2249 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
2250 }
2251 #endif
2252 chanctx_conf->rx_chains_dynamic = lkpi_get_max_rx_chains(ni);
2253 /* Responder ... */
2254 #if 0
2255 chanctx_conf->min_def.chan = chanctx_conf->def.chan;
2256 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
2257 #ifdef LKPI_80211_HT
2258 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) || IEEE80211_IS_CHAN_VHT(ni->ni_chan))
2259 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20;
2260 #endif
2261 chanctx_conf->min_def.center_freq1 = chanctx_conf->def.center_freq1;
2262 chanctx_conf->min_def.center_freq2 = chanctx_conf->def.center_freq2;
2263 #else
2264 chanctx_conf->min_def = chanctx_conf->def;
2265 #endif
2266
2267 /* Set bss info (bss_info_changed). */
2268 bss_changed = 0;
2269 vif->bss_conf.bssid = ni->ni_bssid;
2270 bss_changed |= BSS_CHANGED_BSSID;
2271 vif->bss_conf.txpower = ni->ni_txpower;
2272 bss_changed |= BSS_CHANGED_TXPOWER;
2273 vif->cfg.idle = false;
2274 bss_changed |= BSS_CHANGED_IDLE;
2275
2276 /* vif->bss_conf.basic_rates ? Where exactly? */
2277
2278 lvif->beacons = 0;
2279 /* Should almost assert it is this. */
2280 vif->cfg.assoc = false;
2281 vif->cfg.aid = 0;
2282
2283 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2284
2285 error = 0;
2286 if (vif->bss_conf.chanctx_conf == chanctx_conf) {
2287 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
2288 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
2289 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
2290 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2291 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
2292 } else {
2293 error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
2294 if (error == 0 || error == EOPNOTSUPP) {
2295 vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
2296 vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
2297 vif->bss_conf.chanreq.oper.center_freq1 =
2298 chanctx_conf->def.center_freq1;
2299 vif->bss_conf.chanreq.oper.center_freq2 =
2300 chanctx_conf->def.center_freq2;
2301 } else {
2302 ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
2303 "failed: %d\n", __func__, __LINE__, error);
2304 goto out;
2305 }
2306
2307 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2308 rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
2309
2310 /* Assign vif chanctx. */
2311 if (error == 0)
2312 error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2313 &vif->bss_conf, chanctx_conf);
2314 if (error == EOPNOTSUPP)
2315 error = 0;
2316 if (error != 0) {
2317 ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
2318 "failed: %d\n", __func__, __LINE__, error);
2319 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2320 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2321 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2322 list_del(&lchanctx->entry);
2323 free(lchanctx, M_LKPI80211);
2324 goto out;
2325 }
2326 }
2327 IMPROVE("update radiotap chan fields too");
2328
2329 /* RATES */
2330 IMPROVE("bss info: not all needs to come now and rates are missing");
2331 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2332
2333 /*
2334 * Given ni and lsta are 1:1 from alloc to free we can assert that
2335 * ni always has lsta data attach despite net80211 node swapping
2336 * under the hoods.
2337 */
2338 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
2339 __func__, ni, ni->ni_drv_data));
2340 lsta = ni->ni_drv_data;
2341
2342 /* Insert the [l]sta into the list of known stations. */
2343 list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2344
2345 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
2346 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2347 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
2348 "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2349 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2350 if (error != 0) {
2351 IMPROVE("do we need to undo the chan ctx?");
2352 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2353 "failed: %d\n", __func__, __LINE__, error);
2354 goto out;
2355 }
2356 #if 0
2357 lsta->added_to_drv = true; /* mo manages. */
2358 #endif
2359
2360 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2361
2362 #if 0
2363 /*
2364 * Wakeup all queues now that sta is there so we have as much time to
2365 * possibly prepare the queue in the driver to be ready for the 1st
2366 * packet; lkpi_80211_txq_tx_one() still has a workaround as there
2367 * is no guarantee or way to check.
2368 * XXX-BZ and by now we know that this does not work on all drivers
2369 * for all queues.
2370 */
2371 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
2372 #endif
2373
2374 /* Start mgd_prepare_tx. */
2375 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2376 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2377 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2378 lsta->in_mgd = true;
2379
2380 /*
2381 * What is going to happen next:
2382 * - <twiddle> .. we should end up in "auth_to_assoc"
2383 * - event_callback
2384 * - update sta_state (NONE to AUTH)
2385 * - mgd_complete_tx
2386 * (ideally we'd do that on a callback for something else ...)
2387 */
2388
2389 wiphy_unlock(hw->wiphy);
2390 IEEE80211_LOCK(vap->iv_ic);
2391
2392 LKPI_80211_LVIF_LOCK(lvif);
2393 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2394 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2395 lsta->ni != vap->iv_bss)
2396 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2397 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2398 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2399 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2400 lvif->lvif_bss_synched, ni, lsta);
2401
2402 /*
2403 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2404 * Given we cache lsta we use lsta->ni instead of ni here (even though
2405 * lsta->ni == ni) to be distinct from the rest of the code where we do
2406 * assume that ni == vap->iv_bss which it may or may not be.
2407 * So do NOT use iv_bss here anymore as that may have diverged from our
2408 * function local ni already while ic was unlocked and would lead to
2409 * inconsistencies. Go and see if we lost a race and do not update
2410 * lvif_bss_synched in that case.
2411 */
2412 ieee80211_ref_node(lsta->ni);
2413 lvif->lvif_bss = lsta;
2414 if (lsta->ni == vap->iv_bss) {
2415 lvif->lvif_bss_synched = synched = true;
2416 } else {
2417 /* Set to un-synched no matter what. */
2418 lvif->lvif_bss_synched = synched = false;
2419 /*
2420 * We do not error as someone has to take us down.
2421 * If we are followed by a 2nd, new net80211::join1() going to
2422 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2423 * will take the lvif->lvif_bss node down eventually.
2424 * What happens with the vap->iv_bss node will entirely be up
2425 * to net80211 as we never used the node beyond alloc()/free()
2426 * and we do not hold an extra reference for that anymore given
2427 * ni : lsta == 1:1.
2428 * Problem is if we do not error a MGMT/AUTH frame will be
2429 * sent from net80211::sta_newstate(); disable lsta queue below.
2430 */
2431 }
2432 LKPI_80211_LVIF_UNLOCK(lvif);
2433 /*
2434 * Make sure in case the sta did not change and we re-added it,
2435 * that we can tx again but only if the vif/iv_bss are in sync.
2436 * Otherwise this should prevent the MGMT/AUTH frame from being
2437 * sent triggering a warning in iwlwifi.
2438 */
2439 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2440 lsta->txq_ready = synched;
2441 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2442 goto out_relocked;
2443
2444 out:
2445 wiphy_unlock(hw->wiphy);
2446 IEEE80211_LOCK(vap->iv_ic);
2447 out_relocked:
2448 /*
2449 * Release the reference that kept the ni stable locally
2450 * during the work of this function.
2451 */
2452 if (ni != NULL)
2453 ieee80211_free_node(ni);
2454 return (error);
2455 }
2456
2457 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2458 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2459 {
2460 struct lkpi_hw *lhw;
2461 struct ieee80211_hw *hw;
2462 struct lkpi_vif *lvif;
2463 struct ieee80211_vif *vif;
2464 struct ieee80211_node *ni;
2465 struct lkpi_sta *lsta;
2466 struct ieee80211_sta *sta;
2467 struct ieee80211_prep_tx_info prep_tx_info;
2468 enum ieee80211_bss_changed bss_changed;
2469 int error;
2470
2471 lhw = vap->iv_ic->ic_softc;
2472 hw = LHW_TO_HW(lhw);
2473 lvif = VAP_TO_LVIF(vap);
2474 vif = LVIF_TO_VIF(lvif);
2475
2476 LKPI_80211_LVIF_LOCK(lvif);
2477 #ifdef LINUXKPI_DEBUG_80211
2478 /* XXX-BZ KASSERT later; state going down so no action. */
2479 if (lvif->lvif_bss == NULL)
2480 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2481 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2482 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2483 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2484 lvif->lvif_bss_synched);
2485 #endif
2486
2487 lsta = lvif->lvif_bss;
2488 LKPI_80211_LVIF_UNLOCK(lvif);
2489 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2490 "lvif %p vap %p\n", __func__,
2491 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2492 ni = lsta->ni; /* Reference held for lvif_bss. */
2493 sta = LSTA_TO_STA(lsta);
2494
2495 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2496
2497 IEEE80211_UNLOCK(vap->iv_ic);
2498 wiphy_lock(hw->wiphy);
2499
2500 /* flush, drop. */
2501 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2502
2503 /* Wake tx queues to get packet(s) out. */
2504 lkpi_wake_tx_queues(hw, sta, false, true);
2505
2506 /* flush, no drop */
2507 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2508
2509 /* End mgd_complete_tx. */
2510 if (lsta->in_mgd) {
2511 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2512 prep_tx_info.success = false;
2513 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2514 lsta->in_mgd = false;
2515 }
2516
2517 /* sync_rx_queues */
2518 lkpi_80211_mo_sync_rx_queues(hw);
2519
2520 /* sta_pre_rcu_remove */
2521 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2522
2523 /* Take the station down. */
2524
2525 /* Adjust sta and change state (from NONE) to NOTEXIST. */
2526 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2527 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2528 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2529 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2530 if (error != 0) {
2531 IMPROVE("do we need to undo the chan ctx?");
2532 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2533 "failed: %d\n", __func__, __LINE__, error);
2534 goto out;
2535 }
2536 #if 0
2537 lsta->added_to_drv = false; /* mo manages. */
2538 #endif
2539
2540 bss_changed = 0;
2541 vif->bss_conf.dtim_period = 0; /* go back to 0. */
2542 bss_changed |= BSS_CHANGED_BEACON_INFO;
2543 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2544
2545 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2546
2547 LKPI_80211_LVIF_LOCK(lvif);
2548 /* Remove ni reference for this cache of lsta. */
2549 lvif->lvif_bss = NULL;
2550 lvif->lvif_bss_synched = false;
2551 LKPI_80211_LVIF_UNLOCK(lvif);
2552 lkpi_lsta_remove(lsta, lvif);
2553 /*
2554 * The very last release the reference on the ni for the ni/lsta on
2555 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
2556 * and potentially freed.
2557 */
2558 ieee80211_free_node(ni);
2559
2560 /* conf_tx */
2561
2562 lkpi_remove_chanctx(hw, vif);
2563
2564 out:
2565 wiphy_unlock(hw->wiphy);
2566 IEEE80211_LOCK(vap->iv_ic);
2567 return (error);
2568 }
2569
2570 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2571 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2572 {
2573 int error;
2574
2575 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
2576 if (error == 0)
2577 error = lkpi_sta_scan_to_init(vap, nstate, arg);
2578 return (error);
2579 }
2580
2581 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2582 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2583 {
2584 struct lkpi_hw *lhw;
2585 struct ieee80211_hw *hw;
2586 struct lkpi_vif *lvif;
2587 struct ieee80211_vif *vif;
2588 struct lkpi_sta *lsta;
2589 struct ieee80211_prep_tx_info prep_tx_info;
2590 int error;
2591
2592 lhw = vap->iv_ic->ic_softc;
2593 hw = LHW_TO_HW(lhw);
2594 lvif = VAP_TO_LVIF(vap);
2595 vif = LVIF_TO_VIF(lvif);
2596
2597 IEEE80211_UNLOCK(vap->iv_ic);
2598 wiphy_lock(hw->wiphy);
2599
2600 LKPI_80211_LVIF_LOCK(lvif);
2601 /* XXX-BZ KASSERT later? */
2602 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2603 #ifdef LINUXKPI_DEBUG_80211
2604 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2605 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2606 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2607 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2608 lvif->lvif_bss_synched);
2609 #endif
2610 error = ENOTRECOVERABLE;
2611 LKPI_80211_LVIF_UNLOCK(lvif);
2612 goto out;
2613 }
2614 lsta = lvif->lvif_bss;
2615 LKPI_80211_LVIF_UNLOCK(lvif);
2616
2617 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
2618
2619 /* Finish auth. */
2620 IMPROVE("event callback");
2621
2622 /* Update sta_state (NONE to AUTH). */
2623 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2624 "NONE: %#x\n", __func__, lsta, lsta->state));
2625 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2626 if (error != 0) {
2627 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2628 "failed: %d\n", __func__, __LINE__, error);
2629 goto out;
2630 }
2631
2632 /* End mgd_complete_tx. */
2633 if (lsta->in_mgd) {
2634 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2635 prep_tx_info.success = true;
2636 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2637 lsta->in_mgd = false;
2638 }
2639
2640 /* Now start assoc. */
2641
2642 /* Start mgd_prepare_tx. */
2643 if (!lsta->in_mgd) {
2644 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2645 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2646 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2647 lsta->in_mgd = true;
2648 }
2649
2650 /* Wake tx queue to get packet out. */
2651 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
2652
2653 /*
2654 * <twiddle> .. we end up in "assoc_to_run"
2655 * - update sta_state (AUTH to ASSOC)
2656 * - conf_tx [all]
2657 * - bss_info_changed (assoc, aid, ssid, ..)
2658 * - change_chanctx (if needed)
2659 * - event_callback
2660 * - mgd_complete_tx
2661 */
2662
2663 out:
2664 wiphy_unlock(hw->wiphy);
2665 IEEE80211_LOCK(vap->iv_ic);
2666 return (error);
2667 }
2668
2669 /* auth_to_auth, assoc_to_assoc. */
2670 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2671 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2672 {
2673 struct lkpi_hw *lhw;
2674 struct ieee80211_hw *hw;
2675 struct lkpi_vif *lvif;
2676 struct ieee80211_vif *vif;
2677 struct lkpi_sta *lsta;
2678 struct ieee80211_prep_tx_info prep_tx_info;
2679 int error;
2680
2681 lhw = vap->iv_ic->ic_softc;
2682 hw = LHW_TO_HW(lhw);
2683 lvif = VAP_TO_LVIF(vap);
2684 vif = LVIF_TO_VIF(lvif);
2685
2686 IEEE80211_UNLOCK(vap->iv_ic);
2687 wiphy_lock(hw->wiphy);
2688
2689 LKPI_80211_LVIF_LOCK(lvif);
2690 /* XXX-BZ KASSERT later? */
2691 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2692 #ifdef LINUXKPI_DEBUG_80211
2693 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2694 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2695 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2696 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2697 lvif->lvif_bss_synched);
2698 #endif
2699 LKPI_80211_LVIF_UNLOCK(lvif);
2700 error = ENOTRECOVERABLE;
2701 goto out;
2702 }
2703 lsta = lvif->lvif_bss;
2704 LKPI_80211_LVIF_UNLOCK(lvif);
2705
2706 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
2707 lsta, lvif, vap));
2708
2709 IMPROVE("event callback?");
2710
2711 /* End mgd_complete_tx. */
2712 if (lsta->in_mgd) {
2713 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2714 prep_tx_info.success = false;
2715 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2716 lsta->in_mgd = false;
2717 }
2718
2719 /* Now start assoc. */
2720
2721 /* Start mgd_prepare_tx. */
2722 if (!lsta->in_mgd) {
2723 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2724 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2725 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2726 lsta->in_mgd = true;
2727 }
2728
2729 error = 0;
2730 out:
2731 wiphy_unlock(hw->wiphy);
2732 IEEE80211_LOCK(vap->iv_ic);
2733
2734 return (error);
2735 }
2736
2737 static int
_lkpi_sta_assoc_to_down(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2738 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2739 {
2740 struct lkpi_hw *lhw;
2741 struct ieee80211_hw *hw;
2742 struct lkpi_vif *lvif;
2743 struct ieee80211_vif *vif;
2744 struct ieee80211_node *ni;
2745 struct lkpi_sta *lsta;
2746 struct ieee80211_sta *sta;
2747 struct ieee80211_prep_tx_info prep_tx_info;
2748 enum ieee80211_bss_changed bss_changed;
2749 int error;
2750
2751 lhw = vap->iv_ic->ic_softc;
2752 hw = LHW_TO_HW(lhw);
2753 lvif = VAP_TO_LVIF(vap);
2754 vif = LVIF_TO_VIF(lvif);
2755
2756 IEEE80211_UNLOCK(vap->iv_ic);
2757 wiphy_lock(hw->wiphy);
2758
2759 LKPI_80211_LVIF_LOCK(lvif);
2760 #ifdef LINUXKPI_DEBUG_80211
2761 /* XXX-BZ KASSERT later; state going down so no action. */
2762 if (lvif->lvif_bss == NULL)
2763 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2764 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2765 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2766 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2767 lvif->lvif_bss_synched);
2768 #endif
2769 lsta = lvif->lvif_bss;
2770 LKPI_80211_LVIF_UNLOCK(lvif);
2771 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2772 "lvif %p vap %p\n", __func__,
2773 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2774
2775 ni = lsta->ni; /* Reference held for lvif_bss. */
2776 sta = LSTA_TO_STA(lsta);
2777
2778 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2779
2780 /* flush, drop. */
2781 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2782
2783 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2784 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2785 !lsta->in_mgd) {
2786 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2787 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2788 prep_tx_info.was_assoc = true;
2789 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2790 lsta->in_mgd = true;
2791 }
2792
2793 wiphy_unlock(hw->wiphy);
2794 IEEE80211_LOCK(vap->iv_ic);
2795
2796 /* Call iv_newstate first so we get potential DEAUTH packet out. */
2797 error = lvif->iv_newstate(vap, nstate, arg);
2798 if (error != 0) {
2799 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2800 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2801 goto outni;
2802 }
2803
2804 IEEE80211_UNLOCK(vap->iv_ic);
2805
2806 /* Ensure the packets get out. */
2807 lkpi_80211_flush_tx(lhw, lsta);
2808
2809 wiphy_lock(hw->wiphy);
2810
2811 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2812
2813 /* Wake tx queues to get packet(s) out. */
2814 lkpi_wake_tx_queues(hw, sta, false, true);
2815
2816 /* flush, no drop */
2817 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2818
2819 /* End mgd_complete_tx. */
2820 if (lsta->in_mgd) {
2821 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2822 prep_tx_info.success = false;
2823 prep_tx_info.was_assoc = true;
2824 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2825 lsta->in_mgd = false;
2826 }
2827
2828 /* sync_rx_queues */
2829 lkpi_80211_mo_sync_rx_queues(hw);
2830
2831 /* sta_pre_rcu_remove */
2832 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2833
2834 /* Take the station down. */
2835
2836 /* Update sta and change state (from AUTH) to NONE. */
2837 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2838 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2839 "AUTH: %#x\n", __func__, lsta, lsta->state));
2840 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2841 if (error != 0) {
2842 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2843 "failed: %d\n", __func__, __LINE__, error);
2844 goto out;
2845 }
2846
2847 /* See comment in lkpi_sta_run_to_init(). */
2848 bss_changed = 0;
2849 bss_changed |= lkpi_disassoc(sta, vif, lhw);
2850
2851 #ifdef LKPI_80211_HW_CRYPTO
2852 /*
2853 * In theory we remove keys here but there must not exist any for this
2854 * state change until we clean them up again into small steps and no
2855 * code duplication.
2856 */
2857 #endif
2858
2859 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2860
2861 /* Adjust sta and change state (from NONE) to NOTEXIST. */
2862 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2863 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2864 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2865 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2866 if (error != 0) {
2867 IMPROVE("do we need to undo the chan ctx?");
2868 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2869 "failed: %d\n", __func__, __LINE__, error);
2870 goto out;
2871 }
2872
2873 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
2874
2875 IMPROVE("Any bss_info changes to announce?");
2876 vif->bss_conf.qos = 0;
2877 bss_changed |= BSS_CHANGED_QOS;
2878 vif->cfg.ssid_len = 0;
2879 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2880 bss_changed |= BSS_CHANGED_BSSID;
2881 vif->bss_conf.dtim_period = 0; /* go back to 0. */
2882 bss_changed |= BSS_CHANGED_BEACON_INFO;
2883 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2884
2885 LKPI_80211_LVIF_LOCK(lvif);
2886 /* Remove ni reference for this cache of lsta. */
2887 lvif->lvif_bss = NULL;
2888 lvif->lvif_bss_synched = false;
2889 LKPI_80211_LVIF_UNLOCK(lvif);
2890 lkpi_lsta_remove(lsta, lvif);
2891 /*
2892 * The very last release the reference on the ni for the ni/lsta on
2893 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
2894 * and potentially freed.
2895 */
2896 ieee80211_free_node(ni);
2897
2898 /* conf_tx */
2899
2900 lkpi_remove_chanctx(hw, vif);
2901
2902 error = EALREADY;
2903 out:
2904 wiphy_unlock(hw->wiphy);
2905 IEEE80211_LOCK(vap->iv_ic);
2906 outni:
2907 return (error);
2908 }
2909
2910 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2911 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2912 {
2913 int error;
2914
2915 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2916 if (error != 0 && error != EALREADY)
2917 return (error);
2918
2919 /* At this point iv_bss is long a new node! */
2920
2921 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2922 return (error);
2923 }
2924
2925 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2926 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2927 {
2928 int error;
2929
2930 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2931 return (error);
2932 }
2933
2934 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2935 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2936 {
2937 int error;
2938
2939 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
2940 return (error);
2941 }
2942
2943 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2944 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2945 {
2946 struct lkpi_hw *lhw;
2947 struct ieee80211_hw *hw;
2948 struct lkpi_vif *lvif;
2949 struct ieee80211_vif *vif;
2950 struct ieee80211_node *ni;
2951 struct lkpi_sta *lsta;
2952 struct ieee80211_sta *sta;
2953 struct ieee80211_prep_tx_info prep_tx_info;
2954 enum ieee80211_bss_changed bss_changed;
2955 int error;
2956
2957 lhw = vap->iv_ic->ic_softc;
2958 hw = LHW_TO_HW(lhw);
2959 lvif = VAP_TO_LVIF(vap);
2960 vif = LVIF_TO_VIF(lvif);
2961
2962 IEEE80211_UNLOCK(vap->iv_ic);
2963 wiphy_lock(hw->wiphy);
2964
2965 LKPI_80211_LVIF_LOCK(lvif);
2966 /* XXX-BZ KASSERT later? */
2967 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
2968 #ifdef LINUXKPI_DEBUG_80211
2969 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2970 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2971 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2972 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2973 lvif->lvif_bss_synched);
2974 #endif
2975 LKPI_80211_LVIF_UNLOCK(lvif);
2976 error = ENOTRECOVERABLE;
2977 goto out;
2978 }
2979 lsta = lvif->lvif_bss;
2980 LKPI_80211_LVIF_UNLOCK(lvif);
2981 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2982 "lvif %p vap %p\n", __func__,
2983 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2984
2985 ni = lsta->ni; /* Reference held for lvif_bss. */
2986
2987 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
2988 "and to lesser extend ieee80211_notify_node_join");
2989
2990 /* Finish assoc. */
2991 /* Update sta_state (AUTH to ASSOC) and set aid. */
2992 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2993 "AUTH: %#x\n", __func__, lsta, lsta->state));
2994 sta = LSTA_TO_STA(lsta);
2995 sta->aid = IEEE80211_NODE_AID(ni);
2996 #ifdef LKPI_80211_WME
2997 if (vap->iv_flags & IEEE80211_F_WME)
2998 sta->wme = true;
2999 #endif
3000 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3001 if (error != 0) {
3002 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3003 "failed: %d\n", __func__, __LINE__, error);
3004 goto out;
3005 }
3006
3007 IMPROVE("wme / conf_tx [all]");
3008
3009 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3010 bss_changed = 0;
3011 #ifdef LKPI_80211_WME
3012 bss_changed |= lkpi_wme_update(lhw, vap, true);
3013 #endif
3014 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
3015 lvif->beacons = 0;
3016 vif->cfg.assoc = true;
3017 vif->cfg.aid = IEEE80211_NODE_AID(ni);
3018 bss_changed |= BSS_CHANGED_ASSOC;
3019 }
3020 /* We set SSID but this is not BSSID! */
3021 vif->cfg.ssid_len = ni->ni_esslen;
3022 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
3023 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
3024 vif->bss_conf.use_short_preamble) {
3025 vif->bss_conf.use_short_preamble ^= 1;
3026 /* bss_changed |= BSS_CHANGED_??? */
3027 }
3028 if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
3029 vif->bss_conf.use_short_slot) {
3030 vif->bss_conf.use_short_slot ^= 1;
3031 /* bss_changed |= BSS_CHANGED_??? */
3032 }
3033 if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
3034 vif->bss_conf.qos) {
3035 vif->bss_conf.qos ^= 1;
3036 bss_changed |= BSS_CHANGED_QOS;
3037 }
3038
3039 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3040 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3041
3042 /* - change_chanctx (if needed)
3043 * - event_callback
3044 */
3045
3046 /* End mgd_complete_tx. */
3047 if (lsta->in_mgd) {
3048 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3049 prep_tx_info.success = true;
3050 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3051 lsta->in_mgd = false;
3052 }
3053
3054 lkpi_hw_conf_idle(hw, false);
3055
3056 /*
3057 * And then:
3058 * - (more packets)?
3059 * - set_key
3060 * - set_default_unicast_key
3061 * - set_key (?)
3062 * - ipv6_addr_change (?)
3063 */
3064
3065 if (!ieee80211_node_is_authorized(ni)) {
3066 IMPROVE("net80211 does not consider node authorized");
3067 }
3068
3069 IMPROVE("Is this the right spot, has net80211 done all updates already?");
3070 lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
3071
3072 /* Update thresholds. */
3073 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
3074 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
3075 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
3076 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
3077
3078 /* Update sta_state (ASSOC to AUTHORIZED). */
3079 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3080 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3081 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3082 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
3083 if (error != 0) {
3084 IMPROVE("undo some changes?");
3085 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
3086 "failed: %d\n", __func__, __LINE__, error);
3087 goto out;
3088 }
3089
3090 /* - drv_config (?)
3091 * - bss_info_changed
3092 * - set_rekey_data (?)
3093 *
3094 * And now we should be passing packets.
3095 */
3096 IMPROVE("Need that bssid setting, and the keys");
3097
3098 bss_changed = 0;
3099 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3100 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3101
3102 /* Prepare_multicast && configure_filter. */
3103 lkpi_update_mcast_filter(vap->iv_ic);
3104
3105 out:
3106 wiphy_unlock(hw->wiphy);
3107 IEEE80211_LOCK(vap->iv_ic);
3108 return (error);
3109 }
3110
3111 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3112 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3113 {
3114 int error;
3115
3116 error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
3117 if (error == 0)
3118 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
3119 return (error);
3120 }
3121
3122 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3123 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3124 {
3125 struct lkpi_hw *lhw;
3126 struct ieee80211_hw *hw;
3127 struct lkpi_vif *lvif;
3128 struct ieee80211_vif *vif;
3129 struct ieee80211_node *ni;
3130 struct lkpi_sta *lsta;
3131 struct ieee80211_sta *sta;
3132 struct ieee80211_prep_tx_info prep_tx_info;
3133 #if 0
3134 enum ieee80211_bss_changed bss_changed;
3135 #endif
3136 int error;
3137
3138 lhw = vap->iv_ic->ic_softc;
3139 hw = LHW_TO_HW(lhw);
3140 lvif = VAP_TO_LVIF(vap);
3141 vif = LVIF_TO_VIF(lvif);
3142
3143 LKPI_80211_LVIF_LOCK(lvif);
3144 #ifdef LINUXKPI_DEBUG_80211
3145 /* XXX-BZ KASSERT later; state going down so no action. */
3146 if (lvif->lvif_bss == NULL)
3147 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3148 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3149 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3150 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3151 lvif->lvif_bss_synched);
3152 #endif
3153 lsta = lvif->lvif_bss;
3154 LKPI_80211_LVIF_UNLOCK(lvif);
3155 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3156 "lvif %p vap %p\n", __func__,
3157 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3158
3159 ni = lsta->ni; /* Reference held for lvif_bss. */
3160 sta = LSTA_TO_STA(lsta);
3161
3162 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3163
3164 IEEE80211_UNLOCK(vap->iv_ic);
3165 wiphy_lock(hw->wiphy);
3166
3167 /* flush, drop. */
3168 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3169
3170 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3171 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3172 !lsta->in_mgd) {
3173 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3174 prep_tx_info.duration = PREP_TX_INFO_DURATION;
3175 prep_tx_info.was_assoc = true;
3176 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3177 lsta->in_mgd = true;
3178 }
3179
3180 wiphy_unlock(hw->wiphy);
3181 IEEE80211_LOCK(vap->iv_ic);
3182
3183 /* Call iv_newstate first so we get potential DISASSOC packet out. */
3184 error = lvif->iv_newstate(vap, nstate, arg);
3185 if (error != 0) {
3186 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3187 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3188 goto outni;
3189 }
3190
3191 IEEE80211_UNLOCK(vap->iv_ic);
3192
3193 /* Ensure the packets get out. */
3194 lkpi_80211_flush_tx(lhw, lsta);
3195
3196 wiphy_lock(hw->wiphy);
3197
3198 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3199
3200 /* Wake tx queues to get packet(s) out. */
3201 lkpi_wake_tx_queues(hw, sta, false, true);
3202
3203 /* flush, no drop */
3204 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3205
3206 /* End mgd_complete_tx. */
3207 if (lsta->in_mgd) {
3208 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3209 prep_tx_info.success = false;
3210 prep_tx_info.was_assoc = true;
3211 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3212 lsta->in_mgd = false;
3213 }
3214
3215 #if 0
3216 /* sync_rx_queues */
3217 lkpi_80211_mo_sync_rx_queues(hw);
3218
3219 /* sta_pre_rcu_remove */
3220 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3221 #endif
3222
3223 /* Take the station down. */
3224
3225 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3226 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3227 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3228 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3229 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3230 if (error != 0) {
3231 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3232 "failed: %d\n", __func__, __LINE__, error);
3233 goto out;
3234 }
3235
3236 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3237
3238 #ifdef LKPI_80211_HW_CRYPTO
3239 if (lkpi_hwcrypto) {
3240 error = lkpi_sta_del_keys(hw, vif, lsta);
3241 if (error != 0) {
3242 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3243 "failed: %d\n", __func__, __LINE__, error);
3244 /*
3245 * Either drv/fw will crash or cleanup itself,
3246 * otherwise net80211 will delete the keys (at a
3247 * less appropriate time).
3248 */
3249 /* goto out; */
3250 }
3251 }
3252 #endif
3253
3254 /* Update sta_state (ASSOC to AUTH). */
3255 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3256 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3257 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3258 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3259 if (error != 0) {
3260 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3261 "failed: %d\n", __func__, __LINE__, error);
3262 goto out;
3263 }
3264
3265 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3266
3267 #if 0
3268 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3269 lkpi_disassoc(sta, vif, lhw);
3270 #endif
3271
3272 error = EALREADY;
3273 out:
3274 wiphy_unlock(hw->wiphy);
3275 IEEE80211_LOCK(vap->iv_ic);
3276 outni:
3277 return (error);
3278 }
3279
3280 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3281 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3282 {
3283 struct lkpi_hw *lhw;
3284 struct ieee80211_hw *hw;
3285 struct lkpi_vif *lvif;
3286 struct ieee80211_vif *vif;
3287 struct ieee80211_node *ni;
3288 struct lkpi_sta *lsta;
3289 struct ieee80211_sta *sta;
3290 struct ieee80211_prep_tx_info prep_tx_info;
3291 enum ieee80211_bss_changed bss_changed;
3292 int error;
3293
3294 lhw = vap->iv_ic->ic_softc;
3295 hw = LHW_TO_HW(lhw);
3296 lvif = VAP_TO_LVIF(vap);
3297 vif = LVIF_TO_VIF(lvif);
3298
3299 IEEE80211_UNLOCK(vap->iv_ic);
3300 wiphy_lock(hw->wiphy);
3301
3302 LKPI_80211_LVIF_LOCK(lvif);
3303 #ifdef LINUXKPI_DEBUG_80211
3304 /* XXX-BZ KASSERT later; state going down so no action. */
3305 if (lvif->lvif_bss == NULL)
3306 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3307 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3308 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3309 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3310 lvif->lvif_bss_synched);
3311 #endif
3312 lsta = lvif->lvif_bss;
3313 LKPI_80211_LVIF_UNLOCK(lvif);
3314 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3315 "lvif %p vap %p\n", __func__,
3316 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3317
3318 ni = lsta->ni; /* Reference held for lvif_bss. */
3319 sta = LSTA_TO_STA(lsta);
3320
3321 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3322
3323 /* flush, drop. */
3324 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3325
3326 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
3327 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
3328 !lsta->in_mgd) {
3329 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3330 prep_tx_info.duration = PREP_TX_INFO_DURATION;
3331 prep_tx_info.was_assoc = true;
3332 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3333 lsta->in_mgd = true;
3334 }
3335
3336 wiphy_unlock(hw->wiphy);
3337 IEEE80211_LOCK(vap->iv_ic);
3338
3339 /* Call iv_newstate first so we get potential DISASSOC packet out. */
3340 error = lvif->iv_newstate(vap, nstate, arg);
3341 if (error != 0) {
3342 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3343 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3344 goto outni;
3345 }
3346
3347 IEEE80211_UNLOCK(vap->iv_ic);
3348
3349 /* Ensure the packets get out. */
3350 lkpi_80211_flush_tx(lhw, lsta);
3351
3352 wiphy_lock(hw->wiphy);
3353
3354 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3355
3356 /* Wake tx queues to get packet(s) out. */
3357 lkpi_wake_tx_queues(hw, sta, false, true);
3358
3359 /* flush, no drop */
3360 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3361
3362 /* End mgd_complete_tx. */
3363 if (lsta->in_mgd) {
3364 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3365 prep_tx_info.success = false;
3366 prep_tx_info.was_assoc = true;
3367 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3368 lsta->in_mgd = false;
3369 }
3370
3371 /* sync_rx_queues */
3372 lkpi_80211_mo_sync_rx_queues(hw);
3373
3374 /* sta_pre_rcu_remove */
3375 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3376
3377 /* Take the station down. */
3378
3379 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3380 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3381 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3382 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3383 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3384 if (error != 0) {
3385 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3386 "failed: %d\n", __func__, __LINE__, error);
3387 goto out;
3388 }
3389
3390 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3391
3392 #ifdef LKPI_80211_HW_CRYPTO
3393 if (lkpi_hwcrypto) {
3394 /*
3395 * In theory we only need to do this if we changed assoc.
3396 * If we were not assoc, there should be no keys and we
3397 * should not be here.
3398 */
3399 #ifdef notyet
3400 KASSERT((bss_changed & BSS_CHANGED_ASSOC) != 0, ("%s: "
3401 "trying to remove keys but were not assoc: %#010jx, lvif %p\n",
3402 __func__, (uintmax_t)bss_changed, lvif));
3403 #endif
3404 error = lkpi_sta_del_keys(hw, vif, lsta);
3405 if (error != 0) {
3406 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3407 "failed: %d\n", __func__, __LINE__, error);
3408 /*
3409 * Either drv/fw will crash or cleanup itself,
3410 * otherwise net80211 will delete the keys (at a
3411 * less appropriate time).
3412 */
3413 /* goto out; */
3414 }
3415 }
3416 #endif
3417
3418 /* Update sta_state (ASSOC to AUTH). */
3419 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3420 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3421 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3422 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3423 if (error != 0) {
3424 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3425 "failed: %d\n", __func__, __LINE__, error);
3426 goto out;
3427 }
3428
3429 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3430
3431 /* Update sta and change state (from AUTH) to NONE. */
3432 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3433 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3434 "AUTH: %#x\n", __func__, lsta, lsta->state));
3435 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3436 if (error != 0) {
3437 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3438 "failed: %d\n", __func__, __LINE__, error);
3439 goto out;
3440 }
3441
3442 bss_changed = 0;
3443 /*
3444 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
3445 *
3446 * One would expect this to happen when going off AUTHORIZED.
3447 * See comment there; removes the sta from fw if not careful
3448 * (bss_info_changed() change is executed right away).
3449 *
3450 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
3451 * as otherwise drivers (iwlwifi at least) will silently not remove
3452 * the sta from the firmware and when we will add a new one trigger
3453 * a fw assert.
3454 *
3455 * The order which works best so far avoiding early removal or silent
3456 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
3457 * the iwlwifi:mac80211.c case still to be tested):
3458 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
3459 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
3460 * (removes the sta given assoc is false)
3461 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
3462 * 4) call unassign_vif_chanctx
3463 * 5) call lkpi_hw_conf_idle
3464 * 6) call remove_chanctx
3465 *
3466 * Note: vif->driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC
3467 * might change this.
3468 */
3469 bss_changed |= lkpi_disassoc(sta, vif, lhw);
3470
3471 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3472
3473 /* Adjust sta and change state (from NONE) to NOTEXIST. */
3474 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3475 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3476 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3477 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3478 if (error != 0) {
3479 IMPROVE("do we need to undo the chan ctx?");
3480 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3481 "failed: %d\n", __func__, __LINE__, error);
3482 goto out;
3483 }
3484
3485 lkpi_lsta_remove(lsta, lvif);
3486
3487 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
3488
3489 IMPROVE("Any bss_info changes to announce?");
3490 vif->bss_conf.qos = 0;
3491 bss_changed |= BSS_CHANGED_QOS;
3492 vif->cfg.ssid_len = 0;
3493 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3494 bss_changed |= BSS_CHANGED_BSSID;
3495 vif->bss_conf.use_short_preamble = false;
3496 vif->bss_conf.qos = false;
3497 /* XXX BSS_CHANGED_???? */
3498 vif->bss_conf.dtim_period = 0; /* go back to 0. */
3499 bss_changed |= BSS_CHANGED_BEACON_INFO;
3500 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3501
3502 LKPI_80211_LVIF_LOCK(lvif);
3503 /* Remove ni reference for this cache of lsta. */
3504 lvif->lvif_bss = NULL;
3505 lvif->lvif_bss_synched = false;
3506 LKPI_80211_LVIF_UNLOCK(lvif);
3507 /*
3508 * The very last release the reference on the ni for the ni/lsta on
3509 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
3510 * and potentially freed.
3511 */
3512 ieee80211_free_node(ni);
3513
3514 /* conf_tx */
3515
3516 lkpi_remove_chanctx(hw, vif);
3517
3518 error = EALREADY;
3519 out:
3520 wiphy_unlock(hw->wiphy);
3521 IEEE80211_LOCK(vap->iv_ic);
3522 outni:
3523 return (error);
3524 }
3525
3526 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3527 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3528 {
3529
3530 return (lkpi_sta_run_to_init(vap, nstate, arg));
3531 }
3532
3533 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3534 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3535 {
3536 int error;
3537
3538 error = lkpi_sta_run_to_init(vap, nstate, arg);
3539 if (error != 0 && error != EALREADY)
3540 return (error);
3541
3542 /* At this point iv_bss is long a new node! */
3543
3544 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
3545 return (error);
3546 }
3547
3548 /* -------------------------------------------------------------------------- */
3549
3550 /*
3551 * The matches the documented state changes in net80211::sta_newstate().
3552 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
3553 * there are "invalid" (so there is a room for failure here).
3554 */
3555 struct fsm_state {
3556 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
3557 enum ieee80211_state ostate;
3558 enum ieee80211_state nstate;
3559 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
3560 } sta_state_fsm[] = {
3561 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada },
3562 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* scan_to_init */
3563 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */
3564 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */
3565 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */
3566
3567 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3568 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3569 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
3570 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3571 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */
3572
3573 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3574 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3575 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */
3576 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* Send ?AUTH. */
3577 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */
3578
3579 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* Send ASSOCREQ. */
3580 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */
3581 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* Send ASSOCREQ/REASSOCREQ. */
3582
3583 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run },
3584 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
3585 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada },
3586
3587 /* Dummy at the end without handler. */
3588 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL },
3589 };
3590
3591 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3592 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3593 {
3594 struct ieee80211com *ic;
3595 struct lkpi_hw *lhw;
3596 struct lkpi_vif *lvif;
3597 struct ieee80211_vif *vif;
3598 struct fsm_state *s;
3599 enum ieee80211_state ostate;
3600 int error;
3601
3602 ic = vap->iv_ic;
3603 IEEE80211_LOCK_ASSERT(ic);
3604 ostate = vap->iv_state;
3605
3606 #ifdef LINUXKPI_DEBUG_80211
3607 if (linuxkpi_debug_80211 & D80211_TRACE)
3608 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
3609 __func__, __LINE__, vap, nstate, arg);
3610 #endif
3611
3612 if (vap->iv_opmode == IEEE80211_M_STA) {
3613
3614 lhw = ic->ic_softc;
3615 lvif = VAP_TO_LVIF(vap);
3616 vif = LVIF_TO_VIF(lvif);
3617
3618 /* No need to replicate this in most state handlers. */
3619 if (nstate > IEEE80211_S_SCAN)
3620 lkpi_stop_hw_scan(lhw, vif);
3621
3622 s = sta_state_fsm;
3623
3624 } else {
3625 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
3626 "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
3627 return (ENOSYS);
3628 }
3629
3630 error = 0;
3631 for (; s->handler != NULL; s++) {
3632 if (ostate == s->ostate && nstate == s->nstate) {
3633 #ifdef LINUXKPI_DEBUG_80211
3634 if (linuxkpi_debug_80211 & D80211_TRACE)
3635 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
3636 " %d (%s): arg %d.\n", __func__,
3637 ostate, ieee80211_state_name[ostate],
3638 nstate, ieee80211_state_name[nstate], arg);
3639 #endif
3640 error = s->handler(vap, nstate, arg);
3641 break;
3642 }
3643 }
3644 IEEE80211_LOCK_ASSERT(vap->iv_ic);
3645
3646 if (s->handler == NULL) {
3647 IMPROVE("turn this into a KASSERT\n");
3648 ic_printf(vap->iv_ic, "%s: unsupported state transition "
3649 "%d (%s) -> %d (%s)\n", __func__,
3650 ostate, ieee80211_state_name[ostate],
3651 nstate, ieee80211_state_name[nstate]);
3652 return (ENOSYS);
3653 }
3654
3655 if (error == EALREADY) {
3656 #ifdef LINUXKPI_DEBUG_80211
3657 if (linuxkpi_debug_80211 & D80211_TRACE)
3658 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
3659 "%d (%s): iv_newstate already handled: %d.\n",
3660 __func__, ostate, ieee80211_state_name[ostate],
3661 nstate, ieee80211_state_name[nstate], error);
3662 #endif
3663 return (0);
3664 }
3665
3666 if (error != 0) {
3667 ic_printf(vap->iv_ic, "%s: error %d during state transition "
3668 "%d (%s) -> %d (%s)\n", __func__, error,
3669 ostate, ieee80211_state_name[ostate],
3670 nstate, ieee80211_state_name[nstate]);
3671 return (error);
3672 }
3673
3674 #ifdef LINUXKPI_DEBUG_80211
3675 if (linuxkpi_debug_80211 & D80211_TRACE)
3676 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
3677 "calling net80211 parent\n",
3678 __func__, __LINE__, vap, nstate, arg);
3679 #endif
3680
3681 return (lvif->iv_newstate(vap, nstate, arg));
3682 }
3683
3684 /* -------------------------------------------------------------------------- */
3685
3686 /*
3687 * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
3688 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
3689 * new node without us knowing and thus our ni/lsta are out of sync.
3690 */
3691 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)3692 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
3693 {
3694 struct lkpi_vif *lvif;
3695 struct ieee80211_node *rni;
3696
3697 IEEE80211_LOCK_ASSERT(vap->iv_ic);
3698
3699 lvif = VAP_TO_LVIF(vap);
3700
3701 LKPI_80211_LVIF_LOCK(lvif);
3702 lvif->lvif_bss_synched = false;
3703 LKPI_80211_LVIF_UNLOCK(lvif);
3704
3705 rni = lvif->iv_update_bss(vap, ni);
3706 return (rni);
3707 }
3708
3709 #ifdef LKPI_80211_WME
3710 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)3711 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
3712 {
3713 struct ieee80211com *ic;
3714 struct ieee80211_hw *hw;
3715 struct lkpi_vif *lvif;
3716 struct ieee80211_vif *vif;
3717 struct chanAccParams chp;
3718 struct wmeParams wmeparr[WME_NUM_AC];
3719 struct ieee80211_tx_queue_params txqp;
3720 enum ieee80211_bss_changed changed;
3721 int error;
3722 uint16_t ac;
3723
3724 hw = LHW_TO_HW(lhw);
3725 lockdep_assert_wiphy(hw->wiphy);
3726
3727 IMPROVE();
3728 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
3729 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
3730
3731 if (vap == NULL)
3732 return (0);
3733
3734 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
3735 return (0);
3736
3737 if (lhw->ops->conf_tx == NULL)
3738 return (0);
3739
3740 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
3741 lhw->update_wme = true;
3742 return (0);
3743 }
3744 lhw->update_wme = false;
3745
3746 ic = lhw->ic;
3747 ieee80211_wme_ic_getparams(ic, &chp);
3748 IEEE80211_LOCK(ic);
3749 for (ac = 0; ac < WME_NUM_AC; ac++)
3750 wmeparr[ac] = chp.cap_wmeParams[ac];
3751 IEEE80211_UNLOCK(ic);
3752
3753 lvif = VAP_TO_LVIF(vap);
3754 vif = LVIF_TO_VIF(lvif);
3755
3756 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
3757 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3758 struct wmeParams *wmep;
3759
3760 wmep = &wmeparr[ac];
3761 bzero(&txqp, sizeof(txqp));
3762 txqp.cw_min = wmep->wmep_logcwmin;
3763 txqp.cw_max = wmep->wmep_logcwmax;
3764 txqp.txop = wmep->wmep_txopLimit;
3765 txqp.aifs = wmep->wmep_aifsn;
3766 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
3767 if (error != 0)
3768 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
3769 __func__, ac, error);
3770 }
3771 changed = BSS_CHANGED_QOS;
3772 if (!planned)
3773 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
3774
3775 return (changed);
3776 }
3777 #endif
3778
3779 static int
lkpi_ic_wme_update(struct ieee80211com * ic)3780 lkpi_ic_wme_update(struct ieee80211com *ic)
3781 {
3782 #ifdef LKPI_80211_WME
3783 struct ieee80211vap *vap;
3784 struct lkpi_hw *lhw;
3785 struct ieee80211_hw *hw;
3786
3787 IMPROVE("Use the per-VAP callback in net80211.");
3788 vap = TAILQ_FIRST(&ic->ic_vaps);
3789 if (vap == NULL)
3790 return (0);
3791
3792 lhw = ic->ic_softc;
3793 hw = LHW_TO_HW(lhw);
3794
3795 wiphy_lock(hw->wiphy);
3796 lkpi_wme_update(lhw, vap, false);
3797 wiphy_unlock(hw->wiphy);
3798 #endif
3799 return (0); /* unused */
3800 }
3801
3802 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)3803 lkpi_iv_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
3804 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
3805 {
3806 struct lkpi_hw *lhw;
3807 struct ieee80211_hw *hw;
3808 struct lkpi_vif *lvif;
3809 struct ieee80211_vif *vif;
3810 enum ieee80211_bss_changed bss_changed;
3811
3812 lvif = VAP_TO_LVIF(ni->ni_vap);
3813
3814 lvif->iv_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
3815
3816 switch (subtype) {
3817 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3818 break;
3819 case IEEE80211_FC0_SUBTYPE_BEACON:
3820 lvif->beacons++;
3821 break;
3822 default:
3823 return;
3824 }
3825
3826 vif = LVIF_TO_VIF(lvif);
3827 lhw = ni->ni_ic->ic_softc;
3828 hw = LHW_TO_HW(lhw);
3829
3830 /*
3831 * If this direct call to mo_bss_info_changed will not work due to
3832 * locking, see if queue_work() is fast enough.
3833 */
3834 bss_changed = lkpi_update_dtim_tsf(vif, ni, ni->ni_vap, __func__, __LINE__);
3835 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
3836 }
3837
3838 /*
3839 * Change link-layer address on the vif (if the vap is not started/"UP").
3840 * This can happen if a user changes 'ether' using ifconfig.
3841 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
3842 * we do use a per-[l]vif event handler to be sure we exist as we
3843 * cannot assume that from every vap derives a vif and we have a hard
3844 * time checking based on net80211 information.
3845 * Should this ever become a real problem we could add a callback function
3846 * to wlan_iflladdr() to be set optionally but that would be for a
3847 * single-consumer (or needs a list) -- was just too complicated for an
3848 * otherwise perfect mechanism FreeBSD already provides.
3849 */
3850 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)3851 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
3852 {
3853 struct epoch_tracker et;
3854 struct ieee80211_vif *vif;
3855
3856 NET_EPOCH_ENTER(et);
3857 /* NB: identify vap's by if_transmit; left as an extra check. */
3858 if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
3859 (if_getflags(ifp) & IFF_UP) != 0) {
3860 NET_EPOCH_EXIT(et);
3861 return;
3862 }
3863
3864 vif = arg;
3865 IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
3866 NET_EPOCH_EXIT(et);
3867 }
3868
3869 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])3870 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
3871 int unit, enum ieee80211_opmode opmode, int flags,
3872 const uint8_t bssid[IEEE80211_ADDR_LEN],
3873 const uint8_t mac[IEEE80211_ADDR_LEN])
3874 {
3875 struct lkpi_hw *lhw;
3876 struct ieee80211_hw *hw;
3877 struct lkpi_vif *lvif;
3878 struct ieee80211vap *vap;
3879 struct ieee80211_vif *vif;
3880 struct ieee80211_tx_queue_params txqp;
3881 enum ieee80211_bss_changed changed;
3882 struct sysctl_oid *node;
3883 size_t len;
3884 int error, i;
3885 uint16_t ac;
3886
3887 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
3888 return (NULL);
3889
3890 lhw = ic->ic_softc;
3891 hw = LHW_TO_HW(lhw);
3892
3893 len = sizeof(*lvif);
3894 len += hw->vif_data_size; /* vif->drv_priv */
3895
3896 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
3897 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
3898 TASK_INIT(&lvif->sw_scan_task, 0, lkpi_sw_scan_task, lvif);
3899 INIT_LIST_HEAD(&lvif->lsta_list);
3900 lvif->lvif_bss = NULL;
3901 refcount_init(&lvif->nt_unlocked, 0);
3902 lvif->lvif_bss_synched = false;
3903 vap = LVIF_TO_VAP(lvif);
3904
3905 vif = LVIF_TO_VIF(lvif);
3906 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
3907 vif->p2p = false;
3908 vif->probe_req_reg = false;
3909 vif->type = lkpi_opmode_to_vif_type(opmode);
3910 lvif->wdev.iftype = vif->type;
3911 /* Need to fill in other fields as well. */
3912 IMPROVE();
3913
3914 /* XXX-BZ hardcoded for now! */
3915 #if 1
3916 RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
3917 vif->bss_conf.vif = vif;
3918 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
3919 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
3920 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
3921 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
3922 vif->bss_conf.link_id = 0; /* Non-MLO operation. */
3923 vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
3924 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
3925 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */
3926 vif->bss_conf.qos = false;
3927 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */
3928 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
3929 vif->cfg.aid = 0;
3930 vif->cfg.assoc = false;
3931 vif->cfg.idle = true;
3932 vif->cfg.ps = false;
3933 IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
3934 /*
3935 * We need to initialize it to something as the bss_info_changed call
3936 * will try to copy from it in iwlwifi and NULL is a panic.
3937 * We will set the proper one in scan_to_auth() before being assoc.
3938 */
3939 vif->bss_conf.bssid = ieee80211broadcastaddr;
3940 #endif
3941 #if 0
3942 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
3943 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
3944 vif->bss_conf.beacon_int = ic->ic_bintval;
3945 /* iwlwifi bug. */
3946 if (vif->bss_conf.beacon_int < 16)
3947 vif->bss_conf.beacon_int = 16;
3948 #endif
3949
3950 /* Link Config */
3951 vif->link_conf[0] = &vif->bss_conf;
3952 for (i = 0; i < nitems(vif->link_conf); i++) {
3953 IMPROVE("more than 1 link one day");
3954 }
3955
3956 /* Setup queue defaults; driver may override in (*add_interface). */
3957 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3958 if (ieee80211_hw_check(hw, QUEUE_CONTROL))
3959 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
3960 else if (hw->queues >= IEEE80211_NUM_ACS)
3961 vif->hw_queue[i] = i;
3962 else
3963 vif->hw_queue[i] = 0;
3964
3965 /* Initialize the queue to running. Stopped? */
3966 lvif->hw_queue_stopped[i] = false;
3967 }
3968 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
3969
3970 IMPROVE();
3971
3972 wiphy_lock(hw->wiphy);
3973 error = lkpi_80211_mo_start(hw);
3974 if (error != 0) {
3975 wiphy_unlock(hw->wiphy);
3976 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
3977 mtx_destroy(&lvif->mtx);
3978 free(lvif, M_80211_VAP);
3979 return (NULL);
3980 }
3981
3982 error = lkpi_80211_mo_add_interface(hw, vif);
3983 if (error != 0) {
3984 IMPROVE(); /* XXX-BZ mo_stop()? */
3985 wiphy_unlock(hw->wiphy);
3986 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
3987 mtx_destroy(&lvif->mtx);
3988 free(lvif, M_80211_VAP);
3989 return (NULL);
3990 }
3991 wiphy_unlock(hw->wiphy);
3992
3993 LKPI_80211_LHW_LVIF_LOCK(lhw);
3994 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
3995 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3996
3997 /* Set bss_info. */
3998 changed = 0;
3999 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
4000
4001 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
4002 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
4003 wiphy_lock(hw->wiphy);
4004 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4005
4006 bzero(&txqp, sizeof(txqp));
4007 txqp.cw_min = 15;
4008 txqp.cw_max = 1023;
4009 txqp.txop = 0;
4010 txqp.aifs = 2;
4011 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4012 if (error != 0)
4013 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4014 __func__, ac, error);
4015 }
4016 wiphy_unlock(hw->wiphy);
4017 changed = BSS_CHANGED_QOS;
4018 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
4019
4020 /* Force MC init. */
4021 lkpi_update_mcast_filter(ic);
4022
4023 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
4024
4025 /* Now we have a valid vap->iv_ifp. Any checksum offloading goes below. */
4026
4027 IMPROVE();
4028
4029 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
4030 lvif->iv_newstate = vap->iv_newstate;
4031 vap->iv_newstate = lkpi_iv_newstate;
4032 lvif->iv_update_bss = vap->iv_update_bss;
4033 vap->iv_update_bss = lkpi_iv_update_bss;
4034 lvif->iv_recv_mgmt = vap->iv_recv_mgmt;
4035 vap->iv_recv_mgmt = lkpi_iv_sta_recv_mgmt;
4036
4037 #ifdef LKPI_80211_HW_CRYPTO
4038 /* Key management. */
4039 if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
4040 vap->iv_key_set = lkpi_iv_key_set;
4041 vap->iv_key_delete = lkpi_iv_key_delete;
4042 vap->iv_key_update_begin = lkpi_iv_key_update_begin;
4043 vap->iv_key_update_end = lkpi_iv_key_update_end;
4044 }
4045 #endif
4046
4047 #ifdef LKPI_80211_HT
4048 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
4049 #endif
4050
4051 ieee80211_ratectl_init(vap);
4052
4053 /* Complete setup. */
4054 ieee80211_vap_attach(vap, ieee80211_media_change,
4055 ieee80211_media_status, mac);
4056
4057 #ifdef LKPI_80211_HT
4058 /*
4059 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
4060 * to do so if they cannot do the crypto too.
4061 */
4062 if (!lkpi_hwcrypto && ieee80211_hw_check(hw, AMPDU_AGGREGATION))
4063 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
4064 #endif
4065 #if defined(LKPI_80211_HT)
4066 /* 20250125-BZ Keep A-MPDU TX cleared until we sorted out AddBA for all drivers. */
4067 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
4068 #endif
4069
4070 if (hw->max_listen_interval == 0)
4071 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
4072 hw->conf.listen_interval = hw->max_listen_interval;
4073 ic->ic_set_channel(ic);
4074
4075 /* XXX-BZ do we need to be able to update these? */
4076 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
4077 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
4078 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
4079 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
4080 /* any others? */
4081
4082 /* Add per-VIF/VAP sysctls. */
4083 sysctl_ctx_init(&lvif->sysctl_ctx);
4084
4085 node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
4086 SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
4087 OID_AUTO, if_name(vap->iv_ifp),
4088 CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
4089
4090 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4091 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
4092 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
4093 lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
4094
4095 IMPROVE();
4096
4097 return (vap);
4098 }
4099
4100 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)4101 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
4102 {
4103
4104 wiphy_unregister(hw->wiphy);
4105 linuxkpi_ieee80211_ifdetach(hw);
4106
4107 IMPROVE();
4108 }
4109
4110 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)4111 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
4112 {
4113
4114 TODO();
4115 }
4116
4117 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)4118 lkpi_ic_vap_delete(struct ieee80211vap *vap)
4119 {
4120 struct ieee80211com *ic;
4121 struct lkpi_hw *lhw;
4122 struct ieee80211_hw *hw;
4123 struct lkpi_vif *lvif;
4124 struct ieee80211_vif *vif;
4125
4126 lvif = VAP_TO_LVIF(vap);
4127 vif = LVIF_TO_VIF(lvif);
4128 ic = vap->iv_ic;
4129 lhw = ic->ic_softc;
4130 hw = LHW_TO_HW(lhw);
4131
4132 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
4133
4134 /* Clear up per-VIF/VAP sysctls. */
4135 sysctl_ctx_free(&lvif->sysctl_ctx);
4136
4137 ieee80211_draintask(ic, &lvif->sw_scan_task);
4138
4139 LKPI_80211_LHW_LVIF_LOCK(lhw);
4140 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
4141 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4142
4143 ieee80211_ratectl_deinit(vap);
4144 ieee80211_vap_detach(vap);
4145
4146 IMPROVE("clear up other bits in this state");
4147
4148 lkpi_80211_mo_remove_interface(hw, vif);
4149
4150 /* Single VAP, so we can do this here. */
4151 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4152
4153 mtx_destroy(&lvif->mtx);
4154 free(lvif, M_80211_VAP);
4155 }
4156
4157 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)4158 lkpi_ic_update_mcast(struct ieee80211com *ic)
4159 {
4160 struct ieee80211vap *vap;
4161 struct lkpi_hw *lhw;
4162
4163 lhw = ic->ic_softc;
4164 if (lhw->ops->prepare_multicast == NULL ||
4165 lhw->ops->configure_filter == NULL)
4166 return;
4167
4168 LKPI_80211_LHW_MC_LOCK(lhw);
4169 /* Cleanup anything on the current list. */
4170 lkpi_cleanup_mcast_list_locked(lhw);
4171
4172 /* Build up the new list (or allmulti). */
4173 if (ic->ic_allmulti == 0) {
4174 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
4175 if_foreach_llmaddr(vap->iv_ifp,
4176 lkpi_ic_update_mcast_copy, &lhw->mc_list);
4177 lhw->mc_all_multi = false;
4178 } else {
4179 lhw->mc_all_multi = true;
4180 }
4181 LKPI_80211_LHW_MC_UNLOCK(lhw);
4182
4183 lkpi_update_mcast_filter(ic);
4184 TRACEOK();
4185 }
4186
4187 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)4188 lkpi_ic_update_promisc(struct ieee80211com *ic)
4189 {
4190
4191 UNIMPLEMENTED;
4192 }
4193
4194 static void
lkpi_ic_update_chw(struct ieee80211com * ic)4195 lkpi_ic_update_chw(struct ieee80211com *ic)
4196 {
4197
4198 UNIMPLEMENTED;
4199 }
4200
4201 /* Start / stop device. */
4202 static void
lkpi_ic_parent(struct ieee80211com * ic)4203 lkpi_ic_parent(struct ieee80211com *ic)
4204 {
4205 struct lkpi_hw *lhw;
4206 struct ieee80211_hw *hw;
4207 #ifdef HW_START_STOP
4208 int error;
4209 #endif
4210 bool start_all;
4211
4212 IMPROVE();
4213
4214 lhw = ic->ic_softc;
4215 hw = LHW_TO_HW(lhw);
4216 start_all = false;
4217
4218 /* IEEE80211_UNLOCK(ic); */
4219 wiphy_lock(hw->wiphy);
4220 if (ic->ic_nrunning > 0) {
4221 #ifdef HW_START_STOP
4222 error = lkpi_80211_mo_start(hw);
4223 if (error == 0)
4224 #endif
4225 start_all = true;
4226 } else {
4227 #ifdef HW_START_STOP
4228 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4229 #endif
4230 }
4231 wiphy_unlock(hw->wiphy);
4232 /* IEEE80211_LOCK(ic); */
4233
4234 if (start_all)
4235 ieee80211_start_all(ic);
4236 }
4237
4238 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)4239 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
4240 size_t ie_ids_len)
4241 {
4242 int i;
4243
4244 for (i = 0; i < ie_ids_len; i++) {
4245 if (ie == *ie_ids)
4246 return (true);
4247 }
4248
4249 return (false);
4250 }
4251
4252 /* Return true if skipped; false if error. */
4253 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)4254 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
4255 {
4256 size_t x;
4257 uint8_t l;
4258
4259 x = *xp;
4260
4261 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
4262 __func__, x, ies_len, ies));
4263 l = ies[x + 1];
4264 x += 2 + l;
4265
4266 if (x > ies_len)
4267 return (false);
4268
4269 *xp = x;
4270 return (true);
4271 }
4272
4273 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)4274 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4275 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
4276 {
4277 struct ieee80211_supported_band *supband;
4278 struct linuxkpi_ieee80211_channel *channels;
4279 struct ieee80211com *ic;
4280 const struct ieee80211_channel *chan;
4281 const struct ieee80211_rateset *rs;
4282 uint8_t *pb;
4283 int band, i;
4284
4285 ic = vap->iv_ic;
4286 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4287 if ((band_mask & (1 << band)) == 0)
4288 continue;
4289
4290 supband = hw->wiphy->bands[band];
4291 /*
4292 * This should not happen;
4293 * band_mask is a bitmask of valid bands to scan on.
4294 */
4295 if (supband == NULL || supband->n_channels == 0)
4296 continue;
4297
4298 /* Find a first channel to get the mode and rates from. */
4299 channels = supband->channels;
4300 chan = NULL;
4301 for (i = 0; i < supband->n_channels; i++) {
4302 uint32_t flags;
4303
4304 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4305 continue;
4306
4307 flags = 0;
4308 switch (band) {
4309 case NL80211_BAND_2GHZ:
4310 flags |= IEEE80211_CHAN_G;
4311 break;
4312 case NL80211_BAND_5GHZ:
4313 flags |= IEEE80211_CHAN_A;
4314 break;
4315 default:
4316 panic("%s:%d: unupported band %d\n",
4317 __func__, __LINE__, band);
4318 }
4319
4320 chan = ieee80211_find_channel(ic,
4321 channels[i].center_freq, flags);
4322 if (chan != NULL)
4323 break;
4324 }
4325
4326 /* This really should not happen. */
4327 if (chan == NULL)
4328 continue;
4329
4330 pb = p;
4331 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */
4332 p = ieee80211_add_rates(p, rs);
4333 p = ieee80211_add_xrates(p, rs);
4334
4335 #if defined(LKPI_80211_HT)
4336 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
4337 struct ieee80211_channel *c;
4338
4339 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4340 vap->iv_flags_ht);
4341 p = ieee80211_add_htcap_ch(p, vap, c);
4342 }
4343 #endif
4344 #if defined(LKPI_80211_VHT)
4345 if (band == NL80211_BAND_5GHZ &&
4346 (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
4347 struct ieee80211_channel *c;
4348
4349 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4350 vap->iv_flags_ht);
4351 c = ieee80211_vht_adjust_channel(ic, c,
4352 vap->iv_vht_flags);
4353 p = ieee80211_add_vhtcap_ch(p, vap, c);
4354 }
4355 #endif
4356
4357 scan_ies->ies[band] = pb;
4358 scan_ies->len[band] = p - pb;
4359 }
4360
4361 /* Add common_ies */
4362 pb = p;
4363 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4364 vap->iv_wpa_ie != NULL) {
4365 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4366 p += 2 + vap->iv_wpa_ie[1];
4367 }
4368 if (vap->iv_appie_probereq != NULL) {
4369 memcpy(p, vap->iv_appie_probereq->ie_data,
4370 vap->iv_appie_probereq->ie_len);
4371 p += vap->iv_appie_probereq->ie_len;
4372 }
4373 scan_ies->common_ies = pb;
4374 scan_ies->common_ie_len = p - pb;
4375
4376 return (p);
4377 }
4378
4379 static void
lkpi_enable_hw_scan(struct lkpi_hw * lhw)4380 lkpi_enable_hw_scan(struct lkpi_hw *lhw)
4381 {
4382
4383 if (lhw->ops->hw_scan) {
4384 /*
4385 * Advertise full-offload scanning.
4386 *
4387 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4388 * we essentially disable hw_scan for all drivers not setting
4389 * the flag.
4390 */
4391 lhw->ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4392 lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4393 }
4394 }
4395
4396 #ifndef LKPI_80211_USE_SCANLIST
4397 static const uint32_t chan_pri[] = {
4398 5180, 5500, 5745,
4399 5260, 5580, 5660, 5825,
4400 5220, 5300, 5540, 5620, 5700, 5785, 5865,
4401 2437, 2412, 2422, 2462, 2472, 2432, 2452
4402 };
4403
4404 static int
lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel * lc)4405 lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel *lc)
4406 {
4407 int i;
4408
4409 for (i = 0; i < nitems(chan_pri); i++) {
4410 if (lc->center_freq == chan_pri[i])
4411 return (i);
4412 }
4413
4414 return (-1);
4415 }
4416
4417 static int
lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel * lc1,const struct linuxkpi_ieee80211_channel * lc2)4418 lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel *lc1,
4419 const struct linuxkpi_ieee80211_channel *lc2)
4420 {
4421 int idx1, idx2;
4422
4423 /* Find index in list. */
4424 idx1 = lkpi_scan_chan_list_idx(lc1);
4425 idx2 = lkpi_scan_chan_list_idx(lc2);
4426
4427 if (idx1 == -1 && idx2 != -1)
4428 return (1);
4429 if (idx1 != -1 && idx2 == -1)
4430 return (-1);
4431
4432 /* Neither on the list, use center_freq. */
4433 if (idx1 == -1 && idx2 == -1)
4434 return (lc1->center_freq - lc2->center_freq);
4435
4436 /* Whichever is first in the list. */
4437 return (idx1 - idx2);
4438 }
4439
4440 static void
lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel ** cpp,size_t nchan)4441 lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel **cpp, size_t nchan)
4442 {
4443 struct linuxkpi_ieee80211_channel *lc, *nc;
4444 size_t i, j;
4445 int rc;
4446
4447 for (i = (nchan - 1); i > 0; i--) {
4448 for (j = i; j > 0 ; j--) {
4449 lc = *(cpp + j);
4450 nc = *(cpp + j - 1);
4451 rc = lkpi_scan_chan_list_comp(lc, nc);
4452 if (rc < 0) {
4453 *(cpp + j) = nc;
4454 *(cpp + j - 1) = lc;
4455 }
4456 }
4457 }
4458 }
4459
4460 static bool
lkpi_scan_chan(struct linuxkpi_ieee80211_channel * c,struct ieee80211com * ic,bool log)4461 lkpi_scan_chan(struct linuxkpi_ieee80211_channel *c,
4462 struct ieee80211com *ic, bool log)
4463 {
4464
4465 if ((c->flags & IEEE80211_CHAN_DISABLED) != 0) {
4466 if (log)
4467 TRACE_SCAN(ic, "Skipping disabled chan "
4468 "on band %s [%#x/%u/%#x]",
4469 lkpi_nl80211_band_name(c->band), c->hw_value,
4470 c->center_freq, c->flags);
4471 return (false);
4472 }
4473 if (isclr(ic->ic_chan_active, ieee80211_mhz2ieee(c->center_freq,
4474 lkpi_nl80211_band_to_net80211_band(c->band)))) {
4475 if (log)
4476 TRACE_SCAN(ic, "Skipping !active chan "
4477 "on band %s [%#x/%u/%#x]",
4478 lkpi_nl80211_band_name(c->band), c->hw_value,
4479 c->center_freq, c->flags);
4480 return (false);
4481 }
4482 return (true);
4483 }
4484 #endif
4485
4486 static void
lkpi_ic_scan_start(struct ieee80211com * ic)4487 lkpi_ic_scan_start(struct ieee80211com *ic)
4488 {
4489 struct lkpi_hw *lhw;
4490 struct ieee80211_hw *hw;
4491 struct lkpi_vif *lvif;
4492 struct ieee80211_vif *vif;
4493 struct ieee80211_scan_state *ss;
4494 struct ieee80211vap *vap;
4495 int error;
4496 bool is_hw_scan;
4497
4498 lhw = ic->ic_softc;
4499 ss = ic->ic_scan;
4500 vap = ss->ss_vap;
4501 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4502
4503 LKPI_80211_LHW_SCAN_LOCK(lhw);
4504 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4505 /* A scan is still running. */
4506 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4507 TRACE_SCAN(ic, "Trying to start new scan while still running; "
4508 "cancelling new net80211 scan; scan_flags %b",
4509 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4510 ieee80211_cancel_scan(vap);
4511 return;
4512 }
4513 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4514 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4515
4516 #if 0
4517 if (vap->iv_state != IEEE80211_S_SCAN) {
4518 TODO("We need to be able to scan if not in S_SCAN");
4519 TRACE_SCAN(ic, "scan_flags %b iv_state %d",
4520 lhw->scan_flags, LKPI_LHW_SCAN_BITS, vap->iv_state);
4521 ieee80211_cancel_scan(vap);
4522 return;
4523 }
4524 #endif
4525
4526 hw = LHW_TO_HW(lhw);
4527 if (!is_hw_scan) {
4528 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4529 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4530
4531 lvif = VAP_TO_LVIF(vap);
4532 vif = LVIF_TO_VIF(lvif);
4533
4534 if (vap->iv_state == IEEE80211_S_SCAN)
4535 lkpi_hw_conf_idle(hw, false);
4536
4537 LKPI_80211_LHW_SCAN_LOCK(lhw);
4538 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
4539 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4540
4541 lkpi_update_mcast_filter(ic);
4542
4543 TRACE_SCAN(vap->iv_ic, "Starting SW_SCAN: scan_flags %b",
4544 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4545 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
4546 /* net80211::scan_start() handled PS for us. */
4547 IMPROVE();
4548 /* XXX Also means it is too late to flush queues?
4549 * need to check iv_sta_ps or overload? */
4550 /* XXX want to adjust ss end time/ maxdwell? */
4551
4552 } else {
4553 struct ieee80211_scan_request *hw_req;
4554 struct linuxkpi_ieee80211_channel *lc, **cpp;
4555 struct cfg80211_ssid *ssids;
4556 struct cfg80211_scan_6ghz_params *s6gp;
4557 size_t chan_len, nchan, ssids_len, s6ghzlen;
4558 int band, i, ssid_count, common_ie_len;
4559 #ifndef LKPI_80211_USE_SCANLIST
4560 int n;
4561 #endif
4562 uint32_t band_mask;
4563 uint8_t *ie, *ieend;
4564 bool running;
4565
4566 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
4567 ssids_len = ssid_count * sizeof(*ssids);
4568 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */
4569
4570 band_mask = 0;
4571 nchan = 0;
4572 if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
4573 #ifdef LKPI_80211_USE_SCANLIST
4574 /* Avoid net80211 scan lists until it has proper scan offload support. */
4575 for (i = ss->ss_next; i < ss->ss_last; i++) {
4576 nchan++;
4577 band = lkpi_net80211_chan_to_nl80211_band(
4578 ss->ss_chans[ss->ss_next + i]);
4579 band_mask |= (1 << band);
4580 }
4581 #else
4582 /* Instead we scan for all channels all the time. */
4583 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4584 switch (band) {
4585 case NL80211_BAND_2GHZ:
4586 case NL80211_BAND_5GHZ:
4587 break;
4588 default:
4589 continue;
4590 }
4591 if (hw->wiphy->bands[band] != NULL) {
4592 struct linuxkpi_ieee80211_channel *channels;
4593 int n;
4594
4595 band_mask |= (1 << band);
4596
4597 channels = hw->wiphy->bands[band]->channels;
4598 n = hw->wiphy->bands[band]->n_channels;
4599 for (i = 0; i < n; i++) {
4600 if (lkpi_scan_chan(&channels[i], ic, true))
4601 nchan++;
4602 }
4603 }
4604 }
4605 #endif
4606 } else {
4607 IMPROVE("individual band scans not yet supported, only scanning first band");
4608 /* In theory net80211 should drive this. */
4609 /* Probably we need to add local logic for now;
4610 * need to deal with scan_complete
4611 * and cancel_scan and keep local state.
4612 * Also cut the nchan down above.
4613 */
4614 /* XXX-BZ ath10k does not set this but still does it? &$%^ */
4615 }
4616
4617 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
4618
4619 common_ie_len = 0;
4620 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4621 vap->iv_wpa_ie != NULL)
4622 common_ie_len += vap->iv_wpa_ie[1];
4623 if (vap->iv_appie_probereq != NULL)
4624 common_ie_len += vap->iv_appie_probereq->ie_len;
4625
4626 /* We would love to check this at an earlier stage... */
4627 if (common_ie_len > hw->wiphy->max_scan_ie_len) {
4628 ic_printf(ic, "WARNING: %s: common_ie_len %d > "
4629 "wiphy->max_scan_ie_len %d\n", __func__,
4630 common_ie_len, hw->wiphy->max_scan_ie_len);
4631 }
4632
4633 hw_req = malloc(sizeof(*hw_req) + ssids_len +
4634 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
4635 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
4636
4637 hw_req->req.flags = 0; /* XXX ??? */
4638 /* hw_req->req.wdev */
4639 hw_req->req.wiphy = hw->wiphy;
4640 hw_req->req.no_cck = false; /* XXX */
4641
4642 /*
4643 * In general setting duration[_mandatory] seems to pessimise
4644 * default scanning behaviour. We only use it for BGSCANnig
4645 * to keep the dwell times small.
4646 * Setting duration_mandatory makes this the maximum dwell
4647 * time (otherwise may be shorter). Duration is in TU.
4648 */
4649 if ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) != 0) {
4650 unsigned long dwell;
4651
4652 if ((ic->ic_caps & IEEE80211_C_BGSCAN) == 0 ||
4653 (vap->iv_flags & IEEE80211_F_BGSCAN) == 0)
4654 ic_printf(ic, "BGSCAN despite off: %b, %b, %b\n",
4655 ic->ic_flags_ext, IEEE80211_FEXT_BITS,
4656 vap->iv_flags, IEEE80211_F_BITS,
4657 ic->ic_caps, IEEE80211_C_BITS);
4658
4659 dwell = ss->ss_mindwell;
4660 if (dwell == 0)
4661 dwell = msecs_to_ticks(20);
4662
4663 hw_req->req.duration_mandatory = true;
4664 hw_req->req.duration = TICKS_2_USEC(dwell) / 1024;
4665 }
4666
4667 #ifdef __notyet__
4668 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
4669 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
4670 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
4671 #endif
4672 eth_broadcast_addr(hw_req->req.bssid);
4673
4674 hw_req->req.n_channels = nchan;
4675 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
4676 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
4677 #ifdef LKPI_80211_USE_SCANLIST
4678 for (i = 0; i < nchan; i++) {
4679 *(cpp + i) =
4680 (struct linuxkpi_ieee80211_channel *)(lc + i);
4681 }
4682 /* Avoid net80211 scan lists until it has proper scan offload support. */
4683 for (i = 0; i < nchan; i++) {
4684 struct ieee80211_channel *c;
4685
4686 c = ss->ss_chans[ss->ss_next + i];
4687 lc->center_freq = c->ic_freq; /* XXX */
4688 /* lc->flags */
4689 lc->band = lkpi_net80211_chan_to_nl80211_band(c);
4690 lc->max_power = c->ic_maxpower;
4691 /* lc-> ... */
4692 lc++;
4693 }
4694 #else
4695 /* Add bands in reverse order for scanning. */
4696 n = 0;
4697 for (band = NUM_NL80211_BANDS - 1; band >= 0; band--) {
4698 struct ieee80211_supported_band *supband;
4699 struct linuxkpi_ieee80211_channel *channels;
4700
4701 /* Band disabled for scanning? */
4702 if ((band_mask & (1 << band)) == 0)
4703 continue;
4704
4705 /* Nothing to scan in band? */
4706 supband = hw->wiphy->bands[band];
4707 if (supband == NULL || supband->n_channels == 0)
4708 continue;
4709
4710 channels = supband->channels;
4711 for (i = 0; i < supband->n_channels; i++) {
4712 if (lkpi_scan_chan(&channels[i], ic, false))
4713 *(cpp + n++) = &channels[i];
4714 }
4715 }
4716 if (lkpi_order_scanlist)
4717 lkpi_scan_chan_list_resort(cpp, nchan);
4718
4719 if ((linuxkpi_debug_80211 & D80211_SCAN) != 0) {
4720 printf("%s:%d: %s SCAN Channel List (nchan=%zu): ",
4721 __func__, __LINE__, ic->ic_name, nchan);
4722 for (i = 0; i < nchan; i++) {
4723 struct linuxkpi_ieee80211_channel *xc;
4724
4725 xc = *(cpp + i);
4726 printf(" %d(%d)",
4727 ieee80211_mhz2ieee(xc->center_freq,
4728 lkpi_nl80211_band_to_net80211_band(
4729 xc->band)),
4730 xc->center_freq);
4731 }
4732 printf("\n");
4733 }
4734 #endif
4735
4736 hw_req->req.n_ssids = ssid_count;
4737 if (hw_req->req.n_ssids > 0) {
4738 ssids = (struct cfg80211_ssid *)lc;
4739 hw_req->req.ssids = ssids;
4740 for (i = 0; i < ssid_count; i++) {
4741 ssids->ssid_len = ss->ss_ssid[i].len;
4742 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
4743 ss->ss_ssid[i].len);
4744 ssids++;
4745 }
4746 s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
4747 } else {
4748 s6gp = (struct cfg80211_scan_6ghz_params *)lc;
4749 }
4750
4751 /* 6GHz one day. */
4752 hw_req->req.n_6ghz_params = 0;
4753 hw_req->req.scan_6ghz_params = NULL;
4754 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */
4755 /* s6gp->... */
4756
4757 ie = ieend = (uint8_t *)s6gp;
4758 /* Copy per-band IEs, copy common IEs */
4759 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
4760 hw_req->req.ie = ie;
4761 hw_req->req.ie_len = ieend - ie;
4762 hw_req->req.scan_start = jiffies;
4763
4764 lvif = VAP_TO_LVIF(vap);
4765 vif = LVIF_TO_VIF(lvif);
4766
4767 LKPI_80211_LHW_SCAN_LOCK(lhw);
4768 /* Re-check under lock. */
4769 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
4770 if (!running) {
4771 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
4772 "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
4773
4774 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
4775 lhw->hw_req = hw_req;
4776 }
4777 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4778 if (running) {
4779 free(hw_req, M_LKPI80211);
4780 TRACE_SCAN(ic, "Trying to start new scan while still "
4781 "running (2); cancelling new net80211 scan; "
4782 "scan_flags %b",
4783 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4784 ieee80211_cancel_scan(vap);
4785 return;
4786 }
4787
4788 lkpi_update_mcast_filter(ic);
4789 TRACE_SCAN(ic, "Starting HW_SCAN: scan_flags %b, "
4790 "ie_len %d, n_ssids %d, n_chan %d, common_ie_len %d [%d, %d]",
4791 lhw->scan_flags, LKPI_LHW_SCAN_BITS, hw_req->req.ie_len,
4792 hw_req->req.n_ssids, hw_req->req.n_channels,
4793 hw_req->ies.common_ie_len,
4794 hw_req->ies.len[NL80211_BAND_2GHZ],
4795 hw_req->ies.len[NL80211_BAND_5GHZ]);
4796
4797 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
4798 if (error != 0) {
4799 bool scan_done;
4800 int e;
4801
4802 TRACE_SCAN(ic, "hw_scan failed; scan_flags %b, error %d",
4803 lhw->scan_flags, LKPI_LHW_SCAN_BITS, error);
4804 ieee80211_cancel_scan(vap);
4805
4806 /*
4807 * ieee80211_scan_completed must be called in either
4808 * case of error or none. So let the free happen there
4809 * and only there.
4810 * That would be fine in theory but in practice drivers
4811 * behave differently:
4812 * ath10k does not return hw_scan until after scan_complete
4813 * and can then still return an error.
4814 * rtw88 can return 1 or -EBUSY without scan_complete
4815 * iwlwifi can return various errors before scan starts
4816 * ...
4817 * So we cannot rely on that behaviour and have to check
4818 * and balance between both code paths.
4819 */
4820 e = 0;
4821 scan_done = true;
4822 LKPI_80211_LHW_SCAN_LOCK(lhw);
4823 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4824
4825 free(lhw->hw_req, M_LKPI80211);
4826 lhw->hw_req = NULL;
4827 /*
4828 * The ieee80211_cancel_scan() above runs in a
4829 * taskq and it may take ages for the previous
4830 * scan to clear; starting a new one right away
4831 * we run into the problem that the old one is
4832 * still active.
4833 */
4834 e = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz);
4835 scan_done = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
4836
4837 /*
4838 * Now we can clear running if no one else did.
4839 */
4840 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4841 }
4842 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4843 lkpi_update_mcast_filter(ic);
4844 if (!scan_done) {
4845 ic_printf(ic, "ERROR: %s: timeout/error to wait "
4846 "for ieee80211_cancel_scan: %d\n", __func__, e);
4847 return;
4848 }
4849
4850 /*
4851 * XXX-SIGH magic number.
4852 * rtw88 has a magic "return 1" if offloading scan is
4853 * not possible. Fall back to sw scan in that case.
4854 */
4855 if (error == 1) {
4856 /*
4857 * We need to put this into some defered context
4858 * the net80211 scan may not be done yet
4859 * (ic_flags & IEEE80211_F_SCAN) and we cannot
4860 * wait here; if we do scan_curchan_task always
4861 * runs after our timeout to finalize the scan.
4862 */
4863 ieee80211_runtask(ic, &lvif->sw_scan_task);
4864 return;
4865 }
4866
4867 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
4868 __func__, error);
4869 }
4870 }
4871 }
4872
4873 static void
lkpi_sw_scan_task(void * arg,int pending __unused)4874 lkpi_sw_scan_task(void *arg, int pending __unused)
4875 {
4876 struct lkpi_hw *lhw;
4877 struct lkpi_vif *lvif;
4878 struct ieee80211vap *vap;
4879 struct ieee80211_scan_state *ss;
4880
4881 lvif = arg;
4882 vap = LVIF_TO_VAP(lvif);
4883 lhw = vap->iv_ic->ic_softc;
4884 ss = vap->iv_ic->ic_scan;
4885
4886 LKPI_80211_LHW_SCAN_LOCK(lhw);
4887 /*
4888 * We will re-enable this at scan_end calling lkpi_enable_hw_scan().
4889 * IEEE80211_FEXT_SCAN_OFFLOAD will be cleared by lkpi_ic_scan_start.
4890 */
4891 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
4892 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4893
4894 TRACE_SCAN(vap->iv_ic, "Triggering SW_SCAN: pending %d, scan_flags %b",
4895 pending, lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4896
4897 /*
4898 * This will call ic_scan_start() and we will get into the right path
4899 * unless other scans started in between.
4900 */
4901 ieee80211_start_scan(vap,
4902 IEEE80211_SCAN_ONCE,
4903 msecs_to_ticks(10000), /* 10000 ms (=~ 50 chan * 200 ms) */
4904 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
4905 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
4906 vap->iv_des_nssid, vap->iv_des_ssid);
4907 }
4908
4909 static void
lkpi_ic_scan_end(struct ieee80211com * ic)4910 lkpi_ic_scan_end(struct ieee80211com *ic)
4911 {
4912 struct lkpi_hw *lhw;
4913 bool is_hw_scan;
4914
4915 lhw = ic->ic_softc;
4916 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4917
4918 LKPI_80211_LHW_SCAN_LOCK(lhw);
4919 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
4920 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4921 return;
4922 }
4923 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4924 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4925
4926 if (!is_hw_scan) {
4927 struct ieee80211_scan_state *ss;
4928 struct ieee80211vap *vap;
4929 struct ieee80211_hw *hw;
4930 struct lkpi_vif *lvif;
4931 struct ieee80211_vif *vif;
4932
4933 ss = ic->ic_scan;
4934 vap = ss->ss_vap;
4935 hw = LHW_TO_HW(lhw);
4936 lvif = VAP_TO_LVIF(vap);
4937 vif = LVIF_TO_VIF(lvif);
4938
4939 lkpi_80211_mo_sw_scan_complete(hw, vif);
4940
4941 /* Send PS to stop buffering if n80211 does not for us? */
4942
4943 if (vap->iv_state == IEEE80211_S_SCAN)
4944 lkpi_hw_conf_idle(hw, true);
4945 }
4946
4947 /*
4948 * In case we disabled the hw_scan in lkpi_ic_scan_start() and
4949 * switched to swscan, re-enable hw_scan if available.
4950 */
4951 lkpi_enable_hw_scan(lhw);
4952
4953 LKPI_80211_LHW_SCAN_LOCK(lhw);
4954 wakeup(lhw);
4955 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4956 }
4957
4958 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)4959 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
4960 unsigned long maxdwell)
4961 {
4962 struct lkpi_hw *lhw;
4963 bool is_hw_scan;
4964
4965 lhw = ss->ss_ic->ic_softc;
4966 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d maxdwell %lu",
4967 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
4968 ss->ss_ic->ic_curchan->ic_ieee, maxdwell);
4969
4970 LKPI_80211_LHW_SCAN_LOCK(lhw);
4971 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4972 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4973 if (!is_hw_scan)
4974 lhw->ic_scan_curchan(ss, maxdwell);
4975 }
4976
4977 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)4978 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
4979 {
4980 struct lkpi_hw *lhw;
4981 bool is_hw_scan;
4982
4983 lhw = ss->ss_ic->ic_softc;
4984 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d mindwell %lu",
4985 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
4986 ss->ss_ic->ic_curchan->ic_ieee, ss->ss_mindwell);
4987
4988 LKPI_80211_LHW_SCAN_LOCK(lhw);
4989 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4990 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4991 if (!is_hw_scan)
4992 lhw->ic_scan_mindwell(ss);
4993 }
4994
4995 static void
lkpi_ic_set_channel(struct ieee80211com * ic)4996 lkpi_ic_set_channel(struct ieee80211com *ic)
4997 {
4998 struct lkpi_hw *lhw;
4999 struct ieee80211_hw *hw;
5000 struct ieee80211_channel *c;
5001 struct linuxkpi_ieee80211_channel *chan;
5002 int error;
5003 bool hw_scan_running;
5004
5005 lhw = ic->ic_softc;
5006
5007 /* If we do not support (*config)() save us the work. */
5008 if (lhw->ops->config == NULL)
5009 return;
5010
5011 /* If we have a hw_scan running do not switch channels. */
5012 LKPI_80211_LHW_SCAN_LOCK(lhw);
5013 hw_scan_running =
5014 (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
5015 (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
5016 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5017 if (hw_scan_running)
5018 return;
5019
5020 c = ic->ic_curchan;
5021 if (c == NULL || c == IEEE80211_CHAN_ANYC) {
5022 ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
5023 c, lhw->ops->config);
5024 return;
5025 }
5026
5027 chan = lkpi_find_lkpi80211_chan(lhw, c);
5028 if (chan == NULL) {
5029 ic_printf(ic, "%s: c %p chan %p\n", __func__,
5030 c, chan);
5031 return;
5032 }
5033
5034 /* XXX max power for scanning? */
5035 IMPROVE();
5036
5037 hw = LHW_TO_HW(lhw);
5038 cfg80211_chandef_create(&hw->conf.chandef, chan,
5039 #ifdef LKPI_80211_HT
5040 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
5041 #endif
5042 NL80211_CHAN_NO_HT);
5043
5044 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
5045 if (error != 0 && error != EOPNOTSUPP) {
5046 ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
5047 __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
5048 /* XXX should we unroll to the previous chandef? */
5049 IMPROVE();
5050 } else {
5051 /* Update radiotap channels as well. */
5052 lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
5053 lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
5054 lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
5055 lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
5056 }
5057
5058 /* Currently PS is hard coded off! Not sure it belongs here. */
5059 IMPROVE();
5060 if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
5061 (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
5062 hw->conf.flags &= ~IEEE80211_CONF_PS;
5063 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
5064 if (error != 0 && error != EOPNOTSUPP)
5065 ic_printf(ic, "ERROR: %s: config %#0x returned "
5066 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
5067 error);
5068 }
5069 }
5070
5071 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])5072 lkpi_ic_node_alloc(struct ieee80211vap *vap,
5073 const uint8_t mac[IEEE80211_ADDR_LEN])
5074 {
5075 struct ieee80211com *ic;
5076 struct lkpi_hw *lhw;
5077 struct ieee80211_node *ni;
5078 struct ieee80211_hw *hw;
5079 struct lkpi_sta *lsta;
5080
5081 ic = vap->iv_ic;
5082 lhw = ic->ic_softc;
5083
5084 /* We keep allocations de-coupled so we can deal with the two worlds. */
5085 if (lhw->ic_node_alloc == NULL)
5086 return (NULL);
5087
5088 ni = lhw->ic_node_alloc(vap, mac);
5089 if (ni == NULL)
5090 return (NULL);
5091
5092 hw = LHW_TO_HW(lhw);
5093 lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
5094 if (lsta == NULL) {
5095 if (lhw->ic_node_free != NULL)
5096 lhw->ic_node_free(ni);
5097 return (NULL);
5098 }
5099
5100 return (ni);
5101 }
5102
5103 static int
lkpi_ic_node_init(struct ieee80211_node * ni)5104 lkpi_ic_node_init(struct ieee80211_node *ni)
5105 {
5106 struct ieee80211com *ic;
5107 struct lkpi_hw *lhw;
5108 int error;
5109
5110 ic = ni->ni_ic;
5111 lhw = ic->ic_softc;
5112
5113 if (lhw->ic_node_init != NULL) {
5114 error = lhw->ic_node_init(ni);
5115 if (error != 0)
5116 return (error);
5117 }
5118
5119 /* XXX-BZ Sync other state over. */
5120 IMPROVE();
5121
5122 return (0);
5123 }
5124
5125 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)5126 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
5127 {
5128 struct ieee80211com *ic;
5129 struct lkpi_hw *lhw;
5130
5131 ic = ni->ni_ic;
5132 lhw = ic->ic_softc;
5133
5134 /* XXX-BZ remove from driver, ... */
5135 IMPROVE();
5136
5137 if (lhw->ic_node_cleanup != NULL)
5138 lhw->ic_node_cleanup(ni);
5139 }
5140
5141 static void
lkpi_ic_node_free(struct ieee80211_node * ni)5142 lkpi_ic_node_free(struct ieee80211_node *ni)
5143 {
5144 struct ieee80211com *ic;
5145 struct lkpi_hw *lhw;
5146 struct lkpi_sta *lsta;
5147
5148 ic = ni->ni_ic;
5149 lhw = ic->ic_softc;
5150 lsta = ni->ni_drv_data;
5151
5152 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
5153
5154 /*
5155 * Pass in the original ni just in case of error we could check that
5156 * it is the same as lsta->ni.
5157 */
5158 lkpi_lsta_free(lsta, ni);
5159
5160 if (lhw->ic_node_free != NULL)
5161 lhw->ic_node_free(ni);
5162 }
5163
5164 /*
5165 * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
5166 * call path.
5167 * Unfortunately they have slightly different invariants. See
5168 * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
5169 * Both take care of the ni reference in case of error, and otherwise during
5170 * the callback after transmit.
5171 * The difference is that in case of error (*ic_raw_xmit) needs us to release
5172 * the mbuf, while (*ic_transmit) will free the mbuf itself.
5173 */
5174 static int
lkpi_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused,bool freem)5175 lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
5176 const struct ieee80211_bpf_params *params __unused,
5177 bool freem)
5178 {
5179 struct lkpi_sta *lsta;
5180 int error;
5181
5182 lsta = ni->ni_drv_data;
5183 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5184 #if 0
5185 if (!lsta->added_to_drv || !lsta->txq_ready) {
5186 #else
5187 /*
5188 * Backout this part of 886653492945f which breaks rtw88 or
5189 * in general drivers without (*sta_state)() but only the
5190 * legacy fallback to (*sta_add)().
5191 */
5192 if (!lsta->txq_ready) {
5193 #endif
5194 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5195 if (freem)
5196 m_free(m);
5197 return (ENETDOWN);
5198 }
5199
5200 /* Queue the packet and enqueue the task to handle it. */
5201 error = mbufq_enqueue(&lsta->txq, m);
5202 if (error != 0) {
5203 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5204 if (freem)
5205 m_free(m);
5206 #ifdef LINUXKPI_DEBUG_80211
5207 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5208 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
5209 __func__, error);
5210 #endif
5211 return (ENETDOWN);
5212 }
5213 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
5214 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5215
5216 #ifdef LINUXKPI_DEBUG_80211
5217 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5218 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
5219 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
5220 mbufq_len(&lsta->txq));
5221 #endif
5222
5223 return (0);
5224 }
5225
5226 static int
5227 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
5228 const struct ieee80211_bpf_params *params __unused)
5229 {
5230 return (lkpi_xmit(ni, m, NULL, true));
5231 }
5232
5233 #ifdef LKPI_80211_HW_CRYPTO
5234 /*
5235 * This is a bit of a hack given we know we are operating on a
5236 * single frame and we know that hardware will deal with it.
5237 * But otherwise the enmic bit and the encrypt bit need to be
5238 * decoupled.
5239 */
5240 static int
5241 lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
5242 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5243 {
5244 struct ieee80211_hdr *hdr;
5245 uint32_t hlen, hdrlen;
5246 uint8_t *p;
5247
5248 /*
5249 * TKIP only happens on data.
5250 */
5251 hdr = (void *)skb->data;
5252 if (!ieee80211_is_data_present(hdr->frame_control))
5253 return (0);
5254
5255 /*
5256 * "enmic" (though we do not do that).
5257 */
5258 /* any conditions to not apply this? */
5259 if (skb_tailroom(skb) < k->wk_cipher->ic_miclen)
5260 return (ENOBUFS);
5261
5262 p = skb_put(skb, k->wk_cipher->ic_miclen);
5263 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
5264 goto encrypt;
5265
5266 /*
5267 * (*enmic) which we hopefully do not have to do with hw accel.
5268 * That means if we make it here we have a problem.
5269 */
5270 TODO("(*enmic)");
5271 return (ENXIO);
5272
5273 encrypt:
5274 /*
5275 * "encrypt" (though we do not do that).
5276 */
5277 /*
5278 * Check if we have anything to do as requested by driver
5279 * or if we are done?
5280 */
5281 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5282 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
5283 return (0);
5284
5285 hlen = k->wk_cipher->ic_header;
5286 if (skb_headroom(skb) < hlen)
5287 return (ENOBUFS);
5288
5289 hdr = (void *)skb->data;
5290 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5291 p = skb_push(skb, hlen);
5292 memmove(p, p + hlen, hdrlen);
5293
5294 /* If driver request space only we are done. */
5295 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5296 return (0);
5297
5298 p += hdrlen;
5299 k->wk_cipher->ic_setiv(k, p);
5300
5301 /* If we make it hear we do sw encryption. */
5302 TODO("sw encrypt");
5303 return (ENXIO);
5304 }
5305
5306 static int
5307 lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
5308 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5309 {
5310 struct ieee80211_hdr *hdr;
5311 uint32_t hlen, hdrlen;
5312 uint8_t *p;
5313
5314 hdr = (void *)skb->data;
5315
5316 /*
5317 * Check if we have anythig to do as requested by driver
5318 * or if we are done?
5319 */
5320 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5321 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
5322 /* MFP */
5323 !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
5324 ieee80211_is_mgmt(hdr->frame_control)))
5325 return (0);
5326
5327 hlen = k->wk_cipher->ic_header;
5328 if (skb_headroom(skb) < hlen)
5329 return (ENOBUFS);
5330
5331 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5332 p = skb_push(skb, hlen);
5333 memmove(p, p + hlen, hdrlen);
5334
5335 /* If driver request space only we are done. */
5336 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5337 return (0);
5338
5339 p += hdrlen;
5340 k->wk_cipher->ic_setiv(k, p);
5341
5342 return (0);
5343 }
5344
5345 static int
5346 lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
5347 struct sk_buff *skb)
5348 {
5349 struct ieee80211_tx_info *info;
5350 struct ieee80211_key_conf *kc;
5351
5352 KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
5353 KASSERT(k != NULL, ("%s: key is NULL", __func__));
5354 KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
5355
5356 kc = lsta->kc[k->wk_keyix];
5357
5358 info = IEEE80211_SKB_CB(skb);
5359 info->control.hw_key = kc;
5360
5361 /* MUST NOT happen. KASSERT? */
5362 if (kc == NULL) {
5363 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
5364 "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
5365 return (ENXIO);
5366 }
5367
5368 switch (kc->cipher) {
5369 case WLAN_CIPHER_SUITE_TKIP:
5370 return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
5371 case WLAN_CIPHER_SUITE_CCMP:
5372 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5373 case WLAN_CIPHER_SUITE_GCMP:
5374 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5375 case WLAN_CIPHER_SUITE_WEP40:
5376 case WLAN_CIPHER_SUITE_WEP104:
5377 case WLAN_CIPHER_SUITE_CCMP_256:
5378 case WLAN_CIPHER_SUITE_GCMP_256:
5379 case WLAN_CIPHER_SUITE_AES_CMAC:
5380 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5381 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5382 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5383 default:
5384 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
5385 "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
5386 skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
5387 return (EOPNOTSUPP);
5388 }
5389 }
5390
5391 static uint8_t
5392 lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
5393 {
5394 struct ieee80211_key_conf *kc;
5395
5396 kc = lsta->kc[k->wk_keyix];
5397 if (kc == NULL)
5398 return (0);
5399
5400 IMPROVE("which other flags need tailroom?");
5401 if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
5402 return (32); /* Large enough to hold everything and pow2. */
5403
5404 return (0);
5405 }
5406 #endif
5407
5408 static void
5409 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
5410 {
5411 struct ieee80211_node *ni;
5412 struct ieee80211_frame *wh;
5413 struct ieee80211_key *k;
5414 struct sk_buff *skb;
5415 struct ieee80211com *ic;
5416 struct lkpi_hw *lhw;
5417 struct ieee80211_hw *hw;
5418 struct lkpi_vif *lvif;
5419 struct ieee80211_vif *vif;
5420 struct ieee80211_channel *c;
5421 struct ieee80211_tx_control control;
5422 struct ieee80211_tx_info *info;
5423 struct ieee80211_sta *sta;
5424 struct ieee80211_hdr *hdr;
5425 struct lkpi_txq *ltxq;
5426 void *buf;
5427 ieee80211_keyix keyix;
5428 uint8_t ac, tid, tailroom;
5429
5430 M_ASSERTPKTHDR(m);
5431 #ifdef LINUXKPI_DEBUG_80211
5432 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
5433 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
5434 #endif
5435
5436 ni = lsta->ni;
5437 k = NULL;
5438 keyix = IEEE80211_KEYIX_NONE;
5439 wh = mtod(m, struct ieee80211_frame *);
5440 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
5441
5442 #ifdef LKPI_80211_HW_CRYPTO
5443 if (lkpi_hwcrypto) {
5444 k = ieee80211_crypto_get_txkey(ni, m);
5445 if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
5446 keyix = k->wk_keyix;
5447 }
5448 #endif
5449
5450 /* Encrypt the frame if need be. */
5451 if (keyix == IEEE80211_KEYIX_NONE) {
5452 /* Retrieve key for TX && do software encryption. */
5453 k = ieee80211_crypto_encap(ni, m);
5454 if (k == NULL) {
5455 ieee80211_free_node(ni);
5456 m_freem(m);
5457 return;
5458 }
5459 }
5460 }
5461
5462 ic = ni->ni_ic;
5463 lhw = ic->ic_softc;
5464 hw = LHW_TO_HW(lhw);
5465 c = ni->ni_chan;
5466
5467 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
5468 struct lkpi_radiotap_tx_hdr *rtap;
5469
5470 rtap = &lhw->rtap_tx;
5471 rtap->wt_flags = 0;
5472 if (k != NULL)
5473 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
5474 if (m->m_flags & M_FRAG)
5475 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
5476 IMPROVE();
5477 rtap->wt_rate = 0;
5478 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
5479 rtap->wt_chan_freq = htole16(c->ic_freq);
5480 rtap->wt_chan_flags = htole16(c->ic_flags);
5481 }
5482
5483 ieee80211_radiotap_tx(ni->ni_vap, m);
5484 }
5485
5486 #ifdef LKPI_80211_HW_CRYPTO
5487 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
5488 tailroom = lkpi_hw_crypto_tailroom(lsta, k);
5489 else
5490 #endif
5491 tailroom = 0;
5492
5493 /*
5494 * net80211 should handle hw->extra_tx_headroom.
5495 * Though for as long as we are copying we don't mind.
5496 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
5497 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
5498 */
5499 skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
5500 if (skb == NULL) {
5501 static uint8_t skb_alloc_failures = 0;
5502
5503 if (skb_alloc_failures++ == 0) {
5504 int tid;
5505
5506 sta = LSTA_TO_STA(lsta);
5507 ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
5508 __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
5509 for (tid = 0; tid < nitems(sta->txq); tid++) {
5510 if (sta->txq[tid] == NULL)
5511 continue;
5512 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
5513 ic_printf(ic, " tid %d ltxq %p seen_dequeue %d stopped %d skb_queue_len %u\n",
5514 tid, ltxq, ltxq->seen_dequeue, ltxq->stopped, skb_queue_len(<xq->skbq));
5515 }
5516 }
5517 ieee80211_free_node(ni);
5518 m_freem(m);
5519 return;
5520 }
5521 skb_reserve(skb, hw->extra_tx_headroom);
5522
5523 /* XXX-BZ we need a SKB version understanding mbuf. */
5524 /* Save the mbuf for ieee80211_tx_complete(). */
5525 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
5526 skb->m = m;
5527 #if 0
5528 skb_put_data(skb, m->m_data, m->m_pkthdr.len);
5529 #else
5530 buf = skb_put(skb, m->m_pkthdr.len);
5531 m_copydata(m, 0, m->m_pkthdr.len, buf);
5532 #endif
5533 /* Save the ni. */
5534 m->m_pkthdr.PH_loc.ptr = ni;
5535
5536 lvif = VAP_TO_LVIF(ni->ni_vap);
5537 vif = LVIF_TO_VIF(lvif);
5538
5539 hdr = (void *)skb->data;
5540 tid = linuxkpi_ieee80211_get_tid(hdr, true);
5541 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
5542 if (!ieee80211_is_data(hdr->frame_control)) {
5543 /* MGMT and CTRL frames go on TID 7/VO. */
5544 skb->priority = 7;
5545 ac = IEEE80211_AC_VO;
5546 } else {
5547 /* Other non-QOS traffic goes to BE. */
5548 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */
5549 skb->priority = 0;
5550 ac = IEEE80211_AC_BE;
5551 }
5552 } else {
5553 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
5554 ac = ieee80211e_up_to_ac[tid & 7];
5555 }
5556 skb_set_queue_mapping(skb, ac);
5557
5558 info = IEEE80211_SKB_CB(skb);
5559 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5560 /* Slight delay; probably only happens on scanning so fine? */
5561 if (c == NULL || c == IEEE80211_CHAN_ANYC)
5562 c = ic->ic_curchan;
5563 info->band = lkpi_net80211_chan_to_nl80211_band(c);
5564 info->hw_queue = vif->hw_queue[ac];
5565 if (m->m_flags & M_EAPOL)
5566 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
5567 info->control.vif = vif;
5568 /* XXX-BZ info->control.rates */
5569 #ifdef __notyet__
5570 #ifdef LKPI_80211_HT
5571 info->control.rts_cts_rate_idx=
5572 info->control.use_rts= /* RTS */
5573 info->control.use_cts_prot= /* RTS/CTS*/
5574 #endif
5575 #endif
5576
5577 sta = LSTA_TO_STA(lsta);
5578 #ifdef LKPI_80211_HW_CRYPTO
5579 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
5580 int error;
5581
5582 error = lkpi_hw_crypto_prepare(lsta, k, skb);
5583 if (error != 0) {
5584 /*
5585 * We only have to free the skb which will free the
5586 * mbuf and release the reference on the ni.
5587 */
5588 dev_kfree_skb(skb);
5589 return;
5590 }
5591 }
5592 #endif
5593
5594 IMPROVE();
5595
5596 ltxq = NULL;
5597 if (!ieee80211_is_data_present(hdr->frame_control)) {
5598 if (vif->type == NL80211_IFTYPE_STATION &&
5599 lsta->added_to_drv &&
5600 sta->txq[IEEE80211_NUM_TIDS] != NULL)
5601 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
5602 } else if (lsta->added_to_drv &&
5603 sta->txq[skb->priority] != NULL) {
5604 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
5605 }
5606 if (ltxq == NULL)
5607 goto ops_tx;
5608
5609 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
5610 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
5611
5612 LKPI_80211_LTXQ_LOCK(ltxq);
5613 skb_queue_tail(<xq->skbq, skb);
5614 #ifdef LINUXKPI_DEBUG_80211
5615 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5616 printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
5617 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
5618 "WAKE_TX_Q ac %d prio %u qmap %u\n",
5619 __func__, __LINE__,
5620 curthread->td_tid, jiffies,
5621 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
5622 skb_queue_len(<xq->skbq), ltxq->txq.ac,
5623 ltxq->txq.tid, ac, skb->priority, skb->qmap);
5624 #endif
5625 LKPI_80211_LTXQ_UNLOCK(ltxq);
5626 wiphy_lock(hw->wiphy);
5627 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq);
5628 wiphy_unlock(hw->wiphy);
5629 return;
5630
5631 ops_tx:
5632 #ifdef LINUXKPI_DEBUG_80211
5633 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5634 printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
5635 "TX ac %d prio %u qmap %u\n",
5636 __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
5637 skb, ac, skb->priority, skb->qmap);
5638 #endif
5639 memset(&control, 0, sizeof(control));
5640 control.sta = sta;
5641 wiphy_lock(hw->wiphy);
5642 lkpi_80211_mo_tx(hw, &control, skb);
5643 wiphy_unlock(hw->wiphy);
5644 }
5645
5646 static void
5647 lkpi_80211_txq_task(void *ctx, int pending)
5648 {
5649 struct lkpi_sta *lsta;
5650 struct mbufq mq;
5651 struct mbuf *m;
5652 bool shall_tx;
5653
5654 lsta = ctx;
5655
5656 #ifdef LINUXKPI_DEBUG_80211
5657 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5658 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
5659 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
5660 pending, mbufq_len(&lsta->txq));
5661 #endif
5662
5663 mbufq_init(&mq, IFQ_MAXLEN);
5664
5665 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5666 /*
5667 * Do not re-check lsta->txq_ready here; we may have a pending
5668 * disassoc/deauth frame still. On the contrary if txq_ready is
5669 * false we do not have a valid sta anymore in the firmware so no
5670 * point to try to TX.
5671 * We also use txq_ready as a semaphore and will drain the txq manually
5672 * if needed on our way towards SCAN/INIT in the state machine.
5673 */
5674 #if 0
5675 shall_tx = lsta->added_to_drv && lsta->txq_ready;
5676 #else
5677 /*
5678 * Backout this part of 886653492945f which breaks rtw88 or
5679 * in general drivers without (*sta_state)() but only the
5680 * legacy fallback to (*sta_add)().
5681 */
5682 shall_tx = lsta->txq_ready;
5683 #endif
5684 if (__predict_true(shall_tx))
5685 mbufq_concat(&mq, &lsta->txq);
5686 /*
5687 * else a state change will push the packets out manually or
5688 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
5689 */
5690 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5691
5692 m = mbufq_dequeue(&mq);
5693 while (m != NULL) {
5694 lkpi_80211_txq_tx_one(lsta, m);
5695 m = mbufq_dequeue(&mq);
5696 }
5697 }
5698
5699 static int
5700 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
5701 {
5702
5703 /* XXX TODO */
5704 IMPROVE();
5705
5706 /* Quick and dirty cheating hack. */
5707 struct ieee80211_node *ni;
5708
5709 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
5710 return (lkpi_xmit(ni, m, NULL, false));
5711 }
5712
5713 #ifdef LKPI_80211_HT
5714 static int
5715 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
5716 const uint8_t *frm, const uint8_t *efrm)
5717 {
5718 struct ieee80211com *ic;
5719 struct lkpi_hw *lhw;
5720
5721 ic = ni->ni_ic;
5722 lhw = ic->ic_softc;
5723
5724 IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
5725
5726 return (lhw->ic_recv_action(ni, wh, frm, efrm));
5727 }
5728
5729 static int
5730 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
5731 {
5732 struct ieee80211com *ic;
5733 struct lkpi_hw *lhw;
5734
5735 ic = ni->ni_ic;
5736 lhw = ic->ic_softc;
5737
5738 IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
5739
5740 return (lhw->ic_send_action(ni, category, action, sa));
5741 }
5742
5743
5744 static int
5745 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5746 {
5747 struct ieee80211com *ic;
5748 struct lkpi_hw *lhw;
5749
5750 ic = ni->ni_ic;
5751 lhw = ic->ic_softc;
5752
5753 IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
5754
5755 return (lhw->ic_ampdu_enable(ni, tap));
5756 }
5757
5758 /*
5759 * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
5760 * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
5761 *
5762 * NB: returns 0 on ERROR!
5763 */
5764 static int
5765 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5766 int dialogtoken, int baparamset, int batimeout)
5767 {
5768 struct ieee80211com *ic;
5769 struct lkpi_hw *lhw;
5770 struct ieee80211_hw *hw;
5771 struct ieee80211vap *vap;
5772 struct lkpi_vif *lvif;
5773 struct ieee80211_vif *vif;
5774 struct lkpi_sta *lsta;
5775 struct ieee80211_sta *sta;
5776 struct ieee80211_ampdu_params params = { };
5777 int error;
5778
5779 ic = ni->ni_ic;
5780 lhw = ic->ic_softc;
5781 hw = LHW_TO_HW(lhw);
5782 vap = ni->ni_vap;
5783 lvif = VAP_TO_LVIF(vap);
5784 vif = LVIF_TO_VIF(lvif);
5785 lsta = ni->ni_drv_data;
5786 sta = LSTA_TO_STA(lsta);
5787
5788 if (!lsta->added_to_drv) {
5789 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5790 __func__, lsta, ni, sta);
5791 return (0);
5792 }
5793
5794 params.sta = sta;
5795 params.action = IEEE80211_AMPDU_TX_START;
5796 /* Keep 0 here! */
5797 params.buf_size = 0;
5798 params.timeout = 0;
5799 params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
5800 params.tid = tap->txa_tid;
5801 params.amsdu = false;
5802
5803 IEEE80211_UNLOCK(ic);
5804 wiphy_lock(hw->wiphy);
5805 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5806 wiphy_unlock(hw->wiphy);
5807 IEEE80211_LOCK(ic);
5808 if (error != 0) {
5809 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5810 __func__, error, ni, tap);
5811 return (0);
5812 }
5813
5814 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
5815 }
5816
5817 /*
5818 * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
5819 * and calls the default ieee80211_addba_response() which always returns 1.
5820 *
5821 * NB: No error checking in net80211!
5822 * Staying with 0 is an error.
5823 */
5824 static int
5825 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5826 int status, int baparamset, int batimeout)
5827 {
5828 struct ieee80211com *ic;
5829 struct lkpi_hw *lhw;
5830 struct ieee80211_hw *hw;
5831 struct ieee80211vap *vap;
5832 struct lkpi_vif *lvif;
5833 struct ieee80211_vif *vif;
5834 struct lkpi_sta *lsta;
5835 struct ieee80211_sta *sta;
5836 struct ieee80211_ampdu_params params = { };
5837 int error;
5838
5839 ic = ni->ni_ic;
5840 lhw = ic->ic_softc;
5841 hw = LHW_TO_HW(lhw);
5842 vap = ni->ni_vap;
5843 lvif = VAP_TO_LVIF(vap);
5844 vif = LVIF_TO_VIF(lvif);
5845 lsta = ni->ni_drv_data;
5846 sta = LSTA_TO_STA(lsta);
5847
5848 if (!lsta->added_to_drv) {
5849 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5850 __func__, lsta, ni, sta);
5851 return (0);
5852 }
5853
5854 if (status == IEEE80211_STATUS_SUCCESS) {
5855 params.sta = sta;
5856 params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
5857 params.buf_size = tap->txa_wnd;
5858 params.timeout = 0;
5859 params.ssn = 0;
5860 params.tid = tap->txa_tid;
5861 if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
5862 params.amsdu = true;
5863 else
5864 params.amsdu = false;
5865 } else {
5866 /* We need to free the allocated resources. */
5867 params.sta = sta;
5868 switch (status) {
5869 /* params.action = FLUSH, FLUSH_CONT */
5870 default:
5871 params.action = IEEE80211_AMPDU_TX_STOP_CONT;
5872 break;
5873 }
5874 params.buf_size = 0;
5875 params.timeout = 0;
5876 params.ssn = 0;
5877 params.tid = tap->txa_tid;
5878 params.amsdu = false;
5879 }
5880
5881 IEEE80211_UNLOCK(ic);
5882 wiphy_lock(hw->wiphy);
5883 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5884 wiphy_unlock(hw->wiphy);
5885 IEEE80211_LOCK(ic);
5886 if (error != 0) {
5887 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5888 __func__, error, ni, tap);
5889 return (0);
5890 }
5891
5892 IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
5893
5894 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
5895 }
5896
5897 /*
5898 * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
5899 * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
5900 */
5901 static void
5902 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5903 {
5904 struct ieee80211com *ic;
5905 struct lkpi_hw *lhw;
5906 struct ieee80211_hw *hw;
5907 struct ieee80211vap *vap;
5908 struct lkpi_vif *lvif;
5909 struct ieee80211_vif *vif;
5910 struct lkpi_sta *lsta;
5911 struct ieee80211_sta *sta;
5912 struct ieee80211_ampdu_params params = { };
5913 int error;
5914
5915 ic = ni->ni_ic;
5916 lhw = ic->ic_softc;
5917 hw = LHW_TO_HW(lhw);
5918 vap = ni->ni_vap;
5919 lvif = VAP_TO_LVIF(vap);
5920 vif = LVIF_TO_VIF(lvif);
5921 lsta = ni->ni_drv_data;
5922 sta = LSTA_TO_STA(lsta);
5923
5924 if (!lsta->added_to_drv) {
5925 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
5926 __func__, lsta, ni, sta);
5927 goto n80211;
5928 }
5929
5930 /* We need to free the allocated resources. */
5931 params.sta = sta;
5932 IMPROVE("net80211 does not provide a reason to us");
5933 params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
5934 params.buf_size = 0;
5935 params.timeout = 0;
5936 params.ssn = 0;
5937 params.tid = tap->txa_tid;
5938 params.amsdu = false;
5939
5940 IEEE80211_UNLOCK(ic);
5941 wiphy_lock(hw->wiphy);
5942 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
5943 wiphy_unlock(hw->wiphy);
5944 IEEE80211_LOCK(ic);
5945 if (error != 0) {
5946 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
5947 __func__, error, ni, tap);
5948 goto n80211;
5949 }
5950
5951 IMPROVE_HT("anyting else?");
5952
5953 n80211:
5954 lhw->ic_addba_stop(ni, tap);
5955 }
5956
5957 static void
5958 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5959 {
5960 struct ieee80211com *ic;
5961 struct lkpi_hw *lhw;
5962
5963 ic = ni->ni_ic;
5964 lhw = ic->ic_softc;
5965
5966 IMPROVE_HT();
5967
5968 lhw->ic_addba_response_timeout(ni, tap);
5969 }
5970
5971 static void
5972 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5973 int status)
5974 {
5975 struct ieee80211com *ic;
5976 struct lkpi_hw *lhw;
5977
5978 ic = ni->ni_ic;
5979 lhw = ic->ic_softc;
5980
5981 IMPROVE_HT();
5982
5983 lhw->ic_bar_response(ni, tap, status);
5984 }
5985
5986 static int
5987 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
5988 int baparamset, int batimeout, int baseqctl)
5989 {
5990 struct ieee80211com *ic;
5991 struct lkpi_hw *lhw;
5992 struct ieee80211_hw *hw;
5993 struct ieee80211vap *vap;
5994 struct lkpi_vif *lvif;
5995 struct ieee80211_vif *vif;
5996 struct lkpi_sta *lsta;
5997 struct ieee80211_sta *sta;
5998 struct ieee80211_ampdu_params params = { };
5999 int error;
6000
6001 ic = ni->ni_ic;
6002 lhw = ic->ic_softc;
6003 hw = LHW_TO_HW(lhw);
6004 vap = ni->ni_vap;
6005 lvif = VAP_TO_LVIF(vap);
6006 vif = LVIF_TO_VIF(lvif);
6007 lsta = ni->ni_drv_data;
6008 sta = LSTA_TO_STA(lsta);
6009
6010 IEEE80211_UNLOCK_ASSERT(ic);
6011
6012 if (!lsta->added_to_drv) {
6013 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6014 __func__, lsta, ni, vap, sta);
6015 return (-ENXIO);
6016 }
6017
6018 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6019 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6020 __func__, lsta, ni, vap, sta, lsta->state);
6021 return (-ENXIO);
6022 }
6023
6024 params.sta = sta;
6025 params.action = IEEE80211_AMPDU_RX_START;
6026 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
6027 if (params.buf_size == 0)
6028 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
6029 else
6030 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
6031 if (hw->max_rx_aggregation_subframes > 0 &&
6032 params.buf_size > hw->max_rx_aggregation_subframes)
6033 params.buf_size = hw->max_rx_aggregation_subframes;
6034 params.timeout = le16toh(batimeout);
6035 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
6036 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
6037
6038 /* Based on net80211::ampdu_rx_start(). */
6039 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
6040 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
6041 params.amsdu = true;
6042 else
6043 params.amsdu = false;
6044
6045 wiphy_lock(hw->wiphy);
6046 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6047 wiphy_unlock(hw->wiphy);
6048 if (error != 0) {
6049 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6050 __func__, error, ni, rap);
6051 return (error);
6052 }
6053
6054 if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
6055 IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
6056 }
6057
6058 IMPROVE_HT("net80211 is missing the error check on return and assumes success");
6059
6060 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
6061 return (error);
6062 }
6063
6064 static void
6065 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
6066 {
6067 struct ieee80211com *ic;
6068 struct lkpi_hw *lhw;
6069 struct ieee80211_hw *hw;
6070 struct ieee80211vap *vap;
6071 struct lkpi_vif *lvif;
6072 struct ieee80211_vif *vif;
6073 struct lkpi_sta *lsta;
6074 struct ieee80211_sta *sta;
6075 struct ieee80211_ampdu_params params = { };
6076 int error;
6077 uint8_t tid;
6078 bool ic_locked;
6079
6080 ic = ni->ni_ic;
6081 lhw = ic->ic_softc;
6082
6083 /*
6084 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
6085 * we did not START. Some drivers pass it down to firmware which will
6086 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
6087 * ieee80211_ht_node_init() amongst others which will iterate over all
6088 * tid and call ic_ampdu_rx_stop() unconditionally.
6089 * XXX net80211 should probably be more "gentle" in these cases and
6090 * track some state itself.
6091 */
6092 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
6093 goto net80211_only;
6094
6095 hw = LHW_TO_HW(lhw);
6096 vap = ni->ni_vap;
6097 lvif = VAP_TO_LVIF(vap);
6098 vif = LVIF_TO_VIF(lvif);
6099 lsta = ni->ni_drv_data;
6100 if (lsta == NULL) {
6101 ic_printf(ic, "%s: lsta %p ni %p vap %p, lsta is NULL\n",
6102 __func__, lsta, ni, vap);
6103 goto net80211_only;
6104 }
6105 sta = LSTA_TO_STA(lsta);
6106
6107 if (!lsta->added_to_drv) {
6108 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6109 __func__, lsta, ni, vap, sta);
6110 goto net80211_only;
6111 }
6112
6113 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6114 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6115 __func__, lsta, ni, vap, sta, lsta->state);
6116 goto net80211_only;
6117 }
6118
6119 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
6120 for (tid = 0; tid < WME_NUM_TID; tid++) {
6121 if (&ni->ni_rx_ampdu[tid] == rap)
6122 break;
6123 }
6124 if (tid == WME_NUM_TID) {
6125 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p TID not found\n",
6126 __func__, lsta, ni, vap, sta);
6127 goto net80211_only;
6128 }
6129
6130 params.sta = sta;
6131 params.action = IEEE80211_AMPDU_RX_STOP;
6132 params.buf_size = 0;
6133 params.timeout = 0;
6134 params.ssn = 0;
6135 params.tid = tid;
6136 params.amsdu = false;
6137
6138 ic_locked = IEEE80211_IS_LOCKED(ic);
6139 if (ic_locked)
6140 IEEE80211_UNLOCK(ic);
6141 wiphy_lock(hw->wiphy);
6142 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6143 wiphy_unlock(hw->wiphy);
6144 if (ic_locked)
6145 IEEE80211_LOCK(ic);
6146 if (error != 0)
6147 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6148 __func__, error, ni, rap);
6149
6150 net80211_only:
6151 lhw->ic_ampdu_rx_stop(ni, rap);
6152 }
6153 #endif
6154
6155 static void
6156 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
6157 uint8_t *bands, int *chan_flags, enum nl80211_band band)
6158 {
6159 #ifdef LKPI_80211_HT
6160 struct ieee80211_sta_ht_cap *ht_cap;
6161
6162 ht_cap = &hw->wiphy->bands[band]->ht_cap;
6163 if (!ht_cap->ht_supported)
6164 return;
6165
6166 switch (band) {
6167 case NL80211_BAND_2GHZ:
6168 setbit(bands, IEEE80211_MODE_11NG);
6169 break;
6170 case NL80211_BAND_5GHZ:
6171 setbit(bands, IEEE80211_MODE_11NA);
6172 break;
6173 default:
6174 IMPROVE("Unsupported band %d", band);
6175 return;
6176 }
6177
6178 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */
6179
6180 /*
6181 * Rather than manually checking each flag and
6182 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
6183 * simply copy the 16bits.
6184 */
6185 ic->ic_htcaps |= ht_cap->cap;
6186
6187 /* Then deal with the other flags. */
6188 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
6189 ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
6190 #ifdef __notyet__
6191 if (ieee80211_hw_check(hw, TX_AMSDU))
6192 ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
6193 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
6194 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
6195 IEEE80211_HTC_TX_AMSDU_AMPDU);
6196 #endif
6197
6198 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
6199 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
6200
6201 /* Only add HT40 channels if supported. */
6202 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
6203 chan_flags != NULL)
6204 *chan_flags |= NET80211_CBW_FLAG_HT40;
6205 #endif
6206 }
6207
6208 static void
6209 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
6210 int *n, struct ieee80211_channel *c)
6211 {
6212 struct lkpi_hw *lhw;
6213 struct ieee80211_hw *hw;
6214 struct linuxkpi_ieee80211_channel *channels;
6215 uint8_t bands[IEEE80211_MODE_BYTES];
6216 int chan_flags, error, i, nchans;
6217
6218 /* Channels */
6219 lhw = ic->ic_softc;
6220 hw = LHW_TO_HW(lhw);
6221
6222 /* NL80211_BAND_2GHZ */
6223 nchans = 0;
6224 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
6225 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
6226 if (nchans > 0) {
6227 memset(bands, 0, sizeof(bands));
6228 chan_flags = 0;
6229 setbit(bands, IEEE80211_MODE_11B);
6230 /* XXX-BZ unclear how to check for 11g. */
6231
6232 IMPROVE("the bitrates may have flags?");
6233 setbit(bands, IEEE80211_MODE_11G);
6234
6235 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
6236 NL80211_BAND_2GHZ);
6237
6238 channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
6239 for (i = 0; i < nchans && *n < maxchan; i++) {
6240 uint32_t nflags = 0;
6241 int cflags = chan_flags;
6242
6243 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
6244 ic_printf(ic, "%s: Skipping disabled chan "
6245 "[%u/%u/%#x]\n", __func__,
6246 channels[i].hw_value,
6247 channels[i].center_freq, channels[i].flags);
6248 continue;
6249 }
6250 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
6251 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
6252 if (channels[i].flags & IEEE80211_CHAN_RADAR)
6253 nflags |= IEEE80211_CHAN_DFS;
6254 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
6255 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
6256 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
6257 cflags &= ~NET80211_CBW_FLAG_VHT80;
6258 /* XXX how to map the remaining enum ieee80211_channel_flags? */
6259 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
6260 cflags &= ~NET80211_CBW_FLAG_HT40;
6261
6262 error = ieee80211_add_channel_cbw(c, maxchan, n,
6263 ieee80211_mhz2ieee(channels[i].center_freq,
6264 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
6265 channels[i].center_freq, channels[i].max_power,
6266 nflags, bands, cflags);
6267 /* net80211::ENOBUFS: *n >= maxchans */
6268 if (error != 0 && error != ENOBUFS)
6269 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
6270 "returned error %d\n",
6271 __func__, channels[i].hw_value,
6272 channels[i].center_freq, channels[i].flags,
6273 nflags, chan_flags, cflags, error);
6274 if (error != 0)
6275 break;
6276 }
6277 }
6278
6279 /* NL80211_BAND_5GHZ */
6280 nchans = 0;
6281 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
6282 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
6283 if (nchans > 0) {
6284 memset(bands, 0, sizeof(bands));
6285 chan_flags = 0;
6286 setbit(bands, IEEE80211_MODE_11A);
6287
6288 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
6289 NL80211_BAND_5GHZ);
6290
6291 #ifdef LKPI_80211_VHT
6292 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
6293
6294 ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
6295 ic->ic_vht_cap.vht_cap_info =
6296 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
6297 ic->ic_vht_cap.supp_mcs =
6298 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
6299
6300 setbit(bands, IEEE80211_MODE_VHT_5GHZ);
6301 chan_flags |= NET80211_CBW_FLAG_VHT80;
6302 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
6303 ic->ic_vht_cap.vht_cap_info))
6304 chan_flags |= NET80211_CBW_FLAG_VHT160;
6305 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
6306 ic->ic_vht_cap.vht_cap_info))
6307 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
6308 }
6309 #endif
6310
6311 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
6312 for (i = 0; i < nchans && *n < maxchan; i++) {
6313 uint32_t nflags = 0;
6314 int cflags = chan_flags;
6315
6316 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
6317 ic_printf(ic, "%s: Skipping disabled chan "
6318 "[%u/%u/%#x]\n", __func__,
6319 channels[i].hw_value,
6320 channels[i].center_freq, channels[i].flags);
6321 continue;
6322 }
6323 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
6324 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
6325 if (channels[i].flags & IEEE80211_CHAN_RADAR)
6326 nflags |= IEEE80211_CHAN_DFS;
6327 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
6328 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
6329 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
6330 cflags &= ~NET80211_CBW_FLAG_VHT80;
6331 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
6332 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
6333 cflags &= ~NET80211_CBW_FLAG_HT40;
6334
6335 error = ieee80211_add_channel_cbw(c, maxchan, n,
6336 ieee80211_mhz2ieee(channels[i].center_freq,
6337 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
6338 channels[i].center_freq, channels[i].max_power,
6339 nflags, bands, cflags);
6340 /* net80211::ENOBUFS: *n >= maxchans */
6341 if (error != 0 && error != ENOBUFS)
6342 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
6343 "returned error %d\n",
6344 __func__, channels[i].hw_value,
6345 channels[i].center_freq, channels[i].flags,
6346 nflags, chan_flags, cflags, error);
6347 if (error != 0)
6348 break;
6349 }
6350 }
6351 }
6352
6353 static void *
6354 lkpi_ieee80211_ifalloc(void)
6355 {
6356 struct ieee80211com *ic;
6357
6358 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
6359
6360 /* Setting these happens later when we have device information. */
6361 ic->ic_softc = NULL;
6362 ic->ic_name = "linuxkpi";
6363
6364 return (ic);
6365 }
6366
6367 struct ieee80211_hw *
6368 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
6369 {
6370 struct ieee80211_hw *hw;
6371 struct lkpi_hw *lhw;
6372 struct wiphy *wiphy;
6373 int ac;
6374
6375 /* Get us and the driver data also allocated. */
6376 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
6377 if (wiphy == NULL)
6378 return (NULL);
6379
6380 lhw = wiphy_priv(wiphy);
6381 lhw->ops = ops;
6382
6383 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
6384 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
6385 spin_lock_init(&lhw->txq_lock);
6386 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
6387 LKPI_80211_LHW_MC_LOCK_INIT(lhw);
6388 TAILQ_INIT(&lhw->lvif_head);
6389 __hw_addr_init(&lhw->mc_list);
6390 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
6391 lhw->txq_generation[ac] = 1;
6392 TAILQ_INIT(&lhw->scheduled_txqs[ac]);
6393 }
6394
6395 /* Chanctx_conf */
6396 INIT_LIST_HEAD(&lhw->lchanctx_list);
6397
6398 /* Deferred RX path. */
6399 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
6400 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
6401 mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
6402 lhw->rxq_stopped = false;
6403
6404 /*
6405 * XXX-BZ TODO make sure there is a "_null" function to all ops
6406 * not initialized.
6407 */
6408 hw = LHW_TO_HW(lhw);
6409 hw->wiphy = wiphy;
6410 hw->conf.flags |= IEEE80211_CONF_IDLE;
6411 hw->priv = (void *)(lhw + 1);
6412
6413 /* BSD Specific. */
6414 lhw->ic = lkpi_ieee80211_ifalloc();
6415
6416 IMPROVE();
6417
6418 return (hw);
6419 }
6420
6421 void
6422 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
6423 {
6424 struct lkpi_hw *lhw;
6425 struct mbuf *m;
6426
6427 lhw = HW_TO_LHW(hw);
6428 free(lhw->ic, M_LKPI80211);
6429 lhw->ic = NULL;
6430
6431 /*
6432 * Drain the deferred RX path.
6433 */
6434 LKPI_80211_LHW_RXQ_LOCK(lhw);
6435 lhw->rxq_stopped = true;
6436 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
6437
6438 /* Drain taskq, won't be restarted due to rxq_stopped being set. */
6439 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
6440 taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
6441
6442 /* Flush mbufq (make sure to release ni refs!). */
6443 m = mbufq_dequeue(&lhw->rxq);
6444 while (m != NULL) {
6445 #ifdef LKPI_80211_USE_MTAG
6446 struct m_tag *mtag;
6447
6448 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
6449 if (mtag != NULL) {
6450 struct lkpi_80211_tag_rxni *rxni;
6451
6452 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
6453 ieee80211_free_node(rxni->ni);
6454 }
6455 #else
6456 if (m->m_pkthdr.PH_loc.ptr != NULL) {
6457 struct ieee80211_node *ni;
6458
6459 ni = m->m_pkthdr.PH_loc.ptr;
6460 ieee80211_free_node(ni);
6461 }
6462 #endif
6463 m_freem(m);
6464 m = mbufq_dequeue(&lhw->rxq);
6465 }
6466 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
6467 __func__, lhw, mbufq_len(&lhw->rxq)));
6468 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
6469
6470 /* Chanctx_conf. */
6471 if (!list_empty_careful(&lhw->lchanctx_list)) {
6472 struct lkpi_chanctx *lchanctx, *next;
6473 struct ieee80211_chanctx_conf *chanctx_conf;
6474
6475 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
6476 if (lchanctx->added_to_drv) {
6477 /* In reality we should panic? */
6478 chanctx_conf = &lchanctx->chanctx_conf;
6479 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
6480 }
6481 list_del(&lchanctx->entry);
6482 free(lchanctx, M_LKPI80211);
6483 }
6484 }
6485
6486 LKPI_80211_LHW_MC_LOCK(lhw);
6487 lkpi_cleanup_mcast_list_locked(lhw);
6488 LKPI_80211_LHW_MC_UNLOCK(lhw);
6489
6490 /* Cleanup more of lhw here or in wiphy_free()? */
6491 spin_lock_destroy(&lhw->txq_lock);
6492 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
6493 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
6494 sx_destroy(&lhw->lvif_sx);
6495 LKPI_80211_LHW_MC_LOCK_DESTROY(lhw)
6496 IMPROVE();
6497 }
6498
6499 void
6500 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw)
6501 {
6502 struct lkpi_hw *lhw;
6503 struct ieee80211com *ic;
6504 struct device *dev;
6505
6506 lhw = HW_TO_LHW(hw);
6507 ic = lhw->ic;
6508
6509 /* Save the backpointer from net80211 to LinuxKPI. */
6510 ic->ic_softc = lhw;
6511
6512 /*
6513 * Set a proper name before ieee80211_ifattach() if dev is set.
6514 * ath1xk also unset the dev so we need to check.
6515 */
6516 dev = wiphy_dev(hw->wiphy);
6517 if (dev != NULL) {
6518 ic->ic_name = dev_name(dev);
6519 } else {
6520 TODO("adjust arguments to still have the old dev or go through "
6521 "the hoops of getting the bsddev from hw and detach; "
6522 "or do in XXX; check ath1kx drivers");
6523 }
6524
6525 /* XXX-BZ do we also need to set wiphy name? */
6526 }
6527
6528 struct ieee80211_hw *
6529 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
6530 {
6531 struct lkpi_hw *lhw;
6532
6533 lhw = wiphy_priv(wiphy);
6534 return (LHW_TO_HW(lhw));
6535 }
6536
6537 static void
6538 lkpi_radiotap_attach(struct lkpi_hw *lhw)
6539 {
6540 struct ieee80211com *ic;
6541
6542 ic = lhw->ic;
6543 ieee80211_radiotap_attach(ic,
6544 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
6545 LKPI_RTAP_TX_FLAGS_PRESENT,
6546 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
6547 LKPI_RTAP_RX_FLAGS_PRESENT);
6548 }
6549
6550 int
6551 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
6552 {
6553 struct ieee80211com *ic;
6554 struct lkpi_hw *lhw;
6555 int band, i;
6556
6557 lhw = HW_TO_LHW(hw);
6558 ic = lhw->ic;
6559
6560 /* We do it this late as wiphy->dev should be set for the name. */
6561 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
6562 if (lhw->workq == NULL)
6563 return (-EAGAIN);
6564
6565 /* XXX-BZ figure this out how they count his... */
6566 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
6567 IEEE80211_ADDR_COPY(ic->ic_macaddr,
6568 hw->wiphy->perm_addr);
6569 } else if (hw->wiphy->n_addresses > 0) {
6570 /* We take the first one. */
6571 IEEE80211_ADDR_COPY(ic->ic_macaddr,
6572 hw->wiphy->addresses[0].addr);
6573 } else {
6574 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
6575 }
6576
6577 #ifdef __not_yet__
6578 /* See comment in lkpi_80211_txq_tx_one(). */
6579 ic->ic_headroom = hw->extra_tx_headroom;
6580 #endif
6581
6582 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
6583 ic->ic_opmode = IEEE80211_M_STA;
6584
6585 /* Set device capabilities. */
6586 /* XXX-BZ we need to get these from linux80211/drivers and convert. */
6587 ic->ic_caps =
6588 IEEE80211_C_STA |
6589 IEEE80211_C_MONITOR |
6590 IEEE80211_C_WPA | /* WPA/RSN */
6591 #ifdef LKPI_80211_WME
6592 IEEE80211_C_WME |
6593 #endif
6594 #if 0
6595 IEEE80211_C_PMGT |
6596 #endif
6597 IEEE80211_C_SHSLOT | /* short slot time supported */
6598 IEEE80211_C_SHPREAMBLE /* short preamble supported */
6599 ;
6600
6601 #ifdef LKPI_80211_BGSCAN
6602 if (lhw->ops->hw_scan)
6603 ic->ic_caps |= IEEE80211_C_BGSCAN;
6604 #endif
6605
6606 lkpi_enable_hw_scan(lhw);
6607
6608 /* Does HW support Fragmentation offload? */
6609 if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
6610 ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
6611
6612 /*
6613 * The wiphy variables report bitmasks of avail antennas.
6614 * (*get_antenna) get the current bitmask sets which can be
6615 * altered by (*set_antenna) for some drivers.
6616 * XXX-BZ will the count alone do us much good long-term in net80211?
6617 */
6618 if (hw->wiphy->available_antennas_rx ||
6619 hw->wiphy->available_antennas_tx) {
6620 uint32_t rxs, txs;
6621
6622 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
6623 ic->ic_rxstream = bitcount32(rxs);
6624 ic->ic_txstream = bitcount32(txs);
6625 }
6626 }
6627
6628 ic->ic_cryptocaps = 0;
6629 #ifdef LKPI_80211_HW_CRYPTO
6630 if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
6631 uint32_t hwciphers;
6632
6633 hwciphers = 0;
6634 for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
6635 uint32_t cs;
6636
6637 cs = lkpi_l80211_to_net80211_cyphers(
6638 ic, hw->wiphy->cipher_suites[i]);
6639 if (cs == IEEE80211_CRYPTO_TKIP) {
6640 /*
6641 * We do set this here. We will only find out
6642 * when doing a SET_KEY operation depending on
6643 * what the driver returns.
6644 * net80211::ieee80211_crypto_newkey()
6645 * checks this so we will have to do flags
6646 * surgery later.
6647 */
6648 cs |= IEEE80211_CRYPTO_TKIPMIC;
6649 }
6650 hwciphers |= cs;
6651 }
6652 /*
6653 * (20250415) nothing anywhere in the path checks we actually
6654 * support all these in net80211.
6655 * net80211 supports _256 variants but the ioctl does not.
6656 */
6657 IMPROVE("as net80211 grows more support, enable them");
6658 hwciphers &= (IEEE80211_CRYPTO_WEP |
6659 IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
6660 IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
6661 /*
6662 * We only support CCMP here, so further filter.
6663 * Also permit TKIP if turned on.
6664 */
6665 hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
6666 IEEE80211_CRYPTO_AES_GCM_128 |
6667 (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
6668 IEEE80211_CRYPTO_TKIPMIC) : 0));
6669 ieee80211_set_hardware_ciphers(ic, hwciphers);
6670 }
6671 #endif
6672
6673 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
6674 ic->ic_channels);
6675
6676 ieee80211_ifattach(ic);
6677
6678 ic->ic_update_mcast = lkpi_ic_update_mcast;
6679 ic->ic_update_promisc = lkpi_ic_update_promisc;
6680 ic->ic_update_chw = lkpi_ic_update_chw;
6681 ic->ic_parent = lkpi_ic_parent;
6682 ic->ic_scan_start = lkpi_ic_scan_start;
6683 ic->ic_scan_end = lkpi_ic_scan_end;
6684 ic->ic_set_channel = lkpi_ic_set_channel;
6685 ic->ic_transmit = lkpi_ic_transmit;
6686 ic->ic_raw_xmit = lkpi_ic_raw_xmit;
6687 ic->ic_vap_create = lkpi_ic_vap_create;
6688 ic->ic_vap_delete = lkpi_ic_vap_delete;
6689 ic->ic_getradiocaps = lkpi_ic_getradiocaps;
6690 ic->ic_wme.wme_update = lkpi_ic_wme_update;
6691
6692 lhw->ic_scan_curchan = ic->ic_scan_curchan;
6693 ic->ic_scan_curchan = lkpi_ic_scan_curchan;
6694 lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
6695 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
6696
6697 lhw->ic_node_alloc = ic->ic_node_alloc;
6698 ic->ic_node_alloc = lkpi_ic_node_alloc;
6699 lhw->ic_node_init = ic->ic_node_init;
6700 ic->ic_node_init = lkpi_ic_node_init;
6701 lhw->ic_node_cleanup = ic->ic_node_cleanup;
6702 ic->ic_node_cleanup = lkpi_ic_node_cleanup;
6703 lhw->ic_node_free = ic->ic_node_free;
6704 ic->ic_node_free = lkpi_ic_node_free;
6705
6706 #ifdef LKPI_80211_HT
6707 /*
6708 * Only attach if the driver/firmware supports (*ampdu_action)().
6709 * Otherwise it is in the hands of net80211.
6710 */
6711 if (lhw->ops->ampdu_action != NULL) {
6712 lhw->ic_recv_action = ic->ic_recv_action;
6713 ic->ic_recv_action = lkpi_ic_recv_action;
6714 lhw->ic_send_action = ic->ic_send_action;
6715 ic->ic_send_action = lkpi_ic_send_action;
6716
6717 lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
6718 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
6719
6720 lhw->ic_addba_request = ic->ic_addba_request;
6721 ic->ic_addba_request = lkpi_ic_addba_request;
6722 lhw->ic_addba_response = ic->ic_addba_response;
6723 ic->ic_addba_response = lkpi_ic_addba_response;
6724 lhw->ic_addba_stop = ic->ic_addba_stop;
6725 ic->ic_addba_stop = lkpi_ic_addba_stop;
6726 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
6727 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
6728
6729 lhw->ic_bar_response = ic->ic_bar_response;
6730 ic->ic_bar_response = lkpi_ic_bar_response;
6731
6732 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
6733 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
6734 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
6735 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
6736 }
6737 #endif
6738
6739 lkpi_radiotap_attach(lhw);
6740
6741 /*
6742 * Assign the first possible channel for now; seems Realtek drivers
6743 * expect one.
6744 * Also remember the amount of bands we support and the most rates
6745 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
6746 */
6747 lhw->supbands = lhw->max_rates = 0;
6748 for (band = 0; band < NUM_NL80211_BANDS; band++) {
6749 struct ieee80211_supported_band *supband;
6750 struct linuxkpi_ieee80211_channel *channels;
6751
6752 supband = hw->wiphy->bands[band];
6753 if (supband == NULL || supband->n_channels == 0)
6754 continue;
6755
6756 lhw->supbands++;
6757 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
6758
6759 /* If we have a channel, we need to keep counting supbands. */
6760 if (hw->conf.chandef.chan != NULL)
6761 continue;
6762
6763 channels = supband->channels;
6764 for (i = 0; i < supband->n_channels; i++) {
6765
6766 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
6767 continue;
6768
6769 cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
6770 #ifdef LKPI_80211_HT
6771 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
6772 #endif
6773 NL80211_CHAN_NO_HT);
6774 break;
6775 }
6776 }
6777
6778 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
6779
6780 /* Make sure we do not support more than net80211 is willing to take. */
6781 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
6782 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
6783 lhw->max_rates, IEEE80211_RATE_MAXSIZE);
6784 lhw->max_rates = IEEE80211_RATE_MAXSIZE;
6785 }
6786
6787 /*
6788 * The maximum supported bitrates on any band + size for
6789 * DSSS Parameter Set give our per-band IE size.
6790 * SSID is the responsibility of the driver and goes on the side.
6791 * The user specified bits coming from the vap go into the
6792 * "common ies" fields.
6793 */
6794 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
6795 if (lhw->max_rates > IEEE80211_RATE_SIZE)
6796 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
6797
6798 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
6799 /*
6800 * net80211 does not seem to support the DSSS Parameter Set but
6801 * some of the drivers insert it so calculate the extra fixed
6802 * space in.
6803 */
6804 lhw->scan_ie_len += 2 + 1;
6805 }
6806
6807 #if defined(LKPI_80211_HT)
6808 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
6809 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
6810 #endif
6811 #if defined(LKPI_80211_VHT)
6812 if (IEEE80211_CONF_VHT(ic))
6813 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
6814 #endif
6815
6816 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */
6817 if (hw->wiphy->max_scan_ie_len > 0) {
6818 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
6819 goto err;
6820 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
6821 }
6822
6823 if (bootverbose) {
6824 ic_printf(ic, "netdev_features %b\n", hw->netdev_features, NETIF_F_BITS);
6825 ieee80211_announce(ic);
6826 }
6827
6828 return (0);
6829 err:
6830 IMPROVE("TODO FIXME CLEANUP");
6831 return (-EAGAIN);
6832 }
6833
6834 void
6835 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
6836 {
6837 struct lkpi_hw *lhw;
6838 struct ieee80211com *ic;
6839
6840 lhw = HW_TO_LHW(hw);
6841 ic = lhw->ic;
6842 ieee80211_ifdetach(ic);
6843 }
6844
6845 void
6846 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
6847 enum ieee80211_iface_iter flags,
6848 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
6849 void *arg)
6850 {
6851 struct lkpi_hw *lhw;
6852 struct lkpi_vif *lvif;
6853 struct ieee80211_vif *vif;
6854 bool active, atomic, nin_drv;
6855
6856 lhw = HW_TO_LHW(hw);
6857
6858 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
6859 IEEE80211_IFACE_ITER_RESUME_ALL|
6860 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
6861 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
6862 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
6863 __func__, flags);
6864 }
6865
6866 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
6867 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
6868 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
6869
6870 if (atomic)
6871 LKPI_80211_LHW_LVIF_LOCK(lhw);
6872 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6873 struct ieee80211vap *vap;
6874
6875 vif = LVIF_TO_VIF(lvif);
6876
6877 /*
6878 * If we want "active" interfaces, we need to distinguish on
6879 * whether the driver knows about them or not to be able to
6880 * handle the "resume" case correctly. Skip the ones the
6881 * driver does not know about.
6882 */
6883 if (active && !lvif->added_to_drv &&
6884 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
6885 continue;
6886
6887 /*
6888 * If we shall skip interfaces not added to the driver do so
6889 * if we haven't yet.
6890 */
6891 if (nin_drv && !lvif->added_to_drv)
6892 continue;
6893
6894 /*
6895 * Run the iterator function if we are either not asking
6896 * asking for active only or if the VAP is "running".
6897 */
6898 /* XXX-BZ probably should have state in the lvif as well. */
6899 vap = LVIF_TO_VAP(lvif);
6900 if (!active || (vap->iv_state != IEEE80211_S_INIT))
6901 iterfunc(arg, vif->addr, vif);
6902 }
6903 if (atomic)
6904 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6905 }
6906
6907 static void
6908 lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6909 ieee80211_keyix keyix, struct lkpi_sta *lsta,
6910 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6911 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6912 void *arg)
6913 {
6914 if (!lsta->added_to_drv)
6915 return;
6916
6917 if (lsta->kc[keyix] == NULL)
6918 return;
6919
6920 iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
6921 }
6922
6923 void
6924 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
6925 struct ieee80211_vif *vif,
6926 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
6927 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
6928 void *arg, bool rcu)
6929 {
6930 struct lkpi_sta *lsta;
6931 struct lkpi_vif *lvif;
6932
6933 lvif = VIF_TO_LVIF(vif);
6934
6935 if (rcu) {
6936 rcu_read_lock_held(); /* XXX-BZ is this correct? */
6937
6938 if (vif == NULL) {
6939 TODO();
6940 } else {
6941 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
6942 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6943 keyix++)
6944 lkpi_ieee80211_iterate_keys(hw, vif,
6945 keyix, lsta, iterfunc, arg);
6946 }
6947 }
6948 } else {
6949 TODO("Used by suspend/resume; order of keys as installed to "
6950 "firmware is important; we'll need to rewrite some code for that");
6951 lockdep_assert_wiphy(hw->wiphy);
6952
6953 if (vif == NULL) {
6954 TODO();
6955 } else {
6956 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
6957 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
6958 keyix++)
6959 lkpi_ieee80211_iterate_keys(hw, vif,
6960 keyix, lsta, iterfunc, arg);
6961 }
6962 }
6963 }
6964 }
6965
6966 void
6967 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
6968 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
6969 void *),
6970 void *arg)
6971 {
6972 struct lkpi_hw *lhw;
6973 struct lkpi_chanctx *lchanctx;
6974
6975 KASSERT(hw != NULL && iterfunc != NULL,
6976 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
6977
6978 lhw = HW_TO_LHW(hw);
6979
6980 rcu_read_lock();
6981 list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
6982 if (!lchanctx->added_to_drv)
6983 continue;
6984 iterfunc(hw, &lchanctx->chanctx_conf, arg);
6985 }
6986 rcu_read_unlock();
6987 }
6988
6989 void
6990 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
6991 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
6992 {
6993 struct lkpi_hw *lhw;
6994 struct lkpi_vif *lvif;
6995 struct lkpi_sta *lsta;
6996 struct ieee80211_sta *sta;
6997
6998 KASSERT(hw != NULL && iterfunc != NULL,
6999 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7000
7001 lhw = HW_TO_LHW(hw);
7002
7003 LKPI_80211_LHW_LVIF_LOCK(lhw);
7004 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7005
7006 rcu_read_lock();
7007 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7008 if (!lsta->added_to_drv)
7009 continue;
7010 sta = LSTA_TO_STA(lsta);
7011 iterfunc(arg, sta);
7012 }
7013 rcu_read_unlock();
7014 }
7015 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7016 }
7017
7018 struct linuxkpi_ieee80211_regdomain *
7019 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
7020 {
7021 struct linuxkpi_ieee80211_regdomain *regd;
7022
7023 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
7024 GFP_KERNEL);
7025 return (regd);
7026 }
7027
7028 int
7029 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
7030 struct linuxkpi_ieee80211_regdomain *regd)
7031 {
7032 struct lkpi_hw *lhw;
7033 struct ieee80211com *ic;
7034 struct ieee80211_regdomain *rd;
7035
7036 lhw = wiphy_priv(wiphy);
7037 ic = lhw->ic;
7038
7039 rd = &ic->ic_regdomain;
7040 if (rd->isocc[0] == '\0') {
7041 rd->isocc[0] = regd->alpha2[0];
7042 rd->isocc[1] = regd->alpha2[1];
7043 }
7044
7045 TODO();
7046 /* XXX-BZ finish the rest. */
7047
7048 return (0);
7049 }
7050
7051 void
7052 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
7053 struct cfg80211_scan_info *info)
7054 {
7055 struct lkpi_hw *lhw;
7056 struct ieee80211com *ic;
7057 struct ieee80211_scan_state *ss;
7058
7059 lhw = wiphy_priv(hw->wiphy);
7060 ic = lhw->ic;
7061 ss = ic->ic_scan;
7062
7063 TRACE_SCAN(ic, "scan_flags %b info { %ju, %6D, aborted %d }",
7064 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7065 (uintmax_t)info->scan_start_tsf, info->tsf_bssid, ":",
7066 info->aborted);
7067
7068 ieee80211_scan_done(ss->ss_vap);
7069
7070 LKPI_80211_LHW_SCAN_LOCK(lhw);
7071 free(lhw->hw_req, M_LKPI80211);
7072 lhw->hw_req = NULL;
7073 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
7074 /* The wakeup(lhw) will be called from lkpi_ic_scan_end(). */
7075 /* wakeup(lhw); */
7076 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
7077
7078 return;
7079 }
7080
7081 static void
7082 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
7083 {
7084 struct ieee80211_node *ni;
7085 #ifdef LKPI_80211_USE_MTAG
7086 struct m_tag *mtag;
7087 #endif
7088 int ok;
7089
7090 ni = NULL;
7091 #ifdef LKPI_80211_USE_MTAG
7092 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7093 if (mtag != NULL) {
7094 struct lkpi_80211_tag_rxni *rxni;
7095
7096 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7097 ni = rxni->ni;
7098 }
7099 #else
7100 if (m->m_pkthdr.PH_loc.ptr != NULL) {
7101 ni = m->m_pkthdr.PH_loc.ptr;
7102 m->m_pkthdr.PH_loc.ptr = NULL;
7103 }
7104 #endif
7105
7106 if (ni != NULL) {
7107 ok = ieee80211_input_mimo(ni, m);
7108 ieee80211_free_node(ni); /* Release the reference. */
7109 if (ok < 0)
7110 m_freem(m);
7111 } else {
7112 ok = ieee80211_input_mimo_all(lhw->ic, m);
7113 /* mbuf got consumed. */
7114 }
7115
7116 #ifdef LINUXKPI_DEBUG_80211
7117 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7118 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
7119 #endif
7120 }
7121
7122 static void
7123 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
7124 {
7125 struct lkpi_hw *lhw;
7126 struct mbufq mq;
7127 struct mbuf *m;
7128
7129 lhw = ctx;
7130
7131 #ifdef LINUXKPI_DEBUG_80211
7132 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7133 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
7134 __func__, lhw, pending, mbufq_len(&lhw->rxq));
7135 #endif
7136
7137 mbufq_init(&mq, IFQ_MAXLEN);
7138
7139 LKPI_80211_LHW_RXQ_LOCK(lhw);
7140 mbufq_concat(&mq, &lhw->rxq);
7141 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7142
7143 m = mbufq_dequeue(&mq);
7144 while (m != NULL) {
7145 lkpi_80211_lhw_rxq_rx_one(lhw, m);
7146 m = mbufq_dequeue(&mq);
7147 }
7148 }
7149
7150 static void
7151 lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
7152 struct ieee80211_rx_status *rx_status,
7153 struct ieee80211_rx_stats *rx_stats,
7154 uint8_t *rssip)
7155 {
7156 struct ieee80211_supported_band *supband;
7157 struct rate_info rxrate;
7158 int i;
7159 uint8_t rssi;
7160
7161 memset(&rxrate, 0, sizeof(rxrate));
7162 memset(rx_stats, 0, sizeof(*rx_stats));
7163 rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
7164 /* XXX-BZ correct hardcoded noise floor, survey data? */
7165 rx_stats->c_nf = -96;
7166 if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
7167 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
7168 rssi = rx_status->signal;
7169 else
7170 rssi = rx_stats->c_nf;
7171 /*
7172 * net80211 signal strength data are in .5 dBm units relative to
7173 * the current noise floor (see comment in ieee80211_node.h).
7174 */
7175 rssi -= rx_stats->c_nf;
7176 if (rssip != NULL)
7177 *rssip = rssi;
7178 rx_stats->c_rssi = rssi * 2;
7179 rx_stats->r_flags |= IEEE80211_R_BAND;
7180 rx_stats->c_band =
7181 lkpi_nl80211_band_to_net80211_band(rx_status->band);
7182 rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
7183 rx_stats->c_freq = rx_status->freq;
7184 rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
7185
7186 rx_stats->c_rx_tsf = rx_status->mactime;
7187
7188 /* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
7189 if ((rx_status->flag & RX_FLAG_MACTIME) ==
7190 (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
7191 rx_stats->r_flags |= IEEE80211_R_TSF64;
7192 /* XXX RX_FLAG_MACTIME_PLCP_START ? */
7193 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
7194 rx_stats->r_flags |= IEEE80211_R_TSF_START;
7195 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
7196 rx_stats->r_flags |= IEEE80211_R_TSF_END;
7197 /* XXX-BZ if TSF_END will net80211 do the unwind of time? */
7198 }
7199
7200 if (rx_status->chains != 0) {
7201 int cc;
7202 int8_t crssi;
7203
7204 rx_stats->c_chain = rx_status->chains;
7205 rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
7206
7207 cc = 0;
7208 for (i = 0; i < nitems(rx_status->chain_signal); i++) {
7209 if (!(rx_status->chains & BIT(i)))
7210 continue;
7211 crssi = rx_status->chain_signal[i];
7212 crssi -= rx_stats->c_nf;
7213 rx_stats->c_rssi_ctl[i] = crssi * 2;
7214 rx_stats->c_rssi_ext[i] = crssi * 2; /* XXX _ext ??? ATH thing? */
7215 /* We currently only have the global noise floor value. */
7216 rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
7217 rx_stats->c_nf_ext[i] = rx_stats->c_nf;
7218 cc++;
7219 }
7220 if (cc > 0)
7221 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
7222 }
7223
7224 /* XXX-NET80211 We are not going to populate c_phytype! */
7225
7226 switch (rx_status->encoding) {
7227 case RX_ENC_LEGACY:
7228 {
7229 uint32_t legacy = 0;
7230
7231 supband = hw->wiphy->bands[rx_status->band];
7232 if (supband != NULL)
7233 legacy = supband->bitrates[rx_status->rate_idx].bitrate;
7234 rx_stats->c_rate = legacy;
7235 rxrate.legacy = legacy;
7236 /* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
7237 break;
7238 }
7239 case RX_ENC_HT:
7240 rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
7241 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
7242 rxrate.flags |= RATE_INFO_FLAGS_MCS;
7243 rxrate.mcs = rx_status->rate_idx;
7244 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
7245 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
7246 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
7247 }
7248 break;
7249 case RX_ENC_VHT:
7250 rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
7251 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
7252 rx_stats->c_vhtnss = rx_status->nss;
7253 rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
7254 rxrate.mcs = rx_status->rate_idx;
7255 rxrate.nss = rx_status->nss;
7256 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
7257 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
7258 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
7259 }
7260 break;
7261 case RX_ENC_HE:
7262 rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
7263 rxrate.mcs = rx_status->rate_idx;
7264 rxrate.nss = rx_status->nss;
7265 /* XXX TODO */
7266 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
7267 break;
7268 case RX_ENC_EHT:
7269 rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
7270 rxrate.mcs = rx_status->rate_idx;
7271 rxrate.nss = rx_status->nss;
7272 /* XXX TODO */
7273 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
7274 break;
7275 }
7276
7277 rxrate.bw = rx_status->bw;
7278 switch (rx_status->bw) {
7279 case RATE_INFO_BW_20:
7280 rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
7281 break;
7282 case RATE_INFO_BW_40:
7283 rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
7284 break;
7285 case RATE_INFO_BW_80:
7286 rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
7287 break;
7288 case RATE_INFO_BW_160:
7289 rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
7290 break;
7291 case RATE_INFO_BW_320:
7292 case RATE_INFO_BW_HE_RU:
7293 case RATE_INFO_BW_EHT_RU:
7294 case RATE_INFO_BW_5:
7295 case RATE_INFO_BW_10:
7296 TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
7297 break;
7298 }
7299
7300 if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
7301 rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
7302 if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
7303 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
7304
7305 /*
7306 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
7307 * case the hardware does something we do not expect always leave
7308 * these enabled. Leaving this commant as documentation for the || 1.
7309 */
7310 #if defined(LKPI_80211_HW_CRYPTO) || 1
7311 if (rx_status->flag & RX_FLAG_DECRYPTED) {
7312 rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
7313 /* Only valid if decrypted is set. */
7314 if (rx_status->flag & RX_FLAG_PN_VALIDATED)
7315 rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
7316 }
7317 if (rx_status->flag & RX_FLAG_IV_STRIPPED)
7318 rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
7319 if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
7320 rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
7321 if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
7322 rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
7323 if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
7324 rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
7325 if (rx_status->flag & RX_FLAG_MMIC_ERROR)
7326 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
7327 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
7328 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
7329 #endif
7330
7331 /* Fill in some sinfo bits to fill gaps not reported byt the driver. */
7332 if (lsta != NULL) {
7333 memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
7334 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
7335
7336 if (rx_status->chains != 0) {
7337 lsta->sinfo.chains = rx_status->chains;
7338 memcpy(lsta->sinfo.chain_signal, rx_status->chain_signal,
7339 sizeof(lsta->sinfo.chain_signal));
7340 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
7341 }
7342 }
7343 }
7344
7345 #ifdef LINUXKPI_DEBUG_80211
7346 static void
7347 lkpi_rx_log_beacon(struct mbuf *m, struct lkpi_hw *lhw,
7348 struct ieee80211_rx_status *rx_status)
7349 {
7350 struct ieee80211_mgmt *f;
7351 uint8_t *e;
7352 char ssid[IEEE80211_NWID_LEN * 4 + 1];
7353
7354 memset(ssid, '\0', sizeof(ssid));
7355
7356 f = mtod(m, struct ieee80211_mgmt *);
7357 e = f->u.beacon.variable;
7358 /*
7359 * Usually SSID is right after the fixed part and for debugging we will
7360 * be fine should we miss it if it is not.
7361 */
7362 while ((e - (uint8_t *)f) < m->m_len) {
7363 if (*e == IEEE80211_ELEMID_SSID)
7364 break;
7365 e += (2 + *(e + 1));
7366 }
7367 if (*e == IEEE80211_ELEMID_SSID) {
7368 int i, len;
7369 char *p;
7370
7371 p = ssid;
7372 len = m->m_len - ((e + 2) - (uint8_t *)f);
7373 if (len > *(e + 1))
7374 len = *(e + 1);
7375 e += 2;
7376 for (i = 0; i < len; i++) {
7377 /* Printable character? */
7378 if (*e >= 0x20 && *e < 0x7f) {
7379 *p++ = *e++;
7380 } else {
7381 snprintf(p, 5, "%#04x", *e++);
7382 p += 4;
7383 }
7384 }
7385 *p = '\0';
7386 }
7387
7388 /* We print skb, skb->data, m as we are seeing 'ghost beacons'. */
7389 TRACE_SCAN_BEACON(lhw->ic, "Beacon: scan_flags %b, band %s freq %u chan %-4d "
7390 "len %d { %#06x %#06x %6D %6D %6D %#06x %ju %u %#06x SSID '%s' }",
7391 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7392 lkpi_nl80211_band_name(rx_status->band), rx_status->freq,
7393 linuxkpi_ieee80211_frequency_to_channel(rx_status->freq, 0),
7394 m->m_pkthdr.len, f->frame_control, f->duration_id,
7395 f->da, ":", f->sa, ":", f->bssid, ":", f->seq_ctrl,
7396 (uintmax_t)le64_to_cpu(f->u.beacon.timestamp),
7397 le16_to_cpu(f->u.beacon.beacon_int),
7398 le16_to_cpu(f->u.beacon.capab_info), ssid);
7399 }
7400 #endif
7401
7402 /* For %list see comment towards the end of the function. */
7403 void
7404 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
7405 struct ieee80211_sta *sta, struct napi_struct *napi __unused,
7406 struct list_head *list __unused)
7407 {
7408 struct lkpi_hw *lhw;
7409 struct ieee80211com *ic;
7410 struct mbuf *m;
7411 struct skb_shared_info *shinfo;
7412 struct ieee80211_rx_status *rx_status;
7413 struct ieee80211_rx_stats rx_stats;
7414 struct ieee80211_node *ni;
7415 struct ieee80211vap *vap;
7416 struct ieee80211_hdr *hdr;
7417 struct lkpi_sta *lsta;
7418 int i, offset, ok, error;
7419 uint8_t rssi;
7420 bool is_beacon;
7421
7422 lhw = HW_TO_LHW(hw);
7423 ic = lhw->ic;
7424
7425 if (skb->len < 2) {
7426 /* Need 80211 stats here. */
7427 counter_u64_add(ic->ic_ierrors, 1);
7428 IMPROVE();
7429 goto err;
7430 }
7431
7432 /*
7433 * For now do the data copy; we can later improve things. Might even
7434 * have an mbuf backing the skb data then?
7435 */
7436 m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
7437 if (m == NULL) {
7438 counter_u64_add(ic->ic_ierrors, 1);
7439 goto err;
7440 }
7441 m_copyback(m, 0, skb->tail - skb->data, skb->data);
7442
7443 shinfo = skb_shinfo(skb);
7444 offset = m->m_len;
7445 for (i = 0; i < shinfo->nr_frags; i++) {
7446 m_copyback(m, offset, shinfo->frags[i].size,
7447 (uint8_t *)linux_page_address(shinfo->frags[i].page) +
7448 shinfo->frags[i].offset);
7449 offset += shinfo->frags[i].size;
7450 }
7451
7452 rx_status = IEEE80211_SKB_RXCB(skb);
7453
7454 hdr = (void *)skb->data;
7455 is_beacon = ieee80211_is_beacon(hdr->frame_control);
7456
7457 #ifdef LINUXKPI_DEBUG_80211
7458 /*
7459 * We use the mbuf here as otherwise the variable part might
7460 * be in skb frags.
7461 */
7462 if (is_beacon && ((linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0))
7463 lkpi_rx_log_beacon(m, lhw, rx_status);
7464
7465 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
7466 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) == 0)
7467 goto no_trace_beacons;
7468
7469 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7470 printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
7471 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
7472 __func__, skb, skb->len, skb->data_len,
7473 skb->truesize, skb->head, skb->data, skb->tail, skb->end,
7474 shinfo, shinfo->nr_frags,
7475 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
7476
7477 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
7478 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
7479
7480 /* Implement a dump_rxcb() !!! */
7481 if ((linuxkpi_debug_80211 & D80211_TRACE_RX) != 0 ||
7482 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0)
7483 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
7484 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
7485 __func__,
7486 (uintmax_t)rx_status->boottime_ns,
7487 (uintmax_t)rx_status->mactime,
7488 rx_status->device_timestamp,
7489 rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
7490 rx_status->freq,
7491 rx_status->bw,
7492 rx_status->encoding,
7493 rx_status->ampdu_reference,
7494 rx_status->band,
7495 rx_status->chains,
7496 rx_status->chain_signal[0],
7497 rx_status->chain_signal[1],
7498 rx_status->chain_signal[2],
7499 rx_status->chain_signal[3],
7500 rx_status->signal,
7501 rx_status->enc_flags,
7502 rx_status->he_dcm,
7503 rx_status->he_gi,
7504 rx_status->he_ru,
7505 rx_status->zero_length_psdu_type,
7506 rx_status->nss,
7507 rx_status->rate_idx);
7508 no_trace_beacons:
7509 #endif
7510
7511 lsta = NULL;
7512 if (sta != NULL) {
7513 lsta = STA_TO_LSTA(sta);
7514 ni = ieee80211_ref_node(lsta->ni);
7515 } else {
7516 struct ieee80211_frame_min *wh;
7517
7518 wh = mtod(m, struct ieee80211_frame_min *);
7519 ni = ieee80211_find_rxnode(ic, wh);
7520 if (ni != NULL)
7521 lsta = ni->ni_drv_data;
7522 }
7523
7524 rssi = 0;
7525 lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
7526
7527 ok = ieee80211_add_rx_params(m, &rx_stats);
7528 if (ok == 0) {
7529 m_freem(m);
7530 counter_u64_add(ic->ic_ierrors, 1);
7531 goto err;
7532 }
7533
7534 if (ni != NULL)
7535 vap = ni->ni_vap;
7536 else
7537 /*
7538 * XXX-BZ can we improve this by looking at the frame hdr
7539 * or other meta-data passed up?
7540 */
7541 vap = TAILQ_FIRST(&ic->ic_vaps);
7542
7543 #ifdef LINUXKPI_DEBUG_80211
7544 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7545 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
7546 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
7547 ni, vap, is_beacon ? " beacon" : "");
7548 #endif
7549
7550 if (ni != NULL && vap != NULL && is_beacon &&
7551 rx_status->device_timestamp > 0 &&
7552 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
7553 struct lkpi_vif *lvif;
7554 struct ieee80211_vif *vif;
7555 struct ieee80211_frame *wh;
7556
7557 wh = mtod(m, struct ieee80211_frame *);
7558 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
7559 goto skip_device_ts;
7560
7561 lvif = VAP_TO_LVIF(vap);
7562 vif = LVIF_TO_VIF(lvif);
7563
7564 IMPROVE("TIMING_BEACON_ONLY?");
7565 /* mac80211 specific (not net80211) so keep it here. */
7566 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
7567 /*
7568 * net80211 should take care of the other information (sync_tsf,
7569 * sync_dtim_count) as otherwise we need to parse the beacon.
7570 */
7571 skip_device_ts:
7572 ;
7573 }
7574
7575 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
7576 ieee80211_radiotap_active_vap(vap)) {
7577 struct lkpi_radiotap_rx_hdr *rtap;
7578
7579 rtap = &lhw->rtap_rx;
7580 rtap->wr_tsft = rx_status->device_timestamp;
7581 rtap->wr_flags = 0;
7582 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
7583 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7584 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
7585 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
7586 #if 0 /* .. or it does not given we strip it below. */
7587 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7588 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
7589 #endif
7590 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
7591 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
7592 rtap->wr_rate = 0;
7593 IMPROVE();
7594 /* XXX TODO status->encoding / rate_index / bw */
7595 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
7596 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
7597 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
7598 rtap->wr_dbm_antsignal = rssi;
7599 rtap->wr_dbm_antnoise = rx_stats.c_nf;
7600 }
7601
7602 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
7603 m_adj(m, -IEEE80211_CRC_LEN);
7604
7605 #if 0
7606 if (list != NULL) {
7607 /*
7608 * Normally this would be queued up and delivered by
7609 * netif_receive_skb_list(), napi_gro_receive(), or the like.
7610 * See mt76::mac80211.c as only current possible consumer.
7611 */
7612 IMPROVE("we simply pass the packet to net80211 to deal with.");
7613 }
7614 #endif
7615
7616 /* Attach meta-information to the mbuf for the deferred RX path. */
7617 if (ni != NULL) {
7618 #ifdef LKPI_80211_USE_MTAG
7619 struct m_tag *mtag;
7620 struct lkpi_80211_tag_rxni *rxni;
7621
7622 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
7623 sizeof(*rxni), IEEE80211_M_NOWAIT);
7624 if (mtag == NULL) {
7625 m_freem(m);
7626 counter_u64_add(ic->ic_ierrors, 1);
7627 goto err;
7628 }
7629 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7630 rxni->ni = ni; /* We hold a reference. */
7631 m_tag_prepend(m, mtag);
7632 #else
7633 m->m_pkthdr.PH_loc.ptr = ni; /* We hold a reference. */
7634 #endif
7635 }
7636
7637 LKPI_80211_LHW_RXQ_LOCK(lhw);
7638 if (lhw->rxq_stopped) {
7639 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7640 m_freem(m);
7641 counter_u64_add(ic->ic_ierrors, 1);
7642 goto err;
7643 }
7644
7645 error = mbufq_enqueue(&lhw->rxq, m);
7646 if (error != 0) {
7647 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7648 m_freem(m);
7649 counter_u64_add(ic->ic_ierrors, 1);
7650 #ifdef LINUXKPI_DEBUG_80211
7651 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
7652 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
7653 __func__, error);
7654 #endif
7655 goto err;
7656 }
7657 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
7658 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7659
7660 IMPROVE();
7661
7662 err:
7663 /* The skb is ours so we can free it :-) */
7664 kfree_skb(skb);
7665 }
7666
7667 uint8_t
7668 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
7669 {
7670 const struct ieee80211_frame *wh;
7671 uint8_t tid;
7672
7673 /* Linux seems to assume this is a QOS-Data-Frame */
7674 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
7675 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
7676 hdr->frame_control));
7677
7678 wh = (const struct ieee80211_frame *)hdr;
7679 tid = ieee80211_gettid(wh);
7680 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
7681 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
7682
7683 return (tid);
7684 }
7685
7686 /* -------------------------------------------------------------------------- */
7687
7688 static void
7689 lkpi_wiphy_work(struct work_struct *work)
7690 {
7691 struct lkpi_wiphy *lwiphy;
7692 struct wiphy *wiphy;
7693 struct wiphy_work *wk;
7694
7695 lwiphy = container_of(work, struct lkpi_wiphy, wwk);
7696 wiphy = LWIPHY_TO_WIPHY(lwiphy);
7697
7698 wiphy_lock(wiphy);
7699
7700 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7701 wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
7702 /* If there is nothing we do nothing. */
7703 if (wk == NULL) {
7704 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7705 wiphy_unlock(wiphy);
7706 return;
7707 }
7708 list_del_init(&wk->entry);
7709
7710 /* More work to do? */
7711 if (!list_empty(&lwiphy->wwk_list))
7712 schedule_work(work);
7713 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7714
7715 /* Finally call the (*wiphy_work_fn)() function. */
7716 wk->fn(wiphy, wk);
7717
7718 wiphy_unlock(wiphy);
7719 }
7720
7721 void
7722 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
7723 {
7724 struct lkpi_wiphy *lwiphy;
7725
7726 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7727
7728 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7729 /* Do not double-queue. */
7730 if (list_empty(&wwk->entry))
7731 list_add_tail(&wwk->entry, &lwiphy->wwk_list);
7732 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7733
7734 /*
7735 * See how ieee80211_queue_work() work continues in Linux or if things
7736 * migrate here over time?
7737 * Use a system queue from linux/workqueue.h for now.
7738 */
7739 queue_work(system_wq, &lwiphy->wwk);
7740 }
7741
7742 void
7743 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
7744 {
7745 struct lkpi_wiphy *lwiphy;
7746
7747 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7748
7749 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7750 /* Only cancel if queued. */
7751 if (!list_empty(&wwk->entry))
7752 list_del_init(&wwk->entry);
7753 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7754 }
7755
7756 void
7757 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
7758 {
7759 struct lkpi_wiphy *lwiphy;
7760 struct wiphy_work *wk;
7761
7762 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7763 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7764 /* If wwk is unset, flush everything; called when wiphy is shut down. */
7765 if (wwk != NULL && list_empty(&wwk->entry)) {
7766 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7767 return;
7768 }
7769
7770 while (!list_empty(&lwiphy->wwk_list)) {
7771
7772 wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
7773 entry);
7774 list_del_init(&wk->entry);
7775 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7776 wk->fn(wiphy, wk);
7777 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
7778 if (wk == wwk)
7779 break;
7780 }
7781 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
7782 }
7783
7784 void
7785 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
7786 {
7787 struct wiphy_delayed_work *wdwk;
7788
7789 wdwk = from_timer(wdwk, tl, timer);
7790 wiphy_work_queue(wdwk->wiphy, &wdwk->work);
7791 }
7792
7793 void
7794 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
7795 struct wiphy_delayed_work *wdwk, unsigned long delay)
7796 {
7797 if (delay == 0) {
7798 /* Run right away. */
7799 del_timer(&wdwk->timer);
7800 wiphy_work_queue(wiphy, &wdwk->work);
7801 } else {
7802 wdwk->wiphy = wiphy;
7803 mod_timer(&wdwk->timer, jiffies + delay);
7804 }
7805 }
7806
7807 void
7808 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
7809 struct wiphy_delayed_work *wdwk)
7810 {
7811 del_timer_sync(&wdwk->timer);
7812 wiphy_work_cancel(wiphy, &wdwk->work);
7813 }
7814
7815 /* -------------------------------------------------------------------------- */
7816
7817 struct wiphy *
7818 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
7819 {
7820 struct lkpi_wiphy *lwiphy;
7821 struct wiphy *wiphy;
7822
7823 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
7824 if (lwiphy == NULL)
7825 return (NULL);
7826 lwiphy->ops = ops;
7827
7828 LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
7829 INIT_LIST_HEAD(&lwiphy->wwk_list);
7830 INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
7831
7832 wiphy = LWIPHY_TO_WIPHY(lwiphy);
7833
7834 mutex_init(&wiphy->mtx);
7835 TODO();
7836
7837 return (wiphy);
7838 }
7839
7840 void
7841 linuxkpi_wiphy_free(struct wiphy *wiphy)
7842 {
7843 struct lkpi_wiphy *lwiphy;
7844
7845 if (wiphy == NULL)
7846 return;
7847
7848 linuxkpi_wiphy_work_flush(wiphy, NULL);
7849 mutex_destroy(&wiphy->mtx);
7850
7851 lwiphy = WIPHY_TO_LWIPHY(wiphy);
7852 LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
7853
7854 kfree(lwiphy);
7855 }
7856
7857 static uint32_t
7858 lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
7859 {
7860 TODO("cfg80211_calculate_bitrate_ht");
7861 return (rate->legacy);
7862 }
7863
7864 static uint32_t
7865 lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
7866 {
7867 TODO("cfg80211_calculate_bitrate_vht");
7868 return (rate->legacy);
7869 }
7870
7871 uint32_t
7872 linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
7873 {
7874
7875 /* Beware: order! */
7876 if (rate->flags & RATE_INFO_FLAGS_MCS)
7877 return (lkpi_cfg80211_calculate_bitrate_ht(rate));
7878
7879 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
7880 return (lkpi_cfg80211_calculate_bitrate_vht(rate));
7881
7882 IMPROVE("HE/EHT/...");
7883
7884 return (rate->legacy);
7885 }
7886
7887 uint32_t
7888 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
7889 enum nl80211_band band)
7890 {
7891
7892 switch (band) {
7893 case NL80211_BAND_2GHZ:
7894 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
7895 break;
7896 case NL80211_BAND_5GHZ:
7897 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
7898 break;
7899 default:
7900 /* XXX abort, retry, error, panic? */
7901 break;
7902 }
7903
7904 return (0);
7905 }
7906
7907 uint32_t
7908 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
7909 {
7910
7911 return (ieee80211_mhz2ieee(freq, 0));
7912 }
7913
7914 #if 0
7915 static struct lkpi_sta *
7916 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
7917 {
7918 struct lkpi_sta *lsta, *temp;
7919
7920 rcu_read_lock();
7921 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7922 if (lsta->ni == ni) {
7923 rcu_read_unlock();
7924 return (lsta);
7925 }
7926 }
7927 rcu_read_unlock();
7928
7929 return (NULL);
7930 }
7931 #endif
7932
7933 struct ieee80211_sta *
7934 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
7935 {
7936 struct lkpi_vif *lvif;
7937 struct lkpi_sta *lsta;
7938 struct ieee80211_sta *sta;
7939
7940 lvif = VIF_TO_LVIF(vif);
7941
7942 rcu_read_lock();
7943 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7944 sta = LSTA_TO_STA(lsta);
7945 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
7946 rcu_read_unlock();
7947 return (sta);
7948 }
7949 }
7950 rcu_read_unlock();
7951 return (NULL);
7952 }
7953
7954 struct ieee80211_sta *
7955 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7956 const uint8_t *addr, const uint8_t *ourvifaddr)
7957 {
7958 struct lkpi_hw *lhw;
7959 struct lkpi_vif *lvif;
7960 struct lkpi_sta *lsta;
7961 struct ieee80211_vif *vif;
7962 struct ieee80211_sta *sta;
7963
7964 lhw = wiphy_priv(hw->wiphy);
7965 sta = NULL;
7966
7967 LKPI_80211_LHW_LVIF_LOCK(lhw);
7968 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7969
7970 /* XXX-BZ check our address from the vif. */
7971
7972 vif = LVIF_TO_VIF(lvif);
7973 if (ourvifaddr != NULL &&
7974 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
7975 continue;
7976 sta = linuxkpi_ieee80211_find_sta(vif, addr);
7977 if (sta != NULL)
7978 break;
7979 }
7980 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7981
7982 if (sta != NULL) {
7983 lsta = STA_TO_LSTA(sta);
7984 if (!lsta->added_to_drv)
7985 return (NULL);
7986 }
7987
7988 return (sta);
7989 }
7990
7991 struct sk_buff *
7992 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
7993 struct ieee80211_txq *txq)
7994 {
7995 struct lkpi_txq *ltxq;
7996 struct lkpi_vif *lvif;
7997 struct sk_buff *skb;
7998
7999 IMPROVE("wiphy_lock? or assert?");
8000 skb = NULL;
8001 ltxq = TXQ_TO_LTXQ(txq);
8002 ltxq->seen_dequeue = true;
8003
8004 if (ltxq->stopped)
8005 goto stopped;
8006
8007 lvif = VIF_TO_LVIF(ltxq->txq.vif);
8008 if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
8009 ltxq->stopped = true;
8010 goto stopped;
8011 }
8012
8013 IMPROVE("hw(TX_FRAG_LIST)");
8014
8015 LKPI_80211_LTXQ_LOCK(ltxq);
8016 skb = skb_dequeue(<xq->skbq);
8017 LKPI_80211_LTXQ_UNLOCK(ltxq);
8018
8019 stopped:
8020 return (skb);
8021 }
8022
8023 void
8024 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
8025 unsigned long *frame_cnt, unsigned long *byte_cnt)
8026 {
8027 struct lkpi_txq *ltxq;
8028 struct sk_buff *skb;
8029 unsigned long fc, bc;
8030
8031 ltxq = TXQ_TO_LTXQ(txq);
8032
8033 fc = bc = 0;
8034 LKPI_80211_LTXQ_LOCK(ltxq);
8035 skb_queue_walk(<xq->skbq, skb) {
8036 fc++;
8037 bc += skb->len;
8038 }
8039 LKPI_80211_LTXQ_UNLOCK(ltxq);
8040 if (frame_cnt)
8041 *frame_cnt = fc;
8042 if (byte_cnt)
8043 *byte_cnt = bc;
8044
8045 /* Validate that this is doing the correct thing. */
8046 /* Should we keep track on en/dequeue? */
8047 IMPROVE();
8048 }
8049
8050 /*
8051 * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
8052 * The latter tries to derive the success status from the info flags
8053 * passed back from the driver. rawx_mit() saves the ni on the m and the
8054 * m on the skb for us to be able to give feedback to net80211.
8055 */
8056 static void
8057 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
8058 int status)
8059 {
8060 struct ieee80211_node *ni;
8061 struct mbuf *m;
8062
8063 m = skb->m;
8064 skb->m = NULL;
8065
8066 if (m != NULL) {
8067 ni = m->m_pkthdr.PH_loc.ptr;
8068 /* Status: 0 is ok, != 0 is error. */
8069 ieee80211_tx_complete(ni, m, status);
8070 /* ni & mbuf were consumed. */
8071 }
8072 }
8073
8074 void
8075 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
8076 int status)
8077 {
8078
8079 _lkpi_ieee80211_free_txskb(hw, skb, status);
8080 kfree_skb(skb);
8081 }
8082
8083 void
8084 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
8085 struct ieee80211_tx_status *txstat)
8086 {
8087 struct sk_buff *skb;
8088 struct ieee80211_tx_info *info;
8089 struct ieee80211_ratectl_tx_status txs;
8090 struct ieee80211_node *ni;
8091 int status;
8092
8093 skb = txstat->skb;
8094 if (skb->m != NULL) {
8095 struct mbuf *m;
8096
8097 m = skb->m;
8098 ni = m->m_pkthdr.PH_loc.ptr;
8099 memset(&txs, 0, sizeof(txs));
8100 } else {
8101 ni = NULL;
8102 }
8103
8104 info = txstat->info;
8105 if (info->flags & IEEE80211_TX_STAT_ACK) {
8106 status = 0; /* No error. */
8107 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
8108 } else {
8109 status = 1;
8110 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
8111 }
8112
8113 if (ni != NULL) {
8114 txs.pktlen = skb->len;
8115 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
8116 if (info->status.rates[0].count > 1) {
8117 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */
8118 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
8119 }
8120 #if 0 /* Unused in net80211 currently. */
8121 /* XXX-BZ convert check .flags for MCS/VHT/.. */
8122 txs.final_rate = info->status.rates[0].idx;
8123 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
8124 #endif
8125 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
8126 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */
8127 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
8128 }
8129
8130 IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
8131 ieee80211_ratectl_tx_complete(ni, &txs);
8132 ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
8133
8134 #ifdef LINUXKPI_DEBUG_80211
8135 if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
8136 printf("TX-RATE: %s: long_retries %d\n", __func__,
8137 txs.long_retries);
8138 }
8139 #endif
8140 }
8141
8142 #ifdef LINUXKPI_DEBUG_80211
8143 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
8144 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
8145 "band %u hw_queue %u tx_time_est %d : "
8146 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
8147 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
8148 "tx_time %u flags %#x "
8149 "status_driver_data [ %p %p ]\n",
8150 __func__, hw, skb, status, info->flags,
8151 info->band, info->hw_queue, info->tx_time_est,
8152 info->status.rates[0].idx, info->status.rates[0].count,
8153 info->status.rates[0].flags,
8154 info->status.rates[1].idx, info->status.rates[1].count,
8155 info->status.rates[1].flags,
8156 info->status.rates[2].idx, info->status.rates[2].count,
8157 info->status.rates[2].flags,
8158 info->status.rates[3].idx, info->status.rates[3].count,
8159 info->status.rates[3].flags,
8160 info->status.ack_signal, info->status.ampdu_ack_len,
8161 info->status.ampdu_len, info->status.antenna,
8162 info->status.tx_time, info->status.flags,
8163 info->status.status_driver_data[0],
8164 info->status.status_driver_data[1]);
8165 #endif
8166
8167 if (txstat->free_list) {
8168 _lkpi_ieee80211_free_txskb(hw, skb, status);
8169 list_add_tail(&skb->list, txstat->free_list);
8170 } else {
8171 linuxkpi_ieee80211_free_txskb(hw, skb, status);
8172 }
8173 }
8174
8175 void
8176 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
8177 {
8178 struct ieee80211_tx_status status;
8179
8180 memset(&status, 0, sizeof(status));
8181 status.info = IEEE80211_SKB_CB(skb);
8182 status.skb = skb;
8183 /* sta, n_rates, rates, free_list? */
8184
8185 ieee80211_tx_status_ext(hw, &status);
8186 }
8187
8188 /*
8189 * This is an internal bandaid for the moment for the way we glue
8190 * skbs and mbufs together for TX. Once we have skbs backed by
8191 * mbufs this should go away.
8192 * This is a public function but kept on the private KPI (lkpi_)
8193 * and is not exposed by a header file.
8194 */
8195 static void
8196 lkpi_ieee80211_free_skb_mbuf(void *p)
8197 {
8198 struct ieee80211_node *ni;
8199 struct mbuf *m;
8200
8201 if (p == NULL)
8202 return;
8203
8204 m = (struct mbuf *)p;
8205 M_ASSERTPKTHDR(m);
8206
8207 ni = m->m_pkthdr.PH_loc.ptr;
8208 m->m_pkthdr.PH_loc.ptr = NULL;
8209 if (ni != NULL)
8210 ieee80211_free_node(ni);
8211 m_freem(m);
8212 }
8213
8214 void
8215 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
8216 struct delayed_work *w, int delay)
8217 {
8218 struct lkpi_hw *lhw;
8219
8220 /* Need to make sure hw is in a stable (non-suspended) state. */
8221 IMPROVE();
8222
8223 lhw = HW_TO_LHW(hw);
8224 queue_delayed_work(lhw->workq, w, delay);
8225 }
8226
8227 void
8228 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
8229 struct work_struct *w)
8230 {
8231 struct lkpi_hw *lhw;
8232
8233 /* Need to make sure hw is in a stable (non-suspended) state. */
8234 IMPROVE();
8235
8236 lhw = HW_TO_LHW(hw);
8237 queue_work(lhw->workq, w);
8238 }
8239
8240 struct sk_buff *
8241 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, const uint8_t *addr,
8242 const uint8_t *ssid, size_t ssid_len, size_t tailroom)
8243 {
8244 struct sk_buff *skb;
8245 struct ieee80211_frame *wh;
8246 uint8_t *p;
8247 size_t len;
8248
8249 len = sizeof(*wh);
8250 len += 2 + ssid_len;
8251
8252 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
8253 if (skb == NULL)
8254 return (NULL);
8255
8256 skb_reserve(skb, hw->extra_tx_headroom);
8257
8258 wh = skb_put_zero(skb, sizeof(*wh));
8259 wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
8260 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
8261 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
8262 IEEE80211_ADDR_COPY(wh->i_addr2, addr);
8263 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
8264
8265 p = skb_put(skb, 2 + ssid_len);
8266 *p++ = IEEE80211_ELEMID_SSID;
8267 *p++ = ssid_len;
8268 if (ssid_len > 0)
8269 memcpy(p, ssid, ssid_len);
8270
8271 return (skb);
8272 }
8273
8274 struct sk_buff *
8275 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
8276 struct ieee80211_vif *vif)
8277 {
8278 struct lkpi_vif *lvif;
8279 struct ieee80211vap *vap;
8280 struct sk_buff *skb;
8281 struct ieee80211_frame_pspoll *psp;
8282 uint16_t v;
8283
8284 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
8285 if (skb == NULL)
8286 return (NULL);
8287
8288 skb_reserve(skb, hw->extra_tx_headroom);
8289
8290 lvif = VIF_TO_LVIF(vif);
8291 vap = LVIF_TO_VAP(lvif);
8292
8293 psp = skb_put_zero(skb, sizeof(*psp));
8294 psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
8295 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
8296 v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
8297 memcpy(&psp->i_aid, &v, sizeof(v));
8298 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
8299 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
8300
8301 return (skb);
8302 }
8303
8304 struct sk_buff *
8305 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
8306 struct ieee80211_vif *vif, int linkid, bool qos)
8307 {
8308 struct lkpi_vif *lvif;
8309 struct ieee80211vap *vap;
8310 struct sk_buff *skb;
8311 struct ieee80211_frame *nullf;
8312
8313 IMPROVE("linkid");
8314
8315 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
8316 if (skb == NULL)
8317 return (NULL);
8318
8319 skb_reserve(skb, hw->extra_tx_headroom);
8320
8321 lvif = VIF_TO_LVIF(vif);
8322 vap = LVIF_TO_VAP(lvif);
8323
8324 nullf = skb_put_zero(skb, sizeof(*nullf));
8325 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
8326 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
8327 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
8328
8329 IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
8330 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
8331 IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
8332
8333 return (skb);
8334 }
8335
8336 struct wireless_dev *
8337 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
8338 {
8339 struct lkpi_vif *lvif;
8340
8341 lvif = VIF_TO_LVIF(vif);
8342 return (&lvif->wdev);
8343 }
8344
8345 void
8346 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
8347 {
8348 struct lkpi_vif *lvif;
8349 struct ieee80211vap *vap;
8350 enum ieee80211_state nstate;
8351 int arg;
8352
8353 lvif = VIF_TO_LVIF(vif);
8354 vap = LVIF_TO_VAP(lvif);
8355
8356 /*
8357 * Go to init; otherwise we need to elaborately check state and
8358 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
8359 * Let the statemachine handle all neccessary changes.
8360 */
8361 nstate = IEEE80211_S_INIT;
8362 arg = 0; /* Not a valid reason. */
8363
8364 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
8365 "beacons %d dtim_period %d)\n", __func__, vif, vap,
8366 ieee80211_state_name[vap->iv_state],
8367 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
8368 vif->bss_conf.dtim_period);
8369 ieee80211_new_state(vap, nstate, arg);
8370 }
8371
8372 void
8373 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
8374 {
8375 struct lkpi_vif *lvif;
8376 struct ieee80211vap *vap;
8377
8378 lvif = VIF_TO_LVIF(vif);
8379 vap = LVIF_TO_VAP(lvif);
8380
8381 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
8382 "beacons %d dtim_period %d)\n", __func__, vif, vap,
8383 ieee80211_state_name[vap->iv_state],
8384 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
8385 vif->bss_conf.dtim_period);
8386 ieee80211_beacon_miss(vap->iv_ic);
8387 }
8388
8389 /* -------------------------------------------------------------------------- */
8390
8391 void
8392 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
8393 {
8394 struct lkpi_hw *lhw;
8395 struct lkpi_vif *lvif;
8396 struct ieee80211_vif *vif;
8397 int ac_count, ac;
8398
8399 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
8400 __func__, qnum, hw->queues, hw));
8401
8402 lhw = wiphy_priv(hw->wiphy);
8403
8404 /* See lkpi_ic_vap_create(). */
8405 if (hw->queues >= IEEE80211_NUM_ACS)
8406 ac_count = IEEE80211_NUM_ACS;
8407 else
8408 ac_count = 1;
8409
8410 LKPI_80211_LHW_LVIF_LOCK(lhw);
8411 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8412
8413 vif = LVIF_TO_VIF(lvif);
8414 for (ac = 0; ac < ac_count; ac++) {
8415 IMPROVE_TXQ("LOCKING");
8416 if (qnum == vif->hw_queue[ac]) {
8417 #ifdef LINUXKPI_DEBUG_80211
8418 /*
8419 * For now log this to better understand
8420 * how this is supposed to work.
8421 */
8422 if (lvif->hw_queue_stopped[ac] &&
8423 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8424 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8425 "lvif %p vif %p ac %d qnum %d already "
8426 "stopped\n", __func__, __LINE__,
8427 lhw, hw, lvif, vif, ac, qnum);
8428 #endif
8429 lvif->hw_queue_stopped[ac] = true;
8430 }
8431 }
8432 }
8433 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8434 }
8435
8436 void
8437 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
8438 {
8439 int i;
8440
8441 IMPROVE_TXQ("Locking; do we need further info?");
8442 for (i = 0; i < hw->queues; i++)
8443 linuxkpi_ieee80211_stop_queue(hw, i);
8444 }
8445
8446
8447 static void
8448 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
8449 {
8450 struct lkpi_hw *lhw;
8451 struct lkpi_vif *lvif;
8452 struct lkpi_sta *lsta;
8453 int ac_count, ac, tid;
8454
8455 /* See lkpi_ic_vap_create(). */
8456 if (hw->queues >= IEEE80211_NUM_ACS)
8457 ac_count = IEEE80211_NUM_ACS;
8458 else
8459 ac_count = 1;
8460
8461 lhw = wiphy_priv(hw->wiphy);
8462
8463 IMPROVE_TXQ("Locking");
8464 LKPI_80211_LHW_LVIF_LOCK(lhw);
8465 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8466 struct ieee80211_vif *vif;
8467
8468 vif = LVIF_TO_VIF(lvif);
8469 for (ac = 0; ac < ac_count; ac++) {
8470
8471 if (hwq == vif->hw_queue[ac]) {
8472
8473 /* XXX-BZ what about software scan? */
8474
8475 #ifdef LINUXKPI_DEBUG_80211
8476 /*
8477 * For now log this to better understand
8478 * how this is supposed to work.
8479 */
8480 if (!lvif->hw_queue_stopped[ac] &&
8481 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
8482 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
8483 "lvif %p vif %p ac %d hw_q not stopped\n",
8484 __func__, __LINE__,
8485 lhw, hw, lvif, vif, ac);
8486 #endif
8487 lvif->hw_queue_stopped[ac] = false;
8488
8489 rcu_read_lock();
8490 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
8491 struct ieee80211_sta *sta;
8492
8493 sta = LSTA_TO_STA(lsta);
8494 for (tid = 0; tid < nitems(sta->txq); tid++) {
8495 struct lkpi_txq *ltxq;
8496
8497 if (sta->txq[tid] == NULL)
8498 continue;
8499
8500 if (sta->txq[tid]->ac != ac)
8501 continue;
8502
8503 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
8504 if (!ltxq->stopped)
8505 continue;
8506
8507 ltxq->stopped = false;
8508
8509 if (!skb_queue_empty(<xq->skbq))
8510 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
8511 }
8512 }
8513 rcu_read_unlock();
8514 }
8515 }
8516 }
8517 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8518 }
8519
8520 static void
8521 lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *hw)
8522 {
8523 int i;
8524
8525 IMPROVE_TXQ("Is this all/enough here?");
8526 for (i = 0; i < hw->queues; i++)
8527 lkpi_ieee80211_wake_queues(hw, i);
8528 }
8529
8530 void
8531 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
8532 {
8533 struct lkpi_hw *lhw;
8534 unsigned long flags;
8535
8536 lhw = HW_TO_LHW(hw);
8537
8538 spin_lock_irqsave(&lhw->txq_lock, flags);
8539 lkpi_ieee80211_wake_queues_locked(hw);
8540 spin_unlock_irqrestore(&lhw->txq_lock, flags);
8541 }
8542
8543 void
8544 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
8545 {
8546 struct lkpi_hw *lhw;
8547 unsigned long flags;
8548
8549 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
8550 __func__, qnum, hw->queues, hw));
8551
8552 lhw = HW_TO_LHW(hw);
8553
8554 spin_lock_irqsave(&lhw->txq_lock, flags);
8555 lkpi_ieee80211_wake_queues(hw, qnum);
8556 spin_unlock_irqrestore(&lhw->txq_lock, flags);
8557 }
8558
8559 /* This is just hardware queues. */
8560 void
8561 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
8562 {
8563 struct lkpi_hw *lhw;
8564
8565 lhw = HW_TO_LHW(hw);
8566
8567 IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
8568 IMPROVE_TXQ("LOCKING");
8569 if (++lhw->txq_generation[ac] == 0)
8570 lhw->txq_generation[ac]++;
8571 }
8572
8573 struct ieee80211_txq *
8574 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
8575 {
8576 struct lkpi_hw *lhw;
8577 struct ieee80211_txq *txq;
8578 struct lkpi_txq *ltxq;
8579
8580 lhw = HW_TO_LHW(hw);
8581 txq = NULL;
8582
8583 IMPROVE_TXQ("LOCKING");
8584
8585 /* Check that we are scheduled. */
8586 if (lhw->txq_generation[ac] == 0)
8587 goto out;
8588
8589 ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
8590 if (ltxq == NULL)
8591 goto out;
8592 if (ltxq->txq_generation == lhw->txq_generation[ac])
8593 goto out;
8594
8595 ltxq->txq_generation = lhw->txq_generation[ac];
8596 TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
8597 txq = <xq->txq;
8598 TAILQ_ELEM_INIT(ltxq, txq_entry);
8599
8600 out:
8601 return (txq);
8602 }
8603
8604 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
8605 struct ieee80211_txq *txq, bool withoutpkts)
8606 {
8607 struct lkpi_hw *lhw;
8608 struct lkpi_txq *ltxq;
8609 bool ltxq_empty;
8610
8611 ltxq = TXQ_TO_LTXQ(txq);
8612
8613 IMPROVE_TXQ("LOCKING");
8614
8615 /* Only schedule if work to do or asked to anyway. */
8616 LKPI_80211_LTXQ_LOCK(ltxq);
8617 ltxq_empty = skb_queue_empty(<xq->skbq);
8618 LKPI_80211_LTXQ_UNLOCK(ltxq);
8619 if (!withoutpkts && ltxq_empty)
8620 goto out;
8621
8622 /*
8623 * Make sure we do not double-schedule. We do this by checking tqe_prev,
8624 * the previous entry in our tailq. tqe_prev is always valid if this entry
8625 * is queued, tqe_next may be NULL if this is the only element in the list.
8626 */
8627 if (ltxq->txq_entry.tqe_prev != NULL)
8628 goto out;
8629
8630 lhw = HW_TO_LHW(hw);
8631 TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
8632 out:
8633 return;
8634 }
8635
8636 void
8637 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
8638 struct ieee80211_txq *txq)
8639 {
8640 struct lkpi_hw *lhw;
8641 struct ieee80211_txq *ntxq;
8642 struct ieee80211_tx_control control;
8643 struct sk_buff *skb;
8644
8645 lhw = HW_TO_LHW(hw);
8646
8647 LKPI_80211_LHW_TXQ_LOCK(lhw);
8648 ieee80211_txq_schedule_start(hw, txq->ac);
8649 do {
8650 ntxq = ieee80211_next_txq(hw, txq->ac);
8651 if (ntxq == NULL)
8652 break;
8653
8654 memset(&control, 0, sizeof(control));
8655 control.sta = ntxq->sta;
8656 do {
8657 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
8658 if (skb == NULL)
8659 break;
8660 lkpi_80211_mo_tx(hw, &control, skb);
8661 } while(1);
8662
8663 ieee80211_return_txq(hw, ntxq, false);
8664 } while (1);
8665 ieee80211_txq_schedule_end(hw, txq->ac);
8666 LKPI_80211_LHW_TXQ_UNLOCK(lhw);
8667 }
8668
8669 /* -------------------------------------------------------------------------- */
8670
8671 struct lkpi_cfg80211_bss {
8672 u_int refcnt;
8673 struct cfg80211_bss bss;
8674 };
8675
8676 struct lkpi_cfg80211_get_bss_iter_lookup {
8677 struct wiphy *wiphy;
8678 struct linuxkpi_ieee80211_channel *chan;
8679 const uint8_t *bssid;
8680 const uint8_t *ssid;
8681 size_t ssid_len;
8682 enum ieee80211_bss_type bss_type;
8683 enum ieee80211_privacy privacy;
8684
8685 /*
8686 * Something to store a copy of the result as the net80211 scan cache
8687 * is not refoucnted so a scan entry might go away any time.
8688 */
8689 bool match;
8690 struct cfg80211_bss *bss;
8691 };
8692
8693 static void
8694 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
8695 {
8696 struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
8697 size_t ielen;
8698
8699 lookup = arg;
8700
8701 /* Do not try to find another match. */
8702 if (lookup->match)
8703 return;
8704
8705 /* Nothing to store result. */
8706 if (lookup->bss == NULL)
8707 return;
8708
8709 if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
8710 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
8711 /* We have no idea what to compare to as the drivers only request ANY */
8712 return;
8713 }
8714
8715 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
8716 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
8717 /* We have no idea what to compare to as the drivers only request ANY */
8718 return;
8719 }
8720
8721 if (lookup->chan != NULL) {
8722 struct linuxkpi_ieee80211_channel *chan;
8723
8724 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
8725 se->se_chan->ic_freq);
8726 if (chan == NULL || chan != lookup->chan)
8727 return;
8728 }
8729
8730 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
8731 return;
8732
8733 if (lookup->ssid) {
8734 if (lookup->ssid_len != se->se_ssid[1] ||
8735 se->se_ssid[1] == 0)
8736 return;
8737 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
8738 return;
8739 }
8740
8741 ielen = se->se_ies.len;
8742
8743 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
8744 M_LKPI80211, M_NOWAIT | M_ZERO);
8745 if (lookup->bss->ies == NULL)
8746 return;
8747
8748 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
8749 lookup->bss->ies->len = ielen;
8750 if (ielen)
8751 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
8752
8753 lookup->match = true;
8754 }
8755
8756 struct cfg80211_bss *
8757 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
8758 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
8759 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
8760 {
8761 struct lkpi_cfg80211_bss *lbss;
8762 struct lkpi_cfg80211_get_bss_iter_lookup lookup;
8763 struct lkpi_hw *lhw;
8764 struct ieee80211vap *vap;
8765
8766 lhw = wiphy_priv(wiphy);
8767
8768 /* Let's hope we can alloc. */
8769 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
8770 if (lbss == NULL) {
8771 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
8772 return (NULL);
8773 }
8774
8775 lookup.wiphy = wiphy;
8776 lookup.chan = chan;
8777 lookup.bssid = bssid;
8778 lookup.ssid = ssid;
8779 lookup.ssid_len = ssid_len;
8780 lookup.bss_type = bss_type;
8781 lookup.privacy = privacy;
8782 lookup.match = false;
8783 lookup.bss = &lbss->bss;
8784
8785 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
8786 vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
8787 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
8788 if (!lookup.match) {
8789 free(lbss, M_LKPI80211);
8790 return (NULL);
8791 }
8792
8793 refcount_init(&lbss->refcnt, 1);
8794 return (&lbss->bss);
8795 }
8796
8797 void
8798 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
8799 {
8800 struct lkpi_cfg80211_bss *lbss;
8801
8802 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
8803
8804 /* Free everything again on refcount ... */
8805 if (refcount_release(&lbss->refcnt)) {
8806 free(lbss->bss.ies, M_LKPI80211);
8807 free(lbss, M_LKPI80211);
8808 }
8809 }
8810
8811 void
8812 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
8813 {
8814 struct lkpi_hw *lhw;
8815 struct ieee80211com *ic;
8816 struct ieee80211vap *vap;
8817
8818 lhw = wiphy_priv(wiphy);
8819 ic = lhw->ic;
8820
8821 /*
8822 * If we haven't called ieee80211_ifattach() yet
8823 * or there is no VAP, there are no scans to flush.
8824 */
8825 if (ic == NULL ||
8826 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
8827 return;
8828
8829 /* Should only happen on the current one? Not seen it late enough. */
8830 IEEE80211_LOCK(ic);
8831 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
8832 ieee80211_scan_flush(vap);
8833 IEEE80211_UNLOCK(ic);
8834 }
8835
8836 /* -------------------------------------------------------------------------- */
8837
8838 /*
8839 * hw->conf get initialized/set in various places for us:
8840 * - linuxkpi_ieee80211_alloc_hw(): flags
8841 * - linuxkpi_ieee80211_ifattach(): chandef
8842 * - lkpi_ic_vap_create(): listen_interval
8843 * - lkpi_ic_set_channel(): chandef, flags
8844 */
8845
8846 int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
8847 struct ieee80211_chanctx_conf *new)
8848 {
8849 struct cfg80211_chan_def *cd;
8850 uint32_t changed;
8851 int error;
8852
8853 changed = 0;
8854 if (new == NULL || new->def.chan == NULL)
8855 cd = NULL;
8856 else
8857 cd = &new->def;
8858
8859 if (cd && cd->chan != hw->conf.chandef.chan) {
8860 /* Copy; the chan pointer is fine and will stay valid. */
8861 hw->conf.chandef = *cd;
8862 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
8863 }
8864 IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
8865
8866 if (changed == 0)
8867 return (0);
8868
8869 error = lkpi_80211_mo_config(hw, changed);
8870 return (error);
8871 }
8872
8873 /* -------------------------------------------------------------------------- */
8874
8875 MODULE_VERSION(linuxkpi_wlan, 1);
8876 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
8877 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
8878