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