1 /*- 2 * Copyright (c) 2020-2023 The FreeBSD Foundation 3 * Copyright (c) 2020-2022 Bjoern A. Zeeb 4 * 5 * This software was developed by Björn Zeeb under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * Public functions are called linuxkpi_*(). 32 * Internal (static) functions are called lkpi_*(). 33 * 34 * The internal structures holding metadata over public structures are also 35 * called lkpi_xxx (usually with a member at the end called xxx). 36 * Note: we do not replicate the structure names but the general variable names 37 * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta). 38 * There are macros to access one from the other. 39 * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta). 40 */ 41 42 #include <sys/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 %#08x\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 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 %#08x\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 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 return (EBUSY); 1185 } 1186 LKPI_80211_LVIF_UNLOCK(lvif); 1187 1188 IEEE80211_UNLOCK(vap->iv_ic); 1189 LKPI_80211_LHW_LOCK(lhw); 1190 1191 /* Add chanctx (or if exists, change it). */ 1192 if (vif->chanctx_conf != NULL) { 1193 chanctx_conf = vif->chanctx_conf; 1194 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf); 1195 IMPROVE("diff changes for changed, working on live copy, rcu"); 1196 } else { 1197 /* Keep separate alloc as in Linux this is rcu managed? */ 1198 lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size, 1199 M_LKPI80211, M_WAITOK | M_ZERO); 1200 chanctx_conf = &lchanctx->chanctx_conf; 1201 } 1202 1203 chanctx_conf->rx_chains_dynamic = 1; 1204 chanctx_conf->rx_chains_static = 1; 1205 chanctx_conf->radar_enabled = 1206 (chan->flags & IEEE80211_CHAN_RADAR) ? true : false; 1207 chanctx_conf->def.chan = chan; 1208 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT; 1209 chanctx_conf->def.center_freq1 = chan->center_freq; 1210 chanctx_conf->def.center_freq2 = 0; 1211 IMPROVE("Check vht_cap from band not just chan?"); 1212 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC, 1213 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan)); 1214 #ifdef LKPI_80211_HT 1215 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 1216 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) { 1217 chanctx_conf->def.width = NL80211_CHAN_WIDTH_40; 1218 } else 1219 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20; 1220 } 1221 #endif 1222 #ifdef LKPI_80211_VHT 1223 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) { 1224 #ifdef __notyet__ 1225 if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) { 1226 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80; 1227 chanctx_conf->def.center_freq2 = 0; /* XXX */ 1228 } else 1229 #endif 1230 if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan)) 1231 chanctx_conf->def.width = NL80211_CHAN_WIDTH_160; 1232 else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan)) 1233 chanctx_conf->def.width = NL80211_CHAN_WIDTH_80; 1234 } 1235 #endif 1236 /* Responder ... */ 1237 chanctx_conf->min_def.chan = chan; 1238 chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT; 1239 chanctx_conf->min_def.center_freq1 = chan->center_freq; 1240 chanctx_conf->min_def.center_freq2 = 0; 1241 IMPROVE("currently 20_NOHT min_def only"); 1242 1243 /* Set bss info (bss_info_changed). */ 1244 bss_changed = 0; 1245 vif->bss_conf.bssid = ni->ni_bssid; 1246 bss_changed |= BSS_CHANGED_BSSID; 1247 vif->bss_conf.txpower = ni->ni_txpower; 1248 bss_changed |= BSS_CHANGED_TXPOWER; 1249 vif->cfg.idle = false; 1250 bss_changed |= BSS_CHANGED_IDLE; 1251 1252 /* vif->bss_conf.basic_rates ? Where exactly? */ 1253 1254 /* Should almost assert it is this. */ 1255 vif->cfg.assoc = false; 1256 vif->cfg.aid = 0; 1257 1258 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__); 1259 1260 error = 0; 1261 if (vif->chanctx_conf != NULL) { 1262 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH; 1263 changed |= IEEE80211_CHANCTX_CHANGE_RADAR; 1264 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS; 1265 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH; 1266 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed); 1267 } else { 1268 error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf); 1269 if (error == 0 || error == EOPNOTSUPP) { 1270 vif->bss_conf.chandef.chan = chanctx_conf->def.chan; 1271 vif->bss_conf.chandef.width = chanctx_conf->def.width; 1272 vif->bss_conf.chandef.center_freq1 = 1273 chanctx_conf->def.center_freq1; 1274 #ifdef LKPI_80211_HT 1275 if (vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_40) { 1276 /* Note: it is 10 not 20. */ 1277 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan)) 1278 vif->bss_conf.chandef.center_freq1 += 10; 1279 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan)) 1280 vif->bss_conf.chandef.center_freq1 -= 10; 1281 } 1282 #endif 1283 vif->bss_conf.chandef.center_freq2 = 1284 chanctx_conf->def.center_freq2; 1285 } else { 1286 ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx " 1287 "failed: %d\n", __func__, __LINE__, error); 1288 goto out; 1289 } 1290 1291 vif->bss_conf.chanctx_conf = chanctx_conf; 1292 1293 /* Assign vif chanctx. */ 1294 if (error == 0) 1295 error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, 1296 &vif->bss_conf, chanctx_conf); 1297 if (error == EOPNOTSUPP) 1298 error = 0; 1299 if (error != 0) { 1300 ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx " 1301 "failed: %d\n", __func__, __LINE__, error); 1302 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf); 1303 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf); 1304 free(lchanctx, M_LKPI80211); 1305 goto out; 1306 } 1307 } 1308 IMPROVE("update radiotap chan fields too"); 1309 1310 /* RATES */ 1311 IMPROVE("bss info: not all needs to come now and rates are missing"); 1312 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed); 1313 1314 /* 1315 * Given ni and lsta are 1:1 from alloc to free we can assert that 1316 * ni always has lsta data attach despite net80211 node swapping 1317 * under the hoods. 1318 */ 1319 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n", 1320 __func__, ni, ni->ni_drv_data)); 1321 lsta = ni->ni_drv_data; 1322 1323 /* 1324 * Make sure in case the sta did not change and we re-add it, 1325 * that we can tx again. 1326 */ 1327 LKPI_80211_LSTA_TXQ_LOCK(lsta); 1328 lsta->txq_ready = true; 1329 LKPI_80211_LSTA_TXQ_UNLOCK(lsta); 1330 1331 LKPI_80211_LVIF_LOCK(lvif); 1332 /* Insert the [l]sta into the list of known stations. */ 1333 TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry); 1334 LKPI_80211_LVIF_UNLOCK(lvif); 1335 1336 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */ 1337 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 1338 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not " 1339 "NOTEXIST: %#x\n", __func__, lsta, lsta->state)); 1340 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE); 1341 if (error != 0) { 1342 IMPROVE("do we need to undo the chan ctx?"); 1343 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) " 1344 "failed: %d\n", __func__, __LINE__, error); 1345 goto out; 1346 } 1347 #if 0 1348 lsta->added_to_drv = true; /* mo manages. */ 1349 #endif 1350 1351 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1352 1353 #if 0 1354 /* 1355 * Wakeup all queues now that sta is there so we have as much time to 1356 * possibly prepare the queue in the driver to be ready for the 1st 1357 * packet; lkpi_80211_txq_tx_one() still has a workaround as there 1358 * is no guarantee or way to check. 1359 * XXX-BZ and by now we know that this does not work on all drivers 1360 * for all queues. 1361 */ 1362 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false); 1363 #endif 1364 1365 /* Start mgd_prepare_tx. */ 1366 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1367 prep_tx_info.duration = PREP_TX_INFO_DURATION; 1368 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 1369 lsta->in_mgd = true; 1370 1371 /* 1372 * What is going to happen next: 1373 * - <twiddle> .. we should end up in "auth_to_assoc" 1374 * - event_callback 1375 * - update sta_state (NONE to AUTH) 1376 * - mgd_complete_tx 1377 * (ideally we'd do that on a callback for something else ...) 1378 */ 1379 1380 LKPI_80211_LHW_UNLOCK(lhw); 1381 IEEE80211_LOCK(vap->iv_ic); 1382 1383 LKPI_80211_LVIF_LOCK(lvif); 1384 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */ 1385 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL || 1386 lsta->ni != vap->iv_bss) 1387 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1388 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__, 1389 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1390 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1391 lvif->lvif_bss_synched, ni, lsta); 1392 1393 /* 1394 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss. 1395 * Given we cache lsta we use lsta->ni instead of ni here (even though 1396 * lsta->ni == ni) to be distinct from the rest of the code where we do 1397 * assume that ni == vap->iv_bss which it may or may not be. 1398 * So do NOT use iv_bss here anymore as that may have diverged from our 1399 * function local ni already while ic was unlocked and would lead to 1400 * inconsistencies. Go and see if we lost a race and do not update 1401 * lvif_bss_synched in that case. 1402 */ 1403 ieee80211_ref_node(lsta->ni); 1404 lvif->lvif_bss = lsta; 1405 if (lsta->ni == vap->iv_bss) { 1406 lvif->lvif_bss_synched = true; 1407 } else { 1408 /* Set to un-synched no matter what. */ 1409 lvif->lvif_bss_synched = false; 1410 /* 1411 * We do not error as someone has to take us down. 1412 * If we are followed by a 2nd, new net80211::join1() going to 1413 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}() 1414 * will take the lvif->lvif_bss node down eventually. 1415 * What happens with the vap->iv_bss node will entirely be up 1416 * to net80211 as we never used the node beyond alloc()/free() 1417 * and we do not hold an extra reference for that anymore given 1418 * ni : lsta == 1:1. 1419 */ 1420 } 1421 LKPI_80211_LVIF_UNLOCK(lvif); 1422 goto out_relocked; 1423 1424 out: 1425 LKPI_80211_LHW_UNLOCK(lhw); 1426 IEEE80211_LOCK(vap->iv_ic); 1427 out_relocked: 1428 /* 1429 * Release the reference that kept the ni stable locally 1430 * during the work of this function. 1431 */ 1432 if (ni != NULL) 1433 ieee80211_free_node(ni); 1434 return (error); 1435 } 1436 1437 static int 1438 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1439 { 1440 struct lkpi_hw *lhw; 1441 struct ieee80211_hw *hw; 1442 struct lkpi_vif *lvif; 1443 struct ieee80211_vif *vif; 1444 struct ieee80211_node *ni; 1445 struct lkpi_sta *lsta; 1446 struct ieee80211_sta *sta; 1447 struct ieee80211_prep_tx_info prep_tx_info; 1448 int error; 1449 1450 lhw = vap->iv_ic->ic_softc; 1451 hw = LHW_TO_HW(lhw); 1452 lvif = VAP_TO_LVIF(vap); 1453 vif = LVIF_TO_VIF(lvif); 1454 1455 LKPI_80211_LVIF_LOCK(lvif); 1456 #ifdef LINUXKPI_DEBUG_80211 1457 /* XXX-BZ KASSERT later; state going down so no action. */ 1458 if (lvif->lvif_bss == NULL) 1459 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1460 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 1461 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1462 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1463 lvif->lvif_bss_synched); 1464 #endif 1465 1466 lsta = lvif->lvif_bss; 1467 LKPI_80211_LVIF_UNLOCK(lvif); 1468 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p " 1469 "lvif %p vap %p\n", __func__, 1470 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap)); 1471 ni = lsta->ni; /* Reference held for lvif_bss. */ 1472 sta = LSTA_TO_STA(lsta); 1473 1474 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1475 1476 IEEE80211_UNLOCK(vap->iv_ic); 1477 LKPI_80211_LHW_LOCK(lhw); 1478 1479 /* flush, drop. */ 1480 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true); 1481 1482 /* Wake tx queues to get packet(s) out. */ 1483 lkpi_wake_tx_queues(hw, sta, false, true); 1484 1485 /* flush, no drop */ 1486 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false); 1487 1488 /* End mgd_complete_tx. */ 1489 if (lsta->in_mgd) { 1490 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1491 prep_tx_info.success = false; 1492 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 1493 lsta->in_mgd = false; 1494 } 1495 1496 /* sync_rx_queues */ 1497 lkpi_80211_mo_sync_rx_queues(hw); 1498 1499 /* sta_pre_rcu_remove */ 1500 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta); 1501 1502 /* Take the station down. */ 1503 1504 /* Adjust sta and change state (from NONE) to NOTEXIST. */ 1505 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 1506 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not " 1507 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg)); 1508 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST); 1509 if (error != 0) { 1510 IMPROVE("do we need to undo the chan ctx?"); 1511 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) " 1512 "failed: %d\n", __func__, __LINE__, error); 1513 goto out; 1514 } 1515 #if 0 1516 lsta->added_to_drv = false; /* mo manages. */ 1517 #endif 1518 1519 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1520 1521 LKPI_80211_LVIF_LOCK(lvif); 1522 /* Remove ni reference for this cache of lsta. */ 1523 lvif->lvif_bss = NULL; 1524 lvif->lvif_bss_synched = false; 1525 LKPI_80211_LVIF_UNLOCK(lvif); 1526 lkpi_lsta_remove(lsta, lvif); 1527 /* 1528 * The very last release the reference on the ni for the ni/lsta on 1529 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid 1530 * and potentially freed. 1531 */ 1532 ieee80211_free_node(ni); 1533 1534 /* conf_tx */ 1535 1536 /* Take the chan ctx down. */ 1537 if (vif->chanctx_conf != NULL) { 1538 struct lkpi_chanctx *lchanctx; 1539 struct ieee80211_chanctx_conf *chanctx_conf; 1540 1541 chanctx_conf = vif->chanctx_conf; 1542 /* Remove vif context. */ 1543 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf); 1544 /* NB: vif->chanctx_conf is NULL now. */ 1545 1546 lkpi_hw_conf_idle(hw, true); 1547 1548 /* Remove chan ctx. */ 1549 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf); 1550 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf); 1551 free(lchanctx, M_LKPI80211); 1552 } 1553 1554 out: 1555 LKPI_80211_LHW_UNLOCK(lhw); 1556 IEEE80211_LOCK(vap->iv_ic); 1557 return (error); 1558 } 1559 1560 static int 1561 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1562 { 1563 int error; 1564 1565 error = lkpi_sta_auth_to_scan(vap, nstate, arg); 1566 if (error == 0) 1567 error = lkpi_sta_scan_to_init(vap, nstate, arg); 1568 return (error); 1569 } 1570 1571 static int 1572 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1573 { 1574 struct lkpi_hw *lhw; 1575 struct ieee80211_hw *hw; 1576 struct lkpi_vif *lvif; 1577 struct ieee80211_vif *vif; 1578 struct lkpi_sta *lsta; 1579 struct ieee80211_prep_tx_info prep_tx_info; 1580 int error; 1581 1582 lhw = vap->iv_ic->ic_softc; 1583 hw = LHW_TO_HW(lhw); 1584 lvif = VAP_TO_LVIF(vap); 1585 vif = LVIF_TO_VIF(lvif); 1586 1587 IEEE80211_UNLOCK(vap->iv_ic); 1588 LKPI_80211_LHW_LOCK(lhw); 1589 1590 LKPI_80211_LVIF_LOCK(lvif); 1591 /* XXX-BZ KASSERT later? */ 1592 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) { 1593 #ifdef LINUXKPI_DEBUG_80211 1594 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1595 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 1596 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1597 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1598 lvif->lvif_bss_synched); 1599 #endif 1600 error = ENOTRECOVERABLE; 1601 LKPI_80211_LVIF_UNLOCK(lvif); 1602 goto out; 1603 } 1604 lsta = lvif->lvif_bss; 1605 LKPI_80211_LVIF_UNLOCK(lvif); 1606 1607 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta)); 1608 1609 /* Finish auth. */ 1610 IMPROVE("event callback"); 1611 1612 /* Update sta_state (NONE to AUTH). */ 1613 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not " 1614 "NONE: %#x\n", __func__, lsta, lsta->state)); 1615 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH); 1616 if (error != 0) { 1617 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) " 1618 "failed: %d\n", __func__, __LINE__, error); 1619 goto out; 1620 } 1621 1622 /* End mgd_complete_tx. */ 1623 if (lsta->in_mgd) { 1624 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1625 prep_tx_info.success = true; 1626 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 1627 lsta->in_mgd = false; 1628 } 1629 1630 /* Now start assoc. */ 1631 1632 /* Start mgd_prepare_tx. */ 1633 if (!lsta->in_mgd) { 1634 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1635 prep_tx_info.duration = PREP_TX_INFO_DURATION; 1636 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 1637 lsta->in_mgd = true; 1638 } 1639 1640 /* Wake tx queue to get packet out. */ 1641 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true); 1642 1643 /* 1644 * <twiddle> .. we end up in "assoc_to_run" 1645 * - update sta_state (AUTH to ASSOC) 1646 * - conf_tx [all] 1647 * - bss_info_changed (assoc, aid, ssid, ..) 1648 * - change_chanctx (if needed) 1649 * - event_callback 1650 * - mgd_complete_tx 1651 */ 1652 1653 out: 1654 LKPI_80211_LHW_UNLOCK(lhw); 1655 IEEE80211_LOCK(vap->iv_ic); 1656 return (error); 1657 } 1658 1659 /* auth_to_auth, assoc_to_assoc. */ 1660 static int 1661 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1662 { 1663 struct lkpi_hw *lhw; 1664 struct ieee80211_hw *hw; 1665 struct lkpi_vif *lvif; 1666 struct ieee80211_vif *vif; 1667 struct lkpi_sta *lsta; 1668 struct ieee80211_prep_tx_info prep_tx_info; 1669 int error; 1670 1671 lhw = vap->iv_ic->ic_softc; 1672 hw = LHW_TO_HW(lhw); 1673 lvif = VAP_TO_LVIF(vap); 1674 vif = LVIF_TO_VIF(lvif); 1675 1676 IEEE80211_UNLOCK(vap->iv_ic); 1677 LKPI_80211_LHW_LOCK(lhw); 1678 1679 LKPI_80211_LVIF_LOCK(lvif); 1680 /* XXX-BZ KASSERT later? */ 1681 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) { 1682 #ifdef LINUXKPI_DEBUG_80211 1683 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1684 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 1685 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1686 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1687 lvif->lvif_bss_synched); 1688 #endif 1689 LKPI_80211_LVIF_UNLOCK(lvif); 1690 error = ENOTRECOVERABLE; 1691 goto out; 1692 } 1693 lsta = lvif->lvif_bss; 1694 LKPI_80211_LVIF_UNLOCK(lvif); 1695 1696 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__, 1697 lsta, lvif, vap)); 1698 1699 IMPROVE("event callback?"); 1700 1701 /* End mgd_complete_tx. */ 1702 if (lsta->in_mgd) { 1703 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1704 prep_tx_info.success = false; 1705 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 1706 lsta->in_mgd = false; 1707 } 1708 1709 /* Now start assoc. */ 1710 1711 /* Start mgd_prepare_tx. */ 1712 if (!lsta->in_mgd) { 1713 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1714 prep_tx_info.duration = PREP_TX_INFO_DURATION; 1715 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 1716 lsta->in_mgd = true; 1717 } 1718 1719 error = 0; 1720 out: 1721 LKPI_80211_LHW_UNLOCK(lhw); 1722 IEEE80211_LOCK(vap->iv_ic); 1723 1724 return (error); 1725 } 1726 1727 static int 1728 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1729 { 1730 struct lkpi_hw *lhw; 1731 struct ieee80211_hw *hw; 1732 struct lkpi_vif *lvif; 1733 struct ieee80211_vif *vif; 1734 struct ieee80211_node *ni; 1735 struct lkpi_sta *lsta; 1736 struct ieee80211_sta *sta; 1737 struct ieee80211_prep_tx_info prep_tx_info; 1738 enum ieee80211_bss_changed bss_changed; 1739 int error; 1740 1741 lhw = vap->iv_ic->ic_softc; 1742 hw = LHW_TO_HW(lhw); 1743 lvif = VAP_TO_LVIF(vap); 1744 vif = LVIF_TO_VIF(lvif); 1745 1746 IEEE80211_UNLOCK(vap->iv_ic); 1747 LKPI_80211_LHW_LOCK(lhw); 1748 1749 LKPI_80211_LVIF_LOCK(lvif); 1750 #ifdef LINUXKPI_DEBUG_80211 1751 /* XXX-BZ KASSERT later; state going down so no action. */ 1752 if (lvif->lvif_bss == NULL) 1753 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1754 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 1755 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1756 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1757 lvif->lvif_bss_synched); 1758 #endif 1759 lsta = lvif->lvif_bss; 1760 LKPI_80211_LVIF_UNLOCK(lvif); 1761 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p " 1762 "lvif %p vap %p\n", __func__, 1763 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap)); 1764 1765 ni = lsta->ni; /* Reference held for lvif_bss. */ 1766 sta = LSTA_TO_STA(lsta); 1767 1768 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1769 1770 /* flush, drop. */ 1771 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true); 1772 1773 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?"); 1774 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) && 1775 !lsta->in_mgd) { 1776 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1777 prep_tx_info.duration = PREP_TX_INFO_DURATION; 1778 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 1779 lsta->in_mgd = true; 1780 } 1781 1782 LKPI_80211_LHW_UNLOCK(lhw); 1783 IEEE80211_LOCK(vap->iv_ic); 1784 1785 /* Call iv_newstate first so we get potential DEAUTH packet out. */ 1786 error = lvif->iv_newstate(vap, nstate, arg); 1787 if (error != 0) { 1788 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) " 1789 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error); 1790 goto outni; 1791 } 1792 1793 IEEE80211_UNLOCK(vap->iv_ic); 1794 1795 /* Ensure the packets get out. */ 1796 lkpi_80211_flush_tx(lhw, lsta); 1797 1798 LKPI_80211_LHW_LOCK(lhw); 1799 1800 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1801 1802 /* Wake tx queues to get packet(s) out. */ 1803 lkpi_wake_tx_queues(hw, sta, false, true); 1804 1805 /* flush, no drop */ 1806 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false); 1807 1808 /* End mgd_complete_tx. */ 1809 if (lsta->in_mgd) { 1810 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 1811 prep_tx_info.success = false; 1812 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 1813 lsta->in_mgd = false; 1814 } 1815 1816 /* sync_rx_queues */ 1817 lkpi_80211_mo_sync_rx_queues(hw); 1818 1819 /* sta_pre_rcu_remove */ 1820 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta); 1821 1822 /* Take the station down. */ 1823 1824 /* Update sta and change state (from AUTH) to NONE. */ 1825 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 1826 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not " 1827 "AUTH: %#x\n", __func__, lsta, lsta->state)); 1828 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE); 1829 if (error != 0) { 1830 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) " 1831 "failed: %d\n", __func__, __LINE__, error); 1832 goto out; 1833 } 1834 1835 /* See comment in lkpi_sta_run_to_init(). */ 1836 bss_changed = 0; 1837 bss_changed |= lkpi_disassoc(sta, vif, lhw); 1838 1839 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 1840 1841 /* Adjust sta and change state (from NONE) to NOTEXIST. */ 1842 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 1843 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not " 1844 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg)); 1845 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST); 1846 if (error != 0) { 1847 IMPROVE("do we need to undo the chan ctx?"); 1848 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) " 1849 "failed: %d\n", __func__, __LINE__, error); 1850 goto out; 1851 } 1852 1853 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */ 1854 1855 IMPROVE("Any bss_info changes to announce?"); 1856 vif->bss_conf.qos = 0; 1857 bss_changed |= BSS_CHANGED_QOS; 1858 vif->cfg.ssid_len = 0; 1859 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid)); 1860 bss_changed |= BSS_CHANGED_BSSID; 1861 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed); 1862 1863 LKPI_80211_LVIF_LOCK(lvif); 1864 /* Remove ni reference for this cache of lsta. */ 1865 lvif->lvif_bss = NULL; 1866 lvif->lvif_bss_synched = false; 1867 LKPI_80211_LVIF_UNLOCK(lvif); 1868 lkpi_lsta_remove(lsta, lvif); 1869 /* 1870 * The very last release the reference on the ni for the ni/lsta on 1871 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid 1872 * and potentially freed. 1873 */ 1874 ieee80211_free_node(ni); 1875 1876 /* conf_tx */ 1877 1878 /* Take the chan ctx down. */ 1879 if (vif->chanctx_conf != NULL) { 1880 struct lkpi_chanctx *lchanctx; 1881 struct ieee80211_chanctx_conf *chanctx_conf; 1882 1883 chanctx_conf = vif->chanctx_conf; 1884 /* Remove vif context. */ 1885 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf); 1886 /* NB: vif->chanctx_conf is NULL now. */ 1887 1888 lkpi_hw_conf_idle(hw, true); 1889 1890 /* Remove chan ctx. */ 1891 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf); 1892 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf); 1893 free(lchanctx, M_LKPI80211); 1894 } 1895 1896 error = EALREADY; 1897 out: 1898 LKPI_80211_LHW_UNLOCK(lhw); 1899 IEEE80211_LOCK(vap->iv_ic); 1900 outni: 1901 return (error); 1902 } 1903 1904 static int 1905 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1906 { 1907 int error; 1908 1909 error = _lkpi_sta_assoc_to_down(vap, nstate, arg); 1910 if (error != 0 && error != EALREADY) 1911 return (error); 1912 1913 /* At this point iv_bss is long a new node! */ 1914 1915 error |= lkpi_sta_scan_to_auth(vap, nstate, 0); 1916 return (error); 1917 } 1918 1919 static int 1920 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1921 { 1922 int error; 1923 1924 error = _lkpi_sta_assoc_to_down(vap, nstate, arg); 1925 return (error); 1926 } 1927 1928 static int 1929 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1930 { 1931 int error; 1932 1933 error = _lkpi_sta_assoc_to_down(vap, nstate, arg); 1934 return (error); 1935 } 1936 1937 static int 1938 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1939 { 1940 struct lkpi_hw *lhw; 1941 struct ieee80211_hw *hw; 1942 struct lkpi_vif *lvif; 1943 struct ieee80211_vif *vif; 1944 struct ieee80211_node *ni; 1945 struct lkpi_sta *lsta; 1946 struct ieee80211_sta *sta; 1947 struct ieee80211_prep_tx_info prep_tx_info; 1948 enum ieee80211_bss_changed bss_changed; 1949 int error; 1950 1951 lhw = vap->iv_ic->ic_softc; 1952 hw = LHW_TO_HW(lhw); 1953 lvif = VAP_TO_LVIF(vap); 1954 vif = LVIF_TO_VIF(lvif); 1955 1956 IEEE80211_UNLOCK(vap->iv_ic); 1957 LKPI_80211_LHW_LOCK(lhw); 1958 1959 LKPI_80211_LVIF_LOCK(lvif); 1960 /* XXX-BZ KASSERT later? */ 1961 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) { 1962 #ifdef LINUXKPI_DEBUG_80211 1963 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 1964 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 1965 lvif, vap, vap->iv_bss, lvif->lvif_bss, 1966 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 1967 lvif->lvif_bss_synched); 1968 #endif 1969 LKPI_80211_LVIF_UNLOCK(lvif); 1970 error = ENOTRECOVERABLE; 1971 goto out; 1972 } 1973 lsta = lvif->lvif_bss; 1974 LKPI_80211_LVIF_UNLOCK(lvif); 1975 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p " 1976 "lvif %p vap %p\n", __func__, 1977 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap)); 1978 1979 ni = lsta->ni; /* Reference held for lvif_bss. */ 1980 1981 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, " 1982 "and to lesser extend ieee80211_notify_node_join"); 1983 1984 /* Finish assoc. */ 1985 /* Update sta_state (AUTH to ASSOC) and set aid. */ 1986 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not " 1987 "AUTH: %#x\n", __func__, lsta, lsta->state)); 1988 sta = LSTA_TO_STA(lsta); 1989 sta->aid = IEEE80211_NODE_AID(ni); 1990 #ifdef LKPI_80211_WME 1991 if (vap->iv_flags & IEEE80211_F_WME) 1992 sta->wme = true; 1993 #endif 1994 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC); 1995 if (error != 0) { 1996 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) " 1997 "failed: %d\n", __func__, __LINE__, error); 1998 goto out; 1999 } 2000 2001 IMPROVE("wme / conf_tx [all]"); 2002 2003 /* Update bss info (bss_info_changed) (assoc, aid, ..). */ 2004 bss_changed = 0; 2005 #ifdef LKPI_80211_WME 2006 bss_changed |= lkpi_wme_update(lhw, vap, true); 2007 #endif 2008 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) { 2009 vif->cfg.assoc = true; 2010 vif->cfg.aid = IEEE80211_NODE_AID(ni); 2011 bss_changed |= BSS_CHANGED_ASSOC; 2012 } 2013 /* We set SSID but this is not BSSID! */ 2014 vif->cfg.ssid_len = ni->ni_esslen; 2015 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen); 2016 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 2017 vif->bss_conf.use_short_preamble) { 2018 vif->bss_conf.use_short_preamble ^= 1; 2019 /* bss_changed |= BSS_CHANGED_??? */ 2020 } 2021 if ((vap->iv_flags & IEEE80211_F_SHSLOT) != 2022 vif->bss_conf.use_short_slot) { 2023 vif->bss_conf.use_short_slot ^= 1; 2024 /* bss_changed |= BSS_CHANGED_??? */ 2025 } 2026 if ((ni->ni_flags & IEEE80211_NODE_QOS) != 2027 vif->bss_conf.qos) { 2028 vif->bss_conf.qos ^= 1; 2029 bss_changed |= BSS_CHANGED_QOS; 2030 } 2031 2032 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__); 2033 2034 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed); 2035 2036 /* - change_chanctx (if needed) 2037 * - event_callback 2038 */ 2039 2040 /* End mgd_complete_tx. */ 2041 if (lsta->in_mgd) { 2042 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 2043 prep_tx_info.success = true; 2044 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 2045 lsta->in_mgd = false; 2046 } 2047 2048 lkpi_hw_conf_idle(hw, false); 2049 2050 /* 2051 * And then: 2052 * - (more packets)? 2053 * - set_key 2054 * - set_default_unicast_key 2055 * - set_key (?) 2056 * - ipv6_addr_change (?) 2057 */ 2058 /* Prepare_multicast && configure_filter. */ 2059 lhw->update_mc = true; 2060 lkpi_update_mcast_filter(vap->iv_ic, true); 2061 2062 if (!ieee80211_node_is_authorized(ni)) { 2063 IMPROVE("net80211 does not consider node authorized"); 2064 } 2065 2066 #if defined(LKPI_80211_HT) 2067 IMPROVE("Is this the right spot, has net80211 done all updates already?"); 2068 lkpi_sta_sync_ht_from_ni(sta, ni, NULL); 2069 #endif 2070 2071 /* Update sta_state (ASSOC to AUTHORIZED). */ 2072 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2073 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not " 2074 "ASSOC: %#x\n", __func__, lsta, lsta->state)); 2075 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED); 2076 if (error != 0) { 2077 IMPROVE("undo some changes?"); 2078 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) " 2079 "failed: %d\n", __func__, __LINE__, error); 2080 goto out; 2081 } 2082 2083 /* - drv_config (?) 2084 * - bss_info_changed 2085 * - set_rekey_data (?) 2086 * 2087 * And now we should be passing packets. 2088 */ 2089 IMPROVE("Need that bssid setting, and the keys"); 2090 2091 bss_changed = 0; 2092 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__); 2093 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed); 2094 2095 out: 2096 LKPI_80211_LHW_UNLOCK(lhw); 2097 IEEE80211_LOCK(vap->iv_ic); 2098 return (error); 2099 } 2100 2101 static int 2102 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2103 { 2104 int error; 2105 2106 error = lkpi_sta_auth_to_assoc(vap, nstate, arg); 2107 if (error == 0) 2108 error = lkpi_sta_assoc_to_run(vap, nstate, arg); 2109 return (error); 2110 } 2111 2112 static int 2113 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2114 { 2115 struct lkpi_hw *lhw; 2116 struct ieee80211_hw *hw; 2117 struct lkpi_vif *lvif; 2118 struct ieee80211_vif *vif; 2119 struct ieee80211_node *ni; 2120 struct lkpi_sta *lsta; 2121 struct ieee80211_sta *sta; 2122 struct ieee80211_prep_tx_info prep_tx_info; 2123 #if 0 2124 enum ieee80211_bss_changed bss_changed; 2125 #endif 2126 int error; 2127 2128 lhw = vap->iv_ic->ic_softc; 2129 hw = LHW_TO_HW(lhw); 2130 lvif = VAP_TO_LVIF(vap); 2131 vif = LVIF_TO_VIF(lvif); 2132 2133 LKPI_80211_LVIF_LOCK(lvif); 2134 #ifdef LINUXKPI_DEBUG_80211 2135 /* XXX-BZ KASSERT later; state going down so no action. */ 2136 if (lvif->lvif_bss == NULL) 2137 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 2138 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 2139 lvif, vap, vap->iv_bss, lvif->lvif_bss, 2140 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 2141 lvif->lvif_bss_synched); 2142 #endif 2143 lsta = lvif->lvif_bss; 2144 LKPI_80211_LVIF_UNLOCK(lvif); 2145 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p " 2146 "lvif %p vap %p\n", __func__, 2147 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap)); 2148 2149 ni = lsta->ni; /* Reference held for lvif_bss. */ 2150 sta = LSTA_TO_STA(lsta); 2151 2152 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2153 2154 IEEE80211_UNLOCK(vap->iv_ic); 2155 LKPI_80211_LHW_LOCK(lhw); 2156 2157 /* flush, drop. */ 2158 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true); 2159 2160 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?"); 2161 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) && 2162 !lsta->in_mgd) { 2163 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 2164 prep_tx_info.duration = PREP_TX_INFO_DURATION; 2165 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 2166 lsta->in_mgd = true; 2167 } 2168 2169 LKPI_80211_LHW_UNLOCK(lhw); 2170 IEEE80211_LOCK(vap->iv_ic); 2171 2172 /* Call iv_newstate first so we get potential DISASSOC packet out. */ 2173 error = lvif->iv_newstate(vap, nstate, arg); 2174 if (error != 0) { 2175 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) " 2176 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error); 2177 goto outni; 2178 } 2179 2180 IEEE80211_UNLOCK(vap->iv_ic); 2181 2182 /* Ensure the packets get out. */ 2183 lkpi_80211_flush_tx(lhw, lsta); 2184 2185 LKPI_80211_LHW_LOCK(lhw); 2186 2187 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2188 2189 /* Wake tx queues to get packet(s) out. */ 2190 lkpi_wake_tx_queues(hw, sta, false, true); 2191 2192 /* flush, no drop */ 2193 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false); 2194 2195 /* End mgd_complete_tx. */ 2196 if (lsta->in_mgd) { 2197 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 2198 prep_tx_info.success = false; 2199 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 2200 lsta->in_mgd = false; 2201 } 2202 2203 #if 0 2204 /* sync_rx_queues */ 2205 lkpi_80211_mo_sync_rx_queues(hw); 2206 2207 /* sta_pre_rcu_remove */ 2208 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta); 2209 #endif 2210 2211 /* Take the station down. */ 2212 2213 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */ 2214 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2215 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not " 2216 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state)); 2217 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC); 2218 if (error != 0) { 2219 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) " 2220 "failed: %d\n", __func__, __LINE__, error); 2221 goto out; 2222 } 2223 2224 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2225 2226 /* Update sta_state (ASSOC to AUTH). */ 2227 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2228 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not " 2229 "ASSOC: %#x\n", __func__, lsta, lsta->state)); 2230 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH); 2231 if (error != 0) { 2232 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) " 2233 "failed: %d\n", __func__, __LINE__, error); 2234 goto out; 2235 } 2236 2237 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2238 2239 #if 0 2240 /* Update bss info (bss_info_changed) (assoc, aid, ..). */ 2241 lkpi_disassoc(sta, vif, lhw); 2242 #endif 2243 2244 error = EALREADY; 2245 out: 2246 LKPI_80211_LHW_UNLOCK(lhw); 2247 IEEE80211_LOCK(vap->iv_ic); 2248 outni: 2249 return (error); 2250 } 2251 2252 static int 2253 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2254 { 2255 struct lkpi_hw *lhw; 2256 struct ieee80211_hw *hw; 2257 struct lkpi_vif *lvif; 2258 struct ieee80211_vif *vif; 2259 struct ieee80211_node *ni; 2260 struct lkpi_sta *lsta; 2261 struct ieee80211_sta *sta; 2262 struct ieee80211_prep_tx_info prep_tx_info; 2263 enum ieee80211_bss_changed bss_changed; 2264 int error; 2265 2266 lhw = vap->iv_ic->ic_softc; 2267 hw = LHW_TO_HW(lhw); 2268 lvif = VAP_TO_LVIF(vap); 2269 vif = LVIF_TO_VIF(lvif); 2270 2271 IEEE80211_UNLOCK(vap->iv_ic); 2272 LKPI_80211_LHW_LOCK(lhw); 2273 2274 LKPI_80211_LVIF_LOCK(lvif); 2275 #ifdef LINUXKPI_DEBUG_80211 2276 /* XXX-BZ KASSERT later; state going down so no action. */ 2277 if (lvif->lvif_bss == NULL) 2278 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p " 2279 "lvif_bss->ni %p synched %d\n", __func__, __LINE__, 2280 lvif, vap, vap->iv_bss, lvif->lvif_bss, 2281 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL, 2282 lvif->lvif_bss_synched); 2283 #endif 2284 lsta = lvif->lvif_bss; 2285 LKPI_80211_LVIF_UNLOCK(lvif); 2286 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p " 2287 "lvif %p vap %p\n", __func__, 2288 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap)); 2289 2290 ni = lsta->ni; /* Reference held for lvif_bss. */ 2291 sta = LSTA_TO_STA(lsta); 2292 2293 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2294 2295 /* flush, drop. */ 2296 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true); 2297 2298 IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?"); 2299 if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) && 2300 !lsta->in_mgd) { 2301 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 2302 prep_tx_info.duration = PREP_TX_INFO_DURATION; 2303 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info); 2304 lsta->in_mgd = true; 2305 } 2306 2307 LKPI_80211_LHW_UNLOCK(lhw); 2308 IEEE80211_LOCK(vap->iv_ic); 2309 2310 /* Call iv_newstate first so we get potential DISASSOC packet out. */ 2311 error = lvif->iv_newstate(vap, nstate, arg); 2312 if (error != 0) { 2313 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) " 2314 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error); 2315 goto outni; 2316 } 2317 2318 IEEE80211_UNLOCK(vap->iv_ic); 2319 2320 /* Ensure the packets get out. */ 2321 lkpi_80211_flush_tx(lhw, lsta); 2322 2323 LKPI_80211_LHW_LOCK(lhw); 2324 2325 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2326 2327 /* Wake tx queues to get packet(s) out. */ 2328 lkpi_wake_tx_queues(hw, sta, false, true); 2329 2330 /* flush, no drop */ 2331 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false); 2332 2333 /* End mgd_complete_tx. */ 2334 if (lsta->in_mgd) { 2335 memset(&prep_tx_info, 0, sizeof(prep_tx_info)); 2336 prep_tx_info.success = false; 2337 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info); 2338 lsta->in_mgd = false; 2339 } 2340 2341 /* sync_rx_queues */ 2342 lkpi_80211_mo_sync_rx_queues(hw); 2343 2344 /* sta_pre_rcu_remove */ 2345 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta); 2346 2347 /* Take the station down. */ 2348 2349 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */ 2350 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2351 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not " 2352 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state)); 2353 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC); 2354 if (error != 0) { 2355 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) " 2356 "failed: %d\n", __func__, __LINE__, error); 2357 goto out; 2358 } 2359 2360 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2361 2362 /* Update sta_state (ASSOC to AUTH). */ 2363 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2364 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not " 2365 "ASSOC: %#x\n", __func__, lsta, lsta->state)); 2366 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH); 2367 if (error != 0) { 2368 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) " 2369 "failed: %d\n", __func__, __LINE__, error); 2370 goto out; 2371 } 2372 2373 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2374 2375 /* Update sta and change state (from AUTH) to NONE. */ 2376 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2377 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not " 2378 "AUTH: %#x\n", __func__, lsta, lsta->state)); 2379 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE); 2380 if (error != 0) { 2381 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) " 2382 "failed: %d\n", __func__, __LINE__, error); 2383 goto out; 2384 } 2385 2386 bss_changed = 0; 2387 /* 2388 * Start updating bss info (bss_info_changed) (assoc, aid, ..). 2389 * 2390 * One would expect this to happen when going off AUTHORIZED. 2391 * See comment there; removes the sta from fw if not careful 2392 * (bss_info_changed() change is executed right away). 2393 * 2394 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST 2395 * as otherwise drivers (iwlwifi at least) will silently not remove 2396 * the sta from the firmware and when we will add a new one trigger 2397 * a fw assert. 2398 * 2399 * The order which works best so far avoiding early removal or silent 2400 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases; 2401 * the iwlwifi:mac80211.c case still to be tested): 2402 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here) 2403 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST 2404 * (removes the sta given assoc is false) 2405 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed() 2406 * 4) call unassign_vif_chanctx 2407 * 5) call lkpi_hw_conf_idle 2408 * 6) call remove_chanctx 2409 */ 2410 bss_changed |= lkpi_disassoc(sta, vif, lhw); 2411 2412 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); 2413 2414 /* Adjust sta and change state (from NONE) to NOTEXIST. */ 2415 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni)); 2416 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not " 2417 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg)); 2418 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST); 2419 if (error != 0) { 2420 IMPROVE("do we need to undo the chan ctx?"); 2421 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) " 2422 "failed: %d\n", __func__, __LINE__, error); 2423 goto out; 2424 } 2425 2426 lkpi_lsta_remove(lsta, lvif); 2427 2428 lkpi_lsta_dump(lsta, ni, __func__, __LINE__); /* sta no longer save to use. */ 2429 2430 IMPROVE("Any bss_info changes to announce?"); 2431 vif->bss_conf.qos = 0; 2432 bss_changed |= BSS_CHANGED_QOS; 2433 vif->cfg.ssid_len = 0; 2434 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid)); 2435 bss_changed |= BSS_CHANGED_BSSID; 2436 vif->bss_conf.use_short_preamble = false; 2437 vif->bss_conf.qos = false; 2438 /* XXX BSS_CHANGED_???? */ 2439 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed); 2440 2441 LKPI_80211_LVIF_LOCK(lvif); 2442 /* Remove ni reference for this cache of lsta. */ 2443 lvif->lvif_bss = NULL; 2444 lvif->lvif_bss_synched = false; 2445 LKPI_80211_LVIF_UNLOCK(lvif); 2446 /* 2447 * The very last release the reference on the ni for the ni/lsta on 2448 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid 2449 * and potentially freed. 2450 */ 2451 ieee80211_free_node(ni); 2452 2453 /* conf_tx */ 2454 2455 /* Take the chan ctx down. */ 2456 if (vif->chanctx_conf != NULL) { 2457 struct lkpi_chanctx *lchanctx; 2458 struct ieee80211_chanctx_conf *chanctx_conf; 2459 2460 chanctx_conf = vif->chanctx_conf; 2461 /* Remove vif context. */ 2462 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf); 2463 /* NB: vif->chanctx_conf is NULL now. */ 2464 2465 lkpi_hw_conf_idle(hw, true); 2466 2467 /* Remove chan ctx. */ 2468 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf); 2469 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf); 2470 free(lchanctx, M_LKPI80211); 2471 } 2472 2473 error = EALREADY; 2474 out: 2475 LKPI_80211_LHW_UNLOCK(lhw); 2476 IEEE80211_LOCK(vap->iv_ic); 2477 outni: 2478 return (error); 2479 } 2480 2481 static int 2482 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2483 { 2484 2485 return (lkpi_sta_run_to_init(vap, nstate, arg)); 2486 } 2487 2488 static int 2489 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2490 { 2491 int error; 2492 2493 error = lkpi_sta_run_to_init(vap, nstate, arg); 2494 if (error != 0 && error != EALREADY) 2495 return (error); 2496 2497 /* At this point iv_bss is long a new node! */ 2498 2499 error |= lkpi_sta_scan_to_auth(vap, nstate, 0); 2500 return (error); 2501 } 2502 2503 /* -------------------------------------------------------------------------- */ 2504 2505 /* 2506 * The matches the documented state changes in net80211::sta_newstate(). 2507 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases 2508 * there are "invalid" (so there is a room for failure here). 2509 */ 2510 struct fsm_state { 2511 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */ 2512 enum ieee80211_state ostate; 2513 enum ieee80211_state nstate; 2514 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int); 2515 } sta_state_fsm[] = { 2516 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, 2517 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* scan_to_init */ 2518 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */ 2519 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */ 2520 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */ 2521 2522 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_state_do_nada }, 2523 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada }, 2524 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan }, 2525 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan }, 2526 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */ 2527 2528 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */ 2529 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */ 2530 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */ 2531 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* Send ?AUTH. */ 2532 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */ 2533 2534 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* Send ASSOCREQ. */ 2535 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */ 2536 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* Send ASSOCREQ/REASSOCREQ. */ 2537 2538 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run }, 2539 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run }, 2540 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada }, 2541 2542 /* Dummy at the end without handler. */ 2543 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL }, 2544 }; 2545 2546 static int 2547 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2548 { 2549 struct ieee80211com *ic; 2550 struct lkpi_hw *lhw; 2551 struct lkpi_vif *lvif; 2552 struct ieee80211_vif *vif; 2553 struct fsm_state *s; 2554 enum ieee80211_state ostate; 2555 int error; 2556 2557 ic = vap->iv_ic; 2558 IEEE80211_LOCK_ASSERT(ic); 2559 ostate = vap->iv_state; 2560 2561 #ifdef LINUXKPI_DEBUG_80211 2562 if (linuxkpi_debug_80211 & D80211_TRACE) 2563 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n", 2564 __func__, __LINE__, vap, nstate, arg); 2565 #endif 2566 2567 if (vap->iv_opmode == IEEE80211_M_STA) { 2568 2569 lhw = ic->ic_softc; 2570 lvif = VAP_TO_LVIF(vap); 2571 vif = LVIF_TO_VIF(lvif); 2572 2573 /* No need to replicate this in most state handlers. */ 2574 if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN) 2575 lkpi_stop_hw_scan(lhw, vif); 2576 2577 s = sta_state_fsm; 2578 2579 } else { 2580 ic_printf(vap->iv_ic, "%s: only station mode currently supported: " 2581 "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode); 2582 return (ENOSYS); 2583 } 2584 2585 error = 0; 2586 for (; s->handler != NULL; s++) { 2587 if (ostate == s->ostate && nstate == s->nstate) { 2588 #ifdef LINUXKPI_DEBUG_80211 2589 if (linuxkpi_debug_80211 & D80211_TRACE) 2590 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->" 2591 " %d (%s): arg %d.\n", __func__, 2592 ostate, ieee80211_state_name[ostate], 2593 nstate, ieee80211_state_name[nstate], arg); 2594 #endif 2595 error = s->handler(vap, nstate, arg); 2596 break; 2597 } 2598 } 2599 IEEE80211_LOCK_ASSERT(vap->iv_ic); 2600 2601 if (s->handler == NULL) { 2602 IMPROVE("turn this into a KASSERT\n"); 2603 ic_printf(vap->iv_ic, "%s: unsupported state transition " 2604 "%d (%s) -> %d (%s)\n", __func__, 2605 ostate, ieee80211_state_name[ostate], 2606 nstate, ieee80211_state_name[nstate]); 2607 return (ENOSYS); 2608 } 2609 2610 if (error == EALREADY) { 2611 #ifdef LINUXKPI_DEBUG_80211 2612 if (linuxkpi_debug_80211 & D80211_TRACE) 2613 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> " 2614 "%d (%s): iv_newstate already handled: %d.\n", 2615 __func__, ostate, ieee80211_state_name[ostate], 2616 nstate, ieee80211_state_name[nstate], error); 2617 #endif 2618 return (0); 2619 } 2620 2621 if (error != 0) { 2622 ic_printf(vap->iv_ic, "%s: error %d during state transition " 2623 "%d (%s) -> %d (%s)\n", __func__, error, 2624 ostate, ieee80211_state_name[ostate], 2625 nstate, ieee80211_state_name[nstate]); 2626 return (error); 2627 } 2628 2629 #ifdef LINUXKPI_DEBUG_80211 2630 if (linuxkpi_debug_80211 & D80211_TRACE) 2631 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x " 2632 "calling net80211 parent\n", 2633 __func__, __LINE__, vap, nstate, arg); 2634 #endif 2635 2636 return (lvif->iv_newstate(vap, nstate, arg)); 2637 } 2638 2639 /* -------------------------------------------------------------------------- */ 2640 2641 /* 2642 * We overload (*iv_update_bss) as otherwise we have cases in, e.g., 2643 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a 2644 * new node without us knowing and thus our ni/lsta are out of sync. 2645 */ 2646 static struct ieee80211_node * 2647 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni) 2648 { 2649 struct lkpi_vif *lvif; 2650 struct ieee80211_node *rni; 2651 2652 IEEE80211_LOCK_ASSERT(vap->iv_ic); 2653 2654 lvif = VAP_TO_LVIF(vap); 2655 2656 LKPI_80211_LVIF_LOCK(lvif); 2657 lvif->lvif_bss_synched = false; 2658 LKPI_80211_LVIF_UNLOCK(lvif); 2659 2660 rni = lvif->iv_update_bss(vap, ni); 2661 return (rni); 2662 } 2663 2664 #ifdef LKPI_80211_WME 2665 static int 2666 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned) 2667 { 2668 struct ieee80211com *ic; 2669 struct ieee80211_hw *hw; 2670 struct lkpi_vif *lvif; 2671 struct ieee80211_vif *vif; 2672 struct chanAccParams chp; 2673 struct wmeParams wmeparr[WME_NUM_AC]; 2674 struct ieee80211_tx_queue_params txqp; 2675 enum ieee80211_bss_changed changed; 2676 int error; 2677 uint16_t ac; 2678 2679 IMPROVE(); 2680 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != " 2681 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS)); 2682 2683 if (vap == NULL) 2684 return (0); 2685 2686 if ((vap->iv_flags & IEEE80211_F_WME) == 0) 2687 return (0); 2688 2689 if (lhw->ops->conf_tx == NULL) 2690 return (0); 2691 2692 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) { 2693 lhw->update_wme = true; 2694 return (0); 2695 } 2696 lhw->update_wme = false; 2697 2698 ic = lhw->ic; 2699 ieee80211_wme_ic_getparams(ic, &chp); 2700 IEEE80211_LOCK(ic); 2701 for (ac = 0; ac < WME_NUM_AC; ac++) 2702 wmeparr[ac] = chp.cap_wmeParams[ac]; 2703 IEEE80211_UNLOCK(ic); 2704 2705 hw = LHW_TO_HW(lhw); 2706 lvif = VAP_TO_LVIF(vap); 2707 vif = LVIF_TO_VIF(lvif); 2708 2709 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */ 2710 LKPI_80211_LHW_LOCK(lhw); 2711 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2712 struct wmeParams *wmep; 2713 2714 wmep = &wmeparr[ac]; 2715 bzero(&txqp, sizeof(txqp)); 2716 txqp.cw_min = wmep->wmep_logcwmin; 2717 txqp.cw_max = wmep->wmep_logcwmax; 2718 txqp.txop = wmep->wmep_txopLimit; 2719 txqp.aifs = wmep->wmep_aifsn; 2720 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp); 2721 if (error != 0) 2722 ic_printf(ic, "%s: conf_tx ac %u failed %d\n", 2723 __func__, ac, error); 2724 } 2725 LKPI_80211_LHW_UNLOCK(lhw); 2726 changed = BSS_CHANGED_QOS; 2727 if (!planned) 2728 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed); 2729 2730 return (changed); 2731 } 2732 #endif 2733 2734 static int 2735 lkpi_ic_wme_update(struct ieee80211com *ic) 2736 { 2737 #ifdef LKPI_80211_WME 2738 struct ieee80211vap *vap; 2739 struct lkpi_hw *lhw; 2740 2741 IMPROVE("Use the per-VAP callback in net80211."); 2742 vap = TAILQ_FIRST(&ic->ic_vaps); 2743 if (vap == NULL) 2744 return (0); 2745 2746 lhw = ic->ic_softc; 2747 2748 lkpi_wme_update(lhw, vap, false); 2749 #endif 2750 return (0); /* unused */ 2751 } 2752 2753 /* 2754 * Change link-layer address on the vif (if the vap is not started/"UP"). 2755 * This can happen if a user changes 'ether' using ifconfig. 2756 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but 2757 * we do use a per-[l]vif event handler to be sure we exist as we 2758 * cannot assume that from every vap derives a vif and we have a hard 2759 * time checking based on net80211 information. 2760 */ 2761 static void 2762 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp) 2763 { 2764 struct epoch_tracker et; 2765 struct ieee80211_vif *vif; 2766 2767 NET_EPOCH_ENTER(et); 2768 /* NB: identify vap's by if_init; left as an extra check. */ 2769 if (ifp->if_init != ieee80211_init || (ifp->if_flags & IFF_UP) != 0) { 2770 NET_EPOCH_EXIT(et); 2771 return; 2772 } 2773 2774 vif = arg; 2775 IEEE80211_ADDR_COPY(vif->bss_conf.addr, IF_LLADDR(ifp)); 2776 NET_EPOCH_EXIT(et); 2777 } 2778 2779 static struct ieee80211vap * 2780 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], 2781 int unit, enum ieee80211_opmode opmode, int flags, 2782 const uint8_t bssid[IEEE80211_ADDR_LEN], 2783 const uint8_t mac[IEEE80211_ADDR_LEN]) 2784 { 2785 struct lkpi_hw *lhw; 2786 struct ieee80211_hw *hw; 2787 struct lkpi_vif *lvif; 2788 struct ieee80211vap *vap; 2789 struct ieee80211_vif *vif; 2790 struct ieee80211_tx_queue_params txqp; 2791 enum ieee80211_bss_changed changed; 2792 size_t len; 2793 int error, i; 2794 uint16_t ac; 2795 2796 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */ 2797 return (NULL); 2798 2799 lhw = ic->ic_softc; 2800 hw = LHW_TO_HW(lhw); 2801 2802 len = sizeof(*lvif); 2803 len += hw->vif_data_size; /* vif->drv_priv */ 2804 2805 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO); 2806 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF); 2807 TAILQ_INIT(&lvif->lsta_head); 2808 lvif->lvif_bss = NULL; 2809 lvif->lvif_bss_synched = false; 2810 vap = LVIF_TO_VAP(lvif); 2811 2812 vif = LVIF_TO_VIF(lvif); 2813 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN); 2814 vif->p2p = false; 2815 vif->probe_req_reg = false; 2816 vif->type = lkpi_opmode_to_vif_type(opmode); 2817 lvif->wdev.iftype = vif->type; 2818 /* Need to fill in other fields as well. */ 2819 IMPROVE(); 2820 2821 /* XXX-BZ hardcoded for now! */ 2822 #if 1 2823 vif->chanctx_conf = NULL; 2824 vif->bss_conf.vif = vif; 2825 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */ 2826 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac); 2827 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event, 2828 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY); 2829 vif->bss_conf.link_id = 0; /* Non-MLO operation. */ 2830 vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 2831 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */ 2832 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */ 2833 vif->bss_conf.qos = false; 2834 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */ 2835 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE; 2836 vif->cfg.aid = 0; 2837 vif->cfg.assoc = false; 2838 vif->cfg.idle = true; 2839 vif->cfg.ps = false; 2840 IMPROVE("Check other fields and then figure out whats is left elsewhere of them"); 2841 /* 2842 * We need to initialize it to something as the bss_info_changed call 2843 * will try to copy from it in iwlwifi and NULL is a panic. 2844 * We will set the proper one in scan_to_auth() before being assoc. 2845 */ 2846 vif->bss_conf.bssid = ieee80211broadcastaddr; 2847 #endif 2848 #if 0 2849 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */ 2850 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid); 2851 vif->bss_conf.beacon_int = ic->ic_bintval; 2852 /* iwlwifi bug. */ 2853 if (vif->bss_conf.beacon_int < 16) 2854 vif->bss_conf.beacon_int = 16; 2855 #endif 2856 2857 /* Link Config */ 2858 vif->link_conf[0] = &vif->bss_conf; 2859 for (i = 0; i < nitems(vif->link_conf); i++) { 2860 IMPROVE("more than 1 link one day"); 2861 } 2862 2863 /* Setup queue defaults; driver may override in (*add_interface). */ 2864 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 2865 if (ieee80211_hw_check(hw, QUEUE_CONTROL)) 2866 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE; 2867 else if (hw->queues >= IEEE80211_NUM_ACS) 2868 vif->hw_queue[i] = i; 2869 else 2870 vif->hw_queue[i] = 0; 2871 2872 /* Initialize the queue to running. Stopped? */ 2873 lvif->hw_queue_stopped[i] = false; 2874 } 2875 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE; 2876 2877 IMPROVE(); 2878 2879 error = lkpi_80211_mo_start(hw); 2880 if (error != 0) { 2881 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error); 2882 mtx_destroy(&lvif->mtx); 2883 free(lvif, M_80211_VAP); 2884 return (NULL); 2885 } 2886 2887 error = lkpi_80211_mo_add_interface(hw, vif); 2888 if (error != 0) { 2889 IMPROVE(); /* XXX-BZ mo_stop()? */ 2890 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error); 2891 mtx_destroy(&lvif->mtx); 2892 free(lvif, M_80211_VAP); 2893 return (NULL); 2894 } 2895 2896 LKPI_80211_LHW_LVIF_LOCK(lhw); 2897 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry); 2898 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 2899 2900 /* Set bss_info. */ 2901 changed = 0; 2902 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed); 2903 2904 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */ 2905 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element"); 2906 LKPI_80211_LHW_LOCK(lhw); 2907 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2908 2909 bzero(&txqp, sizeof(txqp)); 2910 txqp.cw_min = 15; 2911 txqp.cw_max = 1023; 2912 txqp.txop = 0; 2913 txqp.aifs = 2; 2914 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp); 2915 if (error != 0) 2916 ic_printf(ic, "%s: conf_tx ac %u failed %d\n", 2917 __func__, ac, error); 2918 } 2919 LKPI_80211_LHW_UNLOCK(lhw); 2920 changed = BSS_CHANGED_QOS; 2921 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed); 2922 2923 /* Force MC init. */ 2924 lkpi_update_mcast_filter(ic, true); 2925 2926 IMPROVE(); 2927 2928 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid); 2929 2930 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */ 2931 lvif->iv_newstate = vap->iv_newstate; 2932 vap->iv_newstate = lkpi_iv_newstate; 2933 lvif->iv_update_bss = vap->iv_update_bss; 2934 vap->iv_update_bss = lkpi_iv_update_bss; 2935 2936 /* Key management. */ 2937 if (lhw->ops->set_key != NULL) { 2938 #ifdef LKPI_80211_HW_CRYPTO 2939 vap->iv_key_set = lkpi_iv_key_set; 2940 vap->iv_key_delete = lkpi_iv_key_delete; 2941 #endif 2942 } 2943 2944 #ifdef LKPI_80211_HT 2945 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */ 2946 #endif 2947 2948 ieee80211_ratectl_init(vap); 2949 2950 /* Complete setup. */ 2951 ieee80211_vap_attach(vap, ieee80211_media_change, 2952 ieee80211_media_status, mac); 2953 2954 if (hw->max_listen_interval == 0) 2955 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval); 2956 hw->conf.listen_interval = hw->max_listen_interval; 2957 ic->ic_set_channel(ic); 2958 2959 /* XXX-BZ do we need to be able to update these? */ 2960 hw->wiphy->frag_threshold = vap->iv_fragthreshold; 2961 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold); 2962 hw->wiphy->rts_threshold = vap->iv_rtsthreshold; 2963 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold); 2964 /* any others? */ 2965 IMPROVE(); 2966 2967 return (vap); 2968 } 2969 2970 void 2971 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw) 2972 { 2973 2974 wiphy_unregister(hw->wiphy); 2975 linuxkpi_ieee80211_ifdetach(hw); 2976 2977 IMPROVE(); 2978 } 2979 2980 void 2981 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw) 2982 { 2983 2984 TODO(); 2985 } 2986 2987 static void 2988 lkpi_ic_vap_delete(struct ieee80211vap *vap) 2989 { 2990 struct ieee80211com *ic; 2991 struct lkpi_hw *lhw; 2992 struct ieee80211_hw *hw; 2993 struct lkpi_vif *lvif; 2994 struct ieee80211_vif *vif; 2995 2996 lvif = VAP_TO_LVIF(vap); 2997 vif = LVIF_TO_VIF(lvif); 2998 ic = vap->iv_ic; 2999 lhw = ic->ic_softc; 3000 hw = LHW_TO_HW(lhw); 3001 3002 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent); 3003 3004 LKPI_80211_LHW_LVIF_LOCK(lhw); 3005 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry); 3006 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 3007 3008 ieee80211_ratectl_deinit(vap); 3009 ieee80211_vap_detach(vap); 3010 3011 IMPROVE("clear up other bits in this state"); 3012 3013 lkpi_80211_mo_remove_interface(hw, vif); 3014 3015 /* Single VAP, so we can do this here. */ 3016 lkpi_80211_mo_stop(hw); 3017 3018 mtx_destroy(&lvif->mtx); 3019 free(lvif, M_80211_VAP); 3020 } 3021 3022 static void 3023 lkpi_ic_update_mcast(struct ieee80211com *ic) 3024 { 3025 3026 lkpi_update_mcast_filter(ic, false); 3027 TRACEOK(); 3028 } 3029 3030 static void 3031 lkpi_ic_update_promisc(struct ieee80211com *ic) 3032 { 3033 3034 UNIMPLEMENTED; 3035 } 3036 3037 static void 3038 lkpi_ic_update_chw(struct ieee80211com *ic) 3039 { 3040 3041 UNIMPLEMENTED; 3042 } 3043 3044 /* Start / stop device. */ 3045 static void 3046 lkpi_ic_parent(struct ieee80211com *ic) 3047 { 3048 struct lkpi_hw *lhw; 3049 #ifdef HW_START_STOP 3050 struct ieee80211_hw *hw; 3051 int error; 3052 #endif 3053 bool start_all; 3054 3055 IMPROVE(); 3056 3057 lhw = ic->ic_softc; 3058 #ifdef HW_START_STOP 3059 hw = LHW_TO_HW(lhw); 3060 #endif 3061 start_all = false; 3062 3063 /* IEEE80211_UNLOCK(ic); */ 3064 LKPI_80211_LHW_LOCK(lhw); 3065 if (ic->ic_nrunning > 0) { 3066 #ifdef HW_START_STOP 3067 error = lkpi_80211_mo_start(hw); 3068 if (error == 0) 3069 #endif 3070 start_all = true; 3071 } else { 3072 #ifdef HW_START_STOP 3073 lkpi_80211_mo_stop(hw); 3074 #endif 3075 } 3076 LKPI_80211_LHW_UNLOCK(lhw); 3077 /* IEEE80211_LOCK(ic); */ 3078 3079 if (start_all) 3080 ieee80211_start_all(ic); 3081 } 3082 3083 bool 3084 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids, 3085 size_t ie_ids_len) 3086 { 3087 int i; 3088 3089 for (i = 0; i < ie_ids_len; i++) { 3090 if (ie == *ie_ids) 3091 return (true); 3092 } 3093 3094 return (false); 3095 } 3096 3097 /* Return true if skipped; false if error. */ 3098 bool 3099 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len) 3100 { 3101 size_t x; 3102 uint8_t l; 3103 3104 x = *xp; 3105 3106 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n", 3107 __func__, x, ies_len, ies)); 3108 l = ies[x + 1]; 3109 x += 2 + l; 3110 3111 if (x > ies_len) 3112 return (false); 3113 3114 *xp = x; 3115 return (true); 3116 } 3117 3118 static uint8_t * 3119 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies, 3120 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw) 3121 { 3122 struct ieee80211_supported_band *supband; 3123 struct linuxkpi_ieee80211_channel *channels; 3124 struct ieee80211com *ic; 3125 const struct ieee80211_channel *chan; 3126 const struct ieee80211_rateset *rs; 3127 uint8_t *pb; 3128 int band, i; 3129 3130 ic = vap->iv_ic; 3131 for (band = 0; band < NUM_NL80211_BANDS; band++) { 3132 if ((band_mask & (1 << band)) == 0) 3133 continue; 3134 3135 supband = hw->wiphy->bands[band]; 3136 /* 3137 * This should not happen; 3138 * band_mask is a bitmask of valid bands to scan on. 3139 */ 3140 if (supband == NULL || supband->n_channels == 0) 3141 continue; 3142 3143 /* Find a first channel to get the mode and rates from. */ 3144 channels = supband->channels; 3145 chan = NULL; 3146 for (i = 0; i < supband->n_channels; i++) { 3147 3148 if (channels[i].flags & IEEE80211_CHAN_DISABLED) 3149 continue; 3150 3151 chan = ieee80211_find_channel(ic, 3152 channels[i].center_freq, 0); 3153 if (chan != NULL) 3154 break; 3155 } 3156 3157 /* This really should not happen. */ 3158 if (chan == NULL) 3159 continue; 3160 3161 pb = p; 3162 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */ 3163 p = ieee80211_add_rates(p, rs); 3164 p = ieee80211_add_xrates(p, rs); 3165 3166 #if defined(LKPI_80211_HT) 3167 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) { 3168 struct ieee80211_channel *c; 3169 3170 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 3171 vap->iv_flags_ht); 3172 p = ieee80211_add_htcap_ch(p, vap, c); 3173 } 3174 #endif 3175 #if defined(LKPI_80211_VHT) 3176 if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) { 3177 struct ieee80211_channel *c; 3178 3179 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 3180 vap->iv_flags_ht); 3181 c = ieee80211_vht_adjust_channel(ic, c, 3182 vap->iv_vht_flags); 3183 p = ieee80211_add_vhtcap_ch(p, vap, c); 3184 } 3185 #endif 3186 3187 scan_ies->ies[band] = pb; 3188 scan_ies->len[band] = p - pb; 3189 } 3190 3191 /* Add common_ies */ 3192 pb = p; 3193 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 && 3194 vap->iv_wpa_ie != NULL) { 3195 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]); 3196 p += 2 + vap->iv_wpa_ie[1]; 3197 } 3198 if (vap->iv_appie_probereq != NULL) { 3199 memcpy(p, vap->iv_appie_probereq->ie_data, 3200 vap->iv_appie_probereq->ie_len); 3201 p += vap->iv_appie_probereq->ie_len; 3202 } 3203 scan_ies->common_ies = pb; 3204 scan_ies->common_ie_len = p - pb; 3205 3206 return (p); 3207 } 3208 3209 static void 3210 lkpi_ic_scan_start(struct ieee80211com *ic) 3211 { 3212 struct lkpi_hw *lhw; 3213 struct ieee80211_hw *hw; 3214 struct lkpi_vif *lvif; 3215 struct ieee80211_vif *vif; 3216 struct ieee80211_scan_state *ss; 3217 struct ieee80211vap *vap; 3218 int error; 3219 bool is_hw_scan; 3220 3221 lhw = ic->ic_softc; 3222 LKPI_80211_LHW_SCAN_LOCK(lhw); 3223 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) { 3224 /* A scan is still running. */ 3225 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3226 return; 3227 } 3228 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0; 3229 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3230 3231 ss = ic->ic_scan; 3232 vap = ss->ss_vap; 3233 if (vap->iv_state != IEEE80211_S_SCAN) { 3234 IMPROVE("We need to be able to scan if not in S_SCAN"); 3235 return; 3236 } 3237 3238 hw = LHW_TO_HW(lhw); 3239 if (!is_hw_scan) { 3240 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */ 3241 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD; 3242 sw_scan: 3243 lvif = VAP_TO_LVIF(vap); 3244 vif = LVIF_TO_VIF(lvif); 3245 3246 if (vap->iv_state == IEEE80211_S_SCAN) 3247 lkpi_hw_conf_idle(hw, false); 3248 3249 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr); 3250 /* net80211::scan_start() handled PS for us. */ 3251 IMPROVE(); 3252 /* XXX Also means it is too late to flush queues? 3253 * need to check iv_sta_ps or overload? */ 3254 /* XXX want to adjust ss end time/ maxdwell? */ 3255 3256 } else { 3257 struct ieee80211_channel *c; 3258 struct ieee80211_scan_request *hw_req; 3259 struct linuxkpi_ieee80211_channel *lc, **cpp; 3260 struct cfg80211_ssid *ssids; 3261 struct cfg80211_scan_6ghz_params *s6gp; 3262 size_t chan_len, nchan, ssids_len, s6ghzlen; 3263 int band, i, ssid_count, common_ie_len; 3264 uint32_t band_mask; 3265 uint8_t *ie, *ieend; 3266 bool running; 3267 3268 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids); 3269 ssids_len = ssid_count * sizeof(*ssids); 3270 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */ 3271 3272 band_mask = 0; 3273 nchan = 0; 3274 for (i = ss->ss_next; i < ss->ss_last; i++) { 3275 nchan++; 3276 band = lkpi_net80211_chan_to_nl80211_band( 3277 ss->ss_chans[ss->ss_next + i]); 3278 band_mask |= (1 << band); 3279 } 3280 3281 if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) { 3282 IMPROVE("individual band scans not yet supported, only scanning first band"); 3283 /* In theory net80211 should drive this. */ 3284 /* Probably we need to add local logic for now; 3285 * need to deal with scan_complete 3286 * and cancel_scan and keep local state. 3287 * Also cut the nchan down above. 3288 */ 3289 /* XXX-BZ ath10k does not set this but still does it? &$%^ */ 3290 } 3291 3292 chan_len = nchan * (sizeof(lc) + sizeof(*lc)); 3293 3294 common_ie_len = 0; 3295 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 && 3296 vap->iv_wpa_ie != NULL) 3297 common_ie_len += vap->iv_wpa_ie[1]; 3298 if (vap->iv_appie_probereq != NULL) 3299 common_ie_len += vap->iv_appie_probereq->ie_len; 3300 3301 /* We would love to check this at an earlier stage... */ 3302 if (common_ie_len > hw->wiphy->max_scan_ie_len) { 3303 ic_printf(ic, "WARNING: %s: common_ie_len %d > " 3304 "wiphy->max_scan_ie_len %d\n", __func__, 3305 common_ie_len, hw->wiphy->max_scan_ie_len); 3306 } 3307 3308 hw_req = malloc(sizeof(*hw_req) + ssids_len + 3309 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len + 3310 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO); 3311 3312 hw_req->req.flags = 0; /* XXX ??? */ 3313 /* hw_req->req.wdev */ 3314 hw_req->req.wiphy = hw->wiphy; 3315 hw_req->req.no_cck = false; /* XXX */ 3316 #if 0 3317 /* This seems to pessimise default scanning behaviour. */ 3318 hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell); 3319 hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell); 3320 #endif 3321 #ifdef __notyet__ 3322 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR; 3323 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN); 3324 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN); 3325 #endif 3326 eth_broadcast_addr(hw_req->req.bssid); 3327 3328 hw_req->req.n_channels = nchan; 3329 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1); 3330 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan); 3331 for (i = 0; i < nchan; i++) { 3332 *(cpp + i) = 3333 (struct linuxkpi_ieee80211_channel *)(lc + i); 3334 } 3335 for (i = 0; i < nchan; i++) { 3336 c = ss->ss_chans[ss->ss_next + i]; 3337 3338 lc->hw_value = c->ic_ieee; 3339 lc->center_freq = c->ic_freq; /* XXX */ 3340 /* lc->flags */ 3341 lc->band = lkpi_net80211_chan_to_nl80211_band(c); 3342 lc->max_power = c->ic_maxpower; 3343 /* lc-> ... */ 3344 lc++; 3345 } 3346 3347 hw_req->req.n_ssids = ssid_count; 3348 if (hw_req->req.n_ssids > 0) { 3349 ssids = (struct cfg80211_ssid *)lc; 3350 hw_req->req.ssids = ssids; 3351 for (i = 0; i < ssid_count; i++) { 3352 ssids->ssid_len = ss->ss_ssid[i].len; 3353 memcpy(ssids->ssid, ss->ss_ssid[i].ssid, 3354 ss->ss_ssid[i].len); 3355 ssids++; 3356 } 3357 s6gp = (struct cfg80211_scan_6ghz_params *)ssids; 3358 } else { 3359 s6gp = (struct cfg80211_scan_6ghz_params *)lc; 3360 } 3361 3362 /* 6GHz one day. */ 3363 hw_req->req.n_6ghz_params = 0; 3364 hw_req->req.scan_6ghz_params = NULL; 3365 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */ 3366 /* s6gp->... */ 3367 3368 ie = ieend = (uint8_t *)s6gp; 3369 /* Copy per-band IEs, copy common IEs */ 3370 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw); 3371 hw_req->req.ie = ie; 3372 hw_req->req.ie_len = ieend - ie; 3373 3374 lvif = VAP_TO_LVIF(vap); 3375 vif = LVIF_TO_VIF(lvif); 3376 3377 LKPI_80211_LHW_SCAN_LOCK(lhw); 3378 /* Re-check under lock. */ 3379 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0; 3380 if (!running) { 3381 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p " 3382 "!= NULL\n", __func__, ic, lhw, lhw->hw_req)); 3383 3384 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING; 3385 lhw->hw_req = hw_req; 3386 } 3387 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3388 if (running) { 3389 free(hw_req, M_LKPI80211); 3390 return; 3391 } 3392 3393 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req); 3394 if (error != 0) { 3395 ieee80211_cancel_scan(vap); 3396 3397 /* 3398 * ieee80211_scan_completed must be called in either 3399 * case of error or none. So let the free happen there 3400 * and only there. 3401 * That would be fine in theory but in practice drivers 3402 * behave differently: 3403 * ath10k does not return hw_scan until after scan_complete 3404 * and can then still return an error. 3405 * rtw88 can return 1 or -EBUSY without scan_complete 3406 * iwlwifi can return various errors before scan starts 3407 * ... 3408 * So we cannot rely on that behaviour and have to check 3409 * and balance between both code paths. 3410 */ 3411 LKPI_80211_LHW_SCAN_LOCK(lhw); 3412 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) { 3413 free(lhw->hw_req, M_LKPI80211); 3414 lhw->hw_req = NULL; 3415 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING; 3416 } 3417 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3418 3419 /* 3420 * XXX-SIGH magic number. 3421 * rtw88 has a magic "return 1" if offloading scan is 3422 * not possible. Fall back to sw scan in that case. 3423 */ 3424 if (error == 1) { 3425 LKPI_80211_LHW_SCAN_LOCK(lhw); 3426 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW; 3427 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3428 /* 3429 * XXX If we clear this now and later a driver 3430 * thinks it * can do a hw_scan again, we will 3431 * currently not re-enable it? 3432 */ 3433 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD; 3434 ieee80211_start_scan(vap, 3435 IEEE80211_SCAN_ACTIVE | 3436 IEEE80211_SCAN_NOPICK | 3437 IEEE80211_SCAN_ONCE, 3438 IEEE80211_SCAN_FOREVER, 3439 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20), 3440 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200), 3441 vap->iv_des_nssid, vap->iv_des_ssid); 3442 goto sw_scan; 3443 } 3444 3445 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n", 3446 __func__, error); 3447 } 3448 } 3449 } 3450 3451 static void 3452 lkpi_ic_scan_end(struct ieee80211com *ic) 3453 { 3454 struct lkpi_hw *lhw; 3455 bool is_hw_scan; 3456 3457 lhw = ic->ic_softc; 3458 LKPI_80211_LHW_SCAN_LOCK(lhw); 3459 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) { 3460 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3461 return; 3462 } 3463 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0; 3464 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3465 3466 if (!is_hw_scan) { 3467 struct ieee80211_scan_state *ss; 3468 struct ieee80211vap *vap; 3469 struct ieee80211_hw *hw; 3470 struct lkpi_vif *lvif; 3471 struct ieee80211_vif *vif; 3472 3473 ss = ic->ic_scan; 3474 vap = ss->ss_vap; 3475 hw = LHW_TO_HW(lhw); 3476 lvif = VAP_TO_LVIF(vap); 3477 vif = LVIF_TO_VIF(lvif); 3478 3479 lkpi_80211_mo_sw_scan_complete(hw, vif); 3480 3481 /* Send PS to stop buffering if n80211 does not for us? */ 3482 3483 if (vap->iv_state == IEEE80211_S_SCAN) 3484 lkpi_hw_conf_idle(hw, true); 3485 } 3486 } 3487 3488 static void 3489 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss, 3490 unsigned long maxdwell) 3491 { 3492 struct lkpi_hw *lhw; 3493 bool is_hw_scan; 3494 3495 lhw = ss->ss_ic->ic_softc; 3496 LKPI_80211_LHW_SCAN_LOCK(lhw); 3497 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0; 3498 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3499 if (!is_hw_scan) 3500 lhw->ic_scan_curchan(ss, maxdwell); 3501 } 3502 3503 static void 3504 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss) 3505 { 3506 struct lkpi_hw *lhw; 3507 bool is_hw_scan; 3508 3509 lhw = ss->ss_ic->ic_softc; 3510 LKPI_80211_LHW_SCAN_LOCK(lhw); 3511 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0; 3512 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3513 if (!is_hw_scan) 3514 lhw->ic_scan_mindwell(ss); 3515 } 3516 3517 static void 3518 lkpi_ic_set_channel(struct ieee80211com *ic) 3519 { 3520 struct lkpi_hw *lhw; 3521 struct ieee80211_hw *hw; 3522 struct ieee80211_channel *c; 3523 struct linuxkpi_ieee80211_channel *chan; 3524 int error; 3525 bool hw_scan_running; 3526 3527 lhw = ic->ic_softc; 3528 3529 /* If we do not support (*config)() save us the work. */ 3530 if (lhw->ops->config == NULL) 3531 return; 3532 3533 /* If we have a hw_scan running do not switch channels. */ 3534 LKPI_80211_LHW_SCAN_LOCK(lhw); 3535 hw_scan_running = 3536 (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) == 3537 (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW); 3538 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 3539 if (hw_scan_running) 3540 return; 3541 3542 c = ic->ic_curchan; 3543 if (c == NULL || c == IEEE80211_CHAN_ANYC) { 3544 ic_printf(ic, "%s: c %p ops->config %p\n", __func__, 3545 c, lhw->ops->config); 3546 return; 3547 } 3548 3549 chan = lkpi_find_lkpi80211_chan(lhw, c); 3550 if (chan == NULL) { 3551 ic_printf(ic, "%s: c %p chan %p\n", __func__, 3552 c, chan); 3553 return; 3554 } 3555 3556 /* XXX max power for scanning? */ 3557 IMPROVE(); 3558 3559 hw = LHW_TO_HW(lhw); 3560 cfg80211_chandef_create(&hw->conf.chandef, chan, 3561 #ifdef LKPI_80211_HT 3562 (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 : 3563 #endif 3564 NL80211_CHAN_NO_HT); 3565 3566 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL); 3567 if (error != 0 && error != EOPNOTSUPP) { 3568 ic_printf(ic, "ERROR: %s: config %#0x returned %d\n", 3569 __func__, IEEE80211_CONF_CHANGE_CHANNEL, error); 3570 /* XXX should we unroll to the previous chandef? */ 3571 IMPROVE(); 3572 } else { 3573 /* Update radiotap channels as well. */ 3574 lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq); 3575 lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags); 3576 lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq); 3577 lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags); 3578 } 3579 3580 /* Currently PS is hard coded off! Not sure it belongs here. */ 3581 IMPROVE(); 3582 if (ieee80211_hw_check(hw, SUPPORTS_PS) && 3583 (hw->conf.flags & IEEE80211_CONF_PS) != 0) { 3584 hw->conf.flags &= ~IEEE80211_CONF_PS; 3585 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS); 3586 if (error != 0 && error != EOPNOTSUPP) 3587 ic_printf(ic, "ERROR: %s: config %#0x returned " 3588 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS, 3589 error); 3590 } 3591 } 3592 3593 static struct ieee80211_node * 3594 lkpi_ic_node_alloc(struct ieee80211vap *vap, 3595 const uint8_t mac[IEEE80211_ADDR_LEN]) 3596 { 3597 struct ieee80211com *ic; 3598 struct lkpi_hw *lhw; 3599 struct ieee80211_node *ni; 3600 struct ieee80211_hw *hw; 3601 struct lkpi_sta *lsta; 3602 3603 ic = vap->iv_ic; 3604 lhw = ic->ic_softc; 3605 3606 /* We keep allocations de-coupled so we can deal with the two worlds. */ 3607 if (lhw->ic_node_alloc == NULL) 3608 return (NULL); 3609 3610 ni = lhw->ic_node_alloc(vap, mac); 3611 if (ni == NULL) 3612 return (NULL); 3613 3614 hw = LHW_TO_HW(lhw); 3615 lsta = lkpi_lsta_alloc(vap, mac, hw, ni); 3616 if (lsta == NULL) { 3617 if (lhw->ic_node_free != NULL) 3618 lhw->ic_node_free(ni); 3619 return (NULL); 3620 } 3621 3622 return (ni); 3623 } 3624 3625 static int 3626 lkpi_ic_node_init(struct ieee80211_node *ni) 3627 { 3628 struct ieee80211com *ic; 3629 struct lkpi_hw *lhw; 3630 int error; 3631 3632 ic = ni->ni_ic; 3633 lhw = ic->ic_softc; 3634 3635 if (lhw->ic_node_init != NULL) { 3636 error = lhw->ic_node_init(ni); 3637 if (error != 0) 3638 return (error); 3639 } 3640 3641 /* XXX-BZ Sync other state over. */ 3642 IMPROVE(); 3643 3644 return (0); 3645 } 3646 3647 static void 3648 lkpi_ic_node_cleanup(struct ieee80211_node *ni) 3649 { 3650 struct ieee80211com *ic; 3651 struct lkpi_hw *lhw; 3652 3653 ic = ni->ni_ic; 3654 lhw = ic->ic_softc; 3655 3656 /* XXX-BZ remove from driver, ... */ 3657 IMPROVE(); 3658 3659 if (lhw->ic_node_cleanup != NULL) 3660 lhw->ic_node_cleanup(ni); 3661 } 3662 3663 static void 3664 lkpi_ic_node_free(struct ieee80211_node *ni) 3665 { 3666 struct ieee80211com *ic; 3667 struct lkpi_hw *lhw; 3668 struct lkpi_sta *lsta; 3669 3670 ic = ni->ni_ic; 3671 lhw = ic->ic_softc; 3672 lsta = ni->ni_drv_data; 3673 3674 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */ 3675 3676 /* 3677 * Pass in the original ni just in case of error we could check that 3678 * it is the same as lsta->ni. 3679 */ 3680 lkpi_lsta_free(lsta, ni); 3681 3682 if (lhw->ic_node_free != NULL) 3683 lhw->ic_node_free(ni); 3684 } 3685 3686 static int 3687 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3688 const struct ieee80211_bpf_params *params __unused) 3689 { 3690 struct lkpi_sta *lsta; 3691 3692 lsta = ni->ni_drv_data; 3693 LKPI_80211_LSTA_TXQ_LOCK(lsta); 3694 if (!lsta->added_to_drv || !lsta->txq_ready) { 3695 LKPI_80211_LSTA_TXQ_UNLOCK(lsta); 3696 /* 3697 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif! 3698 * ieee80211_raw_output() does that in case of error). 3699 */ 3700 m_free(m); 3701 return (ENETDOWN); 3702 } 3703 3704 /* Queue the packet and enqueue the task to handle it. */ 3705 mbufq_enqueue(&lsta->txq, m); 3706 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task); 3707 LKPI_80211_LSTA_TXQ_UNLOCK(lsta); 3708 3709 #ifdef LINUXKPI_DEBUG_80211 3710 if (linuxkpi_debug_80211 & D80211_TRACE_TX) 3711 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n", 3712 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":", 3713 mbufq_len(&lsta->txq)); 3714 #endif 3715 3716 return (0); 3717 } 3718 3719 static void 3720 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m) 3721 { 3722 struct ieee80211_node *ni; 3723 #ifndef LKPI_80211_HW_CRYPTO 3724 struct ieee80211_frame *wh; 3725 #endif 3726 struct ieee80211_key *k; 3727 struct sk_buff *skb; 3728 struct ieee80211com *ic; 3729 struct lkpi_hw *lhw; 3730 struct ieee80211_hw *hw; 3731 struct lkpi_vif *lvif; 3732 struct ieee80211_vif *vif; 3733 struct ieee80211_channel *c; 3734 struct ieee80211_tx_control control; 3735 struct ieee80211_tx_info *info; 3736 struct ieee80211_sta *sta; 3737 struct ieee80211_hdr *hdr; 3738 struct lkpi_txq *ltxq; 3739 void *buf; 3740 uint8_t ac, tid; 3741 3742 M_ASSERTPKTHDR(m); 3743 #ifdef LINUXKPI_DEBUG_80211 3744 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP) 3745 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0); 3746 #endif 3747 3748 ni = lsta->ni; 3749 k = NULL; 3750 #ifndef LKPI_80211_HW_CRYPTO 3751 /* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */ 3752 wh = mtod(m, struct ieee80211_frame *); 3753 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 3754 /* Retrieve key for TX && do software encryption. */ 3755 k = ieee80211_crypto_encap(ni, m); 3756 if (k == NULL) { 3757 ieee80211_free_node(ni); 3758 m_freem(m); 3759 return; 3760 } 3761 } 3762 #endif 3763 3764 ic = ni->ni_ic; 3765 lhw = ic->ic_softc; 3766 hw = LHW_TO_HW(lhw); 3767 c = ni->ni_chan; 3768 3769 if (ieee80211_radiotap_active_vap(ni->ni_vap)) { 3770 struct lkpi_radiotap_tx_hdr *rtap; 3771 3772 rtap = &lhw->rtap_tx; 3773 rtap->wt_flags = 0; 3774 if (k != NULL) 3775 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3776 if (m->m_flags & M_FRAG) 3777 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG; 3778 IMPROVE(); 3779 rtap->wt_rate = 0; 3780 if (c != NULL && c != IEEE80211_CHAN_ANYC) { 3781 rtap->wt_chan_freq = htole16(c->ic_freq); 3782 rtap->wt_chan_flags = htole16(c->ic_flags); 3783 } 3784 3785 ieee80211_radiotap_tx(ni->ni_vap, m); 3786 } 3787 3788 /* 3789 * net80211 should handle hw->extra_tx_headroom. 3790 * Though for as long as we are copying we don't mind. 3791 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp: 3792 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html 3793 */ 3794 skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len); 3795 if (skb == NULL) { 3796 ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__); 3797 ieee80211_free_node(ni); 3798 m_freem(m); 3799 return; 3800 } 3801 skb_reserve(skb, hw->extra_tx_headroom); 3802 3803 /* XXX-BZ we need a SKB version understanding mbuf. */ 3804 /* Save the mbuf for ieee80211_tx_complete(). */ 3805 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf; 3806 skb->m = m; 3807 #if 0 3808 skb_put_data(skb, m->m_data, m->m_pkthdr.len); 3809 #else 3810 buf = skb_put(skb, m->m_pkthdr.len); 3811 m_copydata(m, 0, m->m_pkthdr.len, buf); 3812 #endif 3813 /* Save the ni. */ 3814 m->m_pkthdr.PH_loc.ptr = ni; 3815 3816 lvif = VAP_TO_LVIF(ni->ni_vap); 3817 vif = LVIF_TO_VIF(lvif); 3818 3819 hdr = (void *)skb->data; 3820 tid = linuxkpi_ieee80211_get_tid(hdr, true); 3821 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */ 3822 if (!ieee80211_is_data(hdr->frame_control)) { 3823 /* MGMT and CTRL frames go on TID 7/VO. */ 3824 skb->priority = 7; 3825 ac = IEEE80211_AC_VO; 3826 } else { 3827 /* Other non-QOS traffic goes to BE. */ 3828 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */ 3829 skb->priority = 0; 3830 ac = IEEE80211_AC_BE; 3831 } 3832 } else { 3833 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK; 3834 ac = ieee80211e_up_to_ac[tid & 7]; 3835 } 3836 skb_set_queue_mapping(skb, ac); 3837 3838 info = IEEE80211_SKB_CB(skb); 3839 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 3840 /* Slight delay; probably only happens on scanning so fine? */ 3841 if (c == NULL || c == IEEE80211_CHAN_ANYC) 3842 c = ic->ic_curchan; 3843 info->band = lkpi_net80211_chan_to_nl80211_band(c); 3844 info->hw_queue = vif->hw_queue[ac]; 3845 if (m->m_flags & M_EAPOL) 3846 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO; 3847 info->control.vif = vif; 3848 /* XXX-BZ info->control.rates */ 3849 #ifdef __notyet__ 3850 #ifdef LKPI_80211_HT 3851 info->control.rts_cts_rate_idx= 3852 info->control.use_rts= /* RTS */ 3853 info->control.use_cts_prot= /* RTS/CTS*/ 3854 #endif 3855 #endif 3856 3857 sta = LSTA_TO_STA(lsta); 3858 #ifdef LKPI_80211_HW_CRYPTO 3859 info->control.hw_key = lsta->kc; 3860 #endif 3861 3862 IMPROVE(); 3863 3864 ltxq = NULL; 3865 if (!ieee80211_is_data_present(hdr->frame_control)) { 3866 if (vif->type == NL80211_IFTYPE_STATION && 3867 lsta->added_to_drv && 3868 sta->txq[IEEE80211_NUM_TIDS] != NULL) 3869 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]); 3870 } else if (lsta->added_to_drv && 3871 sta->txq[skb->priority] != NULL) { 3872 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]); 3873 } 3874 if (ltxq == NULL) 3875 goto ops_tx; 3876 3877 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p " 3878 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq)); 3879 3880 LKPI_80211_LTXQ_LOCK(ltxq); 3881 skb_queue_tail(<xq->skbq, skb); 3882 #ifdef LINUXKPI_DEBUG_80211 3883 if (linuxkpi_debug_80211 & D80211_TRACE_TX) 3884 printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p " 3885 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } " 3886 "WAKE_TX_Q ac %d prio %u qmap %u\n", 3887 __func__, __LINE__, 3888 curthread->td_tid, (unsigned int)ticks, 3889 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq, 3890 skb_queue_len(<xq->skbq), ltxq->txq.ac, 3891 ltxq->txq.tid, ac, skb->priority, skb->qmap); 3892 #endif 3893 LKPI_80211_LTXQ_UNLOCK(ltxq); 3894 LKPI_80211_LHW_LOCK(lhw); 3895 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq); 3896 LKPI_80211_LHW_UNLOCK(lhw); 3897 return; 3898 3899 ops_tx: 3900 #ifdef LINUXKPI_DEBUG_80211 3901 if (linuxkpi_debug_80211 & D80211_TRACE_TX) 3902 printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p " 3903 "TX ac %d prio %u qmap %u\n", 3904 __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":", 3905 skb, ac, skb->priority, skb->qmap); 3906 #endif 3907 memset(&control, 0, sizeof(control)); 3908 control.sta = sta; 3909 LKPI_80211_LHW_LOCK(lhw); 3910 lkpi_80211_mo_tx(hw, &control, skb); 3911 LKPI_80211_LHW_UNLOCK(lhw); 3912 } 3913 3914 static void 3915 lkpi_80211_txq_task(void *ctx, int pending) 3916 { 3917 struct lkpi_sta *lsta; 3918 struct mbufq mq; 3919 struct mbuf *m; 3920 bool shall_tx; 3921 3922 lsta = ctx; 3923 3924 #ifdef LINUXKPI_DEBUG_80211 3925 if (linuxkpi_debug_80211 & D80211_TRACE_TX) 3926 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n", 3927 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":", 3928 pending, mbufq_len(&lsta->txq)); 3929 #endif 3930 3931 mbufq_init(&mq, IFQ_MAXLEN); 3932 3933 LKPI_80211_LSTA_TXQ_LOCK(lsta); 3934 /* 3935 * Do not re-check lsta->txq_ready here; we may have a pending 3936 * disassoc/deauth frame still. On the contrary if txq_ready is 3937 * false we do not have a valid sta anymore in the firmware so no 3938 * point to try to TX. 3939 * We also use txq_ready as a semaphore and will drain the txq manually 3940 * if needed on our way towards SCAN/INIT in the state machine. 3941 */ 3942 shall_tx = lsta->added_to_drv && lsta->txq_ready; 3943 if (__predict_true(shall_tx)) 3944 mbufq_concat(&mq, &lsta->txq); 3945 /* 3946 * else a state change will push the packets out manually or 3947 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs. 3948 */ 3949 LKPI_80211_LSTA_TXQ_UNLOCK(lsta); 3950 3951 m = mbufq_dequeue(&mq); 3952 while (m != NULL) { 3953 lkpi_80211_txq_tx_one(lsta, m); 3954 m = mbufq_dequeue(&mq); 3955 } 3956 } 3957 3958 static int 3959 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m) 3960 { 3961 3962 /* XXX TODO */ 3963 IMPROVE(); 3964 3965 /* Quick and dirty cheating hack. */ 3966 struct ieee80211_node *ni; 3967 3968 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 3969 return (lkpi_ic_raw_xmit(ni, m, NULL)); 3970 } 3971 3972 #ifdef LKPI_80211_HT 3973 static int 3974 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh, 3975 const uint8_t *frm, const uint8_t *efrm) 3976 { 3977 struct ieee80211com *ic; 3978 struct lkpi_hw *lhw; 3979 3980 ic = ni->ni_ic; 3981 lhw = ic->ic_softc; 3982 3983 IMPROVE_HT(); 3984 3985 return (lhw->ic_recv_action(ni, wh, frm, efrm)); 3986 } 3987 3988 static int 3989 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa) 3990 { 3991 struct ieee80211com *ic; 3992 struct lkpi_hw *lhw; 3993 3994 ic = ni->ni_ic; 3995 lhw = ic->ic_softc; 3996 3997 IMPROVE_HT(); 3998 3999 return (lhw->ic_send_action(ni, category, action, sa)); 4000 } 4001 4002 4003 static int 4004 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 4005 { 4006 struct ieee80211com *ic; 4007 struct lkpi_hw *lhw; 4008 4009 ic = ni->ni_ic; 4010 lhw = ic->ic_softc; 4011 4012 IMPROVE_HT(); 4013 4014 return (lhw->ic_ampdu_enable(ni, tap)); 4015 } 4016 4017 static int 4018 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 4019 int dialogtoken, int baparamset, int batimeout) 4020 { 4021 struct ieee80211com *ic; 4022 struct lkpi_hw *lhw; 4023 4024 ic = ni->ni_ic; 4025 lhw = ic->ic_softc; 4026 4027 IMPROVE_HT(); 4028 4029 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout)); 4030 } 4031 4032 static int 4033 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 4034 int status, int baparamset, int batimeout) 4035 { 4036 struct ieee80211com *ic; 4037 struct lkpi_hw *lhw; 4038 4039 ic = ni->ni_ic; 4040 lhw = ic->ic_softc; 4041 4042 IMPROVE_HT(); 4043 4044 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout)); 4045 } 4046 4047 static void 4048 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 4049 { 4050 struct ieee80211com *ic; 4051 struct lkpi_hw *lhw; 4052 4053 ic = ni->ni_ic; 4054 lhw = ic->ic_softc; 4055 4056 IMPROVE_HT(); 4057 4058 lhw->ic_addba_stop(ni, tap); 4059 } 4060 4061 static void 4062 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 4063 { 4064 struct ieee80211com *ic; 4065 struct lkpi_hw *lhw; 4066 4067 ic = ni->ni_ic; 4068 lhw = ic->ic_softc; 4069 4070 IMPROVE_HT(); 4071 4072 lhw->ic_addba_response_timeout(ni, tap); 4073 } 4074 4075 static void 4076 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 4077 int status) 4078 { 4079 struct ieee80211com *ic; 4080 struct lkpi_hw *lhw; 4081 4082 ic = ni->ni_ic; 4083 lhw = ic->ic_softc; 4084 4085 IMPROVE_HT(); 4086 4087 lhw->ic_bar_response(ni, tap, status); 4088 } 4089 4090 static int 4091 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap, 4092 int baparamset, int batimeout, int baseqctl) 4093 { 4094 struct ieee80211com *ic; 4095 struct lkpi_hw *lhw; 4096 struct ieee80211_hw *hw; 4097 struct ieee80211vap *vap; 4098 struct lkpi_vif *lvif; 4099 struct ieee80211_vif *vif; 4100 struct lkpi_sta *lsta; 4101 struct ieee80211_sta *sta; 4102 struct ieee80211_ampdu_params params; 4103 int error; 4104 4105 ic = ni->ni_ic; 4106 lhw = ic->ic_softc; 4107 hw = LHW_TO_HW(lhw); 4108 vap = ni->ni_vap; 4109 lvif = VAP_TO_LVIF(vap); 4110 vif = LVIF_TO_VIF(lvif); 4111 lsta = ni->ni_drv_data; 4112 sta = LSTA_TO_STA(lsta); 4113 4114 params.sta = sta; 4115 params.action = IEEE80211_AMPDU_RX_START; 4116 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ); 4117 if (params.buf_size == 0) 4118 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT; 4119 else 4120 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT); 4121 if (params.buf_size > hw->max_rx_aggregation_subframes) 4122 params.buf_size = hw->max_rx_aggregation_subframes; 4123 params.timeout = le16toh(batimeout); 4124 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START); 4125 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID); 4126 params.amsdu = false; 4127 4128 IMPROVE_HT("Do we need to distinguish based on SUPPORTS_REORDERING_BUFFER?"); 4129 4130 /* This may call kalloc. Make sure we can sleep. */ 4131 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms); 4132 if (error != 0) { 4133 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n", 4134 __func__, error, ni, rap); 4135 return (error); 4136 } 4137 IMPROVE_HT("net80211 is missing the error check on return and assumes success"); 4138 4139 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl); 4140 return (error); 4141 } 4142 4143 static void 4144 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap) 4145 { 4146 struct ieee80211com *ic; 4147 struct lkpi_hw *lhw; 4148 struct ieee80211_hw *hw; 4149 struct ieee80211vap *vap; 4150 struct lkpi_vif *lvif; 4151 struct ieee80211_vif *vif; 4152 struct lkpi_sta *lsta; 4153 struct ieee80211_sta *sta; 4154 struct ieee80211_ampdu_params params; 4155 int error; 4156 uint8_t tid; 4157 4158 ic = ni->ni_ic; 4159 lhw = ic->ic_softc; 4160 4161 /* 4162 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if 4163 * we did not START. Some drivers pass it down to firmware which will 4164 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from 4165 * ieee80211_ht_node_init() amongst others which will iterate over all 4166 * tid and call ic_ampdu_rx_stop() unconditionally. 4167 * XXX net80211 should probably be more "gentle" in these cases and 4168 * track some state itself. 4169 */ 4170 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0) 4171 goto net80211_only; 4172 4173 hw = LHW_TO_HW(lhw); 4174 vap = ni->ni_vap; 4175 lvif = VAP_TO_LVIF(vap); 4176 vif = LVIF_TO_VIF(lvif); 4177 lsta = ni->ni_drv_data; 4178 sta = LSTA_TO_STA(lsta); 4179 4180 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba."); 4181 for (tid = 0; tid < WME_NUM_TID; tid++) { 4182 if (&ni->ni_rx_ampdu[tid] == rap) 4183 break; 4184 } 4185 4186 params.sta = sta; 4187 params.action = IEEE80211_AMPDU_RX_STOP; 4188 params.buf_size = 0; 4189 params.timeout = 0; 4190 params.ssn = 0; 4191 params.tid = tid; 4192 params.amsdu = false; 4193 4194 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms); 4195 if (error != 0) 4196 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n", 4197 __func__, error, ni, rap); 4198 4199 net80211_only: 4200 lhw->ic_ampdu_rx_stop(ni, rap); 4201 } 4202 #endif 4203 4204 static void 4205 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw, 4206 uint8_t *bands, int *chan_flags, enum nl80211_band band) 4207 { 4208 #ifdef LKPI_80211_HT 4209 struct ieee80211_sta_ht_cap *ht_cap; 4210 4211 ht_cap = &hw->wiphy->bands[band]->ht_cap; 4212 if (!ht_cap->ht_supported) 4213 return; 4214 4215 switch (band) { 4216 case NL80211_BAND_2GHZ: 4217 setbit(bands, IEEE80211_MODE_11NG); 4218 break; 4219 case NL80211_BAND_5GHZ: 4220 setbit(bands, IEEE80211_MODE_11NA); 4221 break; 4222 default: 4223 IMPROVE("Unsupported band %d", band); 4224 return; 4225 } 4226 4227 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */ 4228 4229 /* 4230 * Rather than manually checking each flag and 4231 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_, 4232 * simply copy the 16bits. 4233 */ 4234 ic->ic_htcaps |= ht_cap->cap; 4235 4236 /* Then deal with the other flags. */ 4237 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) 4238 ic->ic_htcaps |= IEEE80211_HTC_AMPDU; 4239 #ifdef __notyet__ 4240 if (ieee80211_hw_check(hw, TX_AMSDU)) 4241 ic->ic_htcaps |= IEEE80211_HTC_AMSDU; 4242 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU)) 4243 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU | 4244 IEEE80211_HTC_TX_AMSDU_AMPDU); 4245 #endif 4246 4247 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ..."); 4248 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF; 4249 4250 /* Only add HT40 channels if supported. */ 4251 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 && 4252 chan_flags != NULL) 4253 *chan_flags |= NET80211_CBW_FLAG_HT40; 4254 #endif 4255 } 4256 4257 static void 4258 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan, 4259 int *n, struct ieee80211_channel *c) 4260 { 4261 struct lkpi_hw *lhw; 4262 struct ieee80211_hw *hw; 4263 struct linuxkpi_ieee80211_channel *channels; 4264 uint8_t bands[IEEE80211_MODE_BYTES]; 4265 int chan_flags, error, i, nchans; 4266 4267 /* Channels */ 4268 lhw = ic->ic_softc; 4269 hw = LHW_TO_HW(lhw); 4270 4271 /* NL80211_BAND_2GHZ */ 4272 nchans = 0; 4273 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL) 4274 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels; 4275 if (nchans > 0) { 4276 memset(bands, 0, sizeof(bands)); 4277 chan_flags = 0; 4278 setbit(bands, IEEE80211_MODE_11B); 4279 /* XXX-BZ unclear how to check for 11g. */ 4280 4281 IMPROVE("the bitrates may have flags?"); 4282 setbit(bands, IEEE80211_MODE_11G); 4283 4284 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags, 4285 NL80211_BAND_2GHZ); 4286 4287 channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels; 4288 for (i = 0; i < nchans && *n < maxchan; i++) { 4289 uint32_t nflags = 0; 4290 int cflags = chan_flags; 4291 4292 if (channels[i].flags & IEEE80211_CHAN_DISABLED) { 4293 ic_printf(ic, "%s: Skipping disabled chan " 4294 "[%u/%u/%#x]\n", __func__, 4295 channels[i].hw_value, 4296 channels[i].center_freq, channels[i].flags); 4297 continue; 4298 } 4299 if (channels[i].flags & IEEE80211_CHAN_NO_IR) 4300 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE); 4301 if (channels[i].flags & IEEE80211_CHAN_RADAR) 4302 nflags |= IEEE80211_CHAN_DFS; 4303 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ) 4304 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80); 4305 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ) 4306 cflags &= ~NET80211_CBW_FLAG_VHT80; 4307 /* XXX how to map the remaining enum ieee80211_channel_flags? */ 4308 if (channels[i].flags & IEEE80211_CHAN_NO_HT40) 4309 cflags &= ~NET80211_CBW_FLAG_HT40; 4310 4311 error = ieee80211_add_channel_cbw(c, maxchan, n, 4312 channels[i].hw_value, channels[i].center_freq, 4313 channels[i].max_power, 4314 nflags, bands, cflags); 4315 /* net80211::ENOBUFS: *n >= maxchans */ 4316 if (error != 0 && error != ENOBUFS) 4317 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x " 4318 "returned error %d\n", 4319 __func__, channels[i].hw_value, 4320 channels[i].center_freq, channels[i].flags, 4321 nflags, chan_flags, cflags, error); 4322 if (error != 0) 4323 break; 4324 } 4325 } 4326 4327 /* NL80211_BAND_5GHZ */ 4328 nchans = 0; 4329 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL) 4330 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels; 4331 if (nchans > 0) { 4332 memset(bands, 0, sizeof(bands)); 4333 chan_flags = 0; 4334 setbit(bands, IEEE80211_MODE_11A); 4335 4336 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags, 4337 NL80211_BAND_5GHZ); 4338 4339 #ifdef LKPI_80211_VHT 4340 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){ 4341 4342 ic->ic_flags_ext |= IEEE80211_FEXT_VHT; 4343 ic->ic_vht_cap.vht_cap_info = 4344 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap; 4345 4346 setbit(bands, IEEE80211_MODE_VHT_5GHZ); 4347 chan_flags |= NET80211_CBW_FLAG_VHT80; 4348 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ( 4349 ic->ic_vht_cap.vht_cap_info)) 4350 chan_flags |= NET80211_CBW_FLAG_VHT160; 4351 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ( 4352 ic->ic_vht_cap.vht_cap_info)) 4353 chan_flags |= NET80211_CBW_FLAG_VHT80P80; 4354 } 4355 #endif 4356 4357 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels; 4358 for (i = 0; i < nchans && *n < maxchan; i++) { 4359 uint32_t nflags = 0; 4360 int cflags = chan_flags; 4361 4362 if (channels[i].flags & IEEE80211_CHAN_DISABLED) { 4363 ic_printf(ic, "%s: Skipping disabled chan " 4364 "[%u/%u/%#x]\n", __func__, 4365 channels[i].hw_value, 4366 channels[i].center_freq, channels[i].flags); 4367 continue; 4368 } 4369 if (channels[i].flags & IEEE80211_CHAN_NO_IR) 4370 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE); 4371 if (channels[i].flags & IEEE80211_CHAN_RADAR) 4372 nflags |= IEEE80211_CHAN_DFS; 4373 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ) 4374 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80); 4375 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ) 4376 cflags &= ~NET80211_CBW_FLAG_VHT80; 4377 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */ 4378 if (channels[i].flags & IEEE80211_CHAN_NO_HT40) 4379 cflags &= ~NET80211_CBW_FLAG_HT40; 4380 4381 error = ieee80211_add_channel_cbw(c, maxchan, n, 4382 channels[i].hw_value, channels[i].center_freq, 4383 channels[i].max_power, 4384 nflags, bands, cflags); 4385 /* net80211::ENOBUFS: *n >= maxchans */ 4386 if (error != 0 && error != ENOBUFS) 4387 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x " 4388 "returned error %d\n", 4389 __func__, channels[i].hw_value, 4390 channels[i].center_freq, channels[i].flags, 4391 nflags, chan_flags, cflags, error); 4392 if (error != 0) 4393 break; 4394 } 4395 } 4396 } 4397 4398 static void * 4399 lkpi_ieee80211_ifalloc(void) 4400 { 4401 struct ieee80211com *ic; 4402 4403 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO); 4404 4405 /* Setting these happens later when we have device information. */ 4406 ic->ic_softc = NULL; 4407 ic->ic_name = "linuxkpi"; 4408 4409 return (ic); 4410 } 4411 4412 struct ieee80211_hw * 4413 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops) 4414 { 4415 struct ieee80211_hw *hw; 4416 struct lkpi_hw *lhw; 4417 struct wiphy *wiphy; 4418 int ac; 4419 4420 /* Get us and the driver data also allocated. */ 4421 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len); 4422 if (wiphy == NULL) 4423 return (NULL); 4424 4425 lhw = wiphy_priv(wiphy); 4426 lhw->ops = ops; 4427 4428 LKPI_80211_LHW_LOCK_INIT(lhw); 4429 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw); 4430 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw); 4431 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK); 4432 TAILQ_INIT(&lhw->lvif_head); 4433 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 4434 lhw->txq_generation[ac] = 1; 4435 TAILQ_INIT(&lhw->scheduled_txqs[ac]); 4436 } 4437 4438 /* Deferred RX path. */ 4439 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw); 4440 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw); 4441 mbufq_init(&lhw->rxq, IFQ_MAXLEN); 4442 lhw->rxq_stopped = false; 4443 4444 /* 4445 * XXX-BZ TODO make sure there is a "_null" function to all ops 4446 * not initialized. 4447 */ 4448 hw = LHW_TO_HW(lhw); 4449 hw->wiphy = wiphy; 4450 hw->conf.flags |= IEEE80211_CONF_IDLE; 4451 hw->priv = (void *)(lhw + 1); 4452 4453 /* BSD Specific. */ 4454 lhw->ic = lkpi_ieee80211_ifalloc(); 4455 4456 IMPROVE(); 4457 4458 return (hw); 4459 } 4460 4461 void 4462 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw) 4463 { 4464 struct lkpi_hw *lhw; 4465 struct mbuf *m; 4466 4467 lhw = HW_TO_LHW(hw); 4468 free(lhw->ic, M_LKPI80211); 4469 lhw->ic = NULL; 4470 4471 /* 4472 * Drain the deferred RX path. 4473 */ 4474 LKPI_80211_LHW_RXQ_LOCK(lhw); 4475 lhw->rxq_stopped = true; 4476 LKPI_80211_LHW_RXQ_UNLOCK(lhw); 4477 4478 /* Drain taskq, won't be restarted due to rxq_stopped being set. */ 4479 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0) 4480 taskqueue_drain(taskqueue_thread, &lhw->rxq_task); 4481 4482 /* Flush mbufq (make sure to release ni refs!). */ 4483 m = mbufq_dequeue(&lhw->rxq); 4484 while (m != NULL) { 4485 struct m_tag *mtag; 4486 4487 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL); 4488 if (mtag != NULL) { 4489 struct lkpi_80211_tag_rxni *rxni; 4490 4491 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1); 4492 ieee80211_free_node(rxni->ni); 4493 } 4494 m_freem(m); 4495 m = mbufq_dequeue(&lhw->rxq); 4496 } 4497 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n", 4498 __func__, lhw, mbufq_len(&lhw->rxq))); 4499 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw); 4500 4501 /* Cleanup more of lhw here or in wiphy_free()? */ 4502 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw); 4503 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw); 4504 LKPI_80211_LHW_LOCK_DESTROY(lhw); 4505 sx_destroy(&lhw->lvif_sx); 4506 IMPROVE(); 4507 } 4508 4509 void 4510 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name) 4511 { 4512 struct lkpi_hw *lhw; 4513 struct ieee80211com *ic; 4514 4515 lhw = HW_TO_LHW(hw); 4516 ic = lhw->ic; 4517 4518 /* Now set a proper name before ieee80211_ifattach(). */ 4519 ic->ic_softc = lhw; 4520 ic->ic_name = name; 4521 4522 /* XXX-BZ do we also need to set wiphy name? */ 4523 } 4524 4525 struct ieee80211_hw * 4526 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy) 4527 { 4528 struct lkpi_hw *lhw; 4529 4530 lhw = wiphy_priv(wiphy); 4531 return (LHW_TO_HW(lhw)); 4532 } 4533 4534 static void 4535 lkpi_radiotap_attach(struct lkpi_hw *lhw) 4536 { 4537 struct ieee80211com *ic; 4538 4539 ic = lhw->ic; 4540 ieee80211_radiotap_attach(ic, 4541 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx), 4542 LKPI_RTAP_TX_FLAGS_PRESENT, 4543 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx), 4544 LKPI_RTAP_RX_FLAGS_PRESENT); 4545 } 4546 4547 int 4548 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw) 4549 { 4550 struct ieee80211com *ic; 4551 struct lkpi_hw *lhw; 4552 int band, i; 4553 4554 lhw = HW_TO_LHW(hw); 4555 ic = lhw->ic; 4556 4557 /* We do it this late as wiphy->dev should be set for the name. */ 4558 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0); 4559 if (lhw->workq == NULL) 4560 return (-EAGAIN); 4561 4562 /* XXX-BZ figure this out how they count his... */ 4563 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) { 4564 IEEE80211_ADDR_COPY(ic->ic_macaddr, 4565 hw->wiphy->perm_addr); 4566 } else if (hw->wiphy->n_addresses > 0) { 4567 /* We take the first one. */ 4568 IEEE80211_ADDR_COPY(ic->ic_macaddr, 4569 hw->wiphy->addresses[0].addr); 4570 } else { 4571 ic_printf(ic, "%s: warning, no hardware address!\n", __func__); 4572 } 4573 4574 #ifdef __not_yet__ 4575 /* See comment in lkpi_80211_txq_tx_one(). */ 4576 ic->ic_headroom = hw->extra_tx_headroom; 4577 #endif 4578 4579 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 4580 ic->ic_opmode = IEEE80211_M_STA; 4581 4582 /* Set device capabilities. */ 4583 /* XXX-BZ we need to get these from linux80211/drivers and convert. */ 4584 ic->ic_caps = 4585 IEEE80211_C_STA | 4586 IEEE80211_C_MONITOR | 4587 IEEE80211_C_WPA | /* WPA/RSN */ 4588 #ifdef LKPI_80211_WME 4589 IEEE80211_C_WME | 4590 #endif 4591 #if 0 4592 IEEE80211_C_PMGT | 4593 #endif 4594 IEEE80211_C_SHSLOT | /* short slot time supported */ 4595 IEEE80211_C_SHPREAMBLE /* short preamble supported */ 4596 ; 4597 #if 0 4598 /* Scanning is a different kind of beast to re-work. */ 4599 ic->ic_caps |= IEEE80211_C_BGSCAN; 4600 #endif 4601 if (lhw->ops->hw_scan) { 4602 /* 4603 * Advertise full-offload scanning. 4604 * 4605 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise 4606 * we essentially disable hw_scan for all drivers not setting 4607 * the flag. 4608 */ 4609 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD; 4610 lhw->scan_flags |= LKPI_LHW_SCAN_HW; 4611 } 4612 4613 /* 4614 * The wiphy variables report bitmasks of avail antennas. 4615 * (*get_antenna) get the current bitmask sets which can be 4616 * altered by (*set_antenna) for some drivers. 4617 * XXX-BZ will the count alone do us much good long-term in net80211? 4618 */ 4619 if (hw->wiphy->available_antennas_rx || 4620 hw->wiphy->available_antennas_tx) { 4621 uint32_t rxs, txs; 4622 4623 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) { 4624 ic->ic_rxstream = bitcount32(rxs); 4625 ic->ic_txstream = bitcount32(txs); 4626 } 4627 } 4628 4629 ic->ic_cryptocaps = 0; 4630 #ifdef LKPI_80211_HW_CRYPTO 4631 if (hw->wiphy->n_cipher_suites > 0) { 4632 for (i = 0; i < hw->wiphy->n_cipher_suites; i++) 4633 ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers( 4634 hw->wiphy->cipher_suites[i]); 4635 } 4636 #endif 4637 4638 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans, 4639 ic->ic_channels); 4640 4641 ieee80211_ifattach(ic); 4642 4643 ic->ic_update_mcast = lkpi_ic_update_mcast; 4644 ic->ic_update_promisc = lkpi_ic_update_promisc; 4645 ic->ic_update_chw = lkpi_ic_update_chw; 4646 ic->ic_parent = lkpi_ic_parent; 4647 ic->ic_scan_start = lkpi_ic_scan_start; 4648 ic->ic_scan_end = lkpi_ic_scan_end; 4649 ic->ic_set_channel = lkpi_ic_set_channel; 4650 ic->ic_transmit = lkpi_ic_transmit; 4651 ic->ic_raw_xmit = lkpi_ic_raw_xmit; 4652 ic->ic_vap_create = lkpi_ic_vap_create; 4653 ic->ic_vap_delete = lkpi_ic_vap_delete; 4654 ic->ic_getradiocaps = lkpi_ic_getradiocaps; 4655 ic->ic_wme.wme_update = lkpi_ic_wme_update; 4656 4657 lhw->ic_scan_curchan = ic->ic_scan_curchan; 4658 ic->ic_scan_curchan = lkpi_ic_scan_curchan; 4659 lhw->ic_scan_mindwell = ic->ic_scan_mindwell; 4660 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell; 4661 4662 lhw->ic_node_alloc = ic->ic_node_alloc; 4663 ic->ic_node_alloc = lkpi_ic_node_alloc; 4664 lhw->ic_node_init = ic->ic_node_init; 4665 ic->ic_node_init = lkpi_ic_node_init; 4666 lhw->ic_node_cleanup = ic->ic_node_cleanup; 4667 ic->ic_node_cleanup = lkpi_ic_node_cleanup; 4668 lhw->ic_node_free = ic->ic_node_free; 4669 ic->ic_node_free = lkpi_ic_node_free; 4670 4671 #ifdef LKPI_80211_HT 4672 lhw->ic_recv_action = ic->ic_recv_action; 4673 ic->ic_recv_action = lkpi_ic_recv_action; 4674 lhw->ic_send_action = ic->ic_send_action; 4675 ic->ic_send_action = lkpi_ic_send_action; 4676 4677 lhw->ic_ampdu_enable = ic->ic_ampdu_enable; 4678 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable; 4679 4680 lhw->ic_addba_request = ic->ic_addba_request; 4681 ic->ic_addba_request = lkpi_ic_addba_request; 4682 lhw->ic_addba_response = ic->ic_addba_response; 4683 ic->ic_addba_response = lkpi_ic_addba_response; 4684 lhw->ic_addba_stop = ic->ic_addba_stop; 4685 ic->ic_addba_stop = lkpi_ic_addba_stop; 4686 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout; 4687 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout; 4688 4689 lhw->ic_bar_response = ic->ic_bar_response; 4690 ic->ic_bar_response = lkpi_ic_bar_response; 4691 4692 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start; 4693 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start; 4694 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop; 4695 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop; 4696 #endif 4697 4698 lkpi_radiotap_attach(lhw); 4699 4700 /* 4701 * Assign the first possible channel for now; seems Realtek drivers 4702 * expect one. 4703 * Also remember the amount of bands we support and the most rates 4704 * in any band so we can scale [(ext) sup rates] IE(s) accordingly. 4705 */ 4706 lhw->supbands = lhw->max_rates = 0; 4707 for (band = 0; band < NUM_NL80211_BANDS; band++) { 4708 struct ieee80211_supported_band *supband; 4709 struct linuxkpi_ieee80211_channel *channels; 4710 4711 supband = hw->wiphy->bands[band]; 4712 if (supband == NULL || supband->n_channels == 0) 4713 continue; 4714 4715 lhw->supbands++; 4716 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates); 4717 4718 /* If we have a channel, we need to keep counting supbands. */ 4719 if (hw->conf.chandef.chan != NULL) 4720 continue; 4721 4722 channels = supband->channels; 4723 for (i = 0; i < supband->n_channels; i++) { 4724 4725 if (channels[i].flags & IEEE80211_CHAN_DISABLED) 4726 continue; 4727 4728 cfg80211_chandef_create(&hw->conf.chandef, &channels[i], 4729 #ifdef LKPI_80211_HT 4730 (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 : 4731 #endif 4732 NL80211_CHAN_NO_HT); 4733 break; 4734 } 4735 } 4736 4737 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?"); 4738 4739 /* Make sure we do not support more than net80211 is willing to take. */ 4740 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) { 4741 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__, 4742 lhw->max_rates, IEEE80211_RATE_MAXSIZE); 4743 lhw->max_rates = IEEE80211_RATE_MAXSIZE; 4744 } 4745 4746 /* 4747 * The maximum supported bitrates on any band + size for 4748 * DSSS Parameter Set give our per-band IE size. 4749 * SSID is the responsibility of the driver and goes on the side. 4750 * The user specified bits coming from the vap go into the 4751 * "common ies" fields. 4752 */ 4753 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE; 4754 if (lhw->max_rates > IEEE80211_RATE_SIZE) 4755 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE); 4756 4757 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) { 4758 /* 4759 * net80211 does not seem to support the DSSS Parameter Set but 4760 * some of the drivers insert it so calculate the extra fixed 4761 * space in. 4762 */ 4763 lhw->scan_ie_len += 2 + 1; 4764 } 4765 4766 #if defined(LKPI_80211_HT) 4767 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0) 4768 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap); 4769 #endif 4770 #if defined(LKPI_80211_VHT) 4771 if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0) 4772 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap); 4773 #endif 4774 4775 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */ 4776 if (hw->wiphy->max_scan_ie_len > 0) { 4777 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len) 4778 goto err; 4779 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len; 4780 } 4781 4782 if (bootverbose) 4783 ieee80211_announce(ic); 4784 4785 return (0); 4786 err: 4787 IMPROVE("TODO FIXME CLEANUP"); 4788 return (-EAGAIN); 4789 } 4790 4791 void 4792 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw) 4793 { 4794 struct lkpi_hw *lhw; 4795 struct ieee80211com *ic; 4796 4797 lhw = HW_TO_LHW(hw); 4798 ic = lhw->ic; 4799 ieee80211_ifdetach(ic); 4800 } 4801 4802 void 4803 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw, 4804 enum ieee80211_iface_iter flags, 4805 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *), 4806 void *arg) 4807 { 4808 struct lkpi_hw *lhw; 4809 struct lkpi_vif *lvif; 4810 struct ieee80211_vif *vif; 4811 bool active, atomic, nin_drv; 4812 4813 lhw = HW_TO_LHW(hw); 4814 4815 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL| 4816 IEEE80211_IFACE_ITER_RESUME_ALL| 4817 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER| 4818 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) { 4819 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n", 4820 __func__, flags); 4821 } 4822 4823 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0; 4824 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0; 4825 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0; 4826 4827 if (atomic) 4828 LKPI_80211_LHW_LVIF_LOCK(lhw); 4829 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 4830 struct ieee80211vap *vap; 4831 4832 vif = LVIF_TO_VIF(lvif); 4833 4834 /* 4835 * If we want "active" interfaces, we need to distinguish on 4836 * whether the driver knows about them or not to be able to 4837 * handle the "resume" case correctly. Skip the ones the 4838 * driver does not know about. 4839 */ 4840 if (active && !lvif->added_to_drv && 4841 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0) 4842 continue; 4843 4844 /* 4845 * If we shall skip interfaces not added to the driver do so 4846 * if we haven't yet. 4847 */ 4848 if (nin_drv && !lvif->added_to_drv) 4849 continue; 4850 4851 /* 4852 * Run the iterator function if we are either not asking 4853 * asking for active only or if the VAP is "running". 4854 */ 4855 /* XXX-BZ probably should have state in the lvif as well. */ 4856 vap = LVIF_TO_VAP(lvif); 4857 if (!active || (vap->iv_state != IEEE80211_S_INIT)) 4858 iterfunc(arg, vif->addr, vif); 4859 } 4860 if (atomic) 4861 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 4862 } 4863 4864 void 4865 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, 4866 struct ieee80211_vif *vif, 4867 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *, 4868 struct ieee80211_sta *, struct ieee80211_key_conf *, void *), 4869 void *arg) 4870 { 4871 4872 UNIMPLEMENTED; 4873 } 4874 4875 void 4876 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw, 4877 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *, 4878 void *), 4879 void *arg) 4880 { 4881 struct lkpi_hw *lhw; 4882 struct lkpi_vif *lvif; 4883 struct ieee80211_vif *vif; 4884 struct lkpi_chanctx *lchanctx; 4885 4886 KASSERT(hw != NULL && iterfunc != NULL, 4887 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg)); 4888 4889 lhw = HW_TO_LHW(hw); 4890 4891 IMPROVE("lchanctx should be its own list somewhere"); 4892 4893 LKPI_80211_LHW_LVIF_LOCK(lhw); 4894 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 4895 4896 vif = LVIF_TO_VIF(lvif); 4897 if (vif->chanctx_conf == NULL) 4898 continue; 4899 4900 lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf); 4901 if (!lchanctx->added_to_drv) 4902 continue; 4903 4904 iterfunc(hw, &lchanctx->chanctx_conf, arg); 4905 } 4906 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 4907 } 4908 4909 void 4910 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw, 4911 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg) 4912 { 4913 struct lkpi_hw *lhw; 4914 struct lkpi_vif *lvif; 4915 struct lkpi_sta *lsta; 4916 struct ieee80211_sta *sta; 4917 4918 KASSERT(hw != NULL && iterfunc != NULL, 4919 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg)); 4920 4921 lhw = HW_TO_LHW(hw); 4922 4923 LKPI_80211_LHW_LVIF_LOCK(lhw); 4924 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 4925 4926 LKPI_80211_LVIF_LOCK(lvif); 4927 TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) { 4928 if (!lsta->added_to_drv) 4929 continue; 4930 sta = LSTA_TO_STA(lsta); 4931 iterfunc(arg, sta); 4932 } 4933 LKPI_80211_LVIF_UNLOCK(lvif); 4934 } 4935 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 4936 } 4937 4938 struct linuxkpi_ieee80211_regdomain * 4939 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n) 4940 { 4941 struct linuxkpi_ieee80211_regdomain *regd; 4942 4943 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule), 4944 GFP_KERNEL); 4945 return (regd); 4946 } 4947 4948 int 4949 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy, 4950 struct linuxkpi_ieee80211_regdomain *regd) 4951 { 4952 struct lkpi_hw *lhw; 4953 struct ieee80211com *ic; 4954 struct ieee80211_regdomain *rd; 4955 4956 lhw = wiphy_priv(wiphy); 4957 ic = lhw->ic; 4958 4959 rd = &ic->ic_regdomain; 4960 if (rd->isocc[0] == '\0') { 4961 rd->isocc[0] = regd->alpha2[0]; 4962 rd->isocc[1] = regd->alpha2[1]; 4963 } 4964 4965 TODO(); 4966 /* XXX-BZ finish the rest. */ 4967 4968 return (0); 4969 } 4970 4971 void 4972 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw, 4973 struct cfg80211_scan_info *info) 4974 { 4975 struct lkpi_hw *lhw; 4976 struct ieee80211com *ic; 4977 struct ieee80211_scan_state *ss; 4978 4979 lhw = wiphy_priv(hw->wiphy); 4980 ic = lhw->ic; 4981 ss = ic->ic_scan; 4982 4983 ieee80211_scan_done(ss->ss_vap); 4984 4985 LKPI_80211_LHW_SCAN_LOCK(lhw); 4986 free(lhw->hw_req, M_LKPI80211); 4987 lhw->hw_req = NULL; 4988 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING; 4989 wakeup(lhw); 4990 LKPI_80211_LHW_SCAN_UNLOCK(lhw); 4991 4992 return; 4993 } 4994 4995 static void 4996 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m) 4997 { 4998 struct ieee80211_node *ni; 4999 struct m_tag *mtag; 5000 int ok; 5001 5002 ni = NULL; 5003 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL); 5004 if (mtag != NULL) { 5005 struct lkpi_80211_tag_rxni *rxni; 5006 5007 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1); 5008 ni = rxni->ni; 5009 } 5010 5011 if (ni != NULL) { 5012 ok = ieee80211_input_mimo(ni, m); 5013 ieee80211_free_node(ni); /* Release the reference. */ 5014 if (ok < 0) 5015 m_freem(m); 5016 } else { 5017 ok = ieee80211_input_mimo_all(lhw->ic, m); 5018 /* mbuf got consumed. */ 5019 } 5020 5021 #ifdef LINUXKPI_DEBUG_80211 5022 if (linuxkpi_debug_80211 & D80211_TRACE_RX) 5023 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok); 5024 #endif 5025 } 5026 5027 static void 5028 lkpi_80211_lhw_rxq_task(void *ctx, int pending) 5029 { 5030 struct lkpi_hw *lhw; 5031 struct mbufq mq; 5032 struct mbuf *m; 5033 5034 lhw = ctx; 5035 5036 #ifdef LINUXKPI_DEBUG_80211 5037 if (linuxkpi_debug_80211 & D80211_TRACE_RX) 5038 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n", 5039 __func__, lhw, pending, mbufq_len(&lhw->rxq)); 5040 #endif 5041 5042 mbufq_init(&mq, IFQ_MAXLEN); 5043 5044 LKPI_80211_LHW_RXQ_LOCK(lhw); 5045 mbufq_concat(&mq, &lhw->rxq); 5046 LKPI_80211_LHW_RXQ_UNLOCK(lhw); 5047 5048 m = mbufq_dequeue(&mq); 5049 while (m != NULL) { 5050 lkpi_80211_lhw_rxq_rx_one(lhw, m); 5051 m = mbufq_dequeue(&mq); 5052 } 5053 } 5054 5055 /* For %list see comment towards the end of the function. */ 5056 void 5057 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb, 5058 struct ieee80211_sta *sta, struct napi_struct *napi __unused, 5059 struct list_head *list __unused) 5060 { 5061 struct lkpi_hw *lhw; 5062 struct ieee80211com *ic; 5063 struct mbuf *m; 5064 struct skb_shared_info *shinfo; 5065 struct ieee80211_rx_status *rx_status; 5066 struct ieee80211_rx_stats rx_stats; 5067 struct ieee80211_node *ni; 5068 struct ieee80211vap *vap; 5069 struct ieee80211_hdr *hdr; 5070 struct lkpi_sta *lsta; 5071 int i, offset, ok; 5072 int8_t rssi; 5073 bool is_beacon; 5074 5075 if (skb->len < 2) { 5076 /* Need 80211 stats here. */ 5077 IMPROVE(); 5078 goto err; 5079 } 5080 5081 /* 5082 * For now do the data copy; we can later improve things. Might even 5083 * have an mbuf backing the skb data then? 5084 */ 5085 m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR); 5086 if (m == NULL) 5087 goto err; 5088 m_copyback(m, 0, skb->tail - skb->data, skb->data); 5089 5090 shinfo = skb_shinfo(skb); 5091 offset = m->m_len; 5092 for (i = 0; i < shinfo->nr_frags; i++) { 5093 m_copyback(m, offset, shinfo->frags[i].size, 5094 (uint8_t *)linux_page_address(shinfo->frags[i].page) + 5095 shinfo->frags[i].offset); 5096 offset += shinfo->frags[i].size; 5097 } 5098 5099 rx_status = IEEE80211_SKB_RXCB(skb); 5100 5101 hdr = (void *)skb->data; 5102 is_beacon = ieee80211_is_beacon(hdr->frame_control); 5103 5104 #ifdef LINUXKPI_DEBUG_80211 5105 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0) 5106 goto no_trace_beacons; 5107 5108 if (linuxkpi_debug_80211 & D80211_TRACE_RX) 5109 printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) " 5110 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n", 5111 __func__, skb, skb->_alloc_len, skb->len, skb->data_len, 5112 skb->truesize, skb->head, skb->data, skb->tail, skb->end, 5113 shinfo, shinfo->nr_frags, 5114 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : ""); 5115 5116 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP) 5117 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0); 5118 5119 /* Implement a dump_rxcb() !!! */ 5120 if (linuxkpi_debug_80211 & D80211_TRACE_RX) 5121 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, " 5122 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n", 5123 __func__, 5124 (uintmax_t)rx_status->boottime_ns, 5125 (uintmax_t)rx_status->mactime, 5126 rx_status->device_timestamp, 5127 rx_status->flag, 5128 rx_status->freq, 5129 rx_status->bw, 5130 rx_status->encoding, 5131 rx_status->ampdu_reference, 5132 rx_status->band, 5133 rx_status->chains, 5134 rx_status->chain_signal[0], 5135 rx_status->chain_signal[1], 5136 rx_status->chain_signal[2], 5137 rx_status->chain_signal[3], 5138 rx_status->signal, 5139 rx_status->enc_flags, 5140 rx_status->he_dcm, 5141 rx_status->he_gi, 5142 rx_status->he_ru, 5143 rx_status->zero_length_psdu_type, 5144 rx_status->nss, 5145 rx_status->rate_idx); 5146 no_trace_beacons: 5147 #endif 5148 5149 memset(&rx_stats, 0, sizeof(rx_stats)); 5150 rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI; 5151 /* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */ 5152 rx_stats.c_nf = -96; 5153 if (ieee80211_hw_check(hw, SIGNAL_DBM) && 5154 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)) 5155 rssi = rx_status->signal; 5156 else 5157 rssi = rx_stats.c_nf; 5158 /* 5159 * net80211 signal strength data are in .5 dBm units relative to 5160 * the current noise floor (see comment in ieee80211_node.h). 5161 */ 5162 rssi -= rx_stats.c_nf; 5163 rx_stats.c_rssi = rssi * 2; 5164 rx_stats.r_flags |= IEEE80211_R_BAND; 5165 rx_stats.c_band = 5166 lkpi_nl80211_band_to_net80211_band(rx_status->band); 5167 rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE; 5168 rx_stats.c_freq = rx_status->freq; 5169 rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band); 5170 5171 /* XXX (*sta_statistics)() to get to some of that? */ 5172 /* XXX-BZ dump the FreeBSD version of rx_stats as well! */ 5173 5174 lhw = HW_TO_LHW(hw); 5175 ic = lhw->ic; 5176 5177 ok = ieee80211_add_rx_params(m, &rx_stats); 5178 if (ok == 0) { 5179 m_freem(m); 5180 counter_u64_add(ic->ic_ierrors, 1); 5181 goto err; 5182 } 5183 5184 lsta = NULL; 5185 if (sta != NULL) { 5186 lsta = STA_TO_LSTA(sta); 5187 ni = ieee80211_ref_node(lsta->ni); 5188 } else { 5189 struct ieee80211_frame_min *wh; 5190 5191 wh = mtod(m, struct ieee80211_frame_min *); 5192 ni = ieee80211_find_rxnode(ic, wh); 5193 if (ni != NULL) 5194 lsta = ni->ni_drv_data; 5195 } 5196 5197 if (ni != NULL) 5198 vap = ni->ni_vap; 5199 else 5200 /* 5201 * XXX-BZ can we improve this by looking at the frame hdr 5202 * or other meta-data passed up? 5203 */ 5204 vap = TAILQ_FIRST(&ic->ic_vaps); 5205 5206 #ifdef LINUXKPI_DEBUG_80211 5207 if (linuxkpi_debug_80211 & D80211_TRACE_RX) 5208 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n", 5209 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1, 5210 ni, vap, is_beacon ? " beacon" : ""); 5211 #endif 5212 5213 if (ni != NULL && vap != NULL && is_beacon && 5214 rx_status->device_timestamp > 0 && 5215 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) { 5216 struct lkpi_vif *lvif; 5217 struct ieee80211_vif *vif; 5218 struct ieee80211_frame *wh; 5219 5220 wh = mtod(m, struct ieee80211_frame *); 5221 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) 5222 goto skip_device_ts; 5223 5224 lvif = VAP_TO_LVIF(vap); 5225 vif = LVIF_TO_VIF(lvif); 5226 5227 IMPROVE("TIMING_BEACON_ONLY?"); 5228 /* mac80211 specific (not net80211) so keep it here. */ 5229 vif->bss_conf.sync_device_ts = rx_status->device_timestamp; 5230 /* 5231 * net80211 should take care of the other information (sync_tsf, 5232 * sync_dtim_count) as otherwise we need to parse the beacon. 5233 */ 5234 skip_device_ts: 5235 ; 5236 } 5237 5238 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT && 5239 ieee80211_radiotap_active_vap(vap)) { 5240 struct lkpi_radiotap_rx_hdr *rtap; 5241 5242 rtap = &lhw->rtap_rx; 5243 rtap->wr_tsft = rx_status->device_timestamp; 5244 rtap->wr_flags = 0; 5245 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE) 5246 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 5247 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) 5248 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI; 5249 #if 0 /* .. or it does not given we strip it below. */ 5250 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS)) 5251 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS; 5252 #endif 5253 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC) 5254 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 5255 rtap->wr_rate = 0; 5256 IMPROVE(); 5257 /* XXX TODO status->encoding / rate_index / bw */ 5258 rtap->wr_chan_freq = htole16(rx_stats.c_freq); 5259 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee) 5260 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 5261 rtap->wr_dbm_antsignal = rssi; 5262 rtap->wr_dbm_antnoise = rx_stats.c_nf; 5263 } 5264 5265 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS)) 5266 m_adj(m, -IEEE80211_CRC_LEN); 5267 5268 #if 0 5269 if (list != NULL) { 5270 /* 5271 * Normally this would be queued up and delivered by 5272 * netif_receive_skb_list(), napi_gro_receive(), or the like. 5273 * See mt76::mac80211.c as only current possible consumer. 5274 */ 5275 IMPROVE("we simply pass the packet to net80211 to deal with."); 5276 } 5277 #endif 5278 5279 /* 5280 * Attach meta-information to the mbuf for the deferred RX path. 5281 * Currently this is best-effort. Should we need to be hard, 5282 * drop the frame and goto err; 5283 */ 5284 if (ni != NULL) { 5285 struct m_tag *mtag; 5286 struct lkpi_80211_tag_rxni *rxni; 5287 5288 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, 5289 sizeof(*rxni), IEEE80211_M_NOWAIT); 5290 if (mtag != NULL) { 5291 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1); 5292 rxni->ni = ni; /* We hold a reference. */ 5293 m_tag_prepend(m, mtag); 5294 } 5295 } 5296 5297 LKPI_80211_LHW_RXQ_LOCK(lhw); 5298 if (lhw->rxq_stopped) { 5299 LKPI_80211_LHW_RXQ_UNLOCK(lhw); 5300 m_freem(m); 5301 goto err; 5302 } 5303 5304 mbufq_enqueue(&lhw->rxq, m); 5305 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task); 5306 LKPI_80211_LHW_RXQ_UNLOCK(lhw); 5307 5308 IMPROVE(); 5309 5310 err: 5311 /* The skb is ours so we can free it :-) */ 5312 kfree_skb(skb); 5313 } 5314 5315 uint8_t 5316 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok) 5317 { 5318 const struct ieee80211_frame *wh; 5319 uint8_t tid; 5320 5321 /* Linux seems to assume this is a QOS-Data-Frame */ 5322 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control), 5323 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr, 5324 hdr->frame_control)); 5325 5326 wh = (const struct ieee80211_frame *)hdr; 5327 tid = ieee80211_gettid(wh); 5328 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u " 5329 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID)); 5330 5331 return (tid); 5332 } 5333 5334 struct wiphy * 5335 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len) 5336 { 5337 struct lkpi_wiphy *lwiphy; 5338 5339 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL); 5340 if (lwiphy == NULL) 5341 return (NULL); 5342 lwiphy->ops = ops; 5343 5344 /* XXX TODO */ 5345 return (LWIPHY_TO_WIPHY(lwiphy)); 5346 } 5347 5348 void 5349 linuxkpi_wiphy_free(struct wiphy *wiphy) 5350 { 5351 struct lkpi_wiphy *lwiphy; 5352 5353 if (wiphy == NULL) 5354 return; 5355 5356 lwiphy = WIPHY_TO_LWIPHY(wiphy); 5357 kfree(lwiphy); 5358 } 5359 5360 uint32_t 5361 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel, 5362 enum nl80211_band band) 5363 { 5364 5365 switch (band) { 5366 case NL80211_BAND_2GHZ: 5367 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ)); 5368 break; 5369 case NL80211_BAND_5GHZ: 5370 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ)); 5371 break; 5372 default: 5373 /* XXX abort, retry, error, panic? */ 5374 break; 5375 } 5376 5377 return (0); 5378 } 5379 5380 uint32_t 5381 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused) 5382 { 5383 5384 return (ieee80211_mhz2ieee(freq, 0)); 5385 } 5386 5387 #if 0 5388 static struct lkpi_sta * 5389 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni) 5390 { 5391 struct lkpi_sta *lsta, *temp; 5392 5393 LKPI_80211_LVIF_LOCK(lvif); 5394 TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) { 5395 if (lsta->ni == ni) { 5396 LKPI_80211_LVIF_UNLOCK(lvif); 5397 return (lsta); 5398 } 5399 } 5400 LKPI_80211_LVIF_UNLOCK(lvif); 5401 5402 return (NULL); 5403 } 5404 #endif 5405 5406 struct ieee80211_sta * 5407 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer) 5408 { 5409 struct lkpi_vif *lvif; 5410 struct lkpi_sta *lsta, *temp; 5411 struct ieee80211_sta *sta; 5412 5413 lvif = VIF_TO_LVIF(vif); 5414 5415 LKPI_80211_LVIF_LOCK(lvif); 5416 TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) { 5417 sta = LSTA_TO_STA(lsta); 5418 if (IEEE80211_ADDR_EQ(sta->addr, peer)) { 5419 LKPI_80211_LVIF_UNLOCK(lvif); 5420 return (sta); 5421 } 5422 } 5423 LKPI_80211_LVIF_UNLOCK(lvif); 5424 return (NULL); 5425 } 5426 5427 struct ieee80211_sta * 5428 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 5429 const uint8_t *addr, const uint8_t *ourvifaddr) 5430 { 5431 struct lkpi_hw *lhw; 5432 struct lkpi_vif *lvif; 5433 struct lkpi_sta *lsta; 5434 struct ieee80211_vif *vif; 5435 struct ieee80211_sta *sta; 5436 5437 lhw = wiphy_priv(hw->wiphy); 5438 sta = NULL; 5439 5440 LKPI_80211_LHW_LVIF_LOCK(lhw); 5441 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 5442 5443 /* XXX-BZ check our address from the vif. */ 5444 5445 vif = LVIF_TO_VIF(lvif); 5446 if (ourvifaddr != NULL && 5447 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr)) 5448 continue; 5449 sta = linuxkpi_ieee80211_find_sta(vif, addr); 5450 if (sta != NULL) 5451 break; 5452 } 5453 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 5454 5455 if (sta != NULL) { 5456 lsta = STA_TO_LSTA(sta); 5457 if (!lsta->added_to_drv) 5458 return (NULL); 5459 } 5460 5461 return (sta); 5462 } 5463 5464 struct sk_buff * 5465 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw, 5466 struct ieee80211_txq *txq) 5467 { 5468 struct lkpi_txq *ltxq; 5469 struct lkpi_vif *lvif; 5470 struct sk_buff *skb; 5471 5472 skb = NULL; 5473 ltxq = TXQ_TO_LTXQ(txq); 5474 ltxq->seen_dequeue = true; 5475 5476 if (ltxq->stopped) 5477 goto stopped; 5478 5479 lvif = VIF_TO_LVIF(ltxq->txq.vif); 5480 if (lvif->hw_queue_stopped[ltxq->txq.ac]) { 5481 ltxq->stopped = true; 5482 goto stopped; 5483 } 5484 5485 IMPROVE("hw(TX_FRAG_LIST)"); 5486 5487 LKPI_80211_LTXQ_LOCK(ltxq); 5488 skb = skb_dequeue(<xq->skbq); 5489 LKPI_80211_LTXQ_UNLOCK(ltxq); 5490 5491 stopped: 5492 return (skb); 5493 } 5494 5495 void 5496 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq, 5497 unsigned long *frame_cnt, unsigned long *byte_cnt) 5498 { 5499 struct lkpi_txq *ltxq; 5500 struct sk_buff *skb; 5501 unsigned long fc, bc; 5502 5503 ltxq = TXQ_TO_LTXQ(txq); 5504 5505 fc = bc = 0; 5506 LKPI_80211_LTXQ_LOCK(ltxq); 5507 skb_queue_walk(<xq->skbq, skb) { 5508 fc++; 5509 bc += skb->len; 5510 } 5511 LKPI_80211_LTXQ_UNLOCK(ltxq); 5512 if (frame_cnt) 5513 *frame_cnt = fc; 5514 if (byte_cnt) 5515 *byte_cnt = bc; 5516 5517 /* Validate that this is doing the correct thing. */ 5518 /* Should we keep track on en/dequeue? */ 5519 IMPROVE(); 5520 } 5521 5522 /* 5523 * We are called from ieee80211_free_txskb() or ieee80211_tx_status(). 5524 * The latter tries to derive the success status from the info flags 5525 * passed back from the driver. rawx_mit() saves the ni on the m and the 5526 * m on the skb for us to be able to give feedback to net80211. 5527 */ 5528 static void 5529 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb, 5530 int status) 5531 { 5532 struct ieee80211_node *ni; 5533 struct mbuf *m; 5534 5535 m = skb->m; 5536 skb->m = NULL; 5537 5538 if (m != NULL) { 5539 ni = m->m_pkthdr.PH_loc.ptr; 5540 /* Status: 0 is ok, != 0 is error. */ 5541 ieee80211_tx_complete(ni, m, status); 5542 /* ni & mbuf were consumed. */ 5543 } 5544 } 5545 5546 void 5547 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb, 5548 int status) 5549 { 5550 5551 _lkpi_ieee80211_free_txskb(hw, skb, status); 5552 kfree_skb(skb); 5553 } 5554 5555 void 5556 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw, 5557 struct ieee80211_tx_status *txstat) 5558 { 5559 struct sk_buff *skb; 5560 struct ieee80211_tx_info *info; 5561 struct ieee80211_ratectl_tx_status txs; 5562 struct ieee80211_node *ni; 5563 int status; 5564 5565 skb = txstat->skb; 5566 if (skb->m != NULL) { 5567 struct mbuf *m; 5568 5569 m = skb->m; 5570 ni = m->m_pkthdr.PH_loc.ptr; 5571 memset(&txs, 0, sizeof(txs)); 5572 } else { 5573 ni = NULL; 5574 } 5575 5576 info = txstat->info; 5577 if (info->flags & IEEE80211_TX_STAT_ACK) { 5578 status = 0; /* No error. */ 5579 txs.status = IEEE80211_RATECTL_TX_SUCCESS; 5580 } else { 5581 status = 1; 5582 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED; 5583 } 5584 5585 if (ni != NULL) { 5586 int ridx __unused; 5587 #ifdef LINUXKPI_DEBUG_80211 5588 int old_rate; 5589 5590 old_rate = ni->ni_vap->iv_bss->ni_txrate; 5591 #endif 5592 txs.pktlen = skb->len; 5593 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN; 5594 if (info->status.rates[0].count > 1) { 5595 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */ 5596 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY; 5597 } 5598 #if 0 /* Unused in net80211 currently. */ 5599 /* XXX-BZ convert check .flags for MCS/VHT/.. */ 5600 txs.final_rate = info->status.rates[0].idx; 5601 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE; 5602 #endif 5603 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) { 5604 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */ 5605 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI; 5606 } 5607 5608 IMPROVE("only update of rate matches but that requires us to get a proper rate"); 5609 ieee80211_ratectl_tx_complete(ni, &txs); 5610 ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0); 5611 5612 #ifdef LINUXKPI_DEBUG_80211 5613 if (linuxkpi_debug_80211 & D80211_TRACE_TX) { 5614 printf("TX-RATE: %s: old %d new %d ridx %d, " 5615 "long_retries %d\n", __func__, 5616 old_rate, ni->ni_vap->iv_bss->ni_txrate, 5617 ridx, txs.long_retries); 5618 } 5619 #endif 5620 } 5621 5622 #ifdef LINUXKPI_DEBUG_80211 5623 if (linuxkpi_debug_80211 & D80211_TRACE_TX) 5624 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x " 5625 "band %u hw_queue %u tx_time_est %d : " 5626 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] " 5627 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u " 5628 "tx_time %u flags %#x " 5629 "status_driver_data [ %p %p ]\n", 5630 __func__, hw, skb, status, info->flags, 5631 info->band, info->hw_queue, info->tx_time_est, 5632 info->status.rates[0].idx, info->status.rates[0].count, 5633 info->status.rates[0].flags, 5634 info->status.rates[1].idx, info->status.rates[1].count, 5635 info->status.rates[1].flags, 5636 info->status.rates[2].idx, info->status.rates[2].count, 5637 info->status.rates[2].flags, 5638 info->status.rates[3].idx, info->status.rates[3].count, 5639 info->status.rates[3].flags, 5640 info->status.ack_signal, info->status.ampdu_ack_len, 5641 info->status.ampdu_len, info->status.antenna, 5642 info->status.tx_time, info->status.flags, 5643 info->status.status_driver_data[0], 5644 info->status.status_driver_data[1]); 5645 #endif 5646 5647 if (txstat->free_list) { 5648 _lkpi_ieee80211_free_txskb(hw, skb, status); 5649 list_add_tail(&skb->list, txstat->free_list); 5650 } else { 5651 linuxkpi_ieee80211_free_txskb(hw, skb, status); 5652 } 5653 } 5654 5655 void 5656 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb) 5657 { 5658 struct ieee80211_tx_status status; 5659 5660 memset(&status, 0, sizeof(status)); 5661 status.info = IEEE80211_SKB_CB(skb); 5662 status.skb = skb; 5663 /* sta, n_rates, rates, free_list? */ 5664 5665 ieee80211_tx_status_ext(hw, &status); 5666 } 5667 5668 /* 5669 * This is an internal bandaid for the moment for the way we glue 5670 * skbs and mbufs together for TX. Once we have skbs backed by 5671 * mbufs this should go away. 5672 * This is a public function but kept on the private KPI (lkpi_) 5673 * and is not exposed by a header file. 5674 */ 5675 static void 5676 lkpi_ieee80211_free_skb_mbuf(void *p) 5677 { 5678 struct ieee80211_node *ni; 5679 struct mbuf *m; 5680 5681 if (p == NULL) 5682 return; 5683 5684 m = (struct mbuf *)p; 5685 M_ASSERTPKTHDR(m); 5686 5687 ni = m->m_pkthdr.PH_loc.ptr; 5688 m->m_pkthdr.PH_loc.ptr = NULL; 5689 if (ni != NULL) 5690 ieee80211_free_node(ni); 5691 m_freem(m); 5692 } 5693 5694 void 5695 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw, 5696 struct delayed_work *w, int delay) 5697 { 5698 struct lkpi_hw *lhw; 5699 5700 /* Need to make sure hw is in a stable (non-suspended) state. */ 5701 IMPROVE(); 5702 5703 lhw = HW_TO_LHW(hw); 5704 queue_delayed_work(lhw->workq, w, delay); 5705 } 5706 5707 void 5708 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw, 5709 struct work_struct *w) 5710 { 5711 struct lkpi_hw *lhw; 5712 5713 /* Need to make sure hw is in a stable (non-suspended) state. */ 5714 IMPROVE(); 5715 5716 lhw = HW_TO_LHW(hw); 5717 queue_work(lhw->workq, w); 5718 } 5719 5720 struct sk_buff * 5721 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr, 5722 uint8_t *ssid, size_t ssid_len, size_t tailroom) 5723 { 5724 struct sk_buff *skb; 5725 struct ieee80211_frame *wh; 5726 uint8_t *p; 5727 size_t len; 5728 5729 len = sizeof(*wh); 5730 len += 2 + ssid_len; 5731 5732 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom); 5733 if (skb == NULL) 5734 return (NULL); 5735 5736 skb_reserve(skb, hw->extra_tx_headroom); 5737 5738 wh = skb_put_zero(skb, sizeof(*wh)); 5739 wh->i_fc[0] = IEEE80211_FC0_VERSION_0; 5740 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT; 5741 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr); 5742 IEEE80211_ADDR_COPY(wh->i_addr2, addr); 5743 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr); 5744 5745 p = skb_put(skb, 2 + ssid_len); 5746 *p++ = IEEE80211_ELEMID_SSID; 5747 *p++ = ssid_len; 5748 if (ssid_len > 0) 5749 memcpy(p, ssid, ssid_len); 5750 5751 return (skb); 5752 } 5753 5754 struct sk_buff * 5755 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw, 5756 struct ieee80211_vif *vif) 5757 { 5758 struct lkpi_vif *lvif; 5759 struct ieee80211vap *vap; 5760 struct sk_buff *skb; 5761 struct ieee80211_frame_pspoll *psp; 5762 uint16_t v; 5763 5764 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp)); 5765 if (skb == NULL) 5766 return (NULL); 5767 5768 skb_reserve(skb, hw->extra_tx_headroom); 5769 5770 lvif = VIF_TO_LVIF(vif); 5771 vap = LVIF_TO_VAP(lvif); 5772 5773 psp = skb_put_zero(skb, sizeof(*psp)); 5774 psp->i_fc[0] = IEEE80211_FC0_VERSION_0; 5775 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL; 5776 v = htole16(vif->cfg.aid | 1<<15 | 1<<16); 5777 memcpy(&psp->i_aid, &v, sizeof(v)); 5778 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr); 5779 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr); 5780 5781 return (skb); 5782 } 5783 5784 struct sk_buff * 5785 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw, 5786 struct ieee80211_vif *vif, int linkid, bool qos) 5787 { 5788 struct lkpi_vif *lvif; 5789 struct ieee80211vap *vap; 5790 struct sk_buff *skb; 5791 struct ieee80211_frame *nullf; 5792 5793 IMPROVE("linkid"); 5794 5795 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf)); 5796 if (skb == NULL) 5797 return (NULL); 5798 5799 skb_reserve(skb, hw->extra_tx_headroom); 5800 5801 lvif = VIF_TO_LVIF(vif); 5802 vap = LVIF_TO_VAP(lvif); 5803 5804 nullf = skb_put_zero(skb, sizeof(*nullf)); 5805 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0; 5806 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA; 5807 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS; 5808 5809 IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid); 5810 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr); 5811 IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr); 5812 5813 return (skb); 5814 } 5815 5816 struct wireless_dev * 5817 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif) 5818 { 5819 struct lkpi_vif *lvif; 5820 5821 lvif = VIF_TO_LVIF(vif); 5822 return (&lvif->wdev); 5823 } 5824 5825 void 5826 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif) 5827 { 5828 struct lkpi_vif *lvif; 5829 struct ieee80211vap *vap; 5830 enum ieee80211_state nstate; 5831 int arg; 5832 5833 lvif = VIF_TO_LVIF(vif); 5834 vap = LVIF_TO_VAP(lvif); 5835 5836 /* 5837 * Go to init; otherwise we need to elaborately check state and 5838 * handle accordingly, e.g., if in RUN we could call iv_bmiss. 5839 * Let the statemachine handle all neccessary changes. 5840 */ 5841 nstate = IEEE80211_S_INIT; 5842 arg = 0; /* Not a valid reason. */ 5843 5844 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__, 5845 vif, vap, ieee80211_state_name[vap->iv_state]); 5846 ieee80211_new_state(vap, nstate, arg); 5847 } 5848 5849 void 5850 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif) 5851 { 5852 struct lkpi_vif *lvif; 5853 struct ieee80211vap *vap; 5854 5855 lvif = VIF_TO_LVIF(vif); 5856 vap = LVIF_TO_VAP(lvif); 5857 5858 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__, 5859 vif, vap, ieee80211_state_name[vap->iv_state]); 5860 ieee80211_beacon_miss(vap->iv_ic); 5861 } 5862 5863 /* -------------------------------------------------------------------------- */ 5864 5865 void 5866 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum) 5867 { 5868 struct lkpi_hw *lhw; 5869 struct lkpi_vif *lvif; 5870 struct ieee80211_vif *vif; 5871 int ac_count, ac; 5872 5873 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n", 5874 __func__, qnum, hw->queues, hw)); 5875 5876 lhw = wiphy_priv(hw->wiphy); 5877 5878 /* See lkpi_ic_vap_create(). */ 5879 if (hw->queues >= IEEE80211_NUM_ACS) 5880 ac_count = IEEE80211_NUM_ACS; 5881 else 5882 ac_count = 1; 5883 5884 LKPI_80211_LHW_LVIF_LOCK(lhw); 5885 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 5886 5887 vif = LVIF_TO_VIF(lvif); 5888 for (ac = 0; ac < ac_count; ac++) { 5889 IMPROVE_TXQ("LOCKING"); 5890 if (qnum == vif->hw_queue[ac]) { 5891 #ifdef LINUXKPI_DEBUG_80211 5892 /* 5893 * For now log this to better understand 5894 * how this is supposed to work. 5895 */ 5896 if (lvif->hw_queue_stopped[ac] && 5897 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0) 5898 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p " 5899 "lvif %p vif %p ac %d qnum %d already " 5900 "stopped\n", __func__, __LINE__, 5901 lhw, hw, lvif, vif, ac, qnum); 5902 #endif 5903 lvif->hw_queue_stopped[ac] = true; 5904 } 5905 } 5906 } 5907 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 5908 } 5909 5910 void 5911 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw) 5912 { 5913 int i; 5914 5915 IMPROVE_TXQ("Locking; do we need further info?"); 5916 for (i = 0; i < hw->queues; i++) 5917 linuxkpi_ieee80211_stop_queue(hw, i); 5918 } 5919 5920 5921 static void 5922 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq) 5923 { 5924 struct lkpi_hw *lhw; 5925 struct lkpi_vif *lvif; 5926 struct lkpi_sta *lsta; 5927 int ac_count, ac, tid; 5928 5929 /* See lkpi_ic_vap_create(). */ 5930 if (hw->queues >= IEEE80211_NUM_ACS) 5931 ac_count = IEEE80211_NUM_ACS; 5932 else 5933 ac_count = 1; 5934 5935 lhw = wiphy_priv(hw->wiphy); 5936 5937 IMPROVE_TXQ("Locking"); 5938 LKPI_80211_LHW_LVIF_LOCK(lhw); 5939 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) { 5940 struct ieee80211_vif *vif; 5941 5942 vif = LVIF_TO_VIF(lvif); 5943 for (ac = 0; ac < ac_count; ac++) { 5944 5945 if (hwq == vif->hw_queue[ac]) { 5946 5947 /* XXX-BZ what about software scan? */ 5948 5949 #ifdef LINUXKPI_DEBUG_80211 5950 /* 5951 * For now log this to better understand 5952 * how this is supposed to work. 5953 */ 5954 if (!lvif->hw_queue_stopped[ac] && 5955 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0) 5956 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p " 5957 "lvif %p vif %p ac %d hw_q not stopped\n", 5958 __func__, __LINE__, 5959 lhw, hw, lvif, vif, ac); 5960 #endif 5961 lvif->hw_queue_stopped[ac] = false; 5962 5963 LKPI_80211_LVIF_LOCK(lvif); 5964 TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) { 5965 struct ieee80211_sta *sta; 5966 5967 sta = LSTA_TO_STA(lsta); 5968 for (tid = 0; tid < nitems(sta->txq); tid++) { 5969 struct lkpi_txq *ltxq; 5970 5971 if (sta->txq[tid] == NULL) 5972 continue; 5973 5974 if (sta->txq[tid]->ac != ac) 5975 continue; 5976 5977 ltxq = TXQ_TO_LTXQ(sta->txq[tid]); 5978 if (!ltxq->stopped) 5979 continue; 5980 5981 ltxq->stopped = false; 5982 5983 /* XXX-BZ see when this explodes with all the locking. taskq? */ 5984 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]); 5985 } 5986 } 5987 LKPI_80211_LVIF_UNLOCK(lvif); 5988 } 5989 } 5990 } 5991 LKPI_80211_LHW_LVIF_UNLOCK(lhw); 5992 } 5993 5994 void 5995 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw) 5996 { 5997 int i; 5998 5999 IMPROVE_TXQ("Is this all/enough here?"); 6000 for (i = 0; i < hw->queues; i++) 6001 lkpi_ieee80211_wake_queues(hw, i); 6002 } 6003 6004 void 6005 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum) 6006 { 6007 6008 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n", 6009 __func__, qnum, hw->queues, hw)); 6010 6011 lkpi_ieee80211_wake_queues(hw, qnum); 6012 } 6013 6014 /* This is just hardware queues. */ 6015 void 6016 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac) 6017 { 6018 struct lkpi_hw *lhw; 6019 6020 lhw = HW_TO_LHW(hw); 6021 6022 IMPROVE_TXQ("Are there reasons why we wouldn't schedule?"); 6023 IMPROVE_TXQ("LOCKING"); 6024 if (++lhw->txq_generation[ac] == 0) 6025 lhw->txq_generation[ac]++; 6026 } 6027 6028 struct ieee80211_txq * 6029 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac) 6030 { 6031 struct lkpi_hw *lhw; 6032 struct ieee80211_txq *txq; 6033 struct lkpi_txq *ltxq; 6034 6035 lhw = HW_TO_LHW(hw); 6036 txq = NULL; 6037 6038 IMPROVE_TXQ("LOCKING"); 6039 6040 /* Check that we are scheduled. */ 6041 if (lhw->txq_generation[ac] == 0) 6042 goto out; 6043 6044 ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]); 6045 if (ltxq == NULL) 6046 goto out; 6047 if (ltxq->txq_generation == lhw->txq_generation[ac]) 6048 goto out; 6049 6050 ltxq->txq_generation = lhw->txq_generation[ac]; 6051 TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry); 6052 txq = <xq->txq; 6053 TAILQ_ELEM_INIT(ltxq, txq_entry); 6054 6055 out: 6056 return (txq); 6057 } 6058 6059 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw, 6060 struct ieee80211_txq *txq, bool withoutpkts) 6061 { 6062 struct lkpi_hw *lhw; 6063 struct lkpi_txq *ltxq; 6064 bool ltxq_empty; 6065 6066 ltxq = TXQ_TO_LTXQ(txq); 6067 6068 IMPROVE_TXQ("LOCKING"); 6069 6070 /* Only schedule if work to do or asked to anyway. */ 6071 LKPI_80211_LTXQ_LOCK(ltxq); 6072 ltxq_empty = skb_queue_empty(<xq->skbq); 6073 LKPI_80211_LTXQ_UNLOCK(ltxq); 6074 if (!withoutpkts && ltxq_empty) 6075 goto out; 6076 6077 /* 6078 * Make sure we do not double-schedule. We do this by checking tqe_prev, 6079 * the previous entry in our tailq. tqe_prev is always valid if this entry 6080 * is queued, tqe_next may be NULL if this is the only element in the list. 6081 */ 6082 if (ltxq->txq_entry.tqe_prev != NULL) 6083 goto out; 6084 6085 lhw = HW_TO_LHW(hw); 6086 TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry); 6087 out: 6088 return; 6089 } 6090 6091 void 6092 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw, 6093 struct ieee80211_txq *txq) 6094 { 6095 struct lkpi_hw *lhw; 6096 struct ieee80211_txq *ntxq; 6097 struct ieee80211_tx_control control; 6098 struct sk_buff *skb; 6099 6100 lhw = HW_TO_LHW(hw); 6101 6102 LKPI_80211_LHW_TXQ_LOCK(lhw); 6103 ieee80211_txq_schedule_start(hw, txq->ac); 6104 do { 6105 ntxq = ieee80211_next_txq(hw, txq->ac); 6106 if (ntxq == NULL) 6107 break; 6108 6109 memset(&control, 0, sizeof(control)); 6110 control.sta = ntxq->sta; 6111 do { 6112 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq); 6113 if (skb == NULL) 6114 break; 6115 lkpi_80211_mo_tx(hw, &control, skb); 6116 } while(1); 6117 6118 ieee80211_return_txq(hw, ntxq, false); 6119 } while (1); 6120 ieee80211_txq_schedule_end(hw, txq->ac); 6121 LKPI_80211_LHW_TXQ_UNLOCK(lhw); 6122 } 6123 6124 /* -------------------------------------------------------------------------- */ 6125 6126 struct lkpi_cfg80211_bss { 6127 u_int refcnt; 6128 struct cfg80211_bss bss; 6129 }; 6130 6131 struct lkpi_cfg80211_get_bss_iter_lookup { 6132 struct wiphy *wiphy; 6133 struct linuxkpi_ieee80211_channel *chan; 6134 const uint8_t *bssid; 6135 const uint8_t *ssid; 6136 size_t ssid_len; 6137 enum ieee80211_bss_type bss_type; 6138 enum ieee80211_privacy privacy; 6139 6140 /* 6141 * Something to store a copy of the result as the net80211 scan cache 6142 * is not refoucnted so a scan entry might go away any time. 6143 */ 6144 bool match; 6145 struct cfg80211_bss *bss; 6146 }; 6147 6148 static void 6149 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se) 6150 { 6151 struct lkpi_cfg80211_get_bss_iter_lookup *lookup; 6152 size_t ielen; 6153 6154 lookup = arg; 6155 6156 /* Do not try to find another match. */ 6157 if (lookup->match) 6158 return; 6159 6160 /* Nothing to store result. */ 6161 if (lookup->bss == NULL) 6162 return; 6163 6164 if (lookup->privacy != IEEE80211_PRIVACY_ANY) { 6165 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */ 6166 /* We have no idea what to compare to as the drivers only request ANY */ 6167 return; 6168 } 6169 6170 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) { 6171 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */ 6172 /* We have no idea what to compare to as the drivers only request ANY */ 6173 return; 6174 } 6175 6176 if (lookup->chan != NULL) { 6177 struct linuxkpi_ieee80211_channel *chan; 6178 6179 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy, 6180 se->se_chan->ic_freq); 6181 if (chan == NULL || chan != lookup->chan) 6182 return; 6183 } 6184 6185 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid)) 6186 return; 6187 6188 if (lookup->ssid) { 6189 if (lookup->ssid_len != se->se_ssid[1] || 6190 se->se_ssid[1] == 0) 6191 return; 6192 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0) 6193 return; 6194 } 6195 6196 ielen = se->se_ies.len; 6197 6198 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen, 6199 M_LKPI80211, M_NOWAIT | M_ZERO); 6200 if (lookup->bss->ies == NULL) 6201 return; 6202 6203 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies); 6204 lookup->bss->ies->len = ielen; 6205 if (ielen) 6206 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen); 6207 6208 lookup->match = true; 6209 } 6210 6211 struct cfg80211_bss * 6212 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan, 6213 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len, 6214 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy) 6215 { 6216 struct lkpi_cfg80211_bss *lbss; 6217 struct lkpi_cfg80211_get_bss_iter_lookup lookup; 6218 struct lkpi_hw *lhw; 6219 struct ieee80211vap *vap; 6220 6221 lhw = wiphy_priv(wiphy); 6222 6223 /* Let's hope we can alloc. */ 6224 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO); 6225 if (lbss == NULL) { 6226 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__); 6227 return (NULL); 6228 } 6229 6230 lookup.wiphy = wiphy; 6231 lookup.chan = chan; 6232 lookup.bssid = bssid; 6233 lookup.ssid = ssid; 6234 lookup.ssid_len = ssid_len; 6235 lookup.bss_type = bss_type; 6236 lookup.privacy = privacy; 6237 lookup.match = false; 6238 lookup.bss = &lbss->bss; 6239 6240 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?"); 6241 vap = TAILQ_FIRST(&lhw->ic->ic_vaps); 6242 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup); 6243 if (!lookup.match) { 6244 free(lbss, M_LKPI80211); 6245 return (NULL); 6246 } 6247 6248 refcount_init(&lbss->refcnt, 1); 6249 return (&lbss->bss); 6250 } 6251 6252 void 6253 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss) 6254 { 6255 struct lkpi_cfg80211_bss *lbss; 6256 6257 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss); 6258 6259 /* Free everything again on refcount ... */ 6260 if (refcount_release(&lbss->refcnt)) { 6261 free(lbss->bss.ies, M_LKPI80211); 6262 free(lbss, M_LKPI80211); 6263 } 6264 } 6265 6266 void 6267 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy) 6268 { 6269 struct lkpi_hw *lhw; 6270 struct ieee80211com *ic; 6271 struct ieee80211vap *vap; 6272 6273 lhw = wiphy_priv(wiphy); 6274 ic = lhw->ic; 6275 6276 /* 6277 * If we haven't called ieee80211_ifattach() yet 6278 * or there is no VAP, there are no scans to flush. 6279 */ 6280 if (ic == NULL || 6281 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0) 6282 return; 6283 6284 /* Should only happen on the current one? Not seen it late enough. */ 6285 IEEE80211_LOCK(ic); 6286 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 6287 ieee80211_scan_flush(vap); 6288 IEEE80211_UNLOCK(ic); 6289 } 6290 6291 /* -------------------------------------------------------------------------- */ 6292 6293 MODULE_VERSION(linuxkpi_wlan, 1); 6294 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1); 6295 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1); 6296