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