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