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