1 /*-
2 * Copyright (c) 2020-2024 The FreeBSD Foundation
3 * Copyright (c) 2020-2022 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 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/queue.h>
53 #include <sys/taskqueue.h>
54 #include <sys/libkern.h>
55
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_proto.h>
63 #include <net80211/ieee80211_ratectl.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_vht.h>
66
67 #define LINUXKPI_NET80211
68 #include <net/mac80211.h>
69
70 #include <linux/workqueue.h>
71 #include "linux_80211.h"
72
73 #define LKPI_80211_WME
74 /* #define LKPI_80211_HW_CRYPTO */
75 /* #define LKPI_80211_VHT */
76 /* #define LKPI_80211_HT */
77 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
78 #define LKPI_80211_HT
79 #endif
80
81 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
82
83 /* XXX-BZ really want this and others in queue.h */
84 #define TAILQ_ELEM_INIT(elm, field) do { \
85 (elm)->field.tqe_next = NULL; \
86 (elm)->field.tqe_prev = NULL; \
87 } while (0)
88
89 /* -------------------------------------------------------------------------- */
90
91 /* Keep public for as long as header files are using it too. */
92 int linuxkpi_debug_80211;
93
94 #ifdef LINUXKPI_DEBUG_80211
95 SYSCTL_DECL(_compat_linuxkpi);
96 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
97 "LinuxKPI 802.11 compatibility layer");
98
99 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
100 &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
101
102 #define UNIMPLEMENTED if (linuxkpi_debug_80211 & D80211_TODO) \
103 printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
104 #define TRACEOK() if (linuxkpi_debug_80211 & D80211_TRACEOK) \
105 printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
106 #else
107 #define UNIMPLEMENTED do { } while (0)
108 #define TRACEOK() do { } while (0)
109 #endif
110
111 /* #define PREP_TX_INFO_DURATION (IEEE80211_TRANS_WAIT * 1000) */
112 #ifndef PREP_TX_INFO_DURATION
113 #define PREP_TX_INFO_DURATION 0 /* Let the driver do its thing. */
114 #endif
115
116 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
117 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
118
119 /* IEEE 802.11-05/0257r1 */
120 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
121
122 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
123 static const uint8_t ieee80211e_up_to_ac[] = {
124 IEEE80211_AC_BE,
125 IEEE80211_AC_BK,
126 IEEE80211_AC_BK,
127 IEEE80211_AC_BE,
128 IEEE80211_AC_VI,
129 IEEE80211_AC_VI,
130 IEEE80211_AC_VO,
131 IEEE80211_AC_VO,
132 #if 0
133 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
134 #endif
135 };
136
137 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
138 /*
139 * XXX TODO need a "glue layer" to link cfg80211 ops to
140 * mac80211 and to the driver or net80211.
141 * Can we pass some on 1:1? Need to compare the (*f)().
142 */
143 };
144
145 #if 0
146 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
147 struct ieee80211_node *);
148 #endif
149 static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
150 static void lkpi_80211_txq_task(void *, int);
151 static void lkpi_80211_lhw_rxq_task(void *, int);
152 static void lkpi_ieee80211_free_skb_mbuf(void *);
153 #ifdef LKPI_80211_WME
154 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
155 #endif
156
157 #if defined(LKPI_80211_HT)
158 static void
lkpi_sta_sync_ht_from_ni(struct ieee80211_sta * sta,struct ieee80211_node * ni,int * ht_rx_nss)159 lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
160 {
161 struct ieee80211vap *vap;
162 uint8_t *ie;
163 struct ieee80211_ht_cap *htcap;
164 int i, rx_nss;
165
166 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
167 return;
168
169 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
170 IEEE80211_IS_CHAN_HT40(ni->ni_chan))
171 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
172
173 sta->deflink.ht_cap.ht_supported = true;
174
175 /* htcap->ampdu_params_info */
176 vap = ni->ni_vap;
177 sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
178 if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
179 sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
180 sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
181 if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
182 sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
183
184 ie = ni->ni_ies.htcap_ie;
185 KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
186 if (ie[0] == IEEE80211_ELEMID_VENDOR)
187 ie += 4;
188 ie += 2;
189 htcap = (struct ieee80211_ht_cap *)ie;
190 sta->deflink.ht_cap.cap = htcap->cap_info;
191 sta->deflink.ht_cap.mcs = htcap->mcs;
192
193 rx_nss = 0;
194 for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
195 if (htcap->mcs.rx_mask[i])
196 rx_nss++;
197 }
198 if (ht_rx_nss != NULL)
199 *ht_rx_nss = rx_nss;
200
201 IMPROVE("sta->wme, sta->deflink.agg.max*");
202 }
203 #endif
204
205 #if defined(LKPI_80211_VHT)
206 static void
lkpi_sta_sync_vht_from_ni(struct ieee80211_sta * sta,struct ieee80211_node * ni,int * vht_rx_nss)207 lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
208 {
209
210 if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
211 return;
212
213 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
214 #ifdef __notyet__
215 if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
216 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
217 } else
218 #endif
219 if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
220 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
221 else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
222 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
223 }
224
225 IMPROVE("VHT sync ni to sta");
226 return;
227 }
228 #endif
229
230 static void
lkpi_lsta_dump(struct lkpi_sta * lsta,struct ieee80211_node * ni,const char * _f,int _l)231 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
232 const char *_f, int _l)
233 {
234
235 #ifdef LINUXKPI_DEBUG_80211
236 if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
237 return;
238 if (lsta == NULL)
239 return;
240
241 printf("%s:%d lsta %p ni %p sta %p\n",
242 _f, _l, lsta, ni, &lsta->sta);
243 if (ni != NULL)
244 ieee80211_dump_node(NULL, ni);
245 printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
246 printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
247 lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
248 #endif
249 }
250
251 static void
lkpi_lsta_remove(struct lkpi_sta * lsta,struct lkpi_vif * lvif)252 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
253 {
254
255
256 LKPI_80211_LVIF_LOCK(lvif);
257 KASSERT(lsta->lsta_entry.tqe_prev != NULL,
258 ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
259 lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
260 TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
261 LKPI_80211_LVIF_UNLOCK(lvif);
262 }
263
264 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)265 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
266 struct ieee80211_hw *hw, struct ieee80211_node *ni)
267 {
268 struct lkpi_sta *lsta;
269 struct lkpi_vif *lvif;
270 struct ieee80211_vif *vif;
271 struct ieee80211_sta *sta;
272 int band, i, tid;
273 int ht_rx_nss;
274 int vht_rx_nss;
275
276 lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
277 M_NOWAIT | M_ZERO);
278 if (lsta == NULL)
279 return (NULL);
280
281 lsta->added_to_drv = false;
282 lsta->state = IEEE80211_STA_NOTEXIST;
283 /*
284 * Link the ni to the lsta here without taking a reference.
285 * For one we would have to take the reference in node_init()
286 * as ieee80211_alloc_node() will initialise the refcount after us.
287 * For the other a ni and an lsta are 1:1 mapped and always together
288 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
289 * using the ni references for the lsta as well despite it being
290 * two separate allocations.
291 */
292 lsta->ni = ni;
293 /* The back-pointer "drv_data" to net80211_node let's us get lsta. */
294 ni->ni_drv_data = lsta;
295
296 lvif = VAP_TO_LVIF(vap);
297 vif = LVIF_TO_VIF(lvif);
298 sta = LSTA_TO_STA(lsta);
299
300 IEEE80211_ADDR_COPY(sta->addr, mac);
301
302 /* TXQ */
303 for (tid = 0; tid < nitems(sta->txq); tid++) {
304 struct lkpi_txq *ltxq;
305
306 /* We are not limiting ourselves to hw.queues here. */
307 ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
308 M_LKPI80211, M_NOWAIT | M_ZERO);
309 if (ltxq == NULL)
310 goto cleanup;
311 /* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
312 if (tid == IEEE80211_NUM_TIDS) {
313 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
314 free(ltxq, M_LKPI80211);
315 continue;
316 }
317 IMPROVE("AP/if we support non-STA here too");
318 ltxq->txq.ac = IEEE80211_AC_VO;
319 } else {
320 ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
321 }
322 ltxq->seen_dequeue = false;
323 ltxq->stopped = false;
324 ltxq->txq.vif = vif;
325 ltxq->txq.tid = tid;
326 ltxq->txq.sta = sta;
327 TAILQ_ELEM_INIT(ltxq, txq_entry);
328 skb_queue_head_init(<xq->skbq);
329 LKPI_80211_LTXQ_LOCK_INIT(ltxq);
330 sta->txq[tid] = <xq->txq;
331 }
332
333 /* Deflink information. */
334 for (band = 0; band < NUM_NL80211_BANDS; band++) {
335 struct ieee80211_supported_band *supband;
336
337 supband = hw->wiphy->bands[band];
338 if (supband == NULL)
339 continue;
340
341 for (i = 0; i < supband->n_bitrates; i++) {
342
343 IMPROVE("Further supband->bitrates[i]* checks?");
344 /* or should we get them from the ni? */
345 sta->deflink.supp_rates[band] |= BIT(i);
346 }
347 }
348
349 sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
350 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
351 sta->deflink.rx_nss = 1;
352
353 ht_rx_nss = 0;
354 #if defined(LKPI_80211_HT)
355 lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
356 #endif
357 vht_rx_nss = 0;
358 #if defined(LKPI_80211_VHT)
359 lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
360 #endif
361
362 sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
363 sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
364 IMPROVE("he, ... smps_mode, ..");
365
366 /* Link configuration. */
367 IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
368 sta->link[0] = &sta->deflink;
369 for (i = 1; i < nitems(sta->link); i++) {
370 IMPROVE("more links; only link[0] = deflink currently.");
371 }
372
373 /* Deferred TX path. */
374 LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
375 TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
376 mbufq_init(&lsta->txq, IFQ_MAXLEN);
377 lsta->txq_ready = true;
378
379 return (lsta);
380
381 cleanup:
382 for (; tid >= 0; tid--) {
383 struct lkpi_txq *ltxq;
384
385 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
386 LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
387 free(sta->txq[tid], M_LKPI80211);
388 }
389 free(lsta, M_LKPI80211);
390 return (NULL);
391 }
392
393 static void
lkpi_lsta_free(struct lkpi_sta * lsta,struct ieee80211_node * ni)394 lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
395 {
396 struct mbuf *m;
397
398 if (lsta->added_to_drv)
399 panic("%s: Trying to free an lsta still known to firmware: "
400 "lsta %p ni %p added_to_drv %d\n",
401 __func__, lsta, ni, lsta->added_to_drv);
402
403 /* XXX-BZ free resources, ... */
404 IMPROVE();
405
406 /* Drain sta->txq[] */
407
408 LKPI_80211_LSTA_TXQ_LOCK(lsta);
409 lsta->txq_ready = false;
410 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
411
412 /* Drain taskq, won't be restarted until added_to_drv is set again. */
413 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
414 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
415
416 /* Flush mbufq (make sure to release ni refs!). */
417 m = mbufq_dequeue(&lsta->txq);
418 while (m != NULL) {
419 struct ieee80211_node *nim;
420
421 nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
422 if (nim != NULL)
423 ieee80211_free_node(nim);
424 m_freem(m);
425 m = mbufq_dequeue(&lsta->txq);
426 }
427 KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
428 __func__, lsta, mbufq_len(&lsta->txq)));
429 LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
430
431 /* Remove lsta from vif; that is done by the state machine. Should assert it? */
432
433 IMPROVE("Make sure everything is cleaned up.");
434
435 /* Free lsta. */
436 lsta->ni = NULL;
437 ni->ni_drv_data = NULL;
438 free(lsta, M_LKPI80211);
439 }
440
441
442 static enum nl80211_band
lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel * c)443 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
444 {
445
446 if (IEEE80211_IS_CHAN_2GHZ(c))
447 return (NL80211_BAND_2GHZ);
448 else if (IEEE80211_IS_CHAN_5GHZ(c))
449 return (NL80211_BAND_5GHZ);
450 #ifdef __notyet__
451 else if ()
452 return (NL80211_BAND_6GHZ);
453 else if ()
454 return (NL80211_BAND_60GHZ);
455 else if (IEEE80211_IS_CHAN_GSM(c))
456 return (NL80211_BAND_XXX);
457 #endif
458 else
459 panic("%s: unsupported band. c %p flags %#x\n",
460 __func__, c, c->ic_flags);
461 }
462
463 static uint32_t
lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)464 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
465 {
466
467 /* XXX-BZ this is just silly; net80211 is too convoluted. */
468 /* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
469 switch (band) {
470 case NL80211_BAND_2GHZ:
471 return (IEEE80211_CHAN_2GHZ);
472 break;
473 case NL80211_BAND_5GHZ:
474 return (IEEE80211_CHAN_5GHZ);
475 break;
476 case NL80211_BAND_60GHZ:
477 break;
478 case NL80211_BAND_6GHZ:
479 break;
480 default:
481 panic("%s: unsupported band %u\n", __func__, band);
482 break;
483 }
484
485 IMPROVE();
486 return (0x00);
487 }
488
489 #if 0
490 static enum ieee80211_ac_numbers
491 lkpi_ac_net_to_l80211(int ac)
492 {
493
494 switch (ac) {
495 case WME_AC_VO:
496 return (IEEE80211_AC_VO);
497 case WME_AC_VI:
498 return (IEEE80211_AC_VI);
499 case WME_AC_BE:
500 return (IEEE80211_AC_BE);
501 case WME_AC_BK:
502 return (IEEE80211_AC_BK);
503 default:
504 printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
505 return (IEEE80211_AC_BE);
506 }
507 }
508 #endif
509
510 static enum nl80211_iftype
lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)511 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
512 {
513
514 switch (opmode) {
515 case IEEE80211_M_IBSS:
516 return (NL80211_IFTYPE_ADHOC);
517 break;
518 case IEEE80211_M_STA:
519 return (NL80211_IFTYPE_STATION);
520 break;
521 case IEEE80211_M_WDS:
522 return (NL80211_IFTYPE_WDS);
523 break;
524 case IEEE80211_M_HOSTAP:
525 return (NL80211_IFTYPE_AP);
526 break;
527 case IEEE80211_M_MONITOR:
528 return (NL80211_IFTYPE_MONITOR);
529 break;
530 case IEEE80211_M_MBSS:
531 return (NL80211_IFTYPE_MESH_POINT);
532 break;
533 case IEEE80211_M_AHDEMO:
534 /* FALLTHROUGH */
535 default:
536 printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
537 /* FALLTHROUGH */
538 }
539 return (NL80211_IFTYPE_UNSPECIFIED);
540 }
541
542 #ifdef LKPI_80211_HW_CRYPTO
543 static uint32_t
lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)544 lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
545 {
546
547 switch (wlan_cipher_suite) {
548 case WLAN_CIPHER_SUITE_WEP40:
549 return (IEEE80211_CRYPTO_WEP);
550 case WLAN_CIPHER_SUITE_TKIP:
551 return (IEEE80211_CRYPTO_TKIP);
552 case WLAN_CIPHER_SUITE_CCMP:
553 return (IEEE80211_CRYPTO_AES_CCM);
554 case WLAN_CIPHER_SUITE_WEP104:
555 return (IEEE80211_CRYPTO_WEP);
556 case WLAN_CIPHER_SUITE_AES_CMAC:
557 case WLAN_CIPHER_SUITE_GCMP:
558 case WLAN_CIPHER_SUITE_GCMP_256:
559 case WLAN_CIPHER_SUITE_CCMP_256:
560 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
561 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
562 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
563 printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
564 wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
565 break;
566 default:
567 printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
568 wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
569 }
570
571 return (0);
572 }
573
574 static uint32_t
lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher,uint8_t keylen)575 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
576 {
577
578 switch (cipher) {
579 case IEEE80211_CIPHER_TKIP:
580 return (WLAN_CIPHER_SUITE_TKIP);
581 case IEEE80211_CIPHER_AES_CCM:
582 return (WLAN_CIPHER_SUITE_CCMP);
583 case IEEE80211_CIPHER_WEP:
584 if (keylen < 8)
585 return (WLAN_CIPHER_SUITE_WEP40);
586 else
587 return (WLAN_CIPHER_SUITE_WEP104);
588 break;
589 case IEEE80211_CIPHER_AES_OCB:
590 case IEEE80211_CIPHER_TKIPMIC:
591 case IEEE80211_CIPHER_CKIP:
592 case IEEE80211_CIPHER_NONE:
593 printf("%s: unsupported cipher %#010x\n", __func__, cipher);
594 break;
595 default:
596 printf("%s: unknown cipher %#010x\n", __func__, cipher);
597 };
598 return (0);
599 }
600 #endif
601
602 #ifdef __notyet__
603 static enum ieee80211_sta_state
lkpi_net80211_state_to_sta_state(enum ieee80211_state state)604 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
605 {
606
607 /*
608 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
609 * "done". Also ASSOC/AUTHORIZED are both "RUN" then?
610 */
611 switch (state) {
612 case IEEE80211_S_INIT:
613 return (IEEE80211_STA_NOTEXIST);
614 case IEEE80211_S_SCAN:
615 return (IEEE80211_STA_NONE);
616 case IEEE80211_S_AUTH:
617 return (IEEE80211_STA_AUTH);
618 case IEEE80211_S_ASSOC:
619 return (IEEE80211_STA_ASSOC);
620 case IEEE80211_S_RUN:
621 return (IEEE80211_STA_AUTHORIZED);
622 case IEEE80211_S_CAC:
623 case IEEE80211_S_CSA:
624 case IEEE80211_S_SLEEP:
625 default:
626 UNIMPLEMENTED;
627 };
628
629 return (IEEE80211_STA_NOTEXIST);
630 }
631 #endif
632
633 static struct linuxkpi_ieee80211_channel *
lkpi_find_lkpi80211_chan(struct lkpi_hw * lhw,struct ieee80211_channel * c)634 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
635 struct ieee80211_channel *c)
636 {
637 struct ieee80211_hw *hw;
638 struct linuxkpi_ieee80211_channel *channels;
639 enum nl80211_band band;
640 int i, nchans;
641
642 hw = LHW_TO_HW(lhw);
643 band = lkpi_net80211_chan_to_nl80211_band(c);
644 if (hw->wiphy->bands[band] == NULL)
645 return (NULL);
646
647 nchans = hw->wiphy->bands[band]->n_channels;
648 if (nchans <= 0)
649 return (NULL);
650
651 channels = hw->wiphy->bands[band]->channels;
652 for (i = 0; i < nchans; i++) {
653 if (channels[i].hw_value == c->ic_ieee)
654 return (&channels[i]);
655 }
656
657 return (NULL);
658 }
659
660 #if 0
661 static struct linuxkpi_ieee80211_channel *
662 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
663 {
664 struct linuxkpi_ieee80211_channel *chan;
665 struct ieee80211_channel *c;
666 struct lkpi_hw *lhw;
667
668 chan = NULL;
669 if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
670 c = ni->ni_chan;
671 else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
672 c = ic->ic_bsschan;
673 else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
674 c = ic->ic_curchan;
675 else
676 c = NULL;
677
678 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
679 lhw = ic->ic_softc;
680 chan = lkpi_find_lkpi80211_chan(lhw, c);
681 }
682
683 return (chan);
684 }
685 #endif
686
687 struct linuxkpi_ieee80211_channel *
linuxkpi_ieee80211_get_channel(struct wiphy * wiphy,uint32_t freq)688 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
689 {
690 enum nl80211_band band;
691
692 for (band = 0; band < NUM_NL80211_BANDS; band++) {
693 struct ieee80211_supported_band *supband;
694 struct linuxkpi_ieee80211_channel *channels;
695 int i;
696
697 supband = wiphy->bands[band];
698 if (supband == NULL || supband->n_channels == 0)
699 continue;
700
701 channels = supband->channels;
702 for (i = 0; i < supband->n_channels; i++) {
703 if (channels[i].center_freq == freq)
704 return (&channels[i]);
705 }
706 }
707
708 return (NULL);
709 }
710
711 #ifdef LKPI_80211_HW_CRYPTO
712 static int
_lkpi_iv_key_set_delete(struct ieee80211vap * vap,const struct ieee80211_key * k,enum set_key_cmd cmd)713 _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
714 enum set_key_cmd cmd)
715 {
716 struct ieee80211com *ic;
717 struct lkpi_hw *lhw;
718 struct ieee80211_hw *hw;
719 struct lkpi_vif *lvif;
720 struct ieee80211_vif *vif;
721 struct ieee80211_sta *sta;
722 struct ieee80211_node *ni;
723 struct ieee80211_key_conf *kc;
724 int error;
725
726 /* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
727
728 ic = vap->iv_ic;
729 lhw = ic->ic_softc;
730 hw = LHW_TO_HW(lhw);
731 lvif = VAP_TO_LVIF(vap);
732 vif = LVIF_TO_VIF(lvif);
733
734 memset(&kc, 0, sizeof(kc));
735 kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
736 kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
737 k->wk_cipher->ic_cipher, k->wk_keylen);
738 kc->keyidx = k->wk_keyix;
739 #if 0
740 kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
741 #endif
742 atomic64_set(&kc->tx_pn, k->wk_keytsc);
743 kc->keylen = k->wk_keylen;
744 memcpy(kc->key, k->wk_key, k->wk_keylen);
745
746 switch (kc->cipher) {
747 case WLAN_CIPHER_SUITE_CCMP:
748 kc->iv_len = k->wk_cipher->ic_header;
749 kc->icv_len = k->wk_cipher->ic_trailer;
750 break;
751 case WLAN_CIPHER_SUITE_TKIP:
752 default:
753 IMPROVE();
754 return (0);
755 };
756
757 ni = vap->iv_bss;
758 sta = ieee80211_find_sta(vif, ni->ni_bssid);
759 if (sta != NULL) {
760 struct lkpi_sta *lsta;
761
762 lsta = STA_TO_LSTA(sta);
763 lsta->kc = kc;
764 }
765
766 error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
767 if (error != 0) {
768 /* XXX-BZ leaking kc currently */
769 ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
770 return (0);
771 } else {
772 ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
773 "flags %#10x\n", __func__,
774 kc->keyidx, kc->hw_key_idx, kc->flags);
775 return (1);
776 }
777 }
778
779 static int
lkpi_iv_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)780 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
781 {
782
783 /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
784 return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
785 }
786 static int
lkpi_iv_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)787 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
788 {
789
790 return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
791 }
792 #endif
793
794 static u_int
lkpi_ic_update_mcast_copy(void * arg,struct sockaddr_dl * sdl,u_int cnt)795 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
796 {
797 struct netdev_hw_addr_list *mc_list;
798 struct netdev_hw_addr *addr;
799
800 KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
801 __func__, arg, sdl, cnt));
802
803 mc_list = arg;
804 /* If it is on the list already skip it. */
805 netdev_hw_addr_list_for_each(addr, mc_list) {
806 if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
807 return (0);
808 }
809
810 addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
811 if (addr == NULL)
812 return (0);
813
814 INIT_LIST_HEAD(&addr->addr_list);
815 memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
816 /* XXX this should be a netdev function? */
817 list_add(&addr->addr_list, &mc_list->addr_list);
818 mc_list->count++;
819
820 #ifdef LINUXKPI_DEBUG_80211
821 if (linuxkpi_debug_80211 & D80211_TRACE)
822 printf("%s:%d: mc_list count %d: added %6D\n",
823 __func__, __LINE__, mc_list->count, addr->addr, ":");
824 #endif
825
826 return (1);
827 }
828
829 static void
lkpi_update_mcast_filter(struct ieee80211com * ic,bool force)830 lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
831 {
832 struct lkpi_hw *lhw;
833 struct ieee80211_hw *hw;
834 struct netdev_hw_addr_list mc_list;
835 struct list_head *le, *next;
836 struct netdev_hw_addr *addr;
837 struct ieee80211vap *vap;
838 u64 mc;
839 unsigned int changed_flags, total_flags;
840
841 lhw = ic->ic_softc;
842
843 if (lhw->ops->prepare_multicast == NULL ||
844 lhw->ops->configure_filter == NULL)
845 return;
846
847 if (!lhw->update_mc && !force)
848 return;
849
850 changed_flags = total_flags = 0;
851 mc_list.count = 0;
852 INIT_LIST_HEAD(&mc_list.addr_list);
853 if (ic->ic_allmulti == 0) {
854 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
855 if_foreach_llmaddr(vap->iv_ifp,
856 lkpi_ic_update_mcast_copy, &mc_list);
857 } else {
858 changed_flags |= FIF_ALLMULTI;
859 }
860
861 hw = LHW_TO_HW(lhw);
862 mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
863 /*
864 * XXX-BZ make sure to get this sorted what is a change,
865 * what gets all set; what was already set?
866 */
867 total_flags = changed_flags;
868 lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
869
870 #ifdef LINUXKPI_DEBUG_80211
871 if (linuxkpi_debug_80211 & D80211_TRACE)
872 printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
873 __func__, changed_flags, mc_list.count, total_flags);
874 #endif
875
876 if (mc_list.count != 0) {
877 list_for_each_safe(le, next, &mc_list.addr_list) {
878 addr = list_entry(le, struct netdev_hw_addr, addr_list);
879 free(addr, M_LKPI80211);
880 mc_list.count--;
881 }
882 }
883 KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
884 __func__, &mc_list, mc_list.count));
885 }
886
887 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)888 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
889 struct ieee80211vap *vap, const char *_f, int _l)
890 {
891 enum ieee80211_bss_changed bss_changed;
892
893 bss_changed = 0;
894
895 #ifdef LINUXKPI_DEBUG_80211
896 if (linuxkpi_debug_80211 & D80211_TRACE)
897 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
898 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
899 "sync_device_ts %u bss_changed %#010jx\n",
900 __func__, __LINE__, _f, _l,
901 vif->cfg.assoc, vif->cfg.aid,
902 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
903 vif->bss_conf.sync_dtim_count,
904 (uintmax_t)vif->bss_conf.sync_tsf,
905 vif->bss_conf.sync_device_ts,
906 (uintmax_t)bss_changed);
907 #endif
908
909 if (vif->bss_conf.beacon_int != ni->ni_intval) {
910 vif->bss_conf.beacon_int = ni->ni_intval;
911 /* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
912 if (vif->bss_conf.beacon_int < 16)
913 vif->bss_conf.beacon_int = 16;
914 bss_changed |= BSS_CHANGED_BEACON_INT;
915 }
916 if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
917 vap->iv_dtim_period > 0) {
918 vif->bss_conf.dtim_period = vap->iv_dtim_period;
919 bss_changed |= BSS_CHANGED_BEACON_INFO;
920 }
921
922 vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
923 vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
924 /* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
925
926 #ifdef LINUXKPI_DEBUG_80211
927 if (linuxkpi_debug_80211 & D80211_TRACE)
928 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
929 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
930 "sync_device_ts %u bss_changed %#010jx\n",
931 __func__, __LINE__, _f, _l,
932 vif->cfg.assoc, vif->cfg.aid,
933 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
934 vif->bss_conf.sync_dtim_count,
935 (uintmax_t)vif->bss_conf.sync_tsf,
936 vif->bss_conf.sync_device_ts,
937 (uintmax_t)bss_changed);
938 #endif
939
940 return (bss_changed);
941 }
942
943 static void
lkpi_stop_hw_scan(struct lkpi_hw * lhw,struct ieee80211_vif * vif)944 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
945 {
946 struct ieee80211_hw *hw;
947 int error;
948 bool cancel;
949
950 LKPI_80211_LHW_SCAN_LOCK(lhw);
951 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
952 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
953 if (!cancel)
954 return;
955
956 hw = LHW_TO_HW(lhw);
957
958 IEEE80211_UNLOCK(lhw->ic);
959 LKPI_80211_LHW_LOCK(lhw);
960 /* Need to cancel the scan. */
961 lkpi_80211_mo_cancel_hw_scan(hw, vif);
962 LKPI_80211_LHW_UNLOCK(lhw);
963
964 /* Need to make sure we see ieee80211_scan_completed. */
965 LKPI_80211_LHW_SCAN_LOCK(lhw);
966 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
967 error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
968 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
969 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
970
971 IEEE80211_LOCK(lhw->ic);
972
973 if (cancel)
974 ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
975 __func__, error, lhw, vif);
976 }
977
978 static void
lkpi_hw_conf_idle(struct ieee80211_hw * hw,bool new)979 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
980 {
981 struct lkpi_hw *lhw;
982 int error;
983 bool old;
984
985 old = hw->conf.flags & IEEE80211_CONF_IDLE;
986 if (old == new)
987 return;
988
989 hw->conf.flags ^= IEEE80211_CONF_IDLE;
990 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
991 if (error != 0 && error != EOPNOTSUPP) {
992 lhw = HW_TO_LHW(hw);
993 ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
994 __func__, IEEE80211_CONF_CHANGE_IDLE, error);
995 }
996 }
997
998 static enum ieee80211_bss_changed
lkpi_disassoc(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct lkpi_hw * lhw)999 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1000 struct lkpi_hw *lhw)
1001 {
1002 enum ieee80211_bss_changed changed;
1003
1004 changed = 0;
1005 sta->aid = 0;
1006 if (vif->cfg.assoc) {
1007
1008 lhw->update_mc = true;
1009 lkpi_update_mcast_filter(lhw->ic, true);
1010
1011 vif->cfg.assoc = false;
1012 vif->cfg.aid = 0;
1013 changed |= BSS_CHANGED_ASSOC;
1014 IMPROVE();
1015
1016 /*
1017 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
1018 * assoc = false right away here will remove the sta from
1019 * firmware for iwlwifi.
1020 * We no longer do this but only return the BSS_CHNAGED value.
1021 * The caller is responsible for removing the sta gong to
1022 * IEEE80211_STA_NOTEXIST and then executing the
1023 * bss_info_changed() update.
1024 * See lkpi_sta_run_to_init() for more detailed comment.
1025 */
1026 }
1027
1028 return (changed);
1029 }
1030
1031 static void
lkpi_wake_tx_queues(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool dequeue_seen,bool no_emptyq)1032 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
1033 bool dequeue_seen, bool no_emptyq)
1034 {
1035 struct lkpi_txq *ltxq;
1036 int tid;
1037 bool ltxq_empty;
1038
1039 /* Wake up all queues to know they are allocated in the driver. */
1040 for (tid = 0; tid < nitems(sta->txq); tid++) {
1041
1042 if (tid == IEEE80211_NUM_TIDS) {
1043 IMPROVE("station specific?");
1044 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
1045 continue;
1046 } else if (tid >= hw->queues)
1047 continue;
1048
1049 if (sta->txq[tid] == NULL)
1050 continue;
1051
1052 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
1053 if (dequeue_seen && !ltxq->seen_dequeue)
1054 continue;
1055
1056 LKPI_80211_LTXQ_LOCK(ltxq);
1057 ltxq_empty = skb_queue_empty(<xq->skbq);
1058 LKPI_80211_LTXQ_UNLOCK(ltxq);
1059 if (no_emptyq && ltxq_empty)
1060 continue;
1061
1062 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
1063 }
1064 }
1065
1066 /*
1067 * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
1068 * packet. The problem is that the state machine functions tend to hold the
1069 * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
1070 * We call this after dropping the ic lock and before acquiring the LHW lock.
1071 * we make sure no further packets are queued and if they are queued the task
1072 * will finish or be cancelled. At the end if a packet is left we manually
1073 * send it. scan_to_auth() would re-enable sending if the lsta would be
1074 * re-used.
1075 */
1076 static void
lkpi_80211_flush_tx(struct lkpi_hw * lhw,struct lkpi_sta * lsta)1077 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
1078 {
1079 struct mbufq mq;
1080 struct mbuf *m;
1081 int len;
1082
1083 LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
1084
1085 /* Do not accept any new packets until scan_to_auth or lsta_free(). */
1086 LKPI_80211_LSTA_TXQ_LOCK(lsta);
1087 lsta->txq_ready = false;
1088 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1089
1090 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
1091 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
1092
1093 LKPI_80211_LSTA_TXQ_LOCK(lsta);
1094 len = mbufq_len(&lsta->txq);
1095 if (len <= 0) {
1096 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1097 return;
1098 }
1099
1100 mbufq_init(&mq, IFQ_MAXLEN);
1101 mbufq_concat(&mq, &lsta->txq);
1102 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1103
1104 m = mbufq_dequeue(&mq);
1105 while (m != NULL) {
1106 lkpi_80211_txq_tx_one(lsta, m);
1107 m = mbufq_dequeue(&mq);
1108 }
1109 }
1110
1111 /* -------------------------------------------------------------------------- */
1112
1113 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1114 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1115 {
1116
1117 return (0);
1118 }
1119
1120 /* lkpi_iv_newstate() handles the stop scan case generally. */
1121 #define lkpi_sta_scan_to_init(_v, _n, _a) lkpi_sta_state_do_nada(_v, _n, _a)
1122
1123 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1124 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1125 {
1126 struct linuxkpi_ieee80211_channel *chan;
1127 struct lkpi_chanctx *lchanctx;
1128 struct ieee80211_chanctx_conf *chanctx_conf;
1129 struct lkpi_hw *lhw;
1130 struct ieee80211_hw *hw;
1131 struct lkpi_vif *lvif;
1132 struct ieee80211_vif *vif;
1133 struct ieee80211_node *ni;
1134 struct lkpi_sta *lsta;
1135 enum ieee80211_bss_changed bss_changed;
1136 struct ieee80211_prep_tx_info prep_tx_info;
1137 uint32_t changed;
1138 int error;
1139
1140 /*
1141 * In here we use vap->iv_bss until lvif->lvif_bss is set.
1142 * For all later (STATE >= AUTH) functions we need to use the lvif
1143 * cache which will be tracked even through (*iv_update_bss)().
1144 */
1145
1146 if (vap->iv_bss == NULL) {
1147 ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
1148 return (EINVAL);
1149 }
1150 /*
1151 * Keep the ni alive locally. In theory (and practice) iv_bss can change
1152 * once we unlock here. This is due to net80211 allowing state changes
1153 * and new join1() despite having an active node as well as due to
1154 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
1155 */
1156 ni = ieee80211_ref_node(vap->iv_bss);
1157 if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
1158 ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
1159 "on vap %p\n", __func__, ni, vap);
1160 ieee80211_free_node(ni); /* Error handling for the local ni. */
1161 return (EINVAL);
1162 }
1163
1164 lhw = vap->iv_ic->ic_softc;
1165 chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
1166 if (chan == NULL) {
1167 ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
1168 "iv_bss ni %p on vap %p\n", __func__, ni, vap);
1169 ieee80211_free_node(ni); /* Error handling for the local ni. */
1170 return (ESRCH);
1171 }
1172
1173 hw = LHW_TO_HW(lhw);
1174 lvif = VAP_TO_LVIF(vap);
1175 vif = LVIF_TO_VIF(lvif);
1176
1177 LKPI_80211_LVIF_LOCK(lvif);
1178 /* XXX-BZ KASSERT later? */
1179 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
1180 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1181 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1182 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1183 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1184 lvif->lvif_bss_synched);
1185 return (EBUSY);
1186 }
1187 LKPI_80211_LVIF_UNLOCK(lvif);
1188
1189 IEEE80211_UNLOCK(vap->iv_ic);
1190 LKPI_80211_LHW_LOCK(lhw);
1191
1192 /* Add chanctx (or if exists, change it). */
1193 if (vif->chanctx_conf != NULL) {
1194 chanctx_conf = vif->chanctx_conf;
1195 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1196 IMPROVE("diff changes for changed, working on live copy, rcu");
1197 } else {
1198 /* Keep separate alloc as in Linux this is rcu managed? */
1199 lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
1200 M_LKPI80211, M_WAITOK | M_ZERO);
1201 chanctx_conf = &lchanctx->chanctx_conf;
1202 }
1203
1204 chanctx_conf->rx_chains_dynamic = 1;
1205 chanctx_conf->rx_chains_static = 1;
1206 chanctx_conf->radar_enabled =
1207 (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1208 chanctx_conf->def.chan = chan;
1209 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1210 chanctx_conf->def.center_freq1 = chan->center_freq;
1211 chanctx_conf->def.center_freq2 = 0;
1212 IMPROVE("Check vht_cap from band not just chan?");
1213 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
1214 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
1215 #ifdef LKPI_80211_HT
1216 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
1217 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1218 chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
1219 } else
1220 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
1221 }
1222 #endif
1223 #ifdef LKPI_80211_VHT
1224 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
1225 #ifdef __notyet__
1226 if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1227 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1228 chanctx_conf->def.center_freq2 = 0; /* XXX */
1229 } else
1230 #endif
1231 if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1232 chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
1233 else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1234 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
1235 }
1236 #endif
1237 /* Responder ... */
1238 chanctx_conf->min_def.chan = chan;
1239 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1240 chanctx_conf->min_def.center_freq1 = chan->center_freq;
1241 chanctx_conf->min_def.center_freq2 = 0;
1242 IMPROVE("currently 20_NOHT min_def only");
1243
1244 /* Set bss info (bss_info_changed). */
1245 bss_changed = 0;
1246 vif->bss_conf.bssid = ni->ni_bssid;
1247 bss_changed |= BSS_CHANGED_BSSID;
1248 vif->bss_conf.txpower = ni->ni_txpower;
1249 bss_changed |= BSS_CHANGED_TXPOWER;
1250 vif->cfg.idle = false;
1251 bss_changed |= BSS_CHANGED_IDLE;
1252
1253 /* vif->bss_conf.basic_rates ? Where exactly? */
1254
1255 /* Should almost assert it is this. */
1256 vif->cfg.assoc = false;
1257 vif->cfg.aid = 0;
1258
1259 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1260
1261 error = 0;
1262 if (vif->chanctx_conf != NULL) {
1263 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
1264 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
1265 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
1266 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1267 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
1268 } else {
1269 error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
1270 if (error == 0 || error == EOPNOTSUPP) {
1271 vif->bss_conf.chandef.chan = chanctx_conf->def.chan;
1272 vif->bss_conf.chandef.width = chanctx_conf->def.width;
1273 vif->bss_conf.chandef.center_freq1 =
1274 chanctx_conf->def.center_freq1;
1275 #ifdef LKPI_80211_HT
1276 if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) {
1277 /* Note: it is 10 not 20. */
1278 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1279 vif->bss_conf.chandef.center_freq1 += 10;
1280 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1281 vif->bss_conf.chandef.center_freq1 -= 10;
1282 }
1283 #endif
1284 vif->bss_conf.chandef.center_freq2 =
1285 chanctx_conf->def.center_freq2;
1286 } else {
1287 ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1288 "failed: %d\n", __func__, __LINE__, error);
1289 goto out;
1290 }
1291
1292 vif->bss_conf.chanctx_conf = chanctx_conf;
1293
1294 /* Assign vif chanctx. */
1295 if (error == 0)
1296 error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1297 &vif->bss_conf, chanctx_conf);
1298 if (error == EOPNOTSUPP)
1299 error = 0;
1300 if (error != 0) {
1301 ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1302 "failed: %d\n", __func__, __LINE__, error);
1303 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1304 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1305 free(lchanctx, M_LKPI80211);
1306 goto out;
1307 }
1308 }
1309 IMPROVE("update radiotap chan fields too");
1310
1311 /* RATES */
1312 IMPROVE("bss info: not all needs to come now and rates are missing");
1313 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1314
1315 /*
1316 * Given ni and lsta are 1:1 from alloc to free we can assert that
1317 * ni always has lsta data attach despite net80211 node swapping
1318 * under the hoods.
1319 */
1320 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
1321 __func__, ni, ni->ni_drv_data));
1322 lsta = ni->ni_drv_data;
1323
1324 /*
1325 * Make sure in case the sta did not change and we re-add it,
1326 * that we can tx again.
1327 */
1328 LKPI_80211_LSTA_TXQ_LOCK(lsta);
1329 lsta->txq_ready = true;
1330 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1331
1332 LKPI_80211_LVIF_LOCK(lvif);
1333 /* Insert the [l]sta into the list of known stations. */
1334 TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1335 LKPI_80211_LVIF_UNLOCK(lvif);
1336
1337 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
1338 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1339 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
1340 "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1341 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1342 if (error != 0) {
1343 IMPROVE("do we need to undo the chan ctx?");
1344 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1345 "failed: %d\n", __func__, __LINE__, error);
1346 goto out;
1347 }
1348 #if 0
1349 lsta->added_to_drv = true; /* mo manages. */
1350 #endif
1351
1352 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1353
1354 #if 0
1355 /*
1356 * Wakeup all queues now that sta is there so we have as much time to
1357 * possibly prepare the queue in the driver to be ready for the 1st
1358 * packet; lkpi_80211_txq_tx_one() still has a workaround as there
1359 * is no guarantee or way to check.
1360 * XXX-BZ and by now we know that this does not work on all drivers
1361 * for all queues.
1362 */
1363 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
1364 #endif
1365
1366 /* Start mgd_prepare_tx. */
1367 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1368 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1369 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1370 lsta->in_mgd = true;
1371
1372 /*
1373 * What is going to happen next:
1374 * - <twiddle> .. we should end up in "auth_to_assoc"
1375 * - event_callback
1376 * - update sta_state (NONE to AUTH)
1377 * - mgd_complete_tx
1378 * (ideally we'd do that on a callback for something else ...)
1379 */
1380
1381 LKPI_80211_LHW_UNLOCK(lhw);
1382 IEEE80211_LOCK(vap->iv_ic);
1383
1384 LKPI_80211_LVIF_LOCK(lvif);
1385 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1386 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1387 lsta->ni != vap->iv_bss)
1388 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1389 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1390 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1391 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1392 lvif->lvif_bss_synched, ni, lsta);
1393
1394 /*
1395 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1396 * Given we cache lsta we use lsta->ni instead of ni here (even though
1397 * lsta->ni == ni) to be distinct from the rest of the code where we do
1398 * assume that ni == vap->iv_bss which it may or may not be.
1399 * So do NOT use iv_bss here anymore as that may have diverged from our
1400 * function local ni already while ic was unlocked and would lead to
1401 * inconsistencies. Go and see if we lost a race and do not update
1402 * lvif_bss_synched in that case.
1403 */
1404 ieee80211_ref_node(lsta->ni);
1405 lvif->lvif_bss = lsta;
1406 if (lsta->ni == vap->iv_bss) {
1407 lvif->lvif_bss_synched = true;
1408 } else {
1409 /* Set to un-synched no matter what. */
1410 lvif->lvif_bss_synched = false;
1411 /*
1412 * We do not error as someone has to take us down.
1413 * If we are followed by a 2nd, new net80211::join1() going to
1414 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1415 * will take the lvif->lvif_bss node down eventually.
1416 * What happens with the vap->iv_bss node will entirely be up
1417 * to net80211 as we never used the node beyond alloc()/free()
1418 * and we do not hold an extra reference for that anymore given
1419 * ni : lsta == 1:1.
1420 */
1421 }
1422 LKPI_80211_LVIF_UNLOCK(lvif);
1423 goto out_relocked;
1424
1425 out:
1426 LKPI_80211_LHW_UNLOCK(lhw);
1427 IEEE80211_LOCK(vap->iv_ic);
1428 out_relocked:
1429 /*
1430 * Release the reference that kept the ni stable locally
1431 * during the work of this function.
1432 */
1433 if (ni != NULL)
1434 ieee80211_free_node(ni);
1435 return (error);
1436 }
1437
1438 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1439 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1440 {
1441 struct lkpi_hw *lhw;
1442 struct ieee80211_hw *hw;
1443 struct lkpi_vif *lvif;
1444 struct ieee80211_vif *vif;
1445 struct ieee80211_node *ni;
1446 struct lkpi_sta *lsta;
1447 struct ieee80211_sta *sta;
1448 struct ieee80211_prep_tx_info prep_tx_info;
1449 int error;
1450
1451 lhw = vap->iv_ic->ic_softc;
1452 hw = LHW_TO_HW(lhw);
1453 lvif = VAP_TO_LVIF(vap);
1454 vif = LVIF_TO_VIF(lvif);
1455
1456 LKPI_80211_LVIF_LOCK(lvif);
1457 #ifdef LINUXKPI_DEBUG_80211
1458 /* XXX-BZ KASSERT later; state going down so no action. */
1459 if (lvif->lvif_bss == NULL)
1460 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1461 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1462 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1463 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1464 lvif->lvif_bss_synched);
1465 #endif
1466
1467 lsta = lvif->lvif_bss;
1468 LKPI_80211_LVIF_UNLOCK(lvif);
1469 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1470 "lvif %p vap %p\n", __func__,
1471 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1472 ni = lsta->ni; /* Reference held for lvif_bss. */
1473 sta = LSTA_TO_STA(lsta);
1474
1475 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1476
1477 IEEE80211_UNLOCK(vap->iv_ic);
1478 LKPI_80211_LHW_LOCK(lhw);
1479
1480 /* flush, drop. */
1481 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
1482
1483 /* Wake tx queues to get packet(s) out. */
1484 lkpi_wake_tx_queues(hw, sta, false, true);
1485
1486 /* flush, no drop */
1487 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
1488
1489 /* End mgd_complete_tx. */
1490 if (lsta->in_mgd) {
1491 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1492 prep_tx_info.success = false;
1493 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1494 lsta->in_mgd = false;
1495 }
1496
1497 /* sync_rx_queues */
1498 lkpi_80211_mo_sync_rx_queues(hw);
1499
1500 /* sta_pre_rcu_remove */
1501 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1502
1503 /* Take the station down. */
1504
1505 /* Adjust sta and change state (from NONE) to NOTEXIST. */
1506 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1507 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1508 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1509 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1510 if (error != 0) {
1511 IMPROVE("do we need to undo the chan ctx?");
1512 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1513 "failed: %d\n", __func__, __LINE__, error);
1514 goto out;
1515 }
1516 #if 0
1517 lsta->added_to_drv = false; /* mo manages. */
1518 #endif
1519
1520 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1521
1522 LKPI_80211_LVIF_LOCK(lvif);
1523 /* Remove ni reference for this cache of lsta. */
1524 lvif->lvif_bss = NULL;
1525 lvif->lvif_bss_synched = false;
1526 LKPI_80211_LVIF_UNLOCK(lvif);
1527 lkpi_lsta_remove(lsta, lvif);
1528 /*
1529 * The very last release the reference on the ni for the ni/lsta on
1530 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
1531 * and potentially freed.
1532 */
1533 ieee80211_free_node(ni);
1534
1535 /* conf_tx */
1536
1537 /* Take the chan ctx down. */
1538 if (vif->chanctx_conf != NULL) {
1539 struct lkpi_chanctx *lchanctx;
1540 struct ieee80211_chanctx_conf *chanctx_conf;
1541
1542 chanctx_conf = vif->chanctx_conf;
1543 /* Remove vif context. */
1544 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1545 /* NB: vif->chanctx_conf is NULL now. */
1546
1547 lkpi_hw_conf_idle(hw, true);
1548
1549 /* Remove chan ctx. */
1550 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1551 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1552 free(lchanctx, M_LKPI80211);
1553 }
1554
1555 out:
1556 LKPI_80211_LHW_UNLOCK(lhw);
1557 IEEE80211_LOCK(vap->iv_ic);
1558 return (error);
1559 }
1560
1561 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1562 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1563 {
1564 int error;
1565
1566 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1567 if (error == 0)
1568 error = lkpi_sta_scan_to_init(vap, nstate, arg);
1569 return (error);
1570 }
1571
1572 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1573 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1574 {
1575 struct lkpi_hw *lhw;
1576 struct ieee80211_hw *hw;
1577 struct lkpi_vif *lvif;
1578 struct ieee80211_vif *vif;
1579 struct lkpi_sta *lsta;
1580 struct ieee80211_prep_tx_info prep_tx_info;
1581 int error;
1582
1583 lhw = vap->iv_ic->ic_softc;
1584 hw = LHW_TO_HW(lhw);
1585 lvif = VAP_TO_LVIF(vap);
1586 vif = LVIF_TO_VIF(lvif);
1587
1588 IEEE80211_UNLOCK(vap->iv_ic);
1589 LKPI_80211_LHW_LOCK(lhw);
1590
1591 LKPI_80211_LVIF_LOCK(lvif);
1592 /* XXX-BZ KASSERT later? */
1593 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1594 #ifdef LINUXKPI_DEBUG_80211
1595 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1596 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1597 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1598 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1599 lvif->lvif_bss_synched);
1600 #endif
1601 error = ENOTRECOVERABLE;
1602 LKPI_80211_LVIF_UNLOCK(lvif);
1603 goto out;
1604 }
1605 lsta = lvif->lvif_bss;
1606 LKPI_80211_LVIF_UNLOCK(lvif);
1607
1608 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
1609
1610 /* Finish auth. */
1611 IMPROVE("event callback");
1612
1613 /* Update sta_state (NONE to AUTH). */
1614 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1615 "NONE: %#x\n", __func__, lsta, lsta->state));
1616 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1617 if (error != 0) {
1618 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1619 "failed: %d\n", __func__, __LINE__, error);
1620 goto out;
1621 }
1622
1623 /* End mgd_complete_tx. */
1624 if (lsta->in_mgd) {
1625 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1626 prep_tx_info.success = true;
1627 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1628 lsta->in_mgd = false;
1629 }
1630
1631 /* Now start assoc. */
1632
1633 /* Start mgd_prepare_tx. */
1634 if (!lsta->in_mgd) {
1635 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1636 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1637 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1638 lsta->in_mgd = true;
1639 }
1640
1641 /* Wake tx queue to get packet out. */
1642 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
1643
1644 /*
1645 * <twiddle> .. we end up in "assoc_to_run"
1646 * - update sta_state (AUTH to ASSOC)
1647 * - conf_tx [all]
1648 * - bss_info_changed (assoc, aid, ssid, ..)
1649 * - change_chanctx (if needed)
1650 * - event_callback
1651 * - mgd_complete_tx
1652 */
1653
1654 out:
1655 LKPI_80211_LHW_UNLOCK(lhw);
1656 IEEE80211_LOCK(vap->iv_ic);
1657 return (error);
1658 }
1659
1660 /* auth_to_auth, assoc_to_assoc. */
1661 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1662 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1663 {
1664 struct lkpi_hw *lhw;
1665 struct ieee80211_hw *hw;
1666 struct lkpi_vif *lvif;
1667 struct ieee80211_vif *vif;
1668 struct lkpi_sta *lsta;
1669 struct ieee80211_prep_tx_info prep_tx_info;
1670 int error;
1671
1672 lhw = vap->iv_ic->ic_softc;
1673 hw = LHW_TO_HW(lhw);
1674 lvif = VAP_TO_LVIF(vap);
1675 vif = LVIF_TO_VIF(lvif);
1676
1677 IEEE80211_UNLOCK(vap->iv_ic);
1678 LKPI_80211_LHW_LOCK(lhw);
1679
1680 LKPI_80211_LVIF_LOCK(lvif);
1681 /* XXX-BZ KASSERT later? */
1682 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1683 #ifdef LINUXKPI_DEBUG_80211
1684 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1685 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1686 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1687 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1688 lvif->lvif_bss_synched);
1689 #endif
1690 LKPI_80211_LVIF_UNLOCK(lvif);
1691 error = ENOTRECOVERABLE;
1692 goto out;
1693 }
1694 lsta = lvif->lvif_bss;
1695 LKPI_80211_LVIF_UNLOCK(lvif);
1696
1697 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
1698 lsta, lvif, vap));
1699
1700 IMPROVE("event callback?");
1701
1702 /* End mgd_complete_tx. */
1703 if (lsta->in_mgd) {
1704 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1705 prep_tx_info.success = false;
1706 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1707 lsta->in_mgd = false;
1708 }
1709
1710 /* Now start assoc. */
1711
1712 /* Start mgd_prepare_tx. */
1713 if (!lsta->in_mgd) {
1714 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1715 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1716 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1717 lsta->in_mgd = true;
1718 }
1719
1720 error = 0;
1721 out:
1722 LKPI_80211_LHW_UNLOCK(lhw);
1723 IEEE80211_LOCK(vap->iv_ic);
1724
1725 return (error);
1726 }
1727
1728 static int
_lkpi_sta_assoc_to_down(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1729 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1730 {
1731 struct lkpi_hw *lhw;
1732 struct ieee80211_hw *hw;
1733 struct lkpi_vif *lvif;
1734 struct ieee80211_vif *vif;
1735 struct ieee80211_node *ni;
1736 struct lkpi_sta *lsta;
1737 struct ieee80211_sta *sta;
1738 struct ieee80211_prep_tx_info prep_tx_info;
1739 enum ieee80211_bss_changed bss_changed;
1740 int error;
1741
1742 lhw = vap->iv_ic->ic_softc;
1743 hw = LHW_TO_HW(lhw);
1744 lvif = VAP_TO_LVIF(vap);
1745 vif = LVIF_TO_VIF(lvif);
1746
1747 IEEE80211_UNLOCK(vap->iv_ic);
1748 LKPI_80211_LHW_LOCK(lhw);
1749
1750 LKPI_80211_LVIF_LOCK(lvif);
1751 #ifdef LINUXKPI_DEBUG_80211
1752 /* XXX-BZ KASSERT later; state going down so no action. */
1753 if (lvif->lvif_bss == NULL)
1754 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1755 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1756 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1757 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1758 lvif->lvif_bss_synched);
1759 #endif
1760 lsta = lvif->lvif_bss;
1761 LKPI_80211_LVIF_UNLOCK(lvif);
1762 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1763 "lvif %p vap %p\n", __func__,
1764 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1765
1766 ni = lsta->ni; /* Reference held for lvif_bss. */
1767 sta = LSTA_TO_STA(lsta);
1768
1769 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1770
1771 /* flush, drop. */
1772 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
1773
1774 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1775 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1776 !lsta->in_mgd) {
1777 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1778 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1779 prep_tx_info.was_assoc = true;
1780 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1781 lsta->in_mgd = true;
1782 }
1783
1784 LKPI_80211_LHW_UNLOCK(lhw);
1785 IEEE80211_LOCK(vap->iv_ic);
1786
1787 /* Call iv_newstate first so we get potential DEAUTH packet out. */
1788 error = lvif->iv_newstate(vap, nstate, arg);
1789 if (error != 0) {
1790 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1791 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1792 goto outni;
1793 }
1794
1795 IEEE80211_UNLOCK(vap->iv_ic);
1796
1797 /* Ensure the packets get out. */
1798 lkpi_80211_flush_tx(lhw, lsta);
1799
1800 LKPI_80211_LHW_LOCK(lhw);
1801
1802 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1803
1804 /* Wake tx queues to get packet(s) out. */
1805 lkpi_wake_tx_queues(hw, sta, false, true);
1806
1807 /* flush, no drop */
1808 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
1809
1810 /* End mgd_complete_tx. */
1811 if (lsta->in_mgd) {
1812 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1813 prep_tx_info.success = false;
1814 prep_tx_info.was_assoc = true;
1815 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1816 lsta->in_mgd = false;
1817 }
1818
1819 /* sync_rx_queues */
1820 lkpi_80211_mo_sync_rx_queues(hw);
1821
1822 /* sta_pre_rcu_remove */
1823 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1824
1825 /* Take the station down. */
1826
1827 /* Update sta and change state (from AUTH) to NONE. */
1828 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1829 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1830 "AUTH: %#x\n", __func__, lsta, lsta->state));
1831 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1832 if (error != 0) {
1833 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1834 "failed: %d\n", __func__, __LINE__, error);
1835 goto out;
1836 }
1837
1838 /* See comment in lkpi_sta_run_to_init(). */
1839 bss_changed = 0;
1840 bss_changed |= lkpi_disassoc(sta, vif, lhw);
1841
1842 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1843
1844 /* Adjust sta and change state (from NONE) to NOTEXIST. */
1845 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1846 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1847 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1848 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1849 if (error != 0) {
1850 IMPROVE("do we need to undo the chan ctx?");
1851 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1852 "failed: %d\n", __func__, __LINE__, error);
1853 goto out;
1854 }
1855
1856 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
1857
1858 IMPROVE("Any bss_info changes to announce?");
1859 vif->bss_conf.qos = 0;
1860 bss_changed |= BSS_CHANGED_QOS;
1861 vif->cfg.ssid_len = 0;
1862 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1863 bss_changed |= BSS_CHANGED_BSSID;
1864 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1865
1866 LKPI_80211_LVIF_LOCK(lvif);
1867 /* Remove ni reference for this cache of lsta. */
1868 lvif->lvif_bss = NULL;
1869 lvif->lvif_bss_synched = false;
1870 LKPI_80211_LVIF_UNLOCK(lvif);
1871 lkpi_lsta_remove(lsta, lvif);
1872 /*
1873 * The very last release the reference on the ni for the ni/lsta on
1874 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
1875 * and potentially freed.
1876 */
1877 ieee80211_free_node(ni);
1878
1879 /* conf_tx */
1880
1881 /* Take the chan ctx down. */
1882 if (vif->chanctx_conf != NULL) {
1883 struct lkpi_chanctx *lchanctx;
1884 struct ieee80211_chanctx_conf *chanctx_conf;
1885
1886 chanctx_conf = vif->chanctx_conf;
1887 /* Remove vif context. */
1888 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1889 /* NB: vif->chanctx_conf is NULL now. */
1890
1891 lkpi_hw_conf_idle(hw, true);
1892
1893 /* Remove chan ctx. */
1894 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1895 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1896 free(lchanctx, M_LKPI80211);
1897 }
1898
1899 error = EALREADY;
1900 out:
1901 LKPI_80211_LHW_UNLOCK(lhw);
1902 IEEE80211_LOCK(vap->iv_ic);
1903 outni:
1904 return (error);
1905 }
1906
1907 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1908 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1909 {
1910 int error;
1911
1912 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1913 if (error != 0 && error != EALREADY)
1914 return (error);
1915
1916 /* At this point iv_bss is long a new node! */
1917
1918 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1919 return (error);
1920 }
1921
1922 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1923 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1924 {
1925 int error;
1926
1927 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1928 return (error);
1929 }
1930
1931 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1932 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1933 {
1934 int error;
1935
1936 error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1937 return (error);
1938 }
1939
1940 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1941 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1942 {
1943 struct lkpi_hw *lhw;
1944 struct ieee80211_hw *hw;
1945 struct lkpi_vif *lvif;
1946 struct ieee80211_vif *vif;
1947 struct ieee80211_node *ni;
1948 struct lkpi_sta *lsta;
1949 struct ieee80211_sta *sta;
1950 struct ieee80211_prep_tx_info prep_tx_info;
1951 enum ieee80211_bss_changed bss_changed;
1952 int error;
1953
1954 lhw = vap->iv_ic->ic_softc;
1955 hw = LHW_TO_HW(lhw);
1956 lvif = VAP_TO_LVIF(vap);
1957 vif = LVIF_TO_VIF(lvif);
1958
1959 IEEE80211_UNLOCK(vap->iv_ic);
1960 LKPI_80211_LHW_LOCK(lhw);
1961
1962 LKPI_80211_LVIF_LOCK(lvif);
1963 /* XXX-BZ KASSERT later? */
1964 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1965 #ifdef LINUXKPI_DEBUG_80211
1966 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1967 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1968 lvif, vap, vap->iv_bss, lvif->lvif_bss,
1969 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1970 lvif->lvif_bss_synched);
1971 #endif
1972 LKPI_80211_LVIF_UNLOCK(lvif);
1973 error = ENOTRECOVERABLE;
1974 goto out;
1975 }
1976 lsta = lvif->lvif_bss;
1977 LKPI_80211_LVIF_UNLOCK(lvif);
1978 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1979 "lvif %p vap %p\n", __func__,
1980 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1981
1982 ni = lsta->ni; /* Reference held for lvif_bss. */
1983
1984 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1985 "and to lesser extend ieee80211_notify_node_join");
1986
1987 /* Finish assoc. */
1988 /* Update sta_state (AUTH to ASSOC) and set aid. */
1989 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1990 "AUTH: %#x\n", __func__, lsta, lsta->state));
1991 sta = LSTA_TO_STA(lsta);
1992 sta->aid = IEEE80211_NODE_AID(ni);
1993 #ifdef LKPI_80211_WME
1994 if (vap->iv_flags & IEEE80211_F_WME)
1995 sta->wme = true;
1996 #endif
1997 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1998 if (error != 0) {
1999 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2000 "failed: %d\n", __func__, __LINE__, error);
2001 goto out;
2002 }
2003
2004 IMPROVE("wme / conf_tx [all]");
2005
2006 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
2007 bss_changed = 0;
2008 #ifdef LKPI_80211_WME
2009 bss_changed |= lkpi_wme_update(lhw, vap, true);
2010 #endif
2011 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2012 vif->cfg.assoc = true;
2013 vif->cfg.aid = IEEE80211_NODE_AID(ni);
2014 bss_changed |= BSS_CHANGED_ASSOC;
2015 }
2016 /* We set SSID but this is not BSSID! */
2017 vif->cfg.ssid_len = ni->ni_esslen;
2018 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
2019 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
2020 vif->bss_conf.use_short_preamble) {
2021 vif->bss_conf.use_short_preamble ^= 1;
2022 /* bss_changed |= BSS_CHANGED_??? */
2023 }
2024 if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
2025 vif->bss_conf.use_short_slot) {
2026 vif->bss_conf.use_short_slot ^= 1;
2027 /* bss_changed |= BSS_CHANGED_??? */
2028 }
2029 if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
2030 vif->bss_conf.qos) {
2031 vif->bss_conf.qos ^= 1;
2032 bss_changed |= BSS_CHANGED_QOS;
2033 }
2034
2035 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2036
2037 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2038
2039 /* - change_chanctx (if needed)
2040 * - event_callback
2041 */
2042
2043 /* End mgd_complete_tx. */
2044 if (lsta->in_mgd) {
2045 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2046 prep_tx_info.success = true;
2047 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2048 lsta->in_mgd = false;
2049 }
2050
2051 lkpi_hw_conf_idle(hw, false);
2052
2053 /*
2054 * And then:
2055 * - (more packets)?
2056 * - set_key
2057 * - set_default_unicast_key
2058 * - set_key (?)
2059 * - ipv6_addr_change (?)
2060 */
2061 /* Prepare_multicast && configure_filter. */
2062 lhw->update_mc = true;
2063 lkpi_update_mcast_filter(vap->iv_ic, true);
2064
2065 if (!ieee80211_node_is_authorized(ni)) {
2066 IMPROVE("net80211 does not consider node authorized");
2067 }
2068
2069 #if defined(LKPI_80211_HT)
2070 IMPROVE("Is this the right spot, has net80211 done all updates already?");
2071 lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
2072 #endif
2073
2074 /* Update sta_state (ASSOC to AUTHORIZED). */
2075 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2076 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2077 "ASSOC: %#x\n", __func__, lsta, lsta->state));
2078 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
2079 if (error != 0) {
2080 IMPROVE("undo some changes?");
2081 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2082 "failed: %d\n", __func__, __LINE__, error);
2083 goto out;
2084 }
2085
2086 /* - drv_config (?)
2087 * - bss_info_changed
2088 * - set_rekey_data (?)
2089 *
2090 * And now we should be passing packets.
2091 */
2092 IMPROVE("Need that bssid setting, and the keys");
2093
2094 bss_changed = 0;
2095 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2096 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2097
2098 out:
2099 LKPI_80211_LHW_UNLOCK(lhw);
2100 IEEE80211_LOCK(vap->iv_ic);
2101 return (error);
2102 }
2103
2104 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2105 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2106 {
2107 int error;
2108
2109 error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
2110 if (error == 0)
2111 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
2112 return (error);
2113 }
2114
2115 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2116 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2117 {
2118 struct lkpi_hw *lhw;
2119 struct ieee80211_hw *hw;
2120 struct lkpi_vif *lvif;
2121 struct ieee80211_vif *vif;
2122 struct ieee80211_node *ni;
2123 struct lkpi_sta *lsta;
2124 struct ieee80211_sta *sta;
2125 struct ieee80211_prep_tx_info prep_tx_info;
2126 #if 0
2127 enum ieee80211_bss_changed bss_changed;
2128 #endif
2129 int error;
2130
2131 lhw = vap->iv_ic->ic_softc;
2132 hw = LHW_TO_HW(lhw);
2133 lvif = VAP_TO_LVIF(vap);
2134 vif = LVIF_TO_VIF(lvif);
2135
2136 LKPI_80211_LVIF_LOCK(lvif);
2137 #ifdef LINUXKPI_DEBUG_80211
2138 /* XXX-BZ KASSERT later; state going down so no action. */
2139 if (lvif->lvif_bss == NULL)
2140 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2141 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2142 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2143 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2144 lvif->lvif_bss_synched);
2145 #endif
2146 lsta = lvif->lvif_bss;
2147 LKPI_80211_LVIF_UNLOCK(lvif);
2148 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2149 "lvif %p vap %p\n", __func__,
2150 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2151
2152 ni = lsta->ni; /* Reference held for lvif_bss. */
2153 sta = LSTA_TO_STA(lsta);
2154
2155 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2156
2157 IEEE80211_UNLOCK(vap->iv_ic);
2158 LKPI_80211_LHW_LOCK(lhw);
2159
2160 /* flush, drop. */
2161 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2162
2163 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2164 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2165 !lsta->in_mgd) {
2166 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2167 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2168 prep_tx_info.was_assoc = true;
2169 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2170 lsta->in_mgd = true;
2171 }
2172
2173 LKPI_80211_LHW_UNLOCK(lhw);
2174 IEEE80211_LOCK(vap->iv_ic);
2175
2176 /* Call iv_newstate first so we get potential DISASSOC packet out. */
2177 error = lvif->iv_newstate(vap, nstate, arg);
2178 if (error != 0) {
2179 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2180 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2181 goto outni;
2182 }
2183
2184 IEEE80211_UNLOCK(vap->iv_ic);
2185
2186 /* Ensure the packets get out. */
2187 lkpi_80211_flush_tx(lhw, lsta);
2188
2189 LKPI_80211_LHW_LOCK(lhw);
2190
2191 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2192
2193 /* Wake tx queues to get packet(s) out. */
2194 lkpi_wake_tx_queues(hw, sta, false, true);
2195
2196 /* flush, no drop */
2197 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2198
2199 /* End mgd_complete_tx. */
2200 if (lsta->in_mgd) {
2201 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2202 prep_tx_info.success = false;
2203 prep_tx_info.was_assoc = true;
2204 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2205 lsta->in_mgd = false;
2206 }
2207
2208 #if 0
2209 /* sync_rx_queues */
2210 lkpi_80211_mo_sync_rx_queues(hw);
2211
2212 /* sta_pre_rcu_remove */
2213 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2214 #endif
2215
2216 /* Take the station down. */
2217
2218 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2219 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2220 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2221 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2222 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2223 if (error != 0) {
2224 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2225 "failed: %d\n", __func__, __LINE__, error);
2226 goto out;
2227 }
2228
2229 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2230
2231 /* Update sta_state (ASSOC to AUTH). */
2232 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2233 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2234 "ASSOC: %#x\n", __func__, lsta, lsta->state));
2235 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2236 if (error != 0) {
2237 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2238 "failed: %d\n", __func__, __LINE__, error);
2239 goto out;
2240 }
2241
2242 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2243
2244 #if 0
2245 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
2246 lkpi_disassoc(sta, vif, lhw);
2247 #endif
2248
2249 error = EALREADY;
2250 out:
2251 LKPI_80211_LHW_UNLOCK(lhw);
2252 IEEE80211_LOCK(vap->iv_ic);
2253 outni:
2254 return (error);
2255 }
2256
2257 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2258 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2259 {
2260 struct lkpi_hw *lhw;
2261 struct ieee80211_hw *hw;
2262 struct lkpi_vif *lvif;
2263 struct ieee80211_vif *vif;
2264 struct ieee80211_node *ni;
2265 struct lkpi_sta *lsta;
2266 struct ieee80211_sta *sta;
2267 struct ieee80211_prep_tx_info prep_tx_info;
2268 enum ieee80211_bss_changed bss_changed;
2269 int error;
2270
2271 lhw = vap->iv_ic->ic_softc;
2272 hw = LHW_TO_HW(lhw);
2273 lvif = VAP_TO_LVIF(vap);
2274 vif = LVIF_TO_VIF(lvif);
2275
2276 IEEE80211_UNLOCK(vap->iv_ic);
2277 LKPI_80211_LHW_LOCK(lhw);
2278
2279 LKPI_80211_LVIF_LOCK(lvif);
2280 #ifdef LINUXKPI_DEBUG_80211
2281 /* XXX-BZ KASSERT later; state going down so no action. */
2282 if (lvif->lvif_bss == NULL)
2283 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2284 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2285 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2286 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2287 lvif->lvif_bss_synched);
2288 #endif
2289 lsta = lvif->lvif_bss;
2290 LKPI_80211_LVIF_UNLOCK(lvif);
2291 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2292 "lvif %p vap %p\n", __func__,
2293 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2294
2295 ni = lsta->ni; /* Reference held for lvif_bss. */
2296 sta = LSTA_TO_STA(lsta);
2297
2298 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2299
2300 /* flush, drop. */
2301 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
2302
2303 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2304 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2305 !lsta->in_mgd) {
2306 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2307 prep_tx_info.duration = PREP_TX_INFO_DURATION;
2308 prep_tx_info.was_assoc = true;
2309 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2310 lsta->in_mgd = true;
2311 }
2312
2313 LKPI_80211_LHW_UNLOCK(lhw);
2314 IEEE80211_LOCK(vap->iv_ic);
2315
2316 /* Call iv_newstate first so we get potential DISASSOC packet out. */
2317 error = lvif->iv_newstate(vap, nstate, arg);
2318 if (error != 0) {
2319 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2320 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2321 goto outni;
2322 }
2323
2324 IEEE80211_UNLOCK(vap->iv_ic);
2325
2326 /* Ensure the packets get out. */
2327 lkpi_80211_flush_tx(lhw, lsta);
2328
2329 LKPI_80211_LHW_LOCK(lhw);
2330
2331 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2332
2333 /* Wake tx queues to get packet(s) out. */
2334 lkpi_wake_tx_queues(hw, sta, false, true);
2335
2336 /* flush, no drop */
2337 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
2338
2339 /* End mgd_complete_tx. */
2340 if (lsta->in_mgd) {
2341 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2342 prep_tx_info.success = false;
2343 prep_tx_info.was_assoc = true;
2344 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2345 lsta->in_mgd = false;
2346 }
2347
2348 /* sync_rx_queues */
2349 lkpi_80211_mo_sync_rx_queues(hw);
2350
2351 /* sta_pre_rcu_remove */
2352 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2353
2354 /* Take the station down. */
2355
2356 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2357 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2358 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2359 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2360 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2361 if (error != 0) {
2362 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2363 "failed: %d\n", __func__, __LINE__, error);
2364 goto out;
2365 }
2366
2367 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2368
2369 /* Update sta_state (ASSOC to AUTH). */
2370 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2371 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2372 "ASSOC: %#x\n", __func__, lsta, lsta->state));
2373 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2374 if (error != 0) {
2375 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2376 "failed: %d\n", __func__, __LINE__, error);
2377 goto out;
2378 }
2379
2380 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2381
2382 /* Update sta and change state (from AUTH) to NONE. */
2383 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2384 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2385 "AUTH: %#x\n", __func__, lsta, lsta->state));
2386 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2387 if (error != 0) {
2388 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2389 "failed: %d\n", __func__, __LINE__, error);
2390 goto out;
2391 }
2392
2393 bss_changed = 0;
2394 /*
2395 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
2396 *
2397 * One would expect this to happen when going off AUTHORIZED.
2398 * See comment there; removes the sta from fw if not careful
2399 * (bss_info_changed() change is executed right away).
2400 *
2401 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
2402 * as otherwise drivers (iwlwifi at least) will silently not remove
2403 * the sta from the firmware and when we will add a new one trigger
2404 * a fw assert.
2405 *
2406 * The order which works best so far avoiding early removal or silent
2407 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
2408 * the iwlwifi:mac80211.c case still to be tested):
2409 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
2410 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
2411 * (removes the sta given assoc is false)
2412 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
2413 * 4) call unassign_vif_chanctx
2414 * 5) call lkpi_hw_conf_idle
2415 * 6) call remove_chanctx
2416 */
2417 bss_changed |= lkpi_disassoc(sta, vif, lhw);
2418
2419 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2420
2421 /* Adjust sta and change state (from NONE) to NOTEXIST. */
2422 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2423 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2424 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2425 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2426 if (error != 0) {
2427 IMPROVE("do we need to undo the chan ctx?");
2428 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2429 "failed: %d\n", __func__, __LINE__, error);
2430 goto out;
2431 }
2432
2433 lkpi_lsta_remove(lsta, lvif);
2434
2435 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */
2436
2437 IMPROVE("Any bss_info changes to announce?");
2438 vif->bss_conf.qos = 0;
2439 bss_changed |= BSS_CHANGED_QOS;
2440 vif->cfg.ssid_len = 0;
2441 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2442 bss_changed |= BSS_CHANGED_BSSID;
2443 vif->bss_conf.use_short_preamble = false;
2444 vif->bss_conf.qos = false;
2445 /* XXX BSS_CHANGED_???? */
2446 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2447
2448 LKPI_80211_LVIF_LOCK(lvif);
2449 /* Remove ni reference for this cache of lsta. */
2450 lvif->lvif_bss = NULL;
2451 lvif->lvif_bss_synched = false;
2452 LKPI_80211_LVIF_UNLOCK(lvif);
2453 /*
2454 * The very last release the reference on the ni for the ni/lsta on
2455 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
2456 * and potentially freed.
2457 */
2458 ieee80211_free_node(ni);
2459
2460 /* conf_tx */
2461
2462 /* Take the chan ctx down. */
2463 if (vif->chanctx_conf != NULL) {
2464 struct lkpi_chanctx *lchanctx;
2465 struct ieee80211_chanctx_conf *chanctx_conf;
2466
2467 chanctx_conf = vif->chanctx_conf;
2468 /* Remove vif context. */
2469 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2470 /* NB: vif->chanctx_conf is NULL now. */
2471
2472 lkpi_hw_conf_idle(hw, true);
2473
2474 /* Remove chan ctx. */
2475 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2476 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2477 free(lchanctx, M_LKPI80211);
2478 }
2479
2480 error = EALREADY;
2481 out:
2482 LKPI_80211_LHW_UNLOCK(lhw);
2483 IEEE80211_LOCK(vap->iv_ic);
2484 outni:
2485 return (error);
2486 }
2487
2488 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2489 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2490 {
2491
2492 return (lkpi_sta_run_to_init(vap, nstate, arg));
2493 }
2494
2495 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2496 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2497 {
2498 int error;
2499
2500 error = lkpi_sta_run_to_init(vap, nstate, arg);
2501 if (error != 0 && error != EALREADY)
2502 return (error);
2503
2504 /* At this point iv_bss is long a new node! */
2505
2506 error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2507 return (error);
2508 }
2509
2510 /* -------------------------------------------------------------------------- */
2511
2512 /*
2513 * The matches the documented state changes in net80211::sta_newstate().
2514 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
2515 * there are "invalid" (so there is a room for failure here).
2516 */
2517 struct fsm_state {
2518 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
2519 enum ieee80211_state ostate;
2520 enum ieee80211_state nstate;
2521 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
2522 } sta_state_fsm[] = {
2523 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada },
2524 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* scan_to_init */
2525 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */
2526 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */
2527 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */
2528
2529 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2530 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2531 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
2532 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2533 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */
2534
2535 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
2536 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
2537 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */
2538 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* Send ?AUTH. */
2539 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */
2540
2541 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* Send ASSOCREQ. */
2542 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */
2543 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* Send ASSOCREQ/REASSOCREQ. */
2544
2545 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run },
2546 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
2547 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada },
2548
2549 /* Dummy at the end without handler. */
2550 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL },
2551 };
2552
2553 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2554 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2555 {
2556 struct ieee80211com *ic;
2557 struct lkpi_hw *lhw;
2558 struct lkpi_vif *lvif;
2559 struct ieee80211_vif *vif;
2560 struct fsm_state *s;
2561 enum ieee80211_state ostate;
2562 int error;
2563
2564 ic = vap->iv_ic;
2565 IEEE80211_LOCK_ASSERT(ic);
2566 ostate = vap->iv_state;
2567
2568 #ifdef LINUXKPI_DEBUG_80211
2569 if (linuxkpi_debug_80211 & D80211_TRACE)
2570 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
2571 __func__, __LINE__, vap, nstate, arg);
2572 #endif
2573
2574 if (vap->iv_opmode == IEEE80211_M_STA) {
2575
2576 lhw = ic->ic_softc;
2577 lvif = VAP_TO_LVIF(vap);
2578 vif = LVIF_TO_VIF(lvif);
2579
2580 /* No need to replicate this in most state handlers. */
2581 if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2582 lkpi_stop_hw_scan(lhw, vif);
2583
2584 s = sta_state_fsm;
2585
2586 } else {
2587 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2588 "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2589 return (ENOSYS);
2590 }
2591
2592 error = 0;
2593 for (; s->handler != NULL; s++) {
2594 if (ostate == s->ostate && nstate == s->nstate) {
2595 #ifdef LINUXKPI_DEBUG_80211
2596 if (linuxkpi_debug_80211 & D80211_TRACE)
2597 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2598 " %d (%s): arg %d.\n", __func__,
2599 ostate, ieee80211_state_name[ostate],
2600 nstate, ieee80211_state_name[nstate], arg);
2601 #endif
2602 error = s->handler(vap, nstate, arg);
2603 break;
2604 }
2605 }
2606 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2607
2608 if (s->handler == NULL) {
2609 IMPROVE("turn this into a KASSERT\n");
2610 ic_printf(vap->iv_ic, "%s: unsupported state transition "
2611 "%d (%s) -> %d (%s)\n", __func__,
2612 ostate, ieee80211_state_name[ostate],
2613 nstate, ieee80211_state_name[nstate]);
2614 return (ENOSYS);
2615 }
2616
2617 if (error == EALREADY) {
2618 #ifdef LINUXKPI_DEBUG_80211
2619 if (linuxkpi_debug_80211 & D80211_TRACE)
2620 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2621 "%d (%s): iv_newstate already handled: %d.\n",
2622 __func__, ostate, ieee80211_state_name[ostate],
2623 nstate, ieee80211_state_name[nstate], error);
2624 #endif
2625 return (0);
2626 }
2627
2628 if (error != 0) {
2629 ic_printf(vap->iv_ic, "%s: error %d during state transition "
2630 "%d (%s) -> %d (%s)\n", __func__, error,
2631 ostate, ieee80211_state_name[ostate],
2632 nstate, ieee80211_state_name[nstate]);
2633 return (error);
2634 }
2635
2636 #ifdef LINUXKPI_DEBUG_80211
2637 if (linuxkpi_debug_80211 & D80211_TRACE)
2638 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2639 "calling net80211 parent\n",
2640 __func__, __LINE__, vap, nstate, arg);
2641 #endif
2642
2643 return (lvif->iv_newstate(vap, nstate, arg));
2644 }
2645
2646 /* -------------------------------------------------------------------------- */
2647
2648 /*
2649 * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2650 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2651 * new node without us knowing and thus our ni/lsta are out of sync.
2652 */
2653 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)2654 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2655 {
2656 struct lkpi_vif *lvif;
2657 struct ieee80211_node *rni;
2658
2659 IEEE80211_LOCK_ASSERT(vap->iv_ic);
2660
2661 lvif = VAP_TO_LVIF(vap);
2662
2663 LKPI_80211_LVIF_LOCK(lvif);
2664 lvif->lvif_bss_synched = false;
2665 LKPI_80211_LVIF_UNLOCK(lvif);
2666
2667 rni = lvif->iv_update_bss(vap, ni);
2668 return (rni);
2669 }
2670
2671 #ifdef LKPI_80211_WME
2672 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)2673 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
2674 {
2675 struct ieee80211com *ic;
2676 struct ieee80211_hw *hw;
2677 struct lkpi_vif *lvif;
2678 struct ieee80211_vif *vif;
2679 struct chanAccParams chp;
2680 struct wmeParams wmeparr[WME_NUM_AC];
2681 struct ieee80211_tx_queue_params txqp;
2682 enum ieee80211_bss_changed changed;
2683 int error;
2684 uint16_t ac;
2685
2686 IMPROVE();
2687 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2688 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2689
2690 if (vap == NULL)
2691 return (0);
2692
2693 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
2694 return (0);
2695
2696 if (lhw->ops->conf_tx == NULL)
2697 return (0);
2698
2699 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
2700 lhw->update_wme = true;
2701 return (0);
2702 }
2703 lhw->update_wme = false;
2704
2705 ic = lhw->ic;
2706 ieee80211_wme_ic_getparams(ic, &chp);
2707 IEEE80211_LOCK(ic);
2708 for (ac = 0; ac < WME_NUM_AC; ac++)
2709 wmeparr[ac] = chp.cap_wmeParams[ac];
2710 IEEE80211_UNLOCK(ic);
2711
2712 hw = LHW_TO_HW(lhw);
2713 lvif = VAP_TO_LVIF(vap);
2714 vif = LVIF_TO_VIF(lvif);
2715
2716 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2717 LKPI_80211_LHW_LOCK(lhw);
2718 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2719 struct wmeParams *wmep;
2720
2721 wmep = &wmeparr[ac];
2722 bzero(&txqp, sizeof(txqp));
2723 txqp.cw_min = wmep->wmep_logcwmin;
2724 txqp.cw_max = wmep->wmep_logcwmax;
2725 txqp.txop = wmep->wmep_txopLimit;
2726 txqp.aifs = wmep->wmep_aifsn;
2727 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2728 if (error != 0)
2729 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2730 __func__, ac, error);
2731 }
2732 LKPI_80211_LHW_UNLOCK(lhw);
2733 changed = BSS_CHANGED_QOS;
2734 if (!planned)
2735 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2736
2737 return (changed);
2738 }
2739 #endif
2740
2741 static int
lkpi_ic_wme_update(struct ieee80211com * ic)2742 lkpi_ic_wme_update(struct ieee80211com *ic)
2743 {
2744 #ifdef LKPI_80211_WME
2745 struct ieee80211vap *vap;
2746 struct lkpi_hw *lhw;
2747
2748 IMPROVE("Use the per-VAP callback in net80211.");
2749 vap = TAILQ_FIRST(&ic->ic_vaps);
2750 if (vap == NULL)
2751 return (0);
2752
2753 lhw = ic->ic_softc;
2754
2755 lkpi_wme_update(lhw, vap, false);
2756 #endif
2757 return (0); /* unused */
2758 }
2759
2760 /*
2761 * Change link-layer address on the vif (if the vap is not started/"UP").
2762 * This can happen if a user changes 'ether' using ifconfig.
2763 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
2764 * we do use a per-[l]vif event handler to be sure we exist as we
2765 * cannot assume that from every vap derives a vif and we have a hard
2766 * time checking based on net80211 information.
2767 */
2768 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)2769 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
2770 {
2771 struct epoch_tracker et;
2772 struct ieee80211_vif *vif;
2773
2774 NET_EPOCH_ENTER(et);
2775 /* NB: identify vap's by if_init; left as an extra check. */
2776 if (ifp->if_init != ieee80211_init || (ifp->if_flags & IFF_UP) != 0) {
2777 NET_EPOCH_EXIT(et);
2778 return;
2779 }
2780
2781 vif = arg;
2782 IEEE80211_ADDR_COPY(vif->bss_conf.addr, IF_LLADDR(ifp));
2783 NET_EPOCH_EXIT(et);
2784 }
2785
2786 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])2787 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2788 int unit, enum ieee80211_opmode opmode, int flags,
2789 const uint8_t bssid[IEEE80211_ADDR_LEN],
2790 const uint8_t mac[IEEE80211_ADDR_LEN])
2791 {
2792 struct lkpi_hw *lhw;
2793 struct ieee80211_hw *hw;
2794 struct lkpi_vif *lvif;
2795 struct ieee80211vap *vap;
2796 struct ieee80211_vif *vif;
2797 struct ieee80211_tx_queue_params txqp;
2798 enum ieee80211_bss_changed changed;
2799 size_t len;
2800 int error, i;
2801 uint16_t ac;
2802
2803 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
2804 return (NULL);
2805
2806 lhw = ic->ic_softc;
2807 hw = LHW_TO_HW(lhw);
2808
2809 len = sizeof(*lvif);
2810 len += hw->vif_data_size; /* vif->drv_priv */
2811
2812 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2813 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2814 TAILQ_INIT(&lvif->lsta_head);
2815 lvif->lvif_bss = NULL;
2816 lvif->lvif_bss_synched = false;
2817 vap = LVIF_TO_VAP(lvif);
2818
2819 vif = LVIF_TO_VIF(lvif);
2820 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2821 vif->p2p = false;
2822 vif->probe_req_reg = false;
2823 vif->type = lkpi_opmode_to_vif_type(opmode);
2824 lvif->wdev.iftype = vif->type;
2825 /* Need to fill in other fields as well. */
2826 IMPROVE();
2827
2828 /* XXX-BZ hardcoded for now! */
2829 #if 1
2830 vif->chanctx_conf = NULL;
2831 vif->bss_conf.vif = vif;
2832 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
2833 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
2834 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
2835 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
2836 vif->bss_conf.link_id = 0; /* Non-MLO operation. */
2837 vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2838 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2839 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */
2840 vif->bss_conf.qos = false;
2841 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */
2842 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2843 vif->cfg.aid = 0;
2844 vif->cfg.assoc = false;
2845 vif->cfg.idle = true;
2846 vif->cfg.ps = false;
2847 IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2848 /*
2849 * We need to initialize it to something as the bss_info_changed call
2850 * will try to copy from it in iwlwifi and NULL is a panic.
2851 * We will set the proper one in scan_to_auth() before being assoc.
2852 */
2853 vif->bss_conf.bssid = ieee80211broadcastaddr;
2854 #endif
2855 #if 0
2856 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2857 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2858 vif->bss_conf.beacon_int = ic->ic_bintval;
2859 /* iwlwifi bug. */
2860 if (vif->bss_conf.beacon_int < 16)
2861 vif->bss_conf.beacon_int = 16;
2862 #endif
2863
2864 /* Link Config */
2865 vif->link_conf[0] = &vif->bss_conf;
2866 for (i = 0; i < nitems(vif->link_conf); i++) {
2867 IMPROVE("more than 1 link one day");
2868 }
2869
2870 /* Setup queue defaults; driver may override in (*add_interface). */
2871 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2872 if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2873 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2874 else if (hw->queues >= IEEE80211_NUM_ACS)
2875 vif->hw_queue[i] = i;
2876 else
2877 vif->hw_queue[i] = 0;
2878
2879 /* Initialize the queue to running. Stopped? */
2880 lvif->hw_queue_stopped[i] = false;
2881 }
2882 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2883
2884 IMPROVE();
2885
2886 error = lkpi_80211_mo_start(hw);
2887 if (error != 0) {
2888 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
2889 mtx_destroy(&lvif->mtx);
2890 free(lvif, M_80211_VAP);
2891 return (NULL);
2892 }
2893
2894 error = lkpi_80211_mo_add_interface(hw, vif);
2895 if (error != 0) {
2896 IMPROVE(); /* XXX-BZ mo_stop()? */
2897 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
2898 mtx_destroy(&lvif->mtx);
2899 free(lvif, M_80211_VAP);
2900 return (NULL);
2901 }
2902
2903 LKPI_80211_LHW_LVIF_LOCK(lhw);
2904 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2905 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2906
2907 /* Set bss_info. */
2908 changed = 0;
2909 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2910
2911 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2912 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2913 LKPI_80211_LHW_LOCK(lhw);
2914 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2915
2916 bzero(&txqp, sizeof(txqp));
2917 txqp.cw_min = 15;
2918 txqp.cw_max = 1023;
2919 txqp.txop = 0;
2920 txqp.aifs = 2;
2921 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2922 if (error != 0)
2923 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2924 __func__, ac, error);
2925 }
2926 LKPI_80211_LHW_UNLOCK(lhw);
2927 changed = BSS_CHANGED_QOS;
2928 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2929
2930 /* Force MC init. */
2931 lkpi_update_mcast_filter(ic, true);
2932
2933 IMPROVE();
2934
2935 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2936
2937 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2938 lvif->iv_newstate = vap->iv_newstate;
2939 vap->iv_newstate = lkpi_iv_newstate;
2940 lvif->iv_update_bss = vap->iv_update_bss;
2941 vap->iv_update_bss = lkpi_iv_update_bss;
2942
2943 /* Key management. */
2944 if (lhw->ops->set_key != NULL) {
2945 #ifdef LKPI_80211_HW_CRYPTO
2946 vap->iv_key_set = lkpi_iv_key_set;
2947 vap->iv_key_delete = lkpi_iv_key_delete;
2948 #endif
2949 }
2950
2951 #ifdef LKPI_80211_HT
2952 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
2953 #endif
2954
2955 ieee80211_ratectl_init(vap);
2956
2957 /* Complete setup. */
2958 ieee80211_vap_attach(vap, ieee80211_media_change,
2959 ieee80211_media_status, mac);
2960
2961 if (hw->max_listen_interval == 0)
2962 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2963 hw->conf.listen_interval = hw->max_listen_interval;
2964 ic->ic_set_channel(ic);
2965
2966 /* XXX-BZ do we need to be able to update these? */
2967 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
2968 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2969 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
2970 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2971 /* any others? */
2972 IMPROVE();
2973
2974 return (vap);
2975 }
2976
2977 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)2978 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
2979 {
2980
2981 wiphy_unregister(hw->wiphy);
2982 linuxkpi_ieee80211_ifdetach(hw);
2983
2984 IMPROVE();
2985 }
2986
2987 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)2988 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
2989 {
2990
2991 TODO();
2992 }
2993
2994 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)2995 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2996 {
2997 struct ieee80211com *ic;
2998 struct lkpi_hw *lhw;
2999 struct ieee80211_hw *hw;
3000 struct lkpi_vif *lvif;
3001 struct ieee80211_vif *vif;
3002
3003 lvif = VAP_TO_LVIF(vap);
3004 vif = LVIF_TO_VIF(lvif);
3005 ic = vap->iv_ic;
3006 lhw = ic->ic_softc;
3007 hw = LHW_TO_HW(lhw);
3008
3009 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
3010
3011 LKPI_80211_LHW_LVIF_LOCK(lhw);
3012 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
3013 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3014
3015 ieee80211_ratectl_deinit(vap);
3016 ieee80211_vap_detach(vap);
3017
3018 IMPROVE("clear up other bits in this state");
3019
3020 lkpi_80211_mo_remove_interface(hw, vif);
3021
3022 /* Single VAP, so we can do this here. */
3023 lkpi_80211_mo_stop(hw);
3024
3025 mtx_destroy(&lvif->mtx);
3026 free(lvif, M_80211_VAP);
3027 }
3028
3029 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)3030 lkpi_ic_update_mcast(struct ieee80211com *ic)
3031 {
3032
3033 lkpi_update_mcast_filter(ic, false);
3034 TRACEOK();
3035 }
3036
3037 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)3038 lkpi_ic_update_promisc(struct ieee80211com *ic)
3039 {
3040
3041 UNIMPLEMENTED;
3042 }
3043
3044 static void
lkpi_ic_update_chw(struct ieee80211com * ic)3045 lkpi_ic_update_chw(struct ieee80211com *ic)
3046 {
3047
3048 UNIMPLEMENTED;
3049 }
3050
3051 /* Start / stop device. */
3052 static void
lkpi_ic_parent(struct ieee80211com * ic)3053 lkpi_ic_parent(struct ieee80211com *ic)
3054 {
3055 struct lkpi_hw *lhw;
3056 #ifdef HW_START_STOP
3057 struct ieee80211_hw *hw;
3058 int error;
3059 #endif
3060 bool start_all;
3061
3062 IMPROVE();
3063
3064 lhw = ic->ic_softc;
3065 #ifdef HW_START_STOP
3066 hw = LHW_TO_HW(lhw);
3067 #endif
3068 start_all = false;
3069
3070 /* IEEE80211_UNLOCK(ic); */
3071 LKPI_80211_LHW_LOCK(lhw);
3072 if (ic->ic_nrunning > 0) {
3073 #ifdef HW_START_STOP
3074 error = lkpi_80211_mo_start(hw);
3075 if (error == 0)
3076 #endif
3077 start_all = true;
3078 } else {
3079 #ifdef HW_START_STOP
3080 lkpi_80211_mo_stop(hw);
3081 #endif
3082 }
3083 LKPI_80211_LHW_UNLOCK(lhw);
3084 /* IEEE80211_LOCK(ic); */
3085
3086 if (start_all)
3087 ieee80211_start_all(ic);
3088 }
3089
3090 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)3091 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
3092 size_t ie_ids_len)
3093 {
3094 int i;
3095
3096 for (i = 0; i < ie_ids_len; i++) {
3097 if (ie == *ie_ids)
3098 return (true);
3099 }
3100
3101 return (false);
3102 }
3103
3104 /* Return true if skipped; false if error. */
3105 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)3106 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
3107 {
3108 size_t x;
3109 uint8_t l;
3110
3111 x = *xp;
3112
3113 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
3114 __func__, x, ies_len, ies));
3115 l = ies[x + 1];
3116 x += 2 + l;
3117
3118 if (x > ies_len)
3119 return (false);
3120
3121 *xp = x;
3122 return (true);
3123 }
3124
3125 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)3126 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3127 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
3128 {
3129 struct ieee80211_supported_band *supband;
3130 struct linuxkpi_ieee80211_channel *channels;
3131 struct ieee80211com *ic;
3132 const struct ieee80211_channel *chan;
3133 const struct ieee80211_rateset *rs;
3134 uint8_t *pb;
3135 int band, i;
3136
3137 ic = vap->iv_ic;
3138 for (band = 0; band < NUM_NL80211_BANDS; band++) {
3139 if ((band_mask & (1 << band)) == 0)
3140 continue;
3141
3142 supband = hw->wiphy->bands[band];
3143 /*
3144 * This should not happen;
3145 * band_mask is a bitmask of valid bands to scan on.
3146 */
3147 if (supband == NULL || supband->n_channels == 0)
3148 continue;
3149
3150 /* Find a first channel to get the mode and rates from. */
3151 channels = supband->channels;
3152 chan = NULL;
3153 for (i = 0; i < supband->n_channels; i++) {
3154
3155 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3156 continue;
3157
3158 chan = ieee80211_find_channel(ic,
3159 channels[i].center_freq, 0);
3160 if (chan != NULL)
3161 break;
3162 }
3163
3164 /* This really should not happen. */
3165 if (chan == NULL)
3166 continue;
3167
3168 pb = p;
3169 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */
3170 p = ieee80211_add_rates(p, rs);
3171 p = ieee80211_add_xrates(p, rs);
3172
3173 #if defined(LKPI_80211_HT)
3174 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
3175 struct ieee80211_channel *c;
3176
3177 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
3178 vap->iv_flags_ht);
3179 p = ieee80211_add_htcap_ch(p, vap, c);
3180 }
3181 #endif
3182 #if defined(LKPI_80211_VHT)
3183 if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
3184 struct ieee80211_channel *c;
3185
3186 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
3187 vap->iv_flags_ht);
3188 c = ieee80211_vht_adjust_channel(ic, c,
3189 vap->iv_vht_flags);
3190 p = ieee80211_add_vhtcap_ch(p, vap, c);
3191 }
3192 #endif
3193
3194 scan_ies->ies[band] = pb;
3195 scan_ies->len[band] = p - pb;
3196 }
3197
3198 /* Add common_ies */
3199 pb = p;
3200 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3201 vap->iv_wpa_ie != NULL) {
3202 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3203 p += 2 + vap->iv_wpa_ie[1];
3204 }
3205 if (vap->iv_appie_probereq != NULL) {
3206 memcpy(p, vap->iv_appie_probereq->ie_data,
3207 vap->iv_appie_probereq->ie_len);
3208 p += vap->iv_appie_probereq->ie_len;
3209 }
3210 scan_ies->common_ies = pb;
3211 scan_ies->common_ie_len = p - pb;
3212
3213 return (p);
3214 }
3215
3216 static void
lkpi_ic_scan_start(struct ieee80211com * ic)3217 lkpi_ic_scan_start(struct ieee80211com *ic)
3218 {
3219 struct lkpi_hw *lhw;
3220 struct ieee80211_hw *hw;
3221 struct lkpi_vif *lvif;
3222 struct ieee80211_vif *vif;
3223 struct ieee80211_scan_state *ss;
3224 struct ieee80211vap *vap;
3225 int error;
3226 bool is_hw_scan;
3227
3228 lhw = ic->ic_softc;
3229 LKPI_80211_LHW_SCAN_LOCK(lhw);
3230 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
3231 /* A scan is still running. */
3232 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3233 return;
3234 }
3235 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3236 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3237
3238 ss = ic->ic_scan;
3239 vap = ss->ss_vap;
3240 if (vap->iv_state != IEEE80211_S_SCAN) {
3241 IMPROVE("We need to be able to scan if not in S_SCAN");
3242 return;
3243 }
3244
3245 hw = LHW_TO_HW(lhw);
3246 if (!is_hw_scan) {
3247 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3248 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3249 sw_scan:
3250 lvif = VAP_TO_LVIF(vap);
3251 vif = LVIF_TO_VIF(lvif);
3252
3253 if (vap->iv_state == IEEE80211_S_SCAN)
3254 lkpi_hw_conf_idle(hw, false);
3255
3256 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
3257 /* net80211::scan_start() handled PS for us. */
3258 IMPROVE();
3259 /* XXX Also means it is too late to flush queues?
3260 * need to check iv_sta_ps or overload? */
3261 /* XXX want to adjust ss end time/ maxdwell? */
3262
3263 } else {
3264 struct ieee80211_channel *c;
3265 struct ieee80211_scan_request *hw_req;
3266 struct linuxkpi_ieee80211_channel *lc, **cpp;
3267 struct cfg80211_ssid *ssids;
3268 struct cfg80211_scan_6ghz_params *s6gp;
3269 size_t chan_len, nchan, ssids_len, s6ghzlen;
3270 int band, i, ssid_count, common_ie_len;
3271 uint32_t band_mask;
3272 uint8_t *ie, *ieend;
3273 bool running;
3274
3275 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3276 ssids_len = ssid_count * sizeof(*ssids);
3277 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */
3278
3279 band_mask = 0;
3280 nchan = 0;
3281 for (i = ss->ss_next; i < ss->ss_last; i++) {
3282 nchan++;
3283 band = lkpi_net80211_chan_to_nl80211_band(
3284 ss->ss_chans[ss->ss_next + i]);
3285 band_mask |= (1 << band);
3286 }
3287
3288 if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3289 IMPROVE("individual band scans not yet supported, only scanning first band");
3290 /* In theory net80211 should drive this. */
3291 /* Probably we need to add local logic for now;
3292 * need to deal with scan_complete
3293 * and cancel_scan and keep local state.
3294 * Also cut the nchan down above.
3295 */
3296 /* XXX-BZ ath10k does not set this but still does it? &$%^ */
3297 }
3298
3299 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
3300
3301 common_ie_len = 0;
3302 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3303 vap->iv_wpa_ie != NULL)
3304 common_ie_len += vap->iv_wpa_ie[1];
3305 if (vap->iv_appie_probereq != NULL)
3306 common_ie_len += vap->iv_appie_probereq->ie_len;
3307
3308 /* We would love to check this at an earlier stage... */
3309 if (common_ie_len > hw->wiphy->max_scan_ie_len) {
3310 ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3311 "wiphy->max_scan_ie_len %d\n", __func__,
3312 common_ie_len, hw->wiphy->max_scan_ie_len);
3313 }
3314
3315 hw_req = malloc(sizeof(*hw_req) + ssids_len +
3316 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3317 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
3318
3319 hw_req->req.flags = 0; /* XXX ??? */
3320 /* hw_req->req.wdev */
3321 hw_req->req.wiphy = hw->wiphy;
3322 hw_req->req.no_cck = false; /* XXX */
3323 #if 0
3324 /* This seems to pessimise default scanning behaviour. */
3325 hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
3326 hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
3327 #endif
3328 #ifdef __notyet__
3329 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
3330 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
3331 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
3332 #endif
3333 eth_broadcast_addr(hw_req->req.bssid);
3334
3335 hw_req->req.n_channels = nchan;
3336 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
3337 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
3338 for (i = 0; i < nchan; i++) {
3339 *(cpp + i) =
3340 (struct linuxkpi_ieee80211_channel *)(lc + i);
3341 }
3342 for (i = 0; i < nchan; i++) {
3343 c = ss->ss_chans[ss->ss_next + i];
3344
3345 lc->hw_value = c->ic_ieee;
3346 lc->center_freq = c->ic_freq; /* XXX */
3347 /* lc->flags */
3348 lc->band = lkpi_net80211_chan_to_nl80211_band(c);
3349 lc->max_power = c->ic_maxpower;
3350 /* lc-> ... */
3351 lc++;
3352 }
3353
3354 hw_req->req.n_ssids = ssid_count;
3355 if (hw_req->req.n_ssids > 0) {
3356 ssids = (struct cfg80211_ssid *)lc;
3357 hw_req->req.ssids = ssids;
3358 for (i = 0; i < ssid_count; i++) {
3359 ssids->ssid_len = ss->ss_ssid[i].len;
3360 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
3361 ss->ss_ssid[i].len);
3362 ssids++;
3363 }
3364 s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
3365 } else {
3366 s6gp = (struct cfg80211_scan_6ghz_params *)lc;
3367 }
3368
3369 /* 6GHz one day. */
3370 hw_req->req.n_6ghz_params = 0;
3371 hw_req->req.scan_6ghz_params = NULL;
3372 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */
3373 /* s6gp->... */
3374
3375 ie = ieend = (uint8_t *)s6gp;
3376 /* Copy per-band IEs, copy common IEs */
3377 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3378 hw_req->req.ie = ie;
3379 hw_req->req.ie_len = ieend - ie;
3380
3381 lvif = VAP_TO_LVIF(vap);
3382 vif = LVIF_TO_VIF(lvif);
3383
3384 LKPI_80211_LHW_SCAN_LOCK(lhw);
3385 /* Re-check under lock. */
3386 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
3387 if (!running) {
3388 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
3389 "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
3390
3391 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
3392 lhw->hw_req = hw_req;
3393 }
3394 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3395 if (running) {
3396 free(hw_req, M_LKPI80211);
3397 return;
3398 }
3399
3400 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
3401 if (error != 0) {
3402 ieee80211_cancel_scan(vap);
3403
3404 /*
3405 * ieee80211_scan_completed must be called in either
3406 * case of error or none. So let the free happen there
3407 * and only there.
3408 * That would be fine in theory but in practice drivers
3409 * behave differently:
3410 * ath10k does not return hw_scan until after scan_complete
3411 * and can then still return an error.
3412 * rtw88 can return 1 or -EBUSY without scan_complete
3413 * iwlwifi can return various errors before scan starts
3414 * ...
3415 * So we cannot rely on that behaviour and have to check
3416 * and balance between both code paths.
3417 */
3418 LKPI_80211_LHW_SCAN_LOCK(lhw);
3419 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
3420 free(lhw->hw_req, M_LKPI80211);
3421 lhw->hw_req = NULL;
3422 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
3423 }
3424 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3425
3426 /*
3427 * XXX-SIGH magic number.
3428 * rtw88 has a magic "return 1" if offloading scan is
3429 * not possible. Fall back to sw scan in that case.
3430 */
3431 if (error == 1) {
3432 LKPI_80211_LHW_SCAN_LOCK(lhw);
3433 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
3434 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3435 /*
3436 * XXX If we clear this now and later a driver
3437 * thinks it * can do a hw_scan again, we will
3438 * currently not re-enable it?
3439 */
3440 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3441 ieee80211_start_scan(vap,
3442 IEEE80211_SCAN_ACTIVE |
3443 IEEE80211_SCAN_NOPICK |
3444 IEEE80211_SCAN_ONCE,
3445 IEEE80211_SCAN_FOREVER,
3446 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3447 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3448 vap->iv_des_nssid, vap->iv_des_ssid);
3449 goto sw_scan;
3450 }
3451
3452 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3453 __func__, error);
3454 }
3455 }
3456 }
3457
3458 static void
lkpi_ic_scan_end(struct ieee80211com * ic)3459 lkpi_ic_scan_end(struct ieee80211com *ic)
3460 {
3461 struct lkpi_hw *lhw;
3462 bool is_hw_scan;
3463
3464 lhw = ic->ic_softc;
3465 LKPI_80211_LHW_SCAN_LOCK(lhw);
3466 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
3467 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3468 return;
3469 }
3470 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3471 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3472
3473 if (!is_hw_scan) {
3474 struct ieee80211_scan_state *ss;
3475 struct ieee80211vap *vap;
3476 struct ieee80211_hw *hw;
3477 struct lkpi_vif *lvif;
3478 struct ieee80211_vif *vif;
3479
3480 ss = ic->ic_scan;
3481 vap = ss->ss_vap;
3482 hw = LHW_TO_HW(lhw);
3483 lvif = VAP_TO_LVIF(vap);
3484 vif = LVIF_TO_VIF(lvif);
3485
3486 lkpi_80211_mo_sw_scan_complete(hw, vif);
3487
3488 /* Send PS to stop buffering if n80211 does not for us? */
3489
3490 if (vap->iv_state == IEEE80211_S_SCAN)
3491 lkpi_hw_conf_idle(hw, true);
3492 }
3493 }
3494
3495 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)3496 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3497 unsigned long maxdwell)
3498 {
3499 struct lkpi_hw *lhw;
3500 bool is_hw_scan;
3501
3502 lhw = ss->ss_ic->ic_softc;
3503 LKPI_80211_LHW_SCAN_LOCK(lhw);
3504 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3505 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3506 if (!is_hw_scan)
3507 lhw->ic_scan_curchan(ss, maxdwell);
3508 }
3509
3510 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)3511 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3512 {
3513 struct lkpi_hw *lhw;
3514 bool is_hw_scan;
3515
3516 lhw = ss->ss_ic->ic_softc;
3517 LKPI_80211_LHW_SCAN_LOCK(lhw);
3518 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3519 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3520 if (!is_hw_scan)
3521 lhw->ic_scan_mindwell(ss);
3522 }
3523
3524 static void
lkpi_ic_set_channel(struct ieee80211com * ic)3525 lkpi_ic_set_channel(struct ieee80211com *ic)
3526 {
3527 struct lkpi_hw *lhw;
3528 struct ieee80211_hw *hw;
3529 struct ieee80211_channel *c;
3530 struct linuxkpi_ieee80211_channel *chan;
3531 int error;
3532 bool hw_scan_running;
3533
3534 lhw = ic->ic_softc;
3535
3536 /* If we do not support (*config)() save us the work. */
3537 if (lhw->ops->config == NULL)
3538 return;
3539
3540 /* If we have a hw_scan running do not switch channels. */
3541 LKPI_80211_LHW_SCAN_LOCK(lhw);
3542 hw_scan_running =
3543 (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
3544 (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
3545 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3546 if (hw_scan_running)
3547 return;
3548
3549 c = ic->ic_curchan;
3550 if (c == NULL || c == IEEE80211_CHAN_ANYC) {
3551 ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
3552 c, lhw->ops->config);
3553 return;
3554 }
3555
3556 chan = lkpi_find_lkpi80211_chan(lhw, c);
3557 if (chan == NULL) {
3558 ic_printf(ic, "%s: c %p chan %p\n", __func__,
3559 c, chan);
3560 return;
3561 }
3562
3563 /* XXX max power for scanning? */
3564 IMPROVE();
3565
3566 hw = LHW_TO_HW(lhw);
3567 cfg80211_chandef_create(&hw->conf.chandef, chan,
3568 #ifdef LKPI_80211_HT
3569 (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
3570 #endif
3571 NL80211_CHAN_NO_HT);
3572
3573 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
3574 if (error != 0 && error != EOPNOTSUPP) {
3575 ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
3576 __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
3577 /* XXX should we unroll to the previous chandef? */
3578 IMPROVE();
3579 } else {
3580 /* Update radiotap channels as well. */
3581 lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
3582 lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
3583 lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
3584 lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
3585 }
3586
3587 /* Currently PS is hard coded off! Not sure it belongs here. */
3588 IMPROVE();
3589 if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
3590 (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
3591 hw->conf.flags &= ~IEEE80211_CONF_PS;
3592 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
3593 if (error != 0 && error != EOPNOTSUPP)
3594 ic_printf(ic, "ERROR: %s: config %#0x returned "
3595 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
3596 error);
3597 }
3598 }
3599
3600 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])3601 lkpi_ic_node_alloc(struct ieee80211vap *vap,
3602 const uint8_t mac[IEEE80211_ADDR_LEN])
3603 {
3604 struct ieee80211com *ic;
3605 struct lkpi_hw *lhw;
3606 struct ieee80211_node *ni;
3607 struct ieee80211_hw *hw;
3608 struct lkpi_sta *lsta;
3609
3610 ic = vap->iv_ic;
3611 lhw = ic->ic_softc;
3612
3613 /* We keep allocations de-coupled so we can deal with the two worlds. */
3614 if (lhw->ic_node_alloc == NULL)
3615 return (NULL);
3616
3617 ni = lhw->ic_node_alloc(vap, mac);
3618 if (ni == NULL)
3619 return (NULL);
3620
3621 hw = LHW_TO_HW(lhw);
3622 lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
3623 if (lsta == NULL) {
3624 if (lhw->ic_node_free != NULL)
3625 lhw->ic_node_free(ni);
3626 return (NULL);
3627 }
3628
3629 return (ni);
3630 }
3631
3632 static int
lkpi_ic_node_init(struct ieee80211_node * ni)3633 lkpi_ic_node_init(struct ieee80211_node *ni)
3634 {
3635 struct ieee80211com *ic;
3636 struct lkpi_hw *lhw;
3637 int error;
3638
3639 ic = ni->ni_ic;
3640 lhw = ic->ic_softc;
3641
3642 if (lhw->ic_node_init != NULL) {
3643 error = lhw->ic_node_init(ni);
3644 if (error != 0)
3645 return (error);
3646 }
3647
3648 /* XXX-BZ Sync other state over. */
3649 IMPROVE();
3650
3651 return (0);
3652 }
3653
3654 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)3655 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
3656 {
3657 struct ieee80211com *ic;
3658 struct lkpi_hw *lhw;
3659
3660 ic = ni->ni_ic;
3661 lhw = ic->ic_softc;
3662
3663 /* XXX-BZ remove from driver, ... */
3664 IMPROVE();
3665
3666 if (lhw->ic_node_cleanup != NULL)
3667 lhw->ic_node_cleanup(ni);
3668 }
3669
3670 static void
lkpi_ic_node_free(struct ieee80211_node * ni)3671 lkpi_ic_node_free(struct ieee80211_node *ni)
3672 {
3673 struct ieee80211com *ic;
3674 struct lkpi_hw *lhw;
3675 struct lkpi_sta *lsta;
3676
3677 ic = ni->ni_ic;
3678 lhw = ic->ic_softc;
3679 lsta = ni->ni_drv_data;
3680
3681 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
3682
3683 /*
3684 * Pass in the original ni just in case of error we could check that
3685 * it is the same as lsta->ni.
3686 */
3687 lkpi_lsta_free(lsta, ni);
3688
3689 if (lhw->ic_node_free != NULL)
3690 lhw->ic_node_free(ni);
3691 }
3692
3693 static int
lkpi_ic_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused)3694 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3695 const struct ieee80211_bpf_params *params __unused)
3696 {
3697 struct lkpi_sta *lsta;
3698
3699 lsta = ni->ni_drv_data;
3700 LKPI_80211_LSTA_TXQ_LOCK(lsta);
3701 if (!lsta->added_to_drv || !lsta->txq_ready) {
3702 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3703 /*
3704 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3705 * ieee80211_raw_output() does that in case of error).
3706 */
3707 m_free(m);
3708 return (ENETDOWN);
3709 }
3710
3711 /* Queue the packet and enqueue the task to handle it. */
3712 mbufq_enqueue(&lsta->txq, m);
3713 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3714 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3715
3716 #ifdef LINUXKPI_DEBUG_80211
3717 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3718 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
3719 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
3720 mbufq_len(&lsta->txq));
3721 #endif
3722
3723 return (0);
3724 }
3725
3726 static void
lkpi_80211_txq_tx_one(struct lkpi_sta * lsta,struct mbuf * m)3727 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
3728 {
3729 struct ieee80211_node *ni;
3730 #ifndef LKPI_80211_HW_CRYPTO
3731 struct ieee80211_frame *wh;
3732 #endif
3733 struct ieee80211_key *k;
3734 struct sk_buff *skb;
3735 struct ieee80211com *ic;
3736 struct lkpi_hw *lhw;
3737 struct ieee80211_hw *hw;
3738 struct lkpi_vif *lvif;
3739 struct ieee80211_vif *vif;
3740 struct ieee80211_channel *c;
3741 struct ieee80211_tx_control control;
3742 struct ieee80211_tx_info *info;
3743 struct ieee80211_sta *sta;
3744 struct ieee80211_hdr *hdr;
3745 struct lkpi_txq *ltxq;
3746 void *buf;
3747 uint8_t ac, tid;
3748
3749 M_ASSERTPKTHDR(m);
3750 #ifdef LINUXKPI_DEBUG_80211
3751 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
3752 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
3753 #endif
3754
3755 ni = lsta->ni;
3756 k = NULL;
3757 #ifndef LKPI_80211_HW_CRYPTO
3758 /* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
3759 wh = mtod(m, struct ieee80211_frame *);
3760 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
3761 /* Retrieve key for TX && do software encryption. */
3762 k = ieee80211_crypto_encap(ni, m);
3763 if (k == NULL) {
3764 ieee80211_free_node(ni);
3765 m_freem(m);
3766 return;
3767 }
3768 }
3769 #endif
3770
3771 ic = ni->ni_ic;
3772 lhw = ic->ic_softc;
3773 hw = LHW_TO_HW(lhw);
3774 c = ni->ni_chan;
3775
3776 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
3777 struct lkpi_radiotap_tx_hdr *rtap;
3778
3779 rtap = &lhw->rtap_tx;
3780 rtap->wt_flags = 0;
3781 if (k != NULL)
3782 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3783 if (m->m_flags & M_FRAG)
3784 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
3785 IMPROVE();
3786 rtap->wt_rate = 0;
3787 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3788 rtap->wt_chan_freq = htole16(c->ic_freq);
3789 rtap->wt_chan_flags = htole16(c->ic_flags);
3790 }
3791
3792 ieee80211_radiotap_tx(ni->ni_vap, m);
3793 }
3794
3795 /*
3796 * net80211 should handle hw->extra_tx_headroom.
3797 * Though for as long as we are copying we don't mind.
3798 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
3799 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
3800 */
3801 skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
3802 if (skb == NULL) {
3803 ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
3804 ieee80211_free_node(ni);
3805 m_freem(m);
3806 return;
3807 }
3808 skb_reserve(skb, hw->extra_tx_headroom);
3809
3810 /* XXX-BZ we need a SKB version understanding mbuf. */
3811 /* Save the mbuf for ieee80211_tx_complete(). */
3812 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3813 skb->m = m;
3814 #if 0
3815 skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3816 #else
3817 buf = skb_put(skb, m->m_pkthdr.len);
3818 m_copydata(m, 0, m->m_pkthdr.len, buf);
3819 #endif
3820 /* Save the ni. */
3821 m->m_pkthdr.PH_loc.ptr = ni;
3822
3823 lvif = VAP_TO_LVIF(ni->ni_vap);
3824 vif = LVIF_TO_VIF(lvif);
3825
3826 hdr = (void *)skb->data;
3827 tid = linuxkpi_ieee80211_get_tid(hdr, true);
3828 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3829 if (!ieee80211_is_data(hdr->frame_control)) {
3830 /* MGMT and CTRL frames go on TID 7/VO. */
3831 skb->priority = 7;
3832 ac = IEEE80211_AC_VO;
3833 } else {
3834 /* Other non-QOS traffic goes to BE. */
3835 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */
3836 skb->priority = 0;
3837 ac = IEEE80211_AC_BE;
3838 }
3839 } else {
3840 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3841 ac = ieee80211e_up_to_ac[tid & 7];
3842 }
3843 skb_set_queue_mapping(skb, ac);
3844
3845 info = IEEE80211_SKB_CB(skb);
3846 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3847 /* Slight delay; probably only happens on scanning so fine? */
3848 if (c == NULL || c == IEEE80211_CHAN_ANYC)
3849 c = ic->ic_curchan;
3850 info->band = lkpi_net80211_chan_to_nl80211_band(c);
3851 info->hw_queue = vif->hw_queue[ac];
3852 if (m->m_flags & M_EAPOL)
3853 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3854 info->control.vif = vif;
3855 /* XXX-BZ info->control.rates */
3856 #ifdef __notyet__
3857 #ifdef LKPI_80211_HT
3858 info->control.rts_cts_rate_idx=
3859 info->control.use_rts= /* RTS */
3860 info->control.use_cts_prot= /* RTS/CTS*/
3861 #endif
3862 #endif
3863
3864 sta = LSTA_TO_STA(lsta);
3865 #ifdef LKPI_80211_HW_CRYPTO
3866 info->control.hw_key = lsta->kc;
3867 #endif
3868
3869 IMPROVE();
3870
3871 ltxq = NULL;
3872 if (!ieee80211_is_data_present(hdr->frame_control)) {
3873 if (vif->type == NL80211_IFTYPE_STATION &&
3874 lsta->added_to_drv &&
3875 sta->txq[IEEE80211_NUM_TIDS] != NULL)
3876 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3877 } else if (lsta->added_to_drv &&
3878 sta->txq[skb->priority] != NULL) {
3879 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3880 }
3881 if (ltxq == NULL)
3882 goto ops_tx;
3883
3884 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3885 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3886
3887 LKPI_80211_LTXQ_LOCK(ltxq);
3888 skb_queue_tail(<xq->skbq, skb);
3889 #ifdef LINUXKPI_DEBUG_80211
3890 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3891 printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3892 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3893 "WAKE_TX_Q ac %d prio %u qmap %u\n",
3894 __func__, __LINE__,
3895 curthread->td_tid, (unsigned int)ticks,
3896 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3897 skb_queue_len(<xq->skbq), ltxq->txq.ac,
3898 ltxq->txq.tid, ac, skb->priority, skb->qmap);
3899 #endif
3900 LKPI_80211_LTXQ_UNLOCK(ltxq);
3901 LKPI_80211_LHW_LOCK(lhw);
3902 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq);
3903 LKPI_80211_LHW_UNLOCK(lhw);
3904 return;
3905
3906 ops_tx:
3907 #ifdef LINUXKPI_DEBUG_80211
3908 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3909 printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3910 "TX ac %d prio %u qmap %u\n",
3911 __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3912 skb, ac, skb->priority, skb->qmap);
3913 #endif
3914 memset(&control, 0, sizeof(control));
3915 control.sta = sta;
3916 LKPI_80211_LHW_LOCK(lhw);
3917 lkpi_80211_mo_tx(hw, &control, skb);
3918 LKPI_80211_LHW_UNLOCK(lhw);
3919 }
3920
3921 static void
lkpi_80211_txq_task(void * ctx,int pending)3922 lkpi_80211_txq_task(void *ctx, int pending)
3923 {
3924 struct lkpi_sta *lsta;
3925 struct mbufq mq;
3926 struct mbuf *m;
3927 bool shall_tx;
3928
3929 lsta = ctx;
3930
3931 #ifdef LINUXKPI_DEBUG_80211
3932 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3933 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3934 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3935 pending, mbufq_len(&lsta->txq));
3936 #endif
3937
3938 mbufq_init(&mq, IFQ_MAXLEN);
3939
3940 LKPI_80211_LSTA_TXQ_LOCK(lsta);
3941 /*
3942 * Do not re-check lsta->txq_ready here; we may have a pending
3943 * disassoc/deauth frame still. On the contrary if txq_ready is
3944 * false we do not have a valid sta anymore in the firmware so no
3945 * point to try to TX.
3946 * We also use txq_ready as a semaphore and will drain the txq manually
3947 * if needed on our way towards SCAN/INIT in the state machine.
3948 */
3949 shall_tx = lsta->added_to_drv && lsta->txq_ready;
3950 if (__predict_true(shall_tx))
3951 mbufq_concat(&mq, &lsta->txq);
3952 /*
3953 * else a state change will push the packets out manually or
3954 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
3955 */
3956 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3957
3958 m = mbufq_dequeue(&mq);
3959 while (m != NULL) {
3960 lkpi_80211_txq_tx_one(lsta, m);
3961 m = mbufq_dequeue(&mq);
3962 }
3963 }
3964
3965 static int
lkpi_ic_transmit(struct ieee80211com * ic,struct mbuf * m)3966 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3967 {
3968
3969 /* XXX TODO */
3970 IMPROVE();
3971
3972 /* Quick and dirty cheating hack. */
3973 struct ieee80211_node *ni;
3974
3975 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3976 return (lkpi_ic_raw_xmit(ni, m, NULL));
3977 }
3978
3979 #ifdef LKPI_80211_HT
3980 static int
lkpi_ic_recv_action(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)3981 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
3982 const uint8_t *frm, const uint8_t *efrm)
3983 {
3984 struct ieee80211com *ic;
3985 struct lkpi_hw *lhw;
3986
3987 ic = ni->ni_ic;
3988 lhw = ic->ic_softc;
3989
3990 IMPROVE_HT();
3991
3992 return (lhw->ic_recv_action(ni, wh, frm, efrm));
3993 }
3994
3995 static int
lkpi_ic_send_action(struct ieee80211_node * ni,int category,int action,void * sa)3996 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
3997 {
3998 struct ieee80211com *ic;
3999 struct lkpi_hw *lhw;
4000
4001 ic = ni->ni_ic;
4002 lhw = ic->ic_softc;
4003
4004 IMPROVE_HT();
4005
4006 return (lhw->ic_send_action(ni, category, action, sa));
4007 }
4008
4009
4010 static int
lkpi_ic_ampdu_enable(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4011 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4012 {
4013 struct ieee80211com *ic;
4014 struct lkpi_hw *lhw;
4015
4016 ic = ni->ni_ic;
4017 lhw = ic->ic_softc;
4018
4019 IMPROVE_HT();
4020
4021 return (lhw->ic_ampdu_enable(ni, tap));
4022 }
4023
4024 static int
lkpi_ic_addba_request(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int dialogtoken,int baparamset,int batimeout)4025 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4026 int dialogtoken, int baparamset, int batimeout)
4027 {
4028 struct ieee80211com *ic;
4029 struct lkpi_hw *lhw;
4030
4031 ic = ni->ni_ic;
4032 lhw = ic->ic_softc;
4033
4034 IMPROVE_HT();
4035
4036 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
4037 }
4038
4039 static int
lkpi_ic_addba_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status,int baparamset,int batimeout)4040 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4041 int status, int baparamset, int batimeout)
4042 {
4043 struct ieee80211com *ic;
4044 struct lkpi_hw *lhw;
4045
4046 ic = ni->ni_ic;
4047 lhw = ic->ic_softc;
4048
4049 IMPROVE_HT();
4050
4051 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
4052 }
4053
4054 static void
lkpi_ic_addba_stop(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4055 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4056 {
4057 struct ieee80211com *ic;
4058 struct lkpi_hw *lhw;
4059
4060 ic = ni->ni_ic;
4061 lhw = ic->ic_softc;
4062
4063 IMPROVE_HT();
4064
4065 lhw->ic_addba_stop(ni, tap);
4066 }
4067
4068 static void
lkpi_ic_addba_response_timeout(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4069 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4070 {
4071 struct ieee80211com *ic;
4072 struct lkpi_hw *lhw;
4073
4074 ic = ni->ni_ic;
4075 lhw = ic->ic_softc;
4076
4077 IMPROVE_HT();
4078
4079 lhw->ic_addba_response_timeout(ni, tap);
4080 }
4081
4082 static void
lkpi_ic_bar_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status)4083 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4084 int status)
4085 {
4086 struct ieee80211com *ic;
4087 struct lkpi_hw *lhw;
4088
4089 ic = ni->ni_ic;
4090 lhw = ic->ic_softc;
4091
4092 IMPROVE_HT();
4093
4094 lhw->ic_bar_response(ni, tap, status);
4095 }
4096
4097 static int
lkpi_ic_ampdu_rx_start(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap,int baparamset,int batimeout,int baseqctl)4098 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
4099 int baparamset, int batimeout, int baseqctl)
4100 {
4101 struct ieee80211com *ic;
4102 struct lkpi_hw *lhw;
4103 struct ieee80211_hw *hw;
4104 struct ieee80211vap *vap;
4105 struct lkpi_vif *lvif;
4106 struct ieee80211_vif *vif;
4107 struct lkpi_sta *lsta;
4108 struct ieee80211_sta *sta;
4109 struct ieee80211_ampdu_params params;
4110 int error;
4111
4112 ic = ni->ni_ic;
4113 lhw = ic->ic_softc;
4114 hw = LHW_TO_HW(lhw);
4115 vap = ni->ni_vap;
4116 lvif = VAP_TO_LVIF(vap);
4117 vif = LVIF_TO_VIF(lvif);
4118 lsta = ni->ni_drv_data;
4119 sta = LSTA_TO_STA(lsta);
4120
4121 params.sta = sta;
4122 params.action = IEEE80211_AMPDU_RX_START;
4123 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
4124 if (params.buf_size == 0)
4125 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
4126 else
4127 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4128 if (params.buf_size > hw->max_rx_aggregation_subframes)
4129 params.buf_size = hw->max_rx_aggregation_subframes;
4130 params.timeout = le16toh(batimeout);
4131 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
4132 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4133 params.amsdu = false;
4134
4135 IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?");
4136
4137 /* This may call kalloc. Make sure we can sleep. */
4138 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
4139 if (error != 0) {
4140 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4141 __func__, error, ni, rap);
4142 return (error);
4143 }
4144 IMPROVE_HT("net80211 is missing the error check on return and assumes success");
4145
4146 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
4147 return (error);
4148 }
4149
4150 static void
lkpi_ic_ampdu_rx_stop(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap)4151 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
4152 {
4153 struct ieee80211com *ic;
4154 struct lkpi_hw *lhw;
4155 struct ieee80211_hw *hw;
4156 struct ieee80211vap *vap;
4157 struct lkpi_vif *lvif;
4158 struct ieee80211_vif *vif;
4159 struct lkpi_sta *lsta;
4160 struct ieee80211_sta *sta;
4161 struct ieee80211_ampdu_params params;
4162 int error;
4163 uint8_t tid;
4164
4165 ic = ni->ni_ic;
4166 lhw = ic->ic_softc;
4167
4168 /*
4169 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
4170 * we did not START. Some drivers pass it down to firmware which will
4171 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
4172 * ieee80211_ht_node_init() amongst others which will iterate over all
4173 * tid and call ic_ampdu_rx_stop() unconditionally.
4174 * XXX net80211 should probably be more "gentle" in these cases and
4175 * track some state itself.
4176 */
4177 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
4178 goto net80211_only;
4179
4180 hw = LHW_TO_HW(lhw);
4181 vap = ni->ni_vap;
4182 lvif = VAP_TO_LVIF(vap);
4183 vif = LVIF_TO_VIF(lvif);
4184 lsta = ni->ni_drv_data;
4185 sta = LSTA_TO_STA(lsta);
4186
4187 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
4188 for (tid = 0; tid < WME_NUM_TID; tid++) {
4189 if (&ni->ni_rx_ampdu[tid] == rap)
4190 break;
4191 }
4192
4193 params.sta = sta;
4194 params.action = IEEE80211_AMPDU_RX_STOP;
4195 params.buf_size = 0;
4196 params.timeout = 0;
4197 params.ssn = 0;
4198 params.tid = tid;
4199 params.amsdu = false;
4200
4201 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
4202 if (error != 0)
4203 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4204 __func__, error, ni, rap);
4205
4206 net80211_only:
4207 lhw->ic_ampdu_rx_stop(ni, rap);
4208 }
4209 #endif
4210
4211 static void
lkpi_ic_getradiocaps_ht(struct ieee80211com * ic,struct ieee80211_hw * hw,uint8_t * bands,int * chan_flags,enum nl80211_band band)4212 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
4213 uint8_t *bands, int *chan_flags, enum nl80211_band band)
4214 {
4215 #ifdef LKPI_80211_HT
4216 struct ieee80211_sta_ht_cap *ht_cap;
4217
4218 ht_cap = &hw->wiphy->bands[band]->ht_cap;
4219 if (!ht_cap->ht_supported)
4220 return;
4221
4222 switch (band) {
4223 case NL80211_BAND_2GHZ:
4224 setbit(bands, IEEE80211_MODE_11NG);
4225 break;
4226 case NL80211_BAND_5GHZ:
4227 setbit(bands, IEEE80211_MODE_11NA);
4228 break;
4229 default:
4230 IMPROVE("Unsupported band %d", band);
4231 return;
4232 }
4233
4234 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */
4235
4236 /*
4237 * Rather than manually checking each flag and
4238 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
4239 * simply copy the 16bits.
4240 */
4241 ic->ic_htcaps |= ht_cap->cap;
4242
4243 /* Then deal with the other flags. */
4244 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
4245 ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
4246 #ifdef __notyet__
4247 if (ieee80211_hw_check(hw, TX_AMSDU))
4248 ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
4249 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
4250 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
4251 IEEE80211_HTC_TX_AMSDU_AMPDU);
4252 #endif
4253
4254 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
4255 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
4256
4257 /* Only add HT40 channels if supported. */
4258 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
4259 chan_flags != NULL)
4260 *chan_flags |= NET80211_CBW_FLAG_HT40;
4261 #endif
4262 }
4263
4264 static void
lkpi_ic_getradiocaps(struct ieee80211com * ic,int maxchan,int * n,struct ieee80211_channel * c)4265 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
4266 int *n, struct ieee80211_channel *c)
4267 {
4268 struct lkpi_hw *lhw;
4269 struct ieee80211_hw *hw;
4270 struct linuxkpi_ieee80211_channel *channels;
4271 uint8_t bands[IEEE80211_MODE_BYTES];
4272 int chan_flags, error, i, nchans;
4273
4274 /* Channels */
4275 lhw = ic->ic_softc;
4276 hw = LHW_TO_HW(lhw);
4277
4278 /* NL80211_BAND_2GHZ */
4279 nchans = 0;
4280 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
4281 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
4282 if (nchans > 0) {
4283 memset(bands, 0, sizeof(bands));
4284 chan_flags = 0;
4285 setbit(bands, IEEE80211_MODE_11B);
4286 /* XXX-BZ unclear how to check for 11g. */
4287
4288 IMPROVE("the bitrates may have flags?");
4289 setbit(bands, IEEE80211_MODE_11G);
4290
4291 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4292 NL80211_BAND_2GHZ);
4293
4294 channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4295 for (i = 0; i < nchans && *n < maxchan; i++) {
4296 uint32_t nflags = 0;
4297 int cflags = chan_flags;
4298
4299 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4300 ic_printf(ic, "%s: Skipping disabled chan "
4301 "[%u/%u/%#x]\n", __func__,
4302 channels[i].hw_value,
4303 channels[i].center_freq, channels[i].flags);
4304 continue;
4305 }
4306 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4307 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4308 if (channels[i].flags & IEEE80211_CHAN_RADAR)
4309 nflags |= IEEE80211_CHAN_DFS;
4310 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4311 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4312 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4313 cflags &= ~NET80211_CBW_FLAG_VHT80;
4314 /* XXX how to map the remaining enum ieee80211_channel_flags? */
4315 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4316 cflags &= ~NET80211_CBW_FLAG_HT40;
4317
4318 error = ieee80211_add_channel_cbw(c, maxchan, n,
4319 channels[i].hw_value, channels[i].center_freq,
4320 channels[i].max_power,
4321 nflags, bands, cflags);
4322 /* net80211::ENOBUFS: *n >= maxchans */
4323 if (error != 0 && error != ENOBUFS)
4324 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4325 "returned error %d\n",
4326 __func__, channels[i].hw_value,
4327 channels[i].center_freq, channels[i].flags,
4328 nflags, chan_flags, cflags, error);
4329 if (error != 0)
4330 break;
4331 }
4332 }
4333
4334 /* NL80211_BAND_5GHZ */
4335 nchans = 0;
4336 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
4337 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
4338 if (nchans > 0) {
4339 memset(bands, 0, sizeof(bands));
4340 chan_flags = 0;
4341 setbit(bands, IEEE80211_MODE_11A);
4342
4343 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4344 NL80211_BAND_5GHZ);
4345
4346 #ifdef LKPI_80211_VHT
4347 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
4348
4349 ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4350 ic->ic_vht_cap.vht_cap_info =
4351 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
4352
4353 setbit(bands, IEEE80211_MODE_VHT_5GHZ);
4354 chan_flags |= NET80211_CBW_FLAG_VHT80;
4355 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4356 ic->ic_vht_cap.vht_cap_info))
4357 chan_flags |= NET80211_CBW_FLAG_VHT160;
4358 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4359 ic->ic_vht_cap.vht_cap_info))
4360 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
4361 }
4362 #endif
4363
4364 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4365 for (i = 0; i < nchans && *n < maxchan; i++) {
4366 uint32_t nflags = 0;
4367 int cflags = chan_flags;
4368
4369 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4370 ic_printf(ic, "%s: Skipping disabled chan "
4371 "[%u/%u/%#x]\n", __func__,
4372 channels[i].hw_value,
4373 channels[i].center_freq, channels[i].flags);
4374 continue;
4375 }
4376 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4377 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4378 if (channels[i].flags & IEEE80211_CHAN_RADAR)
4379 nflags |= IEEE80211_CHAN_DFS;
4380 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4381 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4382 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4383 cflags &= ~NET80211_CBW_FLAG_VHT80;
4384 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
4385 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4386 cflags &= ~NET80211_CBW_FLAG_HT40;
4387
4388 error = ieee80211_add_channel_cbw(c, maxchan, n,
4389 channels[i].hw_value, channels[i].center_freq,
4390 channels[i].max_power,
4391 nflags, bands, cflags);
4392 /* net80211::ENOBUFS: *n >= maxchans */
4393 if (error != 0 && error != ENOBUFS)
4394 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4395 "returned error %d\n",
4396 __func__, channels[i].hw_value,
4397 channels[i].center_freq, channels[i].flags,
4398 nflags, chan_flags, cflags, error);
4399 if (error != 0)
4400 break;
4401 }
4402 }
4403 }
4404
4405 static void *
lkpi_ieee80211_ifalloc(void)4406 lkpi_ieee80211_ifalloc(void)
4407 {
4408 struct ieee80211com *ic;
4409
4410 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
4411
4412 /* Setting these happens later when we have device information. */
4413 ic->ic_softc = NULL;
4414 ic->ic_name = "linuxkpi";
4415
4416 return (ic);
4417 }
4418
4419 struct ieee80211_hw *
linuxkpi_ieee80211_alloc_hw(size_t priv_len,const struct ieee80211_ops * ops)4420 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
4421 {
4422 struct ieee80211_hw *hw;
4423 struct lkpi_hw *lhw;
4424 struct wiphy *wiphy;
4425 int ac;
4426
4427 /* Get us and the driver data also allocated. */
4428 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
4429 if (wiphy == NULL)
4430 return (NULL);
4431
4432 lhw = wiphy_priv(wiphy);
4433 lhw->ops = ops;
4434
4435 LKPI_80211_LHW_LOCK_INIT(lhw);
4436 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4437 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
4438 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
4439 TAILQ_INIT(&lhw->lvif_head);
4440 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4441 lhw->txq_generation[ac] = 1;
4442 TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4443 }
4444
4445 /* Deferred RX path. */
4446 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4447 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4448 mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4449 lhw->rxq_stopped = false;
4450
4451 /*
4452 * XXX-BZ TODO make sure there is a "_null" function to all ops
4453 * not initialized.
4454 */
4455 hw = LHW_TO_HW(lhw);
4456 hw->wiphy = wiphy;
4457 hw->conf.flags |= IEEE80211_CONF_IDLE;
4458 hw->priv = (void *)(lhw + 1);
4459
4460 /* BSD Specific. */
4461 lhw->ic = lkpi_ieee80211_ifalloc();
4462
4463 IMPROVE();
4464
4465 return (hw);
4466 }
4467
4468 void
linuxkpi_ieee80211_iffree(struct ieee80211_hw * hw)4469 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
4470 {
4471 struct lkpi_hw *lhw;
4472 struct mbuf *m;
4473
4474 lhw = HW_TO_LHW(hw);
4475 free(lhw->ic, M_LKPI80211);
4476 lhw->ic = NULL;
4477
4478 /*
4479 * Drain the deferred RX path.
4480 */
4481 LKPI_80211_LHW_RXQ_LOCK(lhw);
4482 lhw->rxq_stopped = true;
4483 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4484
4485 /* Drain taskq, won't be restarted due to rxq_stopped being set. */
4486 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4487 taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4488
4489 /* Flush mbufq (make sure to release ni refs!). */
4490 m = mbufq_dequeue(&lhw->rxq);
4491 while (m != NULL) {
4492 struct m_tag *mtag;
4493
4494 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4495 if (mtag != NULL) {
4496 struct lkpi_80211_tag_rxni *rxni;
4497
4498 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4499 ieee80211_free_node(rxni->ni);
4500 }
4501 m_freem(m);
4502 m = mbufq_dequeue(&lhw->rxq);
4503 }
4504 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4505 __func__, lhw, mbufq_len(&lhw->rxq)));
4506 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4507
4508 /* Cleanup more of lhw here or in wiphy_free()? */
4509 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
4510 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4511 LKPI_80211_LHW_LOCK_DESTROY(lhw);
4512 sx_destroy(&lhw->lvif_sx);
4513 IMPROVE();
4514 }
4515
4516 void
linuxkpi_set_ieee80211_dev(struct ieee80211_hw * hw,char * name)4517 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
4518 {
4519 struct lkpi_hw *lhw;
4520 struct ieee80211com *ic;
4521
4522 lhw = HW_TO_LHW(hw);
4523 ic = lhw->ic;
4524
4525 /* Now set a proper name before ieee80211_ifattach(). */
4526 ic->ic_softc = lhw;
4527 ic->ic_name = name;
4528
4529 /* XXX-BZ do we also need to set wiphy name? */
4530 }
4531
4532 struct ieee80211_hw *
linuxkpi_wiphy_to_ieee80211_hw(struct wiphy * wiphy)4533 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
4534 {
4535 struct lkpi_hw *lhw;
4536
4537 lhw = wiphy_priv(wiphy);
4538 return (LHW_TO_HW(lhw));
4539 }
4540
4541 static void
lkpi_radiotap_attach(struct lkpi_hw * lhw)4542 lkpi_radiotap_attach(struct lkpi_hw *lhw)
4543 {
4544 struct ieee80211com *ic;
4545
4546 ic = lhw->ic;
4547 ieee80211_radiotap_attach(ic,
4548 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
4549 LKPI_RTAP_TX_FLAGS_PRESENT,
4550 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
4551 LKPI_RTAP_RX_FLAGS_PRESENT);
4552 }
4553
4554 int
linuxkpi_ieee80211_ifattach(struct ieee80211_hw * hw)4555 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
4556 {
4557 struct ieee80211com *ic;
4558 struct lkpi_hw *lhw;
4559 int band, i;
4560
4561 lhw = HW_TO_LHW(hw);
4562 ic = lhw->ic;
4563
4564 /* We do it this late as wiphy->dev should be set for the name. */
4565 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4566 if (lhw->workq == NULL)
4567 return (-EAGAIN);
4568
4569 /* XXX-BZ figure this out how they count his... */
4570 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
4571 IEEE80211_ADDR_COPY(ic->ic_macaddr,
4572 hw->wiphy->perm_addr);
4573 } else if (hw->wiphy->n_addresses > 0) {
4574 /* We take the first one. */
4575 IEEE80211_ADDR_COPY(ic->ic_macaddr,
4576 hw->wiphy->addresses[0].addr);
4577 } else {
4578 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
4579 }
4580
4581 #ifdef __not_yet__
4582 /* See comment in lkpi_80211_txq_tx_one(). */
4583 ic->ic_headroom = hw->extra_tx_headroom;
4584 #endif
4585
4586 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
4587 ic->ic_opmode = IEEE80211_M_STA;
4588
4589 /* Set device capabilities. */
4590 /* XXX-BZ we need to get these from linux80211/drivers and convert. */
4591 ic->ic_caps =
4592 IEEE80211_C_STA |
4593 IEEE80211_C_MONITOR |
4594 IEEE80211_C_WPA | /* WPA/RSN */
4595 #ifdef LKPI_80211_WME
4596 IEEE80211_C_WME |
4597 #endif
4598 #if 0
4599 IEEE80211_C_PMGT |
4600 #endif
4601 IEEE80211_C_SHSLOT | /* short slot time supported */
4602 IEEE80211_C_SHPREAMBLE /* short preamble supported */
4603 ;
4604 #if 0
4605 /* Scanning is a different kind of beast to re-work. */
4606 ic->ic_caps |= IEEE80211_C_BGSCAN;
4607 #endif
4608 if (lhw->ops->hw_scan) {
4609 /*
4610 * Advertise full-offload scanning.
4611 *
4612 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4613 * we essentially disable hw_scan for all drivers not setting
4614 * the flag.
4615 */
4616 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4617 lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4618 }
4619
4620 /*
4621 * The wiphy variables report bitmasks of avail antennas.
4622 * (*get_antenna) get the current bitmask sets which can be
4623 * altered by (*set_antenna) for some drivers.
4624 * XXX-BZ will the count alone do us much good long-term in net80211?
4625 */
4626 if (hw->wiphy->available_antennas_rx ||
4627 hw->wiphy->available_antennas_tx) {
4628 uint32_t rxs, txs;
4629
4630 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4631 ic->ic_rxstream = bitcount32(rxs);
4632 ic->ic_txstream = bitcount32(txs);
4633 }
4634 }
4635
4636 ic->ic_cryptocaps = 0;
4637 #ifdef LKPI_80211_HW_CRYPTO
4638 if (hw->wiphy->n_cipher_suites > 0) {
4639 for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
4640 ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
4641 hw->wiphy->cipher_suites[i]);
4642 }
4643 #endif
4644
4645 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
4646 ic->ic_channels);
4647
4648 ieee80211_ifattach(ic);
4649
4650 ic->ic_update_mcast = lkpi_ic_update_mcast;
4651 ic->ic_update_promisc = lkpi_ic_update_promisc;
4652 ic->ic_update_chw = lkpi_ic_update_chw;
4653 ic->ic_parent = lkpi_ic_parent;
4654 ic->ic_scan_start = lkpi_ic_scan_start;
4655 ic->ic_scan_end = lkpi_ic_scan_end;
4656 ic->ic_set_channel = lkpi_ic_set_channel;
4657 ic->ic_transmit = lkpi_ic_transmit;
4658 ic->ic_raw_xmit = lkpi_ic_raw_xmit;
4659 ic->ic_vap_create = lkpi_ic_vap_create;
4660 ic->ic_vap_delete = lkpi_ic_vap_delete;
4661 ic->ic_getradiocaps = lkpi_ic_getradiocaps;
4662 ic->ic_wme.wme_update = lkpi_ic_wme_update;
4663
4664 lhw->ic_scan_curchan = ic->ic_scan_curchan;
4665 ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4666 lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4667 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4668
4669 lhw->ic_node_alloc = ic->ic_node_alloc;
4670 ic->ic_node_alloc = lkpi_ic_node_alloc;
4671 lhw->ic_node_init = ic->ic_node_init;
4672 ic->ic_node_init = lkpi_ic_node_init;
4673 lhw->ic_node_cleanup = ic->ic_node_cleanup;
4674 ic->ic_node_cleanup = lkpi_ic_node_cleanup;
4675 lhw->ic_node_free = ic->ic_node_free;
4676 ic->ic_node_free = lkpi_ic_node_free;
4677
4678 #ifdef LKPI_80211_HT
4679 lhw->ic_recv_action = ic->ic_recv_action;
4680 ic->ic_recv_action = lkpi_ic_recv_action;
4681 lhw->ic_send_action = ic->ic_send_action;
4682 ic->ic_send_action = lkpi_ic_send_action;
4683
4684 lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
4685 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
4686
4687 lhw->ic_addba_request = ic->ic_addba_request;
4688 ic->ic_addba_request = lkpi_ic_addba_request;
4689 lhw->ic_addba_response = ic->ic_addba_response;
4690 ic->ic_addba_response = lkpi_ic_addba_response;
4691 lhw->ic_addba_stop = ic->ic_addba_stop;
4692 ic->ic_addba_stop = lkpi_ic_addba_stop;
4693 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
4694 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
4695
4696 lhw->ic_bar_response = ic->ic_bar_response;
4697 ic->ic_bar_response = lkpi_ic_bar_response;
4698
4699 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
4700 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
4701 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
4702 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
4703 #endif
4704
4705 lkpi_radiotap_attach(lhw);
4706
4707 /*
4708 * Assign the first possible channel for now; seems Realtek drivers
4709 * expect one.
4710 * Also remember the amount of bands we support and the most rates
4711 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4712 */
4713 lhw->supbands = lhw->max_rates = 0;
4714 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4715 struct ieee80211_supported_band *supband;
4716 struct linuxkpi_ieee80211_channel *channels;
4717
4718 supband = hw->wiphy->bands[band];
4719 if (supband == NULL || supband->n_channels == 0)
4720 continue;
4721
4722 lhw->supbands++;
4723 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4724
4725 /* If we have a channel, we need to keep counting supbands. */
4726 if (hw->conf.chandef.chan != NULL)
4727 continue;
4728
4729 channels = supband->channels;
4730 for (i = 0; i < supband->n_channels; i++) {
4731
4732 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4733 continue;
4734
4735 cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
4736 #ifdef LKPI_80211_HT
4737 (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
4738 #endif
4739 NL80211_CHAN_NO_HT);
4740 break;
4741 }
4742 }
4743
4744 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4745
4746 /* Make sure we do not support more than net80211 is willing to take. */
4747 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4748 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4749 lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4750 lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4751 }
4752
4753 /*
4754 * The maximum supported bitrates on any band + size for
4755 * DSSS Parameter Set give our per-band IE size.
4756 * SSID is the responsibility of the driver and goes on the side.
4757 * The user specified bits coming from the vap go into the
4758 * "common ies" fields.
4759 */
4760 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4761 if (lhw->max_rates > IEEE80211_RATE_SIZE)
4762 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
4763
4764 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4765 /*
4766 * net80211 does not seem to support the DSSS Parameter Set but
4767 * some of the drivers insert it so calculate the extra fixed
4768 * space in.
4769 */
4770 lhw->scan_ie_len += 2 + 1;
4771 }
4772
4773 #if defined(LKPI_80211_HT)
4774 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
4775 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
4776 #endif
4777 #if defined(LKPI_80211_VHT)
4778 if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
4779 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
4780 #endif
4781
4782 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */
4783 if (hw->wiphy->max_scan_ie_len > 0) {
4784 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
4785 goto err;
4786 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
4787 }
4788
4789 if (bootverbose)
4790 ieee80211_announce(ic);
4791
4792 return (0);
4793 err:
4794 IMPROVE("TODO FIXME CLEANUP");
4795 return (-EAGAIN);
4796 }
4797
4798 void
linuxkpi_ieee80211_ifdetach(struct ieee80211_hw * hw)4799 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
4800 {
4801 struct lkpi_hw *lhw;
4802 struct ieee80211com *ic;
4803
4804 lhw = HW_TO_LHW(hw);
4805 ic = lhw->ic;
4806 ieee80211_ifdetach(ic);
4807 }
4808
4809 void
linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw * hw,enum ieee80211_iface_iter flags,void (* iterfunc)(void *,uint8_t *,struct ieee80211_vif *),void * arg)4810 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
4811 enum ieee80211_iface_iter flags,
4812 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
4813 void *arg)
4814 {
4815 struct lkpi_hw *lhw;
4816 struct lkpi_vif *lvif;
4817 struct ieee80211_vif *vif;
4818 bool active, atomic, nin_drv;
4819
4820 lhw = HW_TO_LHW(hw);
4821
4822 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
4823 IEEE80211_IFACE_ITER_RESUME_ALL|
4824 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
4825 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
4826 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
4827 __func__, flags);
4828 }
4829
4830 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
4831 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
4832 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
4833
4834 if (atomic)
4835 LKPI_80211_LHW_LVIF_LOCK(lhw);
4836 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4837 struct ieee80211vap *vap;
4838
4839 vif = LVIF_TO_VIF(lvif);
4840
4841 /*
4842 * If we want "active" interfaces, we need to distinguish on
4843 * whether the driver knows about them or not to be able to
4844 * handle the "resume" case correctly. Skip the ones the
4845 * driver does not know about.
4846 */
4847 if (active && !lvif->added_to_drv &&
4848 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
4849 continue;
4850
4851 /*
4852 * If we shall skip interfaces not added to the driver do so
4853 * if we haven't yet.
4854 */
4855 if (nin_drv && !lvif->added_to_drv)
4856 continue;
4857
4858 /*
4859 * Run the iterator function if we are either not asking
4860 * asking for active only or if the VAP is "running".
4861 */
4862 /* XXX-BZ probably should have state in the lvif as well. */
4863 vap = LVIF_TO_VAP(lvif);
4864 if (!active || (vap->iv_state != IEEE80211_S_INIT))
4865 iterfunc(arg, vif->addr, vif);
4866 }
4867 if (atomic)
4868 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4869 }
4870
4871 void
linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void (* iterfunc)(struct ieee80211_hw *,struct ieee80211_vif *,struct ieee80211_sta *,struct ieee80211_key_conf *,void *),void * arg)4872 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
4873 struct ieee80211_vif *vif,
4874 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
4875 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
4876 void *arg)
4877 {
4878
4879 UNIMPLEMENTED;
4880 }
4881
4882 void
linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw * hw,void (* iterfunc)(struct ieee80211_hw *,struct ieee80211_chanctx_conf *,void *),void * arg)4883 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
4884 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
4885 void *),
4886 void *arg)
4887 {
4888 struct lkpi_hw *lhw;
4889 struct lkpi_vif *lvif;
4890 struct ieee80211_vif *vif;
4891 struct lkpi_chanctx *lchanctx;
4892
4893 KASSERT(hw != NULL && iterfunc != NULL,
4894 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4895
4896 lhw = HW_TO_LHW(hw);
4897
4898 IMPROVE("lchanctx should be its own list somewhere");
4899
4900 LKPI_80211_LHW_LVIF_LOCK(lhw);
4901 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4902
4903 vif = LVIF_TO_VIF(lvif);
4904 if (vif->chanctx_conf == NULL)
4905 continue;
4906
4907 lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
4908 if (!lchanctx->added_to_drv)
4909 continue;
4910
4911 iterfunc(hw, &lchanctx->chanctx_conf, arg);
4912 }
4913 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4914 }
4915
4916 void
linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw * hw,void (* iterfunc)(void *,struct ieee80211_sta *),void * arg)4917 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
4918 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
4919 {
4920 struct lkpi_hw *lhw;
4921 struct lkpi_vif *lvif;
4922 struct lkpi_sta *lsta;
4923 struct ieee80211_sta *sta;
4924
4925 KASSERT(hw != NULL && iterfunc != NULL,
4926 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
4927
4928 lhw = HW_TO_LHW(hw);
4929
4930 LKPI_80211_LHW_LVIF_LOCK(lhw);
4931 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
4932
4933 LKPI_80211_LVIF_LOCK(lvif);
4934 TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
4935 if (!lsta->added_to_drv)
4936 continue;
4937 sta = LSTA_TO_STA(lsta);
4938 iterfunc(arg, sta);
4939 }
4940 LKPI_80211_LVIF_UNLOCK(lvif);
4941 }
4942 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4943 }
4944
4945 struct linuxkpi_ieee80211_regdomain *
lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)4946 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
4947 {
4948 struct linuxkpi_ieee80211_regdomain *regd;
4949
4950 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
4951 GFP_KERNEL);
4952 return (regd);
4953 }
4954
4955 int
linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy * wiphy,struct linuxkpi_ieee80211_regdomain * regd)4956 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
4957 struct linuxkpi_ieee80211_regdomain *regd)
4958 {
4959 struct lkpi_hw *lhw;
4960 struct ieee80211com *ic;
4961 struct ieee80211_regdomain *rd;
4962
4963 lhw = wiphy_priv(wiphy);
4964 ic = lhw->ic;
4965
4966 rd = &ic->ic_regdomain;
4967 if (rd->isocc[0] == '\0') {
4968 rd->isocc[0] = regd->alpha2[0];
4969 rd->isocc[1] = regd->alpha2[1];
4970 }
4971
4972 TODO();
4973 /* XXX-BZ finish the rest. */
4974
4975 return (0);
4976 }
4977
4978 void
linuxkpi_ieee80211_scan_completed(struct ieee80211_hw * hw,struct cfg80211_scan_info * info)4979 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
4980 struct cfg80211_scan_info *info)
4981 {
4982 struct lkpi_hw *lhw;
4983 struct ieee80211com *ic;
4984 struct ieee80211_scan_state *ss;
4985
4986 lhw = wiphy_priv(hw->wiphy);
4987 ic = lhw->ic;
4988 ss = ic->ic_scan;
4989
4990 ieee80211_scan_done(ss->ss_vap);
4991
4992 LKPI_80211_LHW_SCAN_LOCK(lhw);
4993 free(lhw->hw_req, M_LKPI80211);
4994 lhw->hw_req = NULL;
4995 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
4996 wakeup(lhw);
4997 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4998
4999 return;
5000 }
5001
5002 static void
lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw * lhw,struct mbuf * m)5003 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5004 {
5005 struct ieee80211_node *ni;
5006 struct m_tag *mtag;
5007 int ok;
5008
5009 ni = NULL;
5010 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5011 if (mtag != NULL) {
5012 struct lkpi_80211_tag_rxni *rxni;
5013
5014 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5015 ni = rxni->ni;
5016 }
5017
5018 if (ni != NULL) {
5019 ok = ieee80211_input_mimo(ni, m);
5020 ieee80211_free_node(ni); /* Release the reference. */
5021 if (ok < 0)
5022 m_freem(m);
5023 } else {
5024 ok = ieee80211_input_mimo_all(lhw->ic, m);
5025 /* mbuf got consumed. */
5026 }
5027
5028 #ifdef LINUXKPI_DEBUG_80211
5029 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5030 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5031 #endif
5032 }
5033
5034 static void
lkpi_80211_lhw_rxq_task(void * ctx,int pending)5035 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5036 {
5037 struct lkpi_hw *lhw;
5038 struct mbufq mq;
5039 struct mbuf *m;
5040
5041 lhw = ctx;
5042
5043 #ifdef LINUXKPI_DEBUG_80211
5044 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5045 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5046 __func__, lhw, pending, mbufq_len(&lhw->rxq));
5047 #endif
5048
5049 mbufq_init(&mq, IFQ_MAXLEN);
5050
5051 LKPI_80211_LHW_RXQ_LOCK(lhw);
5052 mbufq_concat(&mq, &lhw->rxq);
5053 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5054
5055 m = mbufq_dequeue(&mq);
5056 while (m != NULL) {
5057 lkpi_80211_lhw_rxq_rx_one(lhw, m);
5058 m = mbufq_dequeue(&mq);
5059 }
5060 }
5061
5062 /* For %list see comment towards the end of the function. */
5063 void
linuxkpi_ieee80211_rx(struct ieee80211_hw * hw,struct sk_buff * skb,struct ieee80211_sta * sta,struct napi_struct * napi __unused,struct list_head * list __unused)5064 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
5065 struct ieee80211_sta *sta, struct napi_struct *napi __unused,
5066 struct list_head *list __unused)
5067 {
5068 struct lkpi_hw *lhw;
5069 struct ieee80211com *ic;
5070 struct mbuf *m;
5071 struct skb_shared_info *shinfo;
5072 struct ieee80211_rx_status *rx_status;
5073 struct ieee80211_rx_stats rx_stats;
5074 struct ieee80211_node *ni;
5075 struct ieee80211vap *vap;
5076 struct ieee80211_hdr *hdr;
5077 struct lkpi_sta *lsta;
5078 int i, offset, ok;
5079 int8_t rssi;
5080 bool is_beacon;
5081
5082 if (skb->len < 2) {
5083 /* Need 80211 stats here. */
5084 IMPROVE();
5085 goto err;
5086 }
5087
5088 /*
5089 * For now do the data copy; we can later improve things. Might even
5090 * have an mbuf backing the skb data then?
5091 */
5092 m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
5093 if (m == NULL)
5094 goto err;
5095 m_copyback(m, 0, skb->tail - skb->data, skb->data);
5096
5097 shinfo = skb_shinfo(skb);
5098 offset = m->m_len;
5099 for (i = 0; i < shinfo->nr_frags; i++) {
5100 m_copyback(m, offset, shinfo->frags[i].size,
5101 (uint8_t *)linux_page_address(shinfo->frags[i].page) +
5102 shinfo->frags[i].offset);
5103 offset += shinfo->frags[i].size;
5104 }
5105
5106 rx_status = IEEE80211_SKB_RXCB(skb);
5107
5108 hdr = (void *)skb->data;
5109 is_beacon = ieee80211_is_beacon(hdr->frame_control);
5110
5111 #ifdef LINUXKPI_DEBUG_80211
5112 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
5113 goto no_trace_beacons;
5114
5115 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5116 printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5117 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
5118 __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
5119 skb->truesize, skb->head, skb->data, skb->tail, skb->end,
5120 shinfo, shinfo->nr_frags,
5121 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
5122
5123 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
5124 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
5125
5126 /* Implement a dump_rxcb() !!! */
5127 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5128 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
5129 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
5130 __func__,
5131 (uintmax_t)rx_status->boottime_ns,
5132 (uintmax_t)rx_status->mactime,
5133 rx_status->device_timestamp,
5134 rx_status->flag,
5135 rx_status->freq,
5136 rx_status->bw,
5137 rx_status->encoding,
5138 rx_status->ampdu_reference,
5139 rx_status->band,
5140 rx_status->chains,
5141 rx_status->chain_signal[0],
5142 rx_status->chain_signal[1],
5143 rx_status->chain_signal[2],
5144 rx_status->chain_signal[3],
5145 rx_status->signal,
5146 rx_status->enc_flags,
5147 rx_status->he_dcm,
5148 rx_status->he_gi,
5149 rx_status->he_ru,
5150 rx_status->zero_length_psdu_type,
5151 rx_status->nss,
5152 rx_status->rate_idx);
5153 no_trace_beacons:
5154 #endif
5155
5156 memset(&rx_stats, 0, sizeof(rx_stats));
5157 rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
5158 /* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
5159 rx_stats.c_nf = -96;
5160 if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
5161 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5162 rssi = rx_status->signal;
5163 else
5164 rssi = rx_stats.c_nf;
5165 /*
5166 * net80211 signal strength data are in .5 dBm units relative to
5167 * the current noise floor (see comment in ieee80211_node.h).
5168 */
5169 rssi -= rx_stats.c_nf;
5170 rx_stats.c_rssi = rssi * 2;
5171 rx_stats.r_flags |= IEEE80211_R_BAND;
5172 rx_stats.c_band =
5173 lkpi_nl80211_band_to_net80211_band(rx_status->band);
5174 rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
5175 rx_stats.c_freq = rx_status->freq;
5176 rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
5177
5178 /* XXX (*sta_statistics)() to get to some of that? */
5179 /* XXX-BZ dump the FreeBSD version of rx_stats as well! */
5180
5181 lhw = HW_TO_LHW(hw);
5182 ic = lhw->ic;
5183
5184 ok = ieee80211_add_rx_params(m, &rx_stats);
5185 if (ok == 0) {
5186 m_freem(m);
5187 counter_u64_add(ic->ic_ierrors, 1);
5188 goto err;
5189 }
5190
5191 lsta = NULL;
5192 if (sta != NULL) {
5193 lsta = STA_TO_LSTA(sta);
5194 ni = ieee80211_ref_node(lsta->ni);
5195 } else {
5196 struct ieee80211_frame_min *wh;
5197
5198 wh = mtod(m, struct ieee80211_frame_min *);
5199 ni = ieee80211_find_rxnode(ic, wh);
5200 if (ni != NULL)
5201 lsta = ni->ni_drv_data;
5202 }
5203
5204 if (ni != NULL)
5205 vap = ni->ni_vap;
5206 else
5207 /*
5208 * XXX-BZ can we improve this by looking at the frame hdr
5209 * or other meta-data passed up?
5210 */
5211 vap = TAILQ_FIRST(&ic->ic_vaps);
5212
5213 #ifdef LINUXKPI_DEBUG_80211
5214 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5215 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5216 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5217 ni, vap, is_beacon ? " beacon" : "");
5218 #endif
5219
5220 if (ni != NULL && vap != NULL && is_beacon &&
5221 rx_status->device_timestamp > 0 &&
5222 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5223 struct lkpi_vif *lvif;
5224 struct ieee80211_vif *vif;
5225 struct ieee80211_frame *wh;
5226
5227 wh = mtod(m, struct ieee80211_frame *);
5228 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5229 goto skip_device_ts;
5230
5231 lvif = VAP_TO_LVIF(vap);
5232 vif = LVIF_TO_VIF(lvif);
5233
5234 IMPROVE("TIMING_BEACON_ONLY?");
5235 /* mac80211 specific (not net80211) so keep it here. */
5236 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5237 /*
5238 * net80211 should take care of the other information (sync_tsf,
5239 * sync_dtim_count) as otherwise we need to parse the beacon.
5240 */
5241 skip_device_ts:
5242 ;
5243 }
5244
5245 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
5246 ieee80211_radiotap_active_vap(vap)) {
5247 struct lkpi_radiotap_rx_hdr *rtap;
5248
5249 rtap = &lhw->rtap_rx;
5250 rtap->wr_tsft = rx_status->device_timestamp;
5251 rtap->wr_flags = 0;
5252 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
5253 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5254 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
5255 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5256 #if 0 /* .. or it does not given we strip it below. */
5257 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5258 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
5259 #endif
5260 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
5261 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
5262 rtap->wr_rate = 0;
5263 IMPROVE();
5264 /* XXX TODO status->encoding / rate_index / bw */
5265 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
5266 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
5267 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5268 rtap->wr_dbm_antsignal = rssi;
5269 rtap->wr_dbm_antnoise = rx_stats.c_nf;
5270 }
5271
5272 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5273 m_adj(m, -IEEE80211_CRC_LEN);
5274
5275 #if 0
5276 if (list != NULL) {
5277 /*
5278 * Normally this would be queued up and delivered by
5279 * netif_receive_skb_list(), napi_gro_receive(), or the like.
5280 * See mt76::mac80211.c as only current possible consumer.
5281 */
5282 IMPROVE("we simply pass the packet to net80211 to deal with.");
5283 }
5284 #endif
5285
5286 /*
5287 * Attach meta-information to the mbuf for the deferred RX path.
5288 * Currently this is best-effort. Should we need to be hard,
5289 * drop the frame and goto err;
5290 */
5291 if (ni != NULL) {
5292 struct m_tag *mtag;
5293 struct lkpi_80211_tag_rxni *rxni;
5294
5295 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5296 sizeof(*rxni), IEEE80211_M_NOWAIT);
5297 if (mtag != NULL) {
5298 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5299 rxni->ni = ni; /* We hold a reference. */
5300 m_tag_prepend(m, mtag);
5301 }
5302 }
5303
5304 LKPI_80211_LHW_RXQ_LOCK(lhw);
5305 if (lhw->rxq_stopped) {
5306 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5307 m_freem(m);
5308 goto err;
5309 }
5310
5311 mbufq_enqueue(&lhw->rxq, m);
5312 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5313 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5314
5315 IMPROVE();
5316
5317 err:
5318 /* The skb is ours so we can free it :-) */
5319 kfree_skb(skb);
5320 }
5321
5322 uint8_t
linuxkpi_ieee80211_get_tid(struct ieee80211_hdr * hdr,bool nonqos_ok)5323 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
5324 {
5325 const struct ieee80211_frame *wh;
5326 uint8_t tid;
5327
5328 /* Linux seems to assume this is a QOS-Data-Frame */
5329 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5330 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5331 hdr->frame_control));
5332
5333 wh = (const struct ieee80211_frame *)hdr;
5334 tid = ieee80211_gettid(wh);
5335 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5336 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5337
5338 return (tid);
5339 }
5340
5341 /* -------------------------------------------------------------------------- */
5342
5343 static void
lkpi_wiphy_work(struct work_struct * work)5344 lkpi_wiphy_work(struct work_struct *work)
5345 {
5346 struct lkpi_wiphy *lwiphy;
5347 struct wiphy *wiphy;
5348 struct wiphy_work *wk;
5349
5350 lwiphy = container_of(work, struct lkpi_wiphy, wwk);
5351 wiphy = LWIPHY_TO_WIPHY(lwiphy);
5352
5353 wiphy_lock(wiphy);
5354
5355 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5356 wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
5357 /* If there is nothing we do nothing. */
5358 if (wk == NULL) {
5359 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5360 wiphy_unlock(wiphy);
5361 return;
5362 }
5363 list_del_init(&wk->entry);
5364
5365 /* More work to do? */
5366 if (!list_empty(&lwiphy->wwk_list))
5367 schedule_work(work);
5368 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5369
5370 /* Finally call the (*wiphy_work_fn)() function. */
5371 wk->fn(wiphy, wk);
5372
5373 wiphy_unlock(wiphy);
5374 }
5375
5376 void
linuxkpi_wiphy_work_queue(struct wiphy * wiphy,struct wiphy_work * wwk)5377 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
5378 {
5379 struct lkpi_wiphy *lwiphy;
5380
5381 lwiphy = WIPHY_TO_LWIPHY(wiphy);
5382
5383 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5384 /* Do not double-queue. */
5385 if (list_empty(&wwk->entry))
5386 list_add_tail(&wwk->entry, &lwiphy->wwk_list);
5387 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5388
5389 /*
5390 * See how ieee80211_queue_work() work continues in Linux or if things
5391 * migrate here over time?
5392 * Use a system queue from linux/workqueue.h for now.
5393 */
5394 queue_work(system_wq, &lwiphy->wwk);
5395 }
5396
5397 void
linuxkpi_wiphy_work_cancel(struct wiphy * wiphy,struct wiphy_work * wwk)5398 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
5399 {
5400 struct lkpi_wiphy *lwiphy;
5401
5402 lwiphy = WIPHY_TO_LWIPHY(wiphy);
5403
5404 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5405 /* Only cancel if queued. */
5406 if (!list_empty(&wwk->entry))
5407 list_del_init(&wwk->entry);
5408 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5409 }
5410
5411 void
linuxkpi_wiphy_work_flush(struct wiphy * wiphy,struct wiphy_work * wwk)5412 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
5413 {
5414 struct lkpi_wiphy *lwiphy;
5415 struct wiphy_work *wk;
5416
5417 lwiphy = WIPHY_TO_LWIPHY(wiphy);
5418 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5419 /* If wwk is unset, flush everything; called when wiphy is shut down. */
5420 if (wwk != NULL && list_empty(&wwk->entry)) {
5421 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5422 return;
5423 }
5424
5425 while (!list_empty(&lwiphy->wwk_list)) {
5426
5427 wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
5428 entry);
5429 list_del_init(&wk->entry);
5430 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5431 wk->fn(wiphy, wk);
5432 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5433 if (wk == wwk)
5434 break;
5435 }
5436 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5437 }
5438
5439 void
lkpi_wiphy_delayed_work_timer(struct timer_list * tl)5440 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
5441 {
5442 struct wiphy_delayed_work *wdwk;
5443
5444 wdwk = from_timer(wdwk, tl, timer);
5445 wiphy_work_queue(wdwk->wiphy, &wdwk->work);
5446 }
5447
5448 void
linuxkpi_wiphy_delayed_work_queue(struct wiphy * wiphy,struct wiphy_delayed_work * wdwk,unsigned long delay)5449 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
5450 struct wiphy_delayed_work *wdwk, unsigned long delay)
5451 {
5452 if (delay == 0) {
5453 /* Run right away. */
5454 del_timer(&wdwk->timer);
5455 wiphy_work_queue(wiphy, &wdwk->work);
5456 } else {
5457 wdwk->wiphy = wiphy;
5458 mod_timer(&wdwk->timer, jiffies + delay);
5459 }
5460 }
5461
5462 void
linuxkpi_wiphy_delayed_work_cancel(struct wiphy * wiphy,struct wiphy_delayed_work * wdwk)5463 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
5464 struct wiphy_delayed_work *wdwk)
5465 {
5466 del_timer_sync(&wdwk->timer);
5467 wiphy_work_cancel(wiphy, &wdwk->work);
5468 }
5469
5470 /* -------------------------------------------------------------------------- */
5471
5472 struct wiphy *
linuxkpi_wiphy_new(const struct cfg80211_ops * ops,size_t priv_len)5473 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
5474 {
5475 struct lkpi_wiphy *lwiphy;
5476 struct wiphy *wiphy;
5477
5478 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
5479 if (lwiphy == NULL)
5480 return (NULL);
5481 lwiphy->ops = ops;
5482
5483 LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
5484 INIT_LIST_HEAD(&lwiphy->wwk_list);
5485 INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
5486
5487 wiphy = LWIPHY_TO_WIPHY(lwiphy);
5488
5489 mutex_init(&wiphy->mtx);
5490 TODO();
5491
5492 return (wiphy);
5493 }
5494
5495 void
linuxkpi_wiphy_free(struct wiphy * wiphy)5496 linuxkpi_wiphy_free(struct wiphy *wiphy)
5497 {
5498 struct lkpi_wiphy *lwiphy;
5499
5500 if (wiphy == NULL)
5501 return;
5502
5503 linuxkpi_wiphy_work_flush(wiphy, NULL);
5504 mutex_destroy(&wiphy->mtx);
5505
5506 lwiphy = WIPHY_TO_LWIPHY(wiphy);
5507 LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
5508
5509 kfree(lwiphy);
5510 }
5511
5512 uint32_t
linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,enum nl80211_band band)5513 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
5514 enum nl80211_band band)
5515 {
5516
5517 switch (band) {
5518 case NL80211_BAND_2GHZ:
5519 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
5520 break;
5521 case NL80211_BAND_5GHZ:
5522 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
5523 break;
5524 default:
5525 /* XXX abort, retry, error, panic? */
5526 break;
5527 }
5528
5529 return (0);
5530 }
5531
5532 uint32_t
linuxkpi_ieee80211_frequency_to_channel(uint32_t freq,uint32_t flags __unused)5533 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
5534 {
5535
5536 return (ieee80211_mhz2ieee(freq, 0));
5537 }
5538
5539 #if 0
5540 static struct lkpi_sta *
5541 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
5542 {
5543 struct lkpi_sta *lsta, *temp;
5544
5545 LKPI_80211_LVIF_LOCK(lvif);
5546 TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5547 if (lsta->ni == ni) {
5548 LKPI_80211_LVIF_UNLOCK(lvif);
5549 return (lsta);
5550 }
5551 }
5552 LKPI_80211_LVIF_UNLOCK(lvif);
5553
5554 return (NULL);
5555 }
5556 #endif
5557
5558 struct ieee80211_sta *
linuxkpi_ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * peer)5559 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
5560 {
5561 struct lkpi_vif *lvif;
5562 struct lkpi_sta *lsta, *temp;
5563 struct ieee80211_sta *sta;
5564
5565 lvif = VIF_TO_LVIF(vif);
5566
5567 LKPI_80211_LVIF_LOCK(lvif);
5568 TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5569 sta = LSTA_TO_STA(lsta);
5570 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
5571 LKPI_80211_LVIF_UNLOCK(lvif);
5572 return (sta);
5573 }
5574 }
5575 LKPI_80211_LVIF_UNLOCK(lvif);
5576 return (NULL);
5577 }
5578
5579 struct ieee80211_sta *
linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const uint8_t * addr,const uint8_t * ourvifaddr)5580 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
5581 const uint8_t *addr, const uint8_t *ourvifaddr)
5582 {
5583 struct lkpi_hw *lhw;
5584 struct lkpi_vif *lvif;
5585 struct lkpi_sta *lsta;
5586 struct ieee80211_vif *vif;
5587 struct ieee80211_sta *sta;
5588
5589 lhw = wiphy_priv(hw->wiphy);
5590 sta = NULL;
5591
5592 LKPI_80211_LHW_LVIF_LOCK(lhw);
5593 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5594
5595 /* XXX-BZ check our address from the vif. */
5596
5597 vif = LVIF_TO_VIF(lvif);
5598 if (ourvifaddr != NULL &&
5599 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
5600 continue;
5601 sta = linuxkpi_ieee80211_find_sta(vif, addr);
5602 if (sta != NULL)
5603 break;
5604 }
5605 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5606
5607 if (sta != NULL) {
5608 lsta = STA_TO_LSTA(sta);
5609 if (!lsta->added_to_drv)
5610 return (NULL);
5611 }
5612
5613 return (sta);
5614 }
5615
5616 struct sk_buff *
linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)5617 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
5618 struct ieee80211_txq *txq)
5619 {
5620 struct lkpi_txq *ltxq;
5621 struct lkpi_vif *lvif;
5622 struct sk_buff *skb;
5623
5624 skb = NULL;
5625 ltxq = TXQ_TO_LTXQ(txq);
5626 ltxq->seen_dequeue = true;
5627
5628 if (ltxq->stopped)
5629 goto stopped;
5630
5631 lvif = VIF_TO_LVIF(ltxq->txq.vif);
5632 if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
5633 ltxq->stopped = true;
5634 goto stopped;
5635 }
5636
5637 IMPROVE("hw(TX_FRAG_LIST)");
5638
5639 LKPI_80211_LTXQ_LOCK(ltxq);
5640 skb = skb_dequeue(<xq->skbq);
5641 LKPI_80211_LTXQ_UNLOCK(ltxq);
5642
5643 stopped:
5644 return (skb);
5645 }
5646
5647 void
linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq * txq,unsigned long * frame_cnt,unsigned long * byte_cnt)5648 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
5649 unsigned long *frame_cnt, unsigned long *byte_cnt)
5650 {
5651 struct lkpi_txq *ltxq;
5652 struct sk_buff *skb;
5653 unsigned long fc, bc;
5654
5655 ltxq = TXQ_TO_LTXQ(txq);
5656
5657 fc = bc = 0;
5658 LKPI_80211_LTXQ_LOCK(ltxq);
5659 skb_queue_walk(<xq->skbq, skb) {
5660 fc++;
5661 bc += skb->len;
5662 }
5663 LKPI_80211_LTXQ_UNLOCK(ltxq);
5664 if (frame_cnt)
5665 *frame_cnt = fc;
5666 if (byte_cnt)
5667 *byte_cnt = bc;
5668
5669 /* Validate that this is doing the correct thing. */
5670 /* Should we keep track on en/dequeue? */
5671 IMPROVE();
5672 }
5673
5674 /*
5675 * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
5676 * The latter tries to derive the success status from the info flags
5677 * passed back from the driver. rawx_mit() saves the ni on the m and the
5678 * m on the skb for us to be able to give feedback to net80211.
5679 */
5680 static void
_lkpi_ieee80211_free_txskb(struct ieee80211_hw * hw,struct sk_buff * skb,int status)5681 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5682 int status)
5683 {
5684 struct ieee80211_node *ni;
5685 struct mbuf *m;
5686
5687 m = skb->m;
5688 skb->m = NULL;
5689
5690 if (m != NULL) {
5691 ni = m->m_pkthdr.PH_loc.ptr;
5692 /* Status: 0 is ok, != 0 is error. */
5693 ieee80211_tx_complete(ni, m, status);
5694 /* ni & mbuf were consumed. */
5695 }
5696 }
5697
5698 void
linuxkpi_ieee80211_free_txskb(struct ieee80211_hw * hw,struct sk_buff * skb,int status)5699 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5700 int status)
5701 {
5702
5703 _lkpi_ieee80211_free_txskb(hw, skb, status);
5704 kfree_skb(skb);
5705 }
5706
5707 void
linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw * hw,struct ieee80211_tx_status * txstat)5708 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5709 struct ieee80211_tx_status *txstat)
5710 {
5711 struct sk_buff *skb;
5712 struct ieee80211_tx_info *info;
5713 struct ieee80211_ratectl_tx_status txs;
5714 struct ieee80211_node *ni;
5715 int status;
5716
5717 skb = txstat->skb;
5718 if (skb->m != NULL) {
5719 struct mbuf *m;
5720
5721 m = skb->m;
5722 ni = m->m_pkthdr.PH_loc.ptr;
5723 memset(&txs, 0, sizeof(txs));
5724 } else {
5725 ni = NULL;
5726 }
5727
5728 info = txstat->info;
5729 if (info->flags & IEEE80211_TX_STAT_ACK) {
5730 status = 0; /* No error. */
5731 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5732 } else {
5733 status = 1;
5734 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5735 }
5736
5737 if (ni != NULL) {
5738 int ridx __unused;
5739 #ifdef LINUXKPI_DEBUG_80211
5740 int old_rate;
5741
5742 old_rate = ni->ni_vap->iv_bss->ni_txrate;
5743 #endif
5744 txs.pktlen = skb->len;
5745 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5746 if (info->status.rates[0].count > 1) {
5747 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */
5748 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5749 }
5750 #if 0 /* Unused in net80211 currently. */
5751 /* XXX-BZ convert check .flags for MCS/VHT/.. */
5752 txs.final_rate = info->status.rates[0].idx;
5753 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5754 #endif
5755 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5756 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */
5757 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5758 }
5759
5760 IMPROVE("only update of rate matches but that requires us to get a proper rate");
5761 ieee80211_ratectl_tx_complete(ni, &txs);
5762 ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5763
5764 #ifdef LINUXKPI_DEBUG_80211
5765 if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5766 printf("TX-RATE: %s: old %d new %d ridx %d, "
5767 "long_retries %d\n", __func__,
5768 old_rate, ni->ni_vap->iv_bss->ni_txrate,
5769 ridx, txs.long_retries);
5770 }
5771 #endif
5772 }
5773
5774 #ifdef LINUXKPI_DEBUG_80211
5775 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5776 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5777 "band %u hw_queue %u tx_time_est %d : "
5778 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5779 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5780 "tx_time %u flags %#x "
5781 "status_driver_data [ %p %p ]\n",
5782 __func__, hw, skb, status, info->flags,
5783 info->band, info->hw_queue, info->tx_time_est,
5784 info->status.rates[0].idx, info->status.rates[0].count,
5785 info->status.rates[0].flags,
5786 info->status.rates[1].idx, info->status.rates[1].count,
5787 info->status.rates[1].flags,
5788 info->status.rates[2].idx, info->status.rates[2].count,
5789 info->status.rates[2].flags,
5790 info->status.rates[3].idx, info->status.rates[3].count,
5791 info->status.rates[3].flags,
5792 info->status.ack_signal, info->status.ampdu_ack_len,
5793 info->status.ampdu_len, info->status.antenna,
5794 info->status.tx_time, info->status.flags,
5795 info->status.status_driver_data[0],
5796 info->status.status_driver_data[1]);
5797 #endif
5798
5799 if (txstat->free_list) {
5800 _lkpi_ieee80211_free_txskb(hw, skb, status);
5801 list_add_tail(&skb->list, txstat->free_list);
5802 } else {
5803 linuxkpi_ieee80211_free_txskb(hw, skb, status);
5804 }
5805 }
5806
5807 void
linuxkpi_ieee80211_tx_status(struct ieee80211_hw * hw,struct sk_buff * skb)5808 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5809 {
5810 struct ieee80211_tx_status status;
5811
5812 memset(&status, 0, sizeof(status));
5813 status.info = IEEE80211_SKB_CB(skb);
5814 status.skb = skb;
5815 /* sta, n_rates, rates, free_list? */
5816
5817 ieee80211_tx_status_ext(hw, &status);
5818 }
5819
5820 /*
5821 * This is an internal bandaid for the moment for the way we glue
5822 * skbs and mbufs together for TX. Once we have skbs backed by
5823 * mbufs this should go away.
5824 * This is a public function but kept on the private KPI (lkpi_)
5825 * and is not exposed by a header file.
5826 */
5827 static void
lkpi_ieee80211_free_skb_mbuf(void * p)5828 lkpi_ieee80211_free_skb_mbuf(void *p)
5829 {
5830 struct ieee80211_node *ni;
5831 struct mbuf *m;
5832
5833 if (p == NULL)
5834 return;
5835
5836 m = (struct mbuf *)p;
5837 M_ASSERTPKTHDR(m);
5838
5839 ni = m->m_pkthdr.PH_loc.ptr;
5840 m->m_pkthdr.PH_loc.ptr = NULL;
5841 if (ni != NULL)
5842 ieee80211_free_node(ni);
5843 m_freem(m);
5844 }
5845
5846 void
linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw * hw,struct delayed_work * w,int delay)5847 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
5848 struct delayed_work *w, int delay)
5849 {
5850 struct lkpi_hw *lhw;
5851
5852 /* Need to make sure hw is in a stable (non-suspended) state. */
5853 IMPROVE();
5854
5855 lhw = HW_TO_LHW(hw);
5856 queue_delayed_work(lhw->workq, w, delay);
5857 }
5858
5859 void
linuxkpi_ieee80211_queue_work(struct ieee80211_hw * hw,struct work_struct * w)5860 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
5861 struct work_struct *w)
5862 {
5863 struct lkpi_hw *lhw;
5864
5865 /* Need to make sure hw is in a stable (non-suspended) state. */
5866 IMPROVE();
5867
5868 lhw = HW_TO_LHW(hw);
5869 queue_work(lhw->workq, w);
5870 }
5871
5872 struct sk_buff *
linuxkpi_ieee80211_probereq_get(struct ieee80211_hw * hw,uint8_t * addr,uint8_t * ssid,size_t ssid_len,size_t tailroom)5873 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
5874 uint8_t *ssid, size_t ssid_len, size_t tailroom)
5875 {
5876 struct sk_buff *skb;
5877 struct ieee80211_frame *wh;
5878 uint8_t *p;
5879 size_t len;
5880
5881 len = sizeof(*wh);
5882 len += 2 + ssid_len;
5883
5884 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
5885 if (skb == NULL)
5886 return (NULL);
5887
5888 skb_reserve(skb, hw->extra_tx_headroom);
5889
5890 wh = skb_put_zero(skb, sizeof(*wh));
5891 wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
5892 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
5893 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
5894 IEEE80211_ADDR_COPY(wh->i_addr2, addr);
5895 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
5896
5897 p = skb_put(skb, 2 + ssid_len);
5898 *p++ = IEEE80211_ELEMID_SSID;
5899 *p++ = ssid_len;
5900 if (ssid_len > 0)
5901 memcpy(p, ssid, ssid_len);
5902
5903 return (skb);
5904 }
5905
5906 struct sk_buff *
linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5907 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
5908 struct ieee80211_vif *vif)
5909 {
5910 struct lkpi_vif *lvif;
5911 struct ieee80211vap *vap;
5912 struct sk_buff *skb;
5913 struct ieee80211_frame_pspoll *psp;
5914 uint16_t v;
5915
5916 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
5917 if (skb == NULL)
5918 return (NULL);
5919
5920 skb_reserve(skb, hw->extra_tx_headroom);
5921
5922 lvif = VIF_TO_LVIF(vif);
5923 vap = LVIF_TO_VAP(lvif);
5924
5925 psp = skb_put_zero(skb, sizeof(*psp));
5926 psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
5927 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
5928 v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
5929 memcpy(&psp->i_aid, &v, sizeof(v));
5930 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
5931 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
5932
5933 return (skb);
5934 }
5935
5936 struct sk_buff *
linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int linkid,bool qos)5937 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5938 struct ieee80211_vif *vif, int linkid, bool qos)
5939 {
5940 struct lkpi_vif *lvif;
5941 struct ieee80211vap *vap;
5942 struct sk_buff *skb;
5943 struct ieee80211_frame *nullf;
5944
5945 IMPROVE("linkid");
5946
5947 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
5948 if (skb == NULL)
5949 return (NULL);
5950
5951 skb_reserve(skb, hw->extra_tx_headroom);
5952
5953 lvif = VIF_TO_LVIF(vif);
5954 vap = LVIF_TO_VAP(lvif);
5955
5956 nullf = skb_put_zero(skb, sizeof(*nullf));
5957 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
5958 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
5959 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5960
5961 IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
5962 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
5963 IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
5964
5965 return (skb);
5966 }
5967
5968 struct wireless_dev *
linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif * vif)5969 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
5970 {
5971 struct lkpi_vif *lvif;
5972
5973 lvif = VIF_TO_LVIF(vif);
5974 return (&lvif->wdev);
5975 }
5976
5977 void
linuxkpi_ieee80211_connection_loss(struct ieee80211_vif * vif)5978 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
5979 {
5980 struct lkpi_vif *lvif;
5981 struct ieee80211vap *vap;
5982 enum ieee80211_state nstate;
5983 int arg;
5984
5985 lvif = VIF_TO_LVIF(vif);
5986 vap = LVIF_TO_VAP(lvif);
5987
5988 /*
5989 * Go to init; otherwise we need to elaborately check state and
5990 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
5991 * Let the statemachine handle all neccessary changes.
5992 */
5993 nstate = IEEE80211_S_INIT;
5994 arg = 0; /* Not a valid reason. */
5995
5996 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
5997 vif, vap, ieee80211_state_name[vap->iv_state]);
5998 ieee80211_new_state(vap, nstate, arg);
5999 }
6000
6001 void
linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif * vif)6002 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
6003 {
6004 struct lkpi_vif *lvif;
6005 struct ieee80211vap *vap;
6006
6007 lvif = VIF_TO_LVIF(vif);
6008 vap = LVIF_TO_VAP(lvif);
6009
6010 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6011 vif, vap, ieee80211_state_name[vap->iv_state]);
6012 ieee80211_beacon_miss(vap->iv_ic);
6013 }
6014
6015 /* -------------------------------------------------------------------------- */
6016
6017 void
linuxkpi_ieee80211_stop_queue(struct ieee80211_hw * hw,int qnum)6018 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
6019 {
6020 struct lkpi_hw *lhw;
6021 struct lkpi_vif *lvif;
6022 struct ieee80211_vif *vif;
6023 int ac_count, ac;
6024
6025 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
6026 __func__, qnum, hw->queues, hw));
6027
6028 lhw = wiphy_priv(hw->wiphy);
6029
6030 /* See lkpi_ic_vap_create(). */
6031 if (hw->queues >= IEEE80211_NUM_ACS)
6032 ac_count = IEEE80211_NUM_ACS;
6033 else
6034 ac_count = 1;
6035
6036 LKPI_80211_LHW_LVIF_LOCK(lhw);
6037 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6038
6039 vif = LVIF_TO_VIF(lvif);
6040 for (ac = 0; ac < ac_count; ac++) {
6041 IMPROVE_TXQ("LOCKING");
6042 if (qnum == vif->hw_queue[ac]) {
6043 #ifdef LINUXKPI_DEBUG_80211
6044 /*
6045 * For now log this to better understand
6046 * how this is supposed to work.
6047 */
6048 if (lvif->hw_queue_stopped[ac] &&
6049 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
6050 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
6051 "lvif %p vif %p ac %d qnum %d already "
6052 "stopped\n", __func__, __LINE__,
6053 lhw, hw, lvif, vif, ac, qnum);
6054 #endif
6055 lvif->hw_queue_stopped[ac] = true;
6056 }
6057 }
6058 }
6059 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6060 }
6061
6062 void
linuxkpi_ieee80211_stop_queues(struct ieee80211_hw * hw)6063 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
6064 {
6065 int i;
6066
6067 IMPROVE_TXQ("Locking; do we need further info?");
6068 for (i = 0; i < hw->queues; i++)
6069 linuxkpi_ieee80211_stop_queue(hw, i);
6070 }
6071
6072
6073 static void
lkpi_ieee80211_wake_queues(struct ieee80211_hw * hw,int hwq)6074 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
6075 {
6076 struct lkpi_hw *lhw;
6077 struct lkpi_vif *lvif;
6078 struct lkpi_sta *lsta;
6079 int ac_count, ac, tid;
6080
6081 /* See lkpi_ic_vap_create(). */
6082 if (hw->queues >= IEEE80211_NUM_ACS)
6083 ac_count = IEEE80211_NUM_ACS;
6084 else
6085 ac_count = 1;
6086
6087 lhw = wiphy_priv(hw->wiphy);
6088
6089 IMPROVE_TXQ("Locking");
6090 LKPI_80211_LHW_LVIF_LOCK(lhw);
6091 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6092 struct ieee80211_vif *vif;
6093
6094 vif = LVIF_TO_VIF(lvif);
6095 for (ac = 0; ac < ac_count; ac++) {
6096
6097 if (hwq == vif->hw_queue[ac]) {
6098
6099 /* XXX-BZ what about software scan? */
6100
6101 #ifdef LINUXKPI_DEBUG_80211
6102 /*
6103 * For now log this to better understand
6104 * how this is supposed to work.
6105 */
6106 if (!lvif->hw_queue_stopped[ac] &&
6107 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
6108 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
6109 "lvif %p vif %p ac %d hw_q not stopped\n",
6110 __func__, __LINE__,
6111 lhw, hw, lvif, vif, ac);
6112 #endif
6113 lvif->hw_queue_stopped[ac] = false;
6114
6115 LKPI_80211_LVIF_LOCK(lvif);
6116 TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
6117 struct ieee80211_sta *sta;
6118
6119 sta = LSTA_TO_STA(lsta);
6120 for (tid = 0; tid < nitems(sta->txq); tid++) {
6121 struct lkpi_txq *ltxq;
6122
6123 if (sta->txq[tid] == NULL)
6124 continue;
6125
6126 if (sta->txq[tid]->ac != ac)
6127 continue;
6128
6129 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
6130 if (!ltxq->stopped)
6131 continue;
6132
6133 ltxq->stopped = false;
6134
6135 /* XXX-BZ see when this explodes with all the locking. taskq? */
6136 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
6137 }
6138 }
6139 LKPI_80211_LVIF_UNLOCK(lvif);
6140 }
6141 }
6142 }
6143 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6144 }
6145
6146 void
linuxkpi_ieee80211_wake_queues(struct ieee80211_hw * hw)6147 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
6148 {
6149 int i;
6150
6151 IMPROVE_TXQ("Is this all/enough here?");
6152 for (i = 0; i < hw->queues; i++)
6153 lkpi_ieee80211_wake_queues(hw, i);
6154 }
6155
6156 void
linuxkpi_ieee80211_wake_queue(struct ieee80211_hw * hw,int qnum)6157 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
6158 {
6159
6160 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
6161 __func__, qnum, hw->queues, hw));
6162
6163 lkpi_ieee80211_wake_queues(hw, qnum);
6164 }
6165
6166 /* This is just hardware queues. */
6167 void
linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw * hw,uint8_t ac)6168 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
6169 {
6170 struct lkpi_hw *lhw;
6171
6172 lhw = HW_TO_LHW(hw);
6173
6174 IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
6175 IMPROVE_TXQ("LOCKING");
6176 if (++lhw->txq_generation[ac] == 0)
6177 lhw->txq_generation[ac]++;
6178 }
6179
6180 struct ieee80211_txq *
linuxkpi_ieee80211_next_txq(struct ieee80211_hw * hw,uint8_t ac)6181 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
6182 {
6183 struct lkpi_hw *lhw;
6184 struct ieee80211_txq *txq;
6185 struct lkpi_txq *ltxq;
6186
6187 lhw = HW_TO_LHW(hw);
6188 txq = NULL;
6189
6190 IMPROVE_TXQ("LOCKING");
6191
6192 /* Check that we are scheduled. */
6193 if (lhw->txq_generation[ac] == 0)
6194 goto out;
6195
6196 ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
6197 if (ltxq == NULL)
6198 goto out;
6199 if (ltxq->txq_generation == lhw->txq_generation[ac])
6200 goto out;
6201
6202 ltxq->txq_generation = lhw->txq_generation[ac];
6203 TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
6204 txq = <xq->txq;
6205 TAILQ_ELEM_INIT(ltxq, txq_entry);
6206
6207 out:
6208 return (txq);
6209 }
6210
linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw * hw,struct ieee80211_txq * txq,bool withoutpkts)6211 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
6212 struct ieee80211_txq *txq, bool withoutpkts)
6213 {
6214 struct lkpi_hw *lhw;
6215 struct lkpi_txq *ltxq;
6216 bool ltxq_empty;
6217
6218 ltxq = TXQ_TO_LTXQ(txq);
6219
6220 IMPROVE_TXQ("LOCKING");
6221
6222 /* Only schedule if work to do or asked to anyway. */
6223 LKPI_80211_LTXQ_LOCK(ltxq);
6224 ltxq_empty = skb_queue_empty(<xq->skbq);
6225 LKPI_80211_LTXQ_UNLOCK(ltxq);
6226 if (!withoutpkts && ltxq_empty)
6227 goto out;
6228
6229 /*
6230 * Make sure we do not double-schedule. We do this by checking tqe_prev,
6231 * the previous entry in our tailq. tqe_prev is always valid if this entry
6232 * is queued, tqe_next may be NULL if this is the only element in the list.
6233 */
6234 if (ltxq->txq_entry.tqe_prev != NULL)
6235 goto out;
6236
6237 lhw = HW_TO_LHW(hw);
6238 TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
6239 out:
6240 return;
6241 }
6242
6243 void
linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)6244 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
6245 struct ieee80211_txq *txq)
6246 {
6247 struct lkpi_hw *lhw;
6248 struct ieee80211_txq *ntxq;
6249 struct ieee80211_tx_control control;
6250 struct sk_buff *skb;
6251
6252 lhw = HW_TO_LHW(hw);
6253
6254 LKPI_80211_LHW_TXQ_LOCK(lhw);
6255 ieee80211_txq_schedule_start(hw, txq->ac);
6256 do {
6257 ntxq = ieee80211_next_txq(hw, txq->ac);
6258 if (ntxq == NULL)
6259 break;
6260
6261 memset(&control, 0, sizeof(control));
6262 control.sta = ntxq->sta;
6263 do {
6264 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6265 if (skb == NULL)
6266 break;
6267 lkpi_80211_mo_tx(hw, &control, skb);
6268 } while(1);
6269
6270 ieee80211_return_txq(hw, ntxq, false);
6271 } while (1);
6272 ieee80211_txq_schedule_end(hw, txq->ac);
6273 LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6274 }
6275
6276 /* -------------------------------------------------------------------------- */
6277
6278 struct lkpi_cfg80211_bss {
6279 u_int refcnt;
6280 struct cfg80211_bss bss;
6281 };
6282
6283 struct lkpi_cfg80211_get_bss_iter_lookup {
6284 struct wiphy *wiphy;
6285 struct linuxkpi_ieee80211_channel *chan;
6286 const uint8_t *bssid;
6287 const uint8_t *ssid;
6288 size_t ssid_len;
6289 enum ieee80211_bss_type bss_type;
6290 enum ieee80211_privacy privacy;
6291
6292 /*
6293 * Something to store a copy of the result as the net80211 scan cache
6294 * is not refoucnted so a scan entry might go away any time.
6295 */
6296 bool match;
6297 struct cfg80211_bss *bss;
6298 };
6299
6300 static void
lkpi_cfg80211_get_bss_iterf(void * arg,const struct ieee80211_scan_entry * se)6301 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
6302 {
6303 struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
6304 size_t ielen;
6305
6306 lookup = arg;
6307
6308 /* Do not try to find another match. */
6309 if (lookup->match)
6310 return;
6311
6312 /* Nothing to store result. */
6313 if (lookup->bss == NULL)
6314 return;
6315
6316 if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
6317 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
6318 /* We have no idea what to compare to as the drivers only request ANY */
6319 return;
6320 }
6321
6322 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
6323 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
6324 /* We have no idea what to compare to as the drivers only request ANY */
6325 return;
6326 }
6327
6328 if (lookup->chan != NULL) {
6329 struct linuxkpi_ieee80211_channel *chan;
6330
6331 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
6332 se->se_chan->ic_freq);
6333 if (chan == NULL || chan != lookup->chan)
6334 return;
6335 }
6336
6337 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
6338 return;
6339
6340 if (lookup->ssid) {
6341 if (lookup->ssid_len != se->se_ssid[1] ||
6342 se->se_ssid[1] == 0)
6343 return;
6344 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
6345 return;
6346 }
6347
6348 ielen = se->se_ies.len;
6349
6350 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
6351 M_LKPI80211, M_NOWAIT | M_ZERO);
6352 if (lookup->bss->ies == NULL)
6353 return;
6354
6355 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
6356 lookup->bss->ies->len = ielen;
6357 if (ielen)
6358 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
6359
6360 lookup->match = true;
6361 }
6362
6363 struct cfg80211_bss *
linuxkpi_cfg80211_get_bss(struct wiphy * wiphy,struct linuxkpi_ieee80211_channel * chan,const uint8_t * bssid,const uint8_t * ssid,size_t ssid_len,enum ieee80211_bss_type bss_type,enum ieee80211_privacy privacy)6364 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
6365 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
6366 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
6367 {
6368 struct lkpi_cfg80211_bss *lbss;
6369 struct lkpi_cfg80211_get_bss_iter_lookup lookup;
6370 struct lkpi_hw *lhw;
6371 struct ieee80211vap *vap;
6372
6373 lhw = wiphy_priv(wiphy);
6374
6375 /* Let's hope we can alloc. */
6376 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
6377 if (lbss == NULL) {
6378 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
6379 return (NULL);
6380 }
6381
6382 lookup.wiphy = wiphy;
6383 lookup.chan = chan;
6384 lookup.bssid = bssid;
6385 lookup.ssid = ssid;
6386 lookup.ssid_len = ssid_len;
6387 lookup.bss_type = bss_type;
6388 lookup.privacy = privacy;
6389 lookup.match = false;
6390 lookup.bss = &lbss->bss;
6391
6392 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
6393 vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
6394 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
6395 if (!lookup.match) {
6396 free(lbss, M_LKPI80211);
6397 return (NULL);
6398 }
6399
6400 refcount_init(&lbss->refcnt, 1);
6401 return (&lbss->bss);
6402 }
6403
6404 void
linuxkpi_cfg80211_put_bss(struct wiphy * wiphy,struct cfg80211_bss * bss)6405 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
6406 {
6407 struct lkpi_cfg80211_bss *lbss;
6408
6409 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
6410
6411 /* Free everything again on refcount ... */
6412 if (refcount_release(&lbss->refcnt)) {
6413 free(lbss->bss.ies, M_LKPI80211);
6414 free(lbss, M_LKPI80211);
6415 }
6416 }
6417
6418 void
linuxkpi_cfg80211_bss_flush(struct wiphy * wiphy)6419 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
6420 {
6421 struct lkpi_hw *lhw;
6422 struct ieee80211com *ic;
6423 struct ieee80211vap *vap;
6424
6425 lhw = wiphy_priv(wiphy);
6426 ic = lhw->ic;
6427
6428 /*
6429 * If we haven't called ieee80211_ifattach() yet
6430 * or there is no VAP, there are no scans to flush.
6431 */
6432 if (ic == NULL ||
6433 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
6434 return;
6435
6436 /* Should only happen on the current one? Not seen it late enough. */
6437 IEEE80211_LOCK(ic);
6438 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6439 ieee80211_scan_flush(vap);
6440 IEEE80211_UNLOCK(ic);
6441 }
6442
6443 /* -------------------------------------------------------------------------- */
6444
6445 /*
6446 * hw->conf get initialized/set in various places for us:
6447 * - linuxkpi_ieee80211_alloc_hw(): flags
6448 * - linuxkpi_ieee80211_ifattach(): chandef
6449 * - lkpi_ic_vap_create(): listen_interval
6450 * - lkpi_ic_set_channel(): chandef, flags
6451 */
6452
lkpi_80211_update_chandef(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * new)6453 int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
6454 struct ieee80211_chanctx_conf *new)
6455 {
6456 struct cfg80211_chan_def *cd;
6457 uint32_t changed;
6458 int error;
6459
6460 changed = 0;
6461 if (new == NULL || new->def.chan == NULL)
6462 cd = NULL;
6463 else
6464 cd = &new->def;
6465
6466 if (cd && cd->chan != hw->conf.chandef.chan) {
6467 /* Copy; the chan pointer is fine and will stay valid. */
6468 hw->conf.chandef = *cd;
6469 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
6470 }
6471 IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
6472
6473 if (changed == 0)
6474 return (0);
6475
6476 error = lkpi_80211_mo_config(hw, changed);
6477 return (error);
6478 }
6479
6480 /* -------------------------------------------------------------------------- */
6481
6482 MODULE_VERSION(linuxkpi_wlan, 1);
6483 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
6484 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6485