1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Interface handling 4 * 5 * Copyright 2002-2005, Instant802 Networks, Inc. 6 * Copyright 2005-2006, Devicescape Software, Inc. 7 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> 8 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> 9 * Copyright 2013-2014 Intel Mobile Communications GmbH 10 * Copyright (c) 2016 Intel Deutschland GmbH 11 * Copyright (C) 2018-2024 Intel Corporation 12 */ 13 #include <linux/slab.h> 14 #include <linux/kernel.h> 15 #include <linux/if_arp.h> 16 #include <linux/netdevice.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/kcov.h> 19 #include <net/mac80211.h> 20 #include <net/ieee80211_radiotap.h> 21 #include "ieee80211_i.h" 22 #include "sta_info.h" 23 #include "debugfs_netdev.h" 24 #include "mesh.h" 25 #include "led.h" 26 #include "driver-ops.h" 27 #include "wme.h" 28 #include "rate.h" 29 30 /** 31 * DOC: Interface list locking 32 * 33 * The interface list in each struct ieee80211_local is protected 34 * three-fold: 35 * 36 * (1) modifications may only be done under the RTNL *and* wiphy mutex 37 * *and* iflist_mtx 38 * (2) modifications are done in an RCU manner so atomic readers 39 * can traverse the list in RCU-safe blocks. 40 * 41 * As a consequence, reads (traversals) of the list can be protected 42 * by either the RTNL, the wiphy mutex, the iflist_mtx or RCU. 43 */ 44 45 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work); 46 47 bool __ieee80211_recalc_txpower(struct ieee80211_link_data *link) 48 { 49 struct ieee80211_chanctx_conf *chanctx_conf; 50 int power; 51 52 rcu_read_lock(); 53 chanctx_conf = rcu_dereference(link->conf->chanctx_conf); 54 if (!chanctx_conf) { 55 rcu_read_unlock(); 56 return false; 57 } 58 59 power = ieee80211_chandef_max_power(&chanctx_conf->def); 60 rcu_read_unlock(); 61 62 if (link->user_power_level != IEEE80211_UNSET_POWER_LEVEL) 63 power = min(power, link->user_power_level); 64 65 if (link->ap_power_level != IEEE80211_UNSET_POWER_LEVEL) 66 power = min(power, link->ap_power_level); 67 68 if (power != link->conf->txpower) { 69 link->conf->txpower = power; 70 return true; 71 } 72 73 return false; 74 } 75 76 void ieee80211_recalc_txpower(struct ieee80211_link_data *link, 77 bool update_bss) 78 { 79 if (__ieee80211_recalc_txpower(link) || 80 (update_bss && ieee80211_sdata_running(link->sdata))) 81 ieee80211_link_info_change_notify(link->sdata, link, 82 BSS_CHANGED_TXPOWER); 83 } 84 85 static u32 __ieee80211_idle_off(struct ieee80211_local *local) 86 { 87 if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE)) 88 return 0; 89 90 local->hw.conf.flags &= ~IEEE80211_CONF_IDLE; 91 return IEEE80211_CONF_CHANGE_IDLE; 92 } 93 94 static u32 __ieee80211_idle_on(struct ieee80211_local *local) 95 { 96 if (local->hw.conf.flags & IEEE80211_CONF_IDLE) 97 return 0; 98 99 ieee80211_flush_queues(local, NULL, false); 100 101 local->hw.conf.flags |= IEEE80211_CONF_IDLE; 102 return IEEE80211_CONF_CHANGE_IDLE; 103 } 104 105 static u32 __ieee80211_recalc_idle(struct ieee80211_local *local, 106 bool force_active) 107 { 108 bool working, scanning, active; 109 unsigned int led_trig_start = 0, led_trig_stop = 0; 110 111 lockdep_assert_wiphy(local->hw.wiphy); 112 113 active = force_active || 114 !list_empty(&local->chanctx_list) || 115 local->monitors; 116 117 working = !local->ops->remain_on_channel && 118 !list_empty(&local->roc_list); 119 120 scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) || 121 test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 122 123 if (working || scanning) 124 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK; 125 else 126 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK; 127 128 if (active) 129 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED; 130 else 131 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED; 132 133 ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop); 134 135 if (working || scanning || active) 136 return __ieee80211_idle_off(local); 137 return __ieee80211_idle_on(local); 138 } 139 140 u32 ieee80211_idle_off(struct ieee80211_local *local) 141 { 142 return __ieee80211_recalc_idle(local, true); 143 } 144 145 void ieee80211_recalc_idle(struct ieee80211_local *local) 146 { 147 u32 change = __ieee80211_recalc_idle(local, false); 148 if (change) 149 ieee80211_hw_config(local, change); 150 } 151 152 static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr, 153 bool check_dup) 154 { 155 struct ieee80211_local *local = sdata->local; 156 struct ieee80211_sub_if_data *iter; 157 u64 new, mask, tmp; 158 u8 *m; 159 int ret = 0; 160 161 lockdep_assert_wiphy(local->hw.wiphy); 162 163 if (is_zero_ether_addr(local->hw.wiphy->addr_mask)) 164 return 0; 165 166 m = addr; 167 new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | 168 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | 169 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); 170 171 m = local->hw.wiphy->addr_mask; 172 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | 173 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | 174 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); 175 176 if (!check_dup) 177 return ret; 178 179 list_for_each_entry(iter, &local->interfaces, list) { 180 if (iter == sdata) 181 continue; 182 183 if (iter->vif.type == NL80211_IFTYPE_MONITOR && 184 !(iter->u.mntr.flags & MONITOR_FLAG_ACTIVE)) 185 continue; 186 187 m = iter->vif.addr; 188 tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | 189 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | 190 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); 191 192 if ((new & ~mask) != (tmp & ~mask)) { 193 ret = -EINVAL; 194 break; 195 } 196 } 197 198 return ret; 199 } 200 201 static int ieee80211_can_powered_addr_change(struct ieee80211_sub_if_data *sdata) 202 { 203 struct ieee80211_roc_work *roc; 204 struct ieee80211_local *local = sdata->local; 205 struct ieee80211_sub_if_data *scan_sdata; 206 int ret = 0; 207 208 lockdep_assert_wiphy(local->hw.wiphy); 209 210 /* To be the most flexible here we want to only limit changing the 211 * address if the specific interface is doing offchannel work or 212 * scanning. 213 */ 214 if (netif_carrier_ok(sdata->dev)) 215 return -EBUSY; 216 217 /* First check no ROC work is happening on this iface */ 218 list_for_each_entry(roc, &local->roc_list, list) { 219 if (roc->sdata != sdata) 220 continue; 221 222 if (roc->started) { 223 ret = -EBUSY; 224 goto unlock; 225 } 226 } 227 228 /* And if this iface is scanning */ 229 if (local->scanning) { 230 scan_sdata = rcu_dereference_protected(local->scan_sdata, 231 lockdep_is_held(&local->hw.wiphy->mtx)); 232 if (sdata == scan_sdata) 233 ret = -EBUSY; 234 } 235 236 switch (sdata->vif.type) { 237 case NL80211_IFTYPE_STATION: 238 case NL80211_IFTYPE_P2P_CLIENT: 239 /* More interface types could be added here but changing the 240 * address while powered makes the most sense in client modes. 241 */ 242 break; 243 default: 244 ret = -EOPNOTSUPP; 245 } 246 247 unlock: 248 return ret; 249 } 250 251 static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata, 252 void *addr) 253 { 254 struct ieee80211_local *local = sdata->local; 255 struct sockaddr *sa = addr; 256 bool check_dup = true; 257 bool live = false; 258 int ret; 259 260 if (ieee80211_sdata_running(sdata)) { 261 ret = ieee80211_can_powered_addr_change(sdata); 262 if (ret) 263 return ret; 264 265 live = true; 266 } 267 268 if (sdata->vif.type == NL80211_IFTYPE_MONITOR && 269 !(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE)) 270 check_dup = false; 271 272 ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup); 273 if (ret) 274 return ret; 275 276 if (live) 277 drv_remove_interface(local, sdata); 278 ret = eth_mac_addr(sdata->dev, sa); 279 280 if (ret == 0) { 281 if (check_dup) { 282 memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN); 283 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr); 284 } else { 285 memset(sdata->vif.addr, 0, ETH_ALEN); 286 memset(sdata->vif.bss_conf.addr, 0, ETH_ALEN); 287 } 288 } 289 290 /* Regardless of eth_mac_addr() return we still want to add the 291 * interface back. This should not fail... 292 */ 293 if (live) 294 WARN_ON(drv_add_interface(local, sdata)); 295 296 return ret; 297 } 298 299 static int ieee80211_change_mac(struct net_device *dev, void *addr) 300 { 301 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 302 struct ieee80211_local *local = sdata->local; 303 int ret; 304 305 /* 306 * This happens during unregistration if there's a bond device 307 * active (maybe other cases?) and we must get removed from it. 308 * But we really don't care anymore if it's not registered now. 309 */ 310 if (!dev->ieee80211_ptr->registered) 311 return 0; 312 313 wiphy_lock(local->hw.wiphy); 314 ret = _ieee80211_change_mac(sdata, addr); 315 wiphy_unlock(local->hw.wiphy); 316 317 return ret; 318 } 319 320 static inline int identical_mac_addr_allowed(int type1, int type2) 321 { 322 return type1 == NL80211_IFTYPE_MONITOR || 323 type2 == NL80211_IFTYPE_MONITOR || 324 type1 == NL80211_IFTYPE_P2P_DEVICE || 325 type2 == NL80211_IFTYPE_P2P_DEVICE || 326 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) || 327 (type1 == NL80211_IFTYPE_AP_VLAN && 328 (type2 == NL80211_IFTYPE_AP || 329 type2 == NL80211_IFTYPE_AP_VLAN)); 330 } 331 332 static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata, 333 enum nl80211_iftype iftype) 334 { 335 struct ieee80211_local *local = sdata->local; 336 struct ieee80211_sub_if_data *nsdata; 337 338 ASSERT_RTNL(); 339 lockdep_assert_wiphy(local->hw.wiphy); 340 341 /* we hold the RTNL here so can safely walk the list */ 342 list_for_each_entry(nsdata, &local->interfaces, list) { 343 if (nsdata != sdata && ieee80211_sdata_running(nsdata)) { 344 /* 345 * Only OCB and monitor mode may coexist 346 */ 347 if ((sdata->vif.type == NL80211_IFTYPE_OCB && 348 nsdata->vif.type != NL80211_IFTYPE_MONITOR) || 349 (sdata->vif.type != NL80211_IFTYPE_MONITOR && 350 nsdata->vif.type == NL80211_IFTYPE_OCB)) 351 return -EBUSY; 352 353 /* 354 * Allow only a single IBSS interface to be up at any 355 * time. This is restricted because beacon distribution 356 * cannot work properly if both are in the same IBSS. 357 * 358 * To remove this restriction we'd have to disallow them 359 * from setting the same SSID on different IBSS interfaces 360 * belonging to the same hardware. Then, however, we're 361 * faced with having to adopt two different TSF timers... 362 */ 363 if (iftype == NL80211_IFTYPE_ADHOC && 364 nsdata->vif.type == NL80211_IFTYPE_ADHOC) 365 return -EBUSY; 366 /* 367 * will not add another interface while any channel 368 * switch is active. 369 */ 370 if (nsdata->vif.bss_conf.csa_active) 371 return -EBUSY; 372 373 /* 374 * The remaining checks are only performed for interfaces 375 * with the same MAC address. 376 */ 377 if (!ether_addr_equal(sdata->vif.addr, 378 nsdata->vif.addr)) 379 continue; 380 381 /* 382 * check whether it may have the same address 383 */ 384 if (!identical_mac_addr_allowed(iftype, 385 nsdata->vif.type)) 386 return -ENOTUNIQ; 387 388 /* No support for VLAN with MLO yet */ 389 if (iftype == NL80211_IFTYPE_AP_VLAN && 390 sdata->wdev.use_4addr && 391 nsdata->vif.type == NL80211_IFTYPE_AP && 392 nsdata->vif.valid_links) 393 return -EOPNOTSUPP; 394 395 /* 396 * can only add VLANs to enabled APs 397 */ 398 if (iftype == NL80211_IFTYPE_AP_VLAN && 399 nsdata->vif.type == NL80211_IFTYPE_AP) 400 sdata->bss = &nsdata->u.ap; 401 } 402 } 403 404 return ieee80211_check_combinations(sdata, NULL, 0, 0, -1); 405 } 406 407 static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata, 408 enum nl80211_iftype iftype) 409 { 410 int n_queues = sdata->local->hw.queues; 411 int i; 412 413 if (iftype == NL80211_IFTYPE_NAN) 414 return 0; 415 416 if (iftype != NL80211_IFTYPE_P2P_DEVICE) { 417 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 418 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] == 419 IEEE80211_INVAL_HW_QUEUE)) 420 return -EINVAL; 421 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >= 422 n_queues)) 423 return -EINVAL; 424 } 425 } 426 427 if ((iftype != NL80211_IFTYPE_AP && 428 iftype != NL80211_IFTYPE_P2P_GO && 429 iftype != NL80211_IFTYPE_MESH_POINT) || 430 !ieee80211_hw_check(&sdata->local->hw, QUEUE_CONTROL)) { 431 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE; 432 return 0; 433 } 434 435 if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE)) 436 return -EINVAL; 437 438 if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues)) 439 return -EINVAL; 440 441 return 0; 442 } 443 444 static int ieee80211_open(struct net_device *dev) 445 { 446 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 447 int err; 448 449 /* fail early if user set an invalid address */ 450 if (!is_valid_ether_addr(dev->dev_addr)) 451 return -EADDRNOTAVAIL; 452 453 wiphy_lock(sdata->local->hw.wiphy); 454 err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type); 455 if (err) 456 goto out; 457 458 err = ieee80211_do_open(&sdata->wdev, true); 459 out: 460 wiphy_unlock(sdata->local->hw.wiphy); 461 462 return err; 463 } 464 465 static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_down) 466 { 467 struct ieee80211_local *local = sdata->local; 468 unsigned long flags; 469 struct sk_buff_head freeq; 470 struct sk_buff *skb, *tmp; 471 u32 hw_reconf_flags = 0; 472 int i, flushed; 473 struct ps_data *ps; 474 struct cfg80211_chan_def chandef; 475 bool cancel_scan; 476 struct cfg80211_nan_func *func; 477 478 lockdep_assert_wiphy(local->hw.wiphy); 479 480 clear_bit(SDATA_STATE_RUNNING, &sdata->state); 481 synchronize_rcu(); /* flush _ieee80211_wake_txqs() */ 482 483 cancel_scan = rcu_access_pointer(local->scan_sdata) == sdata; 484 if (cancel_scan) 485 ieee80211_scan_cancel(local); 486 487 ieee80211_roc_purge(local, sdata); 488 489 switch (sdata->vif.type) { 490 case NL80211_IFTYPE_STATION: 491 ieee80211_mgd_stop(sdata); 492 break; 493 case NL80211_IFTYPE_ADHOC: 494 ieee80211_ibss_stop(sdata); 495 break; 496 case NL80211_IFTYPE_MONITOR: 497 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) 498 break; 499 list_del_rcu(&sdata->u.mntr.list); 500 break; 501 default: 502 break; 503 } 504 505 /* 506 * Remove all stations associated with this interface. 507 * 508 * This must be done before calling ops->remove_interface() 509 * because otherwise we can later invoke ops->sta_notify() 510 * whenever the STAs are removed, and that invalidates driver 511 * assumptions about always getting a vif pointer that is valid 512 * (because if we remove a STA after ops->remove_interface() 513 * the driver will have removed the vif info already!) 514 * 515 * For AP_VLANs stations may exist since there's nothing else that 516 * would have removed them, but in other modes there shouldn't 517 * be any stations. 518 */ 519 flushed = sta_info_flush(sdata, -1); 520 WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP_VLAN && flushed > 0); 521 522 /* don't count this interface for allmulti while it is down */ 523 if (sdata->flags & IEEE80211_SDATA_ALLMULTI) 524 atomic_dec(&local->iff_allmultis); 525 526 if (sdata->vif.type == NL80211_IFTYPE_AP) { 527 local->fif_pspoll--; 528 local->fif_probe_req--; 529 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 530 local->fif_probe_req--; 531 } 532 533 if (sdata->dev) { 534 netif_addr_lock_bh(sdata->dev); 535 spin_lock_bh(&local->filter_lock); 536 __hw_addr_unsync(&local->mc_list, &sdata->dev->mc, 537 sdata->dev->addr_len); 538 spin_unlock_bh(&local->filter_lock); 539 netif_addr_unlock_bh(sdata->dev); 540 } 541 542 del_timer_sync(&local->dynamic_ps_timer); 543 wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work); 544 545 WARN(ieee80211_vif_is_mld(&sdata->vif), 546 "destroying interface with valid links 0x%04x\n", 547 sdata->vif.valid_links); 548 549 sdata->vif.bss_conf.csa_active = false; 550 if (sdata->vif.type == NL80211_IFTYPE_STATION) 551 sdata->deflink.u.mgd.csa.waiting_bcn = false; 552 ieee80211_vif_unblock_queues_csa(sdata); 553 554 wiphy_work_cancel(local->hw.wiphy, &sdata->deflink.csa.finalize_work); 555 wiphy_work_cancel(local->hw.wiphy, 556 &sdata->deflink.color_change_finalize_work); 557 wiphy_delayed_work_cancel(local->hw.wiphy, 558 &sdata->deflink.dfs_cac_timer_work); 559 560 if (sdata->wdev.links[0].cac_started) { 561 chandef = sdata->vif.bss_conf.chanreq.oper; 562 WARN_ON(local->suspended); 563 ieee80211_link_release_channel(&sdata->deflink); 564 cfg80211_cac_event(sdata->dev, &chandef, 565 NL80211_RADAR_CAC_ABORTED, 566 GFP_KERNEL, 0); 567 } 568 569 if (sdata->vif.type == NL80211_IFTYPE_AP) { 570 WARN_ON(!list_empty(&sdata->u.ap.vlans)); 571 } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 572 /* remove all packets in parent bc_buf pointing to this dev */ 573 ps = &sdata->bss->ps; 574 575 spin_lock_irqsave(&ps->bc_buf.lock, flags); 576 skb_queue_walk_safe(&ps->bc_buf, skb, tmp) { 577 if (skb->dev == sdata->dev) { 578 __skb_unlink(skb, &ps->bc_buf); 579 local->total_ps_buffered--; 580 ieee80211_free_txskb(&local->hw, skb); 581 } 582 } 583 spin_unlock_irqrestore(&ps->bc_buf.lock, flags); 584 } 585 586 if (going_down) 587 local->open_count--; 588 589 switch (sdata->vif.type) { 590 case NL80211_IFTYPE_AP_VLAN: 591 list_del(&sdata->u.vlan.list); 592 RCU_INIT_POINTER(sdata->vif.bss_conf.chanctx_conf, NULL); 593 /* see comment in the default case below */ 594 ieee80211_free_keys(sdata, true); 595 /* no need to tell driver */ 596 break; 597 case NL80211_IFTYPE_MONITOR: 598 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) { 599 local->cooked_mntrs--; 600 break; 601 } 602 603 local->monitors--; 604 if (local->monitors == 0) { 605 local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR; 606 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR; 607 } 608 609 ieee80211_adjust_monitor_flags(sdata, -1); 610 break; 611 case NL80211_IFTYPE_NAN: 612 /* clean all the functions */ 613 spin_lock_bh(&sdata->u.nan.func_lock); 614 615 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, i) { 616 idr_remove(&sdata->u.nan.function_inst_ids, i); 617 cfg80211_free_nan_func(func); 618 } 619 idr_destroy(&sdata->u.nan.function_inst_ids); 620 621 spin_unlock_bh(&sdata->u.nan.func_lock); 622 break; 623 case NL80211_IFTYPE_P2P_DEVICE: 624 /* relies on synchronize_rcu() below */ 625 RCU_INIT_POINTER(local->p2p_sdata, NULL); 626 fallthrough; 627 default: 628 wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->work); 629 /* 630 * When we get here, the interface is marked down. 631 * Free the remaining keys, if there are any 632 * (which can happen in AP mode if userspace sets 633 * keys before the interface is operating) 634 * 635 * Force the key freeing to always synchronize_net() 636 * to wait for the RX path in case it is using this 637 * interface enqueuing frames at this very time on 638 * another CPU. 639 */ 640 ieee80211_free_keys(sdata, true); 641 skb_queue_purge(&sdata->skb_queue); 642 skb_queue_purge(&sdata->status_queue); 643 } 644 645 /* 646 * Since ieee80211_free_txskb() may issue __dev_queue_xmit() 647 * which should be called with interrupts enabled, reclamation 648 * is done in two phases: 649 */ 650 __skb_queue_head_init(&freeq); 651 652 /* unlink from local queues... */ 653 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 654 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) { 655 skb_queue_walk_safe(&local->pending[i], skb, tmp) { 656 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 657 if (info->control.vif == &sdata->vif) { 658 __skb_unlink(skb, &local->pending[i]); 659 __skb_queue_tail(&freeq, skb); 660 } 661 } 662 } 663 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 664 665 /* ... and perform actual reclamation with interrupts enabled. */ 666 skb_queue_walk_safe(&freeq, skb, tmp) { 667 __skb_unlink(skb, &freeq); 668 ieee80211_free_txskb(&local->hw, skb); 669 } 670 671 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 672 ieee80211_txq_remove_vlan(local, sdata); 673 674 sdata->bss = NULL; 675 676 if (local->open_count == 0) 677 ieee80211_clear_tx_pending(local); 678 679 sdata->vif.bss_conf.beacon_int = 0; 680 681 /* 682 * If the interface goes down while suspended, presumably because 683 * the device was unplugged and that happens before our resume, 684 * then the driver is already unconfigured and the remainder of 685 * this function isn't needed. 686 * XXX: what about WoWLAN? If the device has software state, e.g. 687 * memory allocated, it might expect teardown commands from 688 * mac80211 here? 689 */ 690 if (local->suspended) { 691 WARN_ON(local->wowlan); 692 WARN_ON(rcu_access_pointer(local->monitor_sdata)); 693 return; 694 } 695 696 switch (sdata->vif.type) { 697 case NL80211_IFTYPE_AP_VLAN: 698 break; 699 case NL80211_IFTYPE_MONITOR: 700 if (local->monitors == 0) 701 ieee80211_del_virtual_monitor(local); 702 703 ieee80211_recalc_idle(local); 704 ieee80211_recalc_offload(local); 705 706 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) && 707 !ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) 708 break; 709 710 ieee80211_link_release_channel(&sdata->deflink); 711 fallthrough; 712 default: 713 if (!going_down) 714 break; 715 drv_remove_interface(local, sdata); 716 717 /* Clear private driver data to prevent reuse */ 718 memset(sdata->vif.drv_priv, 0, local->hw.vif_data_size); 719 } 720 721 ieee80211_recalc_ps(local); 722 723 if (cancel_scan) 724 wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work); 725 726 if (local->open_count == 0) { 727 ieee80211_stop_device(local, false); 728 729 /* no reconfiguring after stop! */ 730 return; 731 } 732 733 /* do after stop to avoid reconfiguring when we stop anyway */ 734 ieee80211_configure_filter(local); 735 ieee80211_hw_config(local, hw_reconf_flags); 736 737 if (local->monitors == local->open_count) 738 ieee80211_add_virtual_monitor(local); 739 } 740 741 static void ieee80211_stop_mbssid(struct ieee80211_sub_if_data *sdata) 742 { 743 struct ieee80211_sub_if_data *tx_sdata, *non_tx_sdata, *tmp_sdata; 744 struct ieee80211_vif *tx_vif = sdata->vif.mbssid_tx_vif; 745 746 if (!tx_vif) 747 return; 748 749 tx_sdata = vif_to_sdata(tx_vif); 750 sdata->vif.mbssid_tx_vif = NULL; 751 752 list_for_each_entry_safe(non_tx_sdata, tmp_sdata, 753 &tx_sdata->local->interfaces, list) { 754 if (non_tx_sdata != sdata && non_tx_sdata != tx_sdata && 755 non_tx_sdata->vif.mbssid_tx_vif == tx_vif && 756 ieee80211_sdata_running(non_tx_sdata)) { 757 non_tx_sdata->vif.mbssid_tx_vif = NULL; 758 dev_close(non_tx_sdata->wdev.netdev); 759 } 760 } 761 762 if (sdata != tx_sdata && ieee80211_sdata_running(tx_sdata)) { 763 tx_sdata->vif.mbssid_tx_vif = NULL; 764 dev_close(tx_sdata->wdev.netdev); 765 } 766 } 767 768 static int ieee80211_stop(struct net_device *dev) 769 { 770 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 771 772 /* close dependent VLAN and MBSSID interfaces before locking wiphy */ 773 if (sdata->vif.type == NL80211_IFTYPE_AP) { 774 struct ieee80211_sub_if_data *vlan, *tmpsdata; 775 776 list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans, 777 u.vlan.list) 778 dev_close(vlan->dev); 779 780 ieee80211_stop_mbssid(sdata); 781 } 782 783 wiphy_lock(sdata->local->hw.wiphy); 784 wiphy_work_cancel(sdata->local->hw.wiphy, &sdata->activate_links_work); 785 786 ieee80211_do_stop(sdata, true); 787 wiphy_unlock(sdata->local->hw.wiphy); 788 789 return 0; 790 } 791 792 static void ieee80211_set_multicast_list(struct net_device *dev) 793 { 794 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 795 struct ieee80211_local *local = sdata->local; 796 int allmulti, sdata_allmulti; 797 798 allmulti = !!(dev->flags & IFF_ALLMULTI); 799 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI); 800 801 if (allmulti != sdata_allmulti) { 802 if (dev->flags & IFF_ALLMULTI) 803 atomic_inc(&local->iff_allmultis); 804 else 805 atomic_dec(&local->iff_allmultis); 806 sdata->flags ^= IEEE80211_SDATA_ALLMULTI; 807 } 808 809 spin_lock_bh(&local->filter_lock); 810 __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len); 811 spin_unlock_bh(&local->filter_lock); 812 wiphy_work_queue(local->hw.wiphy, &local->reconfig_filter); 813 } 814 815 /* 816 * Called when the netdev is removed or, by the code below, before 817 * the interface type changes. 818 */ 819 static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata) 820 { 821 /* free extra data */ 822 ieee80211_free_keys(sdata, false); 823 824 ieee80211_debugfs_remove_netdev(sdata); 825 826 ieee80211_destroy_frag_cache(&sdata->frags); 827 828 if (ieee80211_vif_is_mesh(&sdata->vif)) 829 ieee80211_mesh_teardown_sdata(sdata); 830 831 ieee80211_vif_clear_links(sdata); 832 ieee80211_link_stop(&sdata->deflink); 833 } 834 835 static void ieee80211_uninit(struct net_device *dev) 836 { 837 ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev)); 838 } 839 840 static int ieee80211_netdev_setup_tc(struct net_device *dev, 841 enum tc_setup_type type, void *type_data) 842 { 843 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 844 struct ieee80211_local *local = sdata->local; 845 846 return drv_net_setup_tc(local, sdata, dev, type, type_data); 847 } 848 849 static const struct net_device_ops ieee80211_dataif_ops = { 850 .ndo_open = ieee80211_open, 851 .ndo_stop = ieee80211_stop, 852 .ndo_uninit = ieee80211_uninit, 853 .ndo_start_xmit = ieee80211_subif_start_xmit, 854 .ndo_set_rx_mode = ieee80211_set_multicast_list, 855 .ndo_set_mac_address = ieee80211_change_mac, 856 .ndo_setup_tc = ieee80211_netdev_setup_tc, 857 }; 858 859 static u16 ieee80211_monitor_select_queue(struct net_device *dev, 860 struct sk_buff *skb, 861 struct net_device *sb_dev) 862 { 863 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 864 struct ieee80211_local *local = sdata->local; 865 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 866 struct ieee80211_hdr *hdr; 867 int len_rthdr; 868 869 if (local->hw.queues < IEEE80211_NUM_ACS) 870 return 0; 871 872 /* reset flags and info before parsing radiotap header */ 873 memset(info, 0, sizeof(*info)); 874 875 if (!ieee80211_parse_tx_radiotap(skb, dev)) 876 return 0; /* doesn't matter, frame will be dropped */ 877 878 len_rthdr = ieee80211_get_radiotap_len(skb->data); 879 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr); 880 if (skb->len < len_rthdr + 2 || 881 skb->len < len_rthdr + ieee80211_hdrlen(hdr->frame_control)) 882 return 0; /* doesn't matter, frame will be dropped */ 883 884 return ieee80211_select_queue_80211(sdata, skb, hdr); 885 } 886 887 static const struct net_device_ops ieee80211_monitorif_ops = { 888 .ndo_open = ieee80211_open, 889 .ndo_stop = ieee80211_stop, 890 .ndo_uninit = ieee80211_uninit, 891 .ndo_start_xmit = ieee80211_monitor_start_xmit, 892 .ndo_set_rx_mode = ieee80211_set_multicast_list, 893 .ndo_set_mac_address = ieee80211_change_mac, 894 .ndo_select_queue = ieee80211_monitor_select_queue, 895 }; 896 897 static int ieee80211_netdev_fill_forward_path(struct net_device_path_ctx *ctx, 898 struct net_device_path *path) 899 { 900 struct ieee80211_sub_if_data *sdata; 901 struct ieee80211_local *local; 902 struct sta_info *sta; 903 int ret = -ENOENT; 904 905 sdata = IEEE80211_DEV_TO_SUB_IF(ctx->dev); 906 local = sdata->local; 907 908 if (!local->ops->net_fill_forward_path) 909 return -EOPNOTSUPP; 910 911 rcu_read_lock(); 912 switch (sdata->vif.type) { 913 case NL80211_IFTYPE_AP_VLAN: 914 sta = rcu_dereference(sdata->u.vlan.sta); 915 if (sta) 916 break; 917 if (sdata->wdev.use_4addr) 918 goto out; 919 if (is_multicast_ether_addr(ctx->daddr)) 920 goto out; 921 sta = sta_info_get_bss(sdata, ctx->daddr); 922 break; 923 case NL80211_IFTYPE_AP: 924 if (is_multicast_ether_addr(ctx->daddr)) 925 goto out; 926 sta = sta_info_get(sdata, ctx->daddr); 927 break; 928 case NL80211_IFTYPE_STATION: 929 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) { 930 sta = sta_info_get(sdata, ctx->daddr); 931 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) { 932 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) 933 goto out; 934 935 break; 936 } 937 } 938 939 sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid); 940 break; 941 default: 942 goto out; 943 } 944 945 if (!sta) 946 goto out; 947 948 ret = drv_net_fill_forward_path(local, sdata, &sta->sta, ctx, path); 949 out: 950 rcu_read_unlock(); 951 952 return ret; 953 } 954 955 static const struct net_device_ops ieee80211_dataif_8023_ops = { 956 .ndo_open = ieee80211_open, 957 .ndo_stop = ieee80211_stop, 958 .ndo_uninit = ieee80211_uninit, 959 .ndo_start_xmit = ieee80211_subif_start_xmit_8023, 960 .ndo_set_rx_mode = ieee80211_set_multicast_list, 961 .ndo_set_mac_address = ieee80211_change_mac, 962 .ndo_fill_forward_path = ieee80211_netdev_fill_forward_path, 963 .ndo_setup_tc = ieee80211_netdev_setup_tc, 964 }; 965 966 static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype) 967 { 968 switch (iftype) { 969 /* P2P GO and client are mapped to AP/STATION types */ 970 case NL80211_IFTYPE_AP: 971 case NL80211_IFTYPE_STATION: 972 return true; 973 default: 974 return false; 975 } 976 } 977 978 static bool ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data *sdata) 979 { 980 struct ieee80211_local *local = sdata->local; 981 u32 flags; 982 983 flags = sdata->vif.offload_flags; 984 985 if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) && 986 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) { 987 flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED; 988 989 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) && 990 local->hw.wiphy->frag_threshold != (u32)-1) 991 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED; 992 993 if (local->monitors) 994 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED; 995 } else { 996 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED; 997 } 998 999 if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) && 1000 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) { 1001 flags |= IEEE80211_OFFLOAD_DECAP_ENABLED; 1002 1003 if (local->monitors && 1004 !ieee80211_hw_check(&local->hw, SUPPORTS_CONC_MON_RX_DECAP)) 1005 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED; 1006 } else { 1007 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED; 1008 } 1009 1010 if (sdata->vif.offload_flags == flags) 1011 return false; 1012 1013 sdata->vif.offload_flags = flags; 1014 ieee80211_check_fast_rx_iface(sdata); 1015 return true; 1016 } 1017 1018 static void ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data *sdata) 1019 { 1020 struct ieee80211_local *local = sdata->local; 1021 struct ieee80211_sub_if_data *bss = sdata; 1022 bool enabled; 1023 1024 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1025 if (!sdata->bss) 1026 return; 1027 1028 bss = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); 1029 } 1030 1031 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) || 1032 !ieee80211_iftype_supports_hdr_offload(bss->vif.type)) 1033 return; 1034 1035 enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED; 1036 if (sdata->wdev.use_4addr && 1037 !(bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_4ADDR)) 1038 enabled = false; 1039 1040 sdata->dev->netdev_ops = enabled ? &ieee80211_dataif_8023_ops : 1041 &ieee80211_dataif_ops; 1042 } 1043 1044 static void ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data *sdata) 1045 { 1046 struct ieee80211_local *local = sdata->local; 1047 struct ieee80211_sub_if_data *vsdata; 1048 1049 if (ieee80211_set_sdata_offload_flags(sdata)) { 1050 drv_update_vif_offload(local, sdata); 1051 ieee80211_set_vif_encap_ops(sdata); 1052 } 1053 1054 list_for_each_entry(vsdata, &local->interfaces, list) { 1055 if (vsdata->vif.type != NL80211_IFTYPE_AP_VLAN || 1056 vsdata->bss != &sdata->u.ap) 1057 continue; 1058 1059 ieee80211_set_vif_encap_ops(vsdata); 1060 } 1061 } 1062 1063 void ieee80211_recalc_offload(struct ieee80211_local *local) 1064 { 1065 struct ieee80211_sub_if_data *sdata; 1066 1067 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD)) 1068 return; 1069 1070 lockdep_assert_wiphy(local->hw.wiphy); 1071 1072 list_for_each_entry(sdata, &local->interfaces, list) { 1073 if (!ieee80211_sdata_running(sdata)) 1074 continue; 1075 1076 ieee80211_recalc_sdata_offload(sdata); 1077 } 1078 } 1079 1080 void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata, 1081 const int offset) 1082 { 1083 struct ieee80211_local *local = sdata->local; 1084 u32 flags = sdata->u.mntr.flags; 1085 1086 #define ADJUST(_f, _s) do { \ 1087 if (flags & MONITOR_FLAG_##_f) \ 1088 local->fif_##_s += offset; \ 1089 } while (0) 1090 1091 ADJUST(FCSFAIL, fcsfail); 1092 ADJUST(PLCPFAIL, plcpfail); 1093 ADJUST(CONTROL, control); 1094 ADJUST(CONTROL, pspoll); 1095 ADJUST(OTHER_BSS, other_bss); 1096 if (!(flags & MONITOR_FLAG_SKIP_TX)) 1097 local->tx_mntrs += offset; 1098 1099 #undef ADJUST 1100 } 1101 1102 static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata) 1103 { 1104 struct ieee80211_local *local = sdata->local; 1105 int i; 1106 1107 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 1108 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) 1109 sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE; 1110 else if (local->hw.queues >= IEEE80211_NUM_ACS) 1111 sdata->vif.hw_queue[i] = i; 1112 else 1113 sdata->vif.hw_queue[i] = 0; 1114 } 1115 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE; 1116 } 1117 1118 static void ieee80211_sdata_init(struct ieee80211_local *local, 1119 struct ieee80211_sub_if_data *sdata) 1120 { 1121 sdata->local = local; 1122 1123 /* 1124 * Initialize the default link, so we can use link_id 0 for non-MLD, 1125 * and that continues to work for non-MLD-aware drivers that use just 1126 * vif.bss_conf instead of vif.link_conf. 1127 * 1128 * Note that we never change this, so if link ID 0 isn't used in an 1129 * MLD connection, we get a separate allocation for it. 1130 */ 1131 ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf); 1132 } 1133 1134 int ieee80211_add_virtual_monitor(struct ieee80211_local *local) 1135 { 1136 struct ieee80211_sub_if_data *sdata; 1137 int ret; 1138 1139 ASSERT_RTNL(); 1140 lockdep_assert_wiphy(local->hw.wiphy); 1141 1142 if (local->monitor_sdata || 1143 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) 1144 return 0; 1145 1146 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL); 1147 if (!sdata) 1148 return -ENOMEM; 1149 1150 /* set up data */ 1151 sdata->vif.type = NL80211_IFTYPE_MONITOR; 1152 snprintf(sdata->name, IFNAMSIZ, "%s-monitor", 1153 wiphy_name(local->hw.wiphy)); 1154 sdata->wdev.iftype = NL80211_IFTYPE_MONITOR; 1155 sdata->wdev.wiphy = local->hw.wiphy; 1156 1157 ieee80211_sdata_init(local, sdata); 1158 1159 ieee80211_set_default_queues(sdata); 1160 1161 if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) { 1162 ret = drv_add_interface(local, sdata); 1163 if (WARN_ON(ret)) { 1164 /* ok .. stupid driver, it asked for this! */ 1165 kfree(sdata); 1166 return ret; 1167 } 1168 } 1169 1170 set_bit(SDATA_STATE_RUNNING, &sdata->state); 1171 1172 ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR); 1173 if (ret) { 1174 kfree(sdata); 1175 return ret; 1176 } 1177 1178 mutex_lock(&local->iflist_mtx); 1179 rcu_assign_pointer(local->monitor_sdata, sdata); 1180 mutex_unlock(&local->iflist_mtx); 1181 1182 ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chanreq, 1183 IEEE80211_CHANCTX_EXCLUSIVE); 1184 if (ret) { 1185 mutex_lock(&local->iflist_mtx); 1186 RCU_INIT_POINTER(local->monitor_sdata, NULL); 1187 mutex_unlock(&local->iflist_mtx); 1188 synchronize_net(); 1189 drv_remove_interface(local, sdata); 1190 kfree(sdata); 1191 return ret; 1192 } 1193 1194 skb_queue_head_init(&sdata->skb_queue); 1195 skb_queue_head_init(&sdata->status_queue); 1196 wiphy_work_init(&sdata->work, ieee80211_iface_work); 1197 1198 return 0; 1199 } 1200 1201 void ieee80211_del_virtual_monitor(struct ieee80211_local *local) 1202 { 1203 struct ieee80211_sub_if_data *sdata; 1204 1205 if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) 1206 return; 1207 1208 ASSERT_RTNL(); 1209 lockdep_assert_wiphy(local->hw.wiphy); 1210 1211 mutex_lock(&local->iflist_mtx); 1212 1213 sdata = rcu_dereference_protected(local->monitor_sdata, 1214 lockdep_is_held(&local->iflist_mtx)); 1215 if (!sdata) { 1216 mutex_unlock(&local->iflist_mtx); 1217 return; 1218 } 1219 1220 RCU_INIT_POINTER(local->monitor_sdata, NULL); 1221 mutex_unlock(&local->iflist_mtx); 1222 1223 synchronize_net(); 1224 1225 ieee80211_link_release_channel(&sdata->deflink); 1226 1227 if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) 1228 drv_remove_interface(local, sdata); 1229 1230 kfree(sdata); 1231 } 1232 1233 /* 1234 * NOTE: Be very careful when changing this function, it must NOT return 1235 * an error on interface type changes that have been pre-checked, so most 1236 * checks should be in ieee80211_check_concurrent_iface. 1237 */ 1238 int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up) 1239 { 1240 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev); 1241 struct net_device *dev = wdev->netdev; 1242 struct ieee80211_local *local = sdata->local; 1243 u64 changed = 0; 1244 int res; 1245 u32 hw_reconf_flags = 0; 1246 1247 lockdep_assert_wiphy(local->hw.wiphy); 1248 1249 switch (sdata->vif.type) { 1250 case NL80211_IFTYPE_AP_VLAN: { 1251 struct ieee80211_sub_if_data *master; 1252 1253 if (!sdata->bss) 1254 return -ENOLINK; 1255 1256 list_add(&sdata->u.vlan.list, &sdata->bss->vlans); 1257 1258 master = container_of(sdata->bss, 1259 struct ieee80211_sub_if_data, u.ap); 1260 sdata->control_port_protocol = 1261 master->control_port_protocol; 1262 sdata->control_port_no_encrypt = 1263 master->control_port_no_encrypt; 1264 sdata->control_port_over_nl80211 = 1265 master->control_port_over_nl80211; 1266 sdata->control_port_no_preauth = 1267 master->control_port_no_preauth; 1268 sdata->vif.cab_queue = master->vif.cab_queue; 1269 memcpy(sdata->vif.hw_queue, master->vif.hw_queue, 1270 sizeof(sdata->vif.hw_queue)); 1271 sdata->vif.bss_conf.chanreq = master->vif.bss_conf.chanreq; 1272 1273 sdata->crypto_tx_tailroom_needed_cnt += 1274 master->crypto_tx_tailroom_needed_cnt; 1275 1276 break; 1277 } 1278 case NL80211_IFTYPE_AP: 1279 sdata->bss = &sdata->u.ap; 1280 break; 1281 case NL80211_IFTYPE_MESH_POINT: 1282 case NL80211_IFTYPE_STATION: 1283 case NL80211_IFTYPE_MONITOR: 1284 case NL80211_IFTYPE_ADHOC: 1285 case NL80211_IFTYPE_P2P_DEVICE: 1286 case NL80211_IFTYPE_OCB: 1287 case NL80211_IFTYPE_NAN: 1288 /* no special treatment */ 1289 break; 1290 case NL80211_IFTYPE_UNSPECIFIED: 1291 case NUM_NL80211_IFTYPES: 1292 case NL80211_IFTYPE_P2P_CLIENT: 1293 case NL80211_IFTYPE_P2P_GO: 1294 case NL80211_IFTYPE_WDS: 1295 /* cannot happen */ 1296 WARN_ON(1); 1297 break; 1298 } 1299 1300 if (local->open_count == 0) { 1301 /* here we can consider everything in good order (again) */ 1302 local->reconfig_failure = false; 1303 1304 res = drv_start(local); 1305 if (res) 1306 goto err_del_bss; 1307 ieee80211_led_radio(local, true); 1308 ieee80211_mod_tpt_led_trig(local, 1309 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0); 1310 } 1311 1312 /* 1313 * Copy the hopefully now-present MAC address to 1314 * this interface, if it has the special null one. 1315 */ 1316 if (dev && is_zero_ether_addr(dev->dev_addr)) { 1317 eth_hw_addr_set(dev, local->hw.wiphy->perm_addr); 1318 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN); 1319 1320 if (!is_valid_ether_addr(dev->dev_addr)) { 1321 res = -EADDRNOTAVAIL; 1322 goto err_stop; 1323 } 1324 } 1325 1326 switch (sdata->vif.type) { 1327 case NL80211_IFTYPE_AP_VLAN: 1328 /* no need to tell driver, but set carrier and chanctx */ 1329 if (sdata->bss->active) { 1330 ieee80211_link_vlan_copy_chanctx(&sdata->deflink); 1331 netif_carrier_on(dev); 1332 ieee80211_set_vif_encap_ops(sdata); 1333 } else { 1334 netif_carrier_off(dev); 1335 } 1336 break; 1337 case NL80211_IFTYPE_MONITOR: 1338 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) { 1339 local->cooked_mntrs++; 1340 break; 1341 } 1342 1343 if ((sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) || 1344 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) { 1345 res = drv_add_interface(local, sdata); 1346 if (res) 1347 goto err_stop; 1348 } else if (local->monitors == 0 && local->open_count == 0) { 1349 res = ieee80211_add_virtual_monitor(local); 1350 if (res) 1351 goto err_stop; 1352 } 1353 1354 /* must be before the call to ieee80211_configure_filter */ 1355 local->monitors++; 1356 if (local->monitors == 1) { 1357 local->hw.conf.flags |= IEEE80211_CONF_MONITOR; 1358 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR; 1359 } 1360 1361 ieee80211_adjust_monitor_flags(sdata, 1); 1362 ieee80211_configure_filter(local); 1363 ieee80211_recalc_offload(local); 1364 ieee80211_recalc_idle(local); 1365 1366 netif_carrier_on(dev); 1367 break; 1368 default: 1369 if (coming_up) { 1370 ieee80211_del_virtual_monitor(local); 1371 ieee80211_set_sdata_offload_flags(sdata); 1372 1373 res = drv_add_interface(local, sdata); 1374 if (res) 1375 goto err_stop; 1376 1377 ieee80211_set_vif_encap_ops(sdata); 1378 res = ieee80211_check_queues(sdata, 1379 ieee80211_vif_type_p2p(&sdata->vif)); 1380 if (res) 1381 goto err_del_interface; 1382 } 1383 1384 if (sdata->vif.type == NL80211_IFTYPE_AP) { 1385 local->fif_pspoll++; 1386 local->fif_probe_req++; 1387 1388 ieee80211_configure_filter(local); 1389 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 1390 local->fif_probe_req++; 1391 } 1392 1393 if (sdata->vif.probe_req_reg) 1394 drv_config_iface_filter(local, sdata, 1395 FIF_PROBE_REQ, 1396 FIF_PROBE_REQ); 1397 1398 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && 1399 sdata->vif.type != NL80211_IFTYPE_NAN) 1400 changed |= ieee80211_reset_erp_info(sdata); 1401 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 1402 changed); 1403 1404 switch (sdata->vif.type) { 1405 case NL80211_IFTYPE_STATION: 1406 case NL80211_IFTYPE_ADHOC: 1407 case NL80211_IFTYPE_AP: 1408 case NL80211_IFTYPE_MESH_POINT: 1409 case NL80211_IFTYPE_OCB: 1410 netif_carrier_off(dev); 1411 break; 1412 case NL80211_IFTYPE_P2P_DEVICE: 1413 case NL80211_IFTYPE_NAN: 1414 break; 1415 default: 1416 /* not reached */ 1417 WARN_ON(1); 1418 } 1419 1420 /* 1421 * Set default queue parameters so drivers don't 1422 * need to initialise the hardware if the hardware 1423 * doesn't start up with sane defaults. 1424 * Enable QoS for anything but station interfaces. 1425 */ 1426 ieee80211_set_wmm_default(&sdata->deflink, true, 1427 sdata->vif.type != NL80211_IFTYPE_STATION); 1428 } 1429 1430 switch (sdata->vif.type) { 1431 case NL80211_IFTYPE_P2P_DEVICE: 1432 rcu_assign_pointer(local->p2p_sdata, sdata); 1433 break; 1434 case NL80211_IFTYPE_MONITOR: 1435 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) 1436 break; 1437 list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list); 1438 break; 1439 default: 1440 break; 1441 } 1442 1443 /* 1444 * set_multicast_list will be invoked by the networking core 1445 * which will check whether any increments here were done in 1446 * error and sync them down to the hardware as filter flags. 1447 */ 1448 if (sdata->flags & IEEE80211_SDATA_ALLMULTI) 1449 atomic_inc(&local->iff_allmultis); 1450 1451 if (coming_up) 1452 local->open_count++; 1453 1454 if (local->open_count == 1) 1455 ieee80211_hw_conf_init(local); 1456 else if (hw_reconf_flags) 1457 ieee80211_hw_config(local, hw_reconf_flags); 1458 1459 ieee80211_recalc_ps(local); 1460 1461 set_bit(SDATA_STATE_RUNNING, &sdata->state); 1462 1463 return 0; 1464 err_del_interface: 1465 drv_remove_interface(local, sdata); 1466 err_stop: 1467 if (!local->open_count) 1468 drv_stop(local, false); 1469 err_del_bss: 1470 sdata->bss = NULL; 1471 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1472 list_del(&sdata->u.vlan.list); 1473 /* might already be clear but that doesn't matter */ 1474 clear_bit(SDATA_STATE_RUNNING, &sdata->state); 1475 return res; 1476 } 1477 1478 static void ieee80211_if_setup(struct net_device *dev) 1479 { 1480 ether_setup(dev); 1481 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1482 dev->priv_flags |= IFF_NO_QUEUE; 1483 dev->netdev_ops = &ieee80211_dataif_ops; 1484 dev->needs_free_netdev = true; 1485 } 1486 1487 static void ieee80211_iface_process_skb(struct ieee80211_local *local, 1488 struct ieee80211_sub_if_data *sdata, 1489 struct sk_buff *skb) 1490 { 1491 struct ieee80211_mgmt *mgmt = (void *)skb->data; 1492 1493 lockdep_assert_wiphy(local->hw.wiphy); 1494 1495 if (ieee80211_is_action(mgmt->frame_control) && 1496 mgmt->u.action.category == WLAN_CATEGORY_BACK) { 1497 struct sta_info *sta; 1498 int len = skb->len; 1499 1500 sta = sta_info_get_bss(sdata, mgmt->sa); 1501 if (sta) { 1502 switch (mgmt->u.action.u.addba_req.action_code) { 1503 case WLAN_ACTION_ADDBA_REQ: 1504 ieee80211_process_addba_request(local, sta, 1505 mgmt, len); 1506 break; 1507 case WLAN_ACTION_ADDBA_RESP: 1508 ieee80211_process_addba_resp(local, sta, 1509 mgmt, len); 1510 break; 1511 case WLAN_ACTION_DELBA: 1512 ieee80211_process_delba(sdata, sta, 1513 mgmt, len); 1514 break; 1515 default: 1516 WARN_ON(1); 1517 break; 1518 } 1519 } 1520 } else if (ieee80211_is_action(mgmt->frame_control) && 1521 mgmt->u.action.category == WLAN_CATEGORY_VHT) { 1522 switch (mgmt->u.action.u.vht_group_notif.action_code) { 1523 case WLAN_VHT_ACTION_OPMODE_NOTIF: { 1524 struct ieee80211_rx_status *status; 1525 enum nl80211_band band; 1526 struct sta_info *sta; 1527 u8 opmode; 1528 1529 status = IEEE80211_SKB_RXCB(skb); 1530 band = status->band; 1531 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode; 1532 1533 sta = sta_info_get_bss(sdata, mgmt->sa); 1534 1535 if (sta) 1536 ieee80211_vht_handle_opmode(sdata, 1537 &sta->deflink, 1538 opmode, band); 1539 1540 break; 1541 } 1542 case WLAN_VHT_ACTION_GROUPID_MGMT: 1543 ieee80211_process_mu_groups(sdata, &sdata->deflink, 1544 mgmt); 1545 break; 1546 default: 1547 WARN_ON(1); 1548 break; 1549 } 1550 } else if (ieee80211_is_action(mgmt->frame_control) && 1551 mgmt->u.action.category == WLAN_CATEGORY_S1G) { 1552 switch (mgmt->u.action.u.s1g.action_code) { 1553 case WLAN_S1G_TWT_TEARDOWN: 1554 case WLAN_S1G_TWT_SETUP: 1555 ieee80211_s1g_rx_twt_action(sdata, skb); 1556 break; 1557 default: 1558 break; 1559 } 1560 } else if (ieee80211_is_action(mgmt->frame_control) && 1561 mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_EHT) { 1562 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 1563 switch (mgmt->u.action.u.ttlm_req.action_code) { 1564 case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ: 1565 ieee80211_process_neg_ttlm_req(sdata, mgmt, 1566 skb->len); 1567 break; 1568 case WLAN_PROTECTED_EHT_ACTION_TTLM_RES: 1569 ieee80211_process_neg_ttlm_res(sdata, mgmt, 1570 skb->len); 1571 break; 1572 default: 1573 break; 1574 } 1575 } 1576 } else if (ieee80211_is_ext(mgmt->frame_control)) { 1577 if (sdata->vif.type == NL80211_IFTYPE_STATION) 1578 ieee80211_sta_rx_queued_ext(sdata, skb); 1579 else 1580 WARN_ON(1); 1581 } else if (ieee80211_is_data_qos(mgmt->frame_control)) { 1582 struct ieee80211_hdr *hdr = (void *)mgmt; 1583 struct sta_info *sta; 1584 1585 /* 1586 * So the frame isn't mgmt, but frame_control 1587 * is at the right place anyway, of course, so 1588 * the if statement is correct. 1589 * 1590 * Warn if we have other data frame types here, 1591 * they must not get here. 1592 */ 1593 WARN_ON(hdr->frame_control & 1594 cpu_to_le16(IEEE80211_STYPE_NULLFUNC)); 1595 WARN_ON(!(hdr->seq_ctrl & 1596 cpu_to_le16(IEEE80211_SCTL_FRAG))); 1597 /* 1598 * This was a fragment of a frame, received while 1599 * a block-ack session was active. That cannot be 1600 * right, so terminate the session. 1601 */ 1602 sta = sta_info_get_bss(sdata, mgmt->sa); 1603 if (sta) { 1604 u16 tid = ieee80211_get_tid(hdr); 1605 1606 __ieee80211_stop_rx_ba_session( 1607 sta, tid, WLAN_BACK_RECIPIENT, 1608 WLAN_REASON_QSTA_REQUIRE_SETUP, 1609 true); 1610 } 1611 } else switch (sdata->vif.type) { 1612 case NL80211_IFTYPE_STATION: 1613 ieee80211_sta_rx_queued_mgmt(sdata, skb); 1614 break; 1615 case NL80211_IFTYPE_ADHOC: 1616 ieee80211_ibss_rx_queued_mgmt(sdata, skb); 1617 break; 1618 case NL80211_IFTYPE_MESH_POINT: 1619 if (!ieee80211_vif_is_mesh(&sdata->vif)) 1620 break; 1621 ieee80211_mesh_rx_queued_mgmt(sdata, skb); 1622 break; 1623 default: 1624 WARN(1, "frame for unexpected interface type"); 1625 break; 1626 } 1627 } 1628 1629 static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata, 1630 struct sk_buff *skb) 1631 { 1632 struct ieee80211_mgmt *mgmt = (void *)skb->data; 1633 1634 if (ieee80211_is_action(mgmt->frame_control) && 1635 mgmt->u.action.category == WLAN_CATEGORY_S1G) { 1636 switch (mgmt->u.action.u.s1g.action_code) { 1637 case WLAN_S1G_TWT_TEARDOWN: 1638 case WLAN_S1G_TWT_SETUP: 1639 ieee80211_s1g_status_twt_action(sdata, skb); 1640 break; 1641 default: 1642 break; 1643 } 1644 } 1645 } 1646 1647 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work) 1648 { 1649 struct ieee80211_sub_if_data *sdata = 1650 container_of(work, struct ieee80211_sub_if_data, work); 1651 struct ieee80211_local *local = sdata->local; 1652 struct sk_buff *skb; 1653 1654 if (!ieee80211_sdata_running(sdata)) 1655 return; 1656 1657 if (test_bit(SCAN_SW_SCANNING, &local->scanning)) 1658 return; 1659 1660 if (!ieee80211_can_run_worker(local)) 1661 return; 1662 1663 /* first process frames */ 1664 while ((skb = skb_dequeue(&sdata->skb_queue))) { 1665 kcov_remote_start_common(skb_get_kcov_handle(skb)); 1666 1667 if (skb->protocol == cpu_to_be16(ETH_P_TDLS)) 1668 ieee80211_process_tdls_channel_switch(sdata, skb); 1669 else 1670 ieee80211_iface_process_skb(local, sdata, skb); 1671 1672 kfree_skb(skb); 1673 kcov_remote_stop(); 1674 } 1675 1676 /* process status queue */ 1677 while ((skb = skb_dequeue(&sdata->status_queue))) { 1678 kcov_remote_start_common(skb_get_kcov_handle(skb)); 1679 1680 ieee80211_iface_process_status(sdata, skb); 1681 kfree_skb(skb); 1682 1683 kcov_remote_stop(); 1684 } 1685 1686 /* then other type-dependent work */ 1687 switch (sdata->vif.type) { 1688 case NL80211_IFTYPE_STATION: 1689 ieee80211_sta_work(sdata); 1690 break; 1691 case NL80211_IFTYPE_ADHOC: 1692 ieee80211_ibss_work(sdata); 1693 break; 1694 case NL80211_IFTYPE_MESH_POINT: 1695 if (!ieee80211_vif_is_mesh(&sdata->vif)) 1696 break; 1697 ieee80211_mesh_work(sdata); 1698 break; 1699 case NL80211_IFTYPE_OCB: 1700 ieee80211_ocb_work(sdata); 1701 break; 1702 default: 1703 break; 1704 } 1705 } 1706 1707 static void ieee80211_activate_links_work(struct wiphy *wiphy, 1708 struct wiphy_work *work) 1709 { 1710 struct ieee80211_sub_if_data *sdata = 1711 container_of(work, struct ieee80211_sub_if_data, 1712 activate_links_work); 1713 struct ieee80211_local *local = wiphy_priv(wiphy); 1714 1715 if (local->in_reconfig) 1716 return; 1717 1718 ieee80211_set_active_links(&sdata->vif, sdata->desired_active_links); 1719 sdata->desired_active_links = 0; 1720 } 1721 1722 /* 1723 * Helper function to initialise an interface to a specific type. 1724 */ 1725 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata, 1726 enum nl80211_iftype type) 1727 { 1728 static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff, 1729 0xff, 0xff, 0xff}; 1730 1731 /* clear type-dependent unions */ 1732 memset(&sdata->u, 0, sizeof(sdata->u)); 1733 memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u)); 1734 1735 /* and set some type-dependent values */ 1736 sdata->vif.type = type; 1737 sdata->vif.p2p = false; 1738 sdata->wdev.iftype = type; 1739 1740 sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE); 1741 sdata->control_port_no_encrypt = false; 1742 sdata->control_port_over_nl80211 = false; 1743 sdata->control_port_no_preauth = false; 1744 sdata->vif.cfg.idle = true; 1745 sdata->vif.bss_conf.txpower = INT_MIN; /* unset */ 1746 1747 sdata->noack_map = 0; 1748 1749 /* only monitor/p2p-device differ */ 1750 if (sdata->dev) { 1751 sdata->dev->netdev_ops = &ieee80211_dataif_ops; 1752 sdata->dev->type = ARPHRD_ETHER; 1753 } 1754 1755 skb_queue_head_init(&sdata->skb_queue); 1756 skb_queue_head_init(&sdata->status_queue); 1757 wiphy_work_init(&sdata->work, ieee80211_iface_work); 1758 wiphy_work_init(&sdata->activate_links_work, 1759 ieee80211_activate_links_work); 1760 1761 switch (type) { 1762 case NL80211_IFTYPE_P2P_GO: 1763 type = NL80211_IFTYPE_AP; 1764 sdata->vif.type = type; 1765 sdata->vif.p2p = true; 1766 fallthrough; 1767 case NL80211_IFTYPE_AP: 1768 skb_queue_head_init(&sdata->u.ap.ps.bc_buf); 1769 INIT_LIST_HEAD(&sdata->u.ap.vlans); 1770 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1771 break; 1772 case NL80211_IFTYPE_P2P_CLIENT: 1773 type = NL80211_IFTYPE_STATION; 1774 sdata->vif.type = type; 1775 sdata->vif.p2p = true; 1776 fallthrough; 1777 case NL80211_IFTYPE_STATION: 1778 sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid; 1779 ieee80211_sta_setup_sdata(sdata); 1780 break; 1781 case NL80211_IFTYPE_OCB: 1782 sdata->vif.bss_conf.bssid = bssid_wildcard; 1783 ieee80211_ocb_setup_sdata(sdata); 1784 break; 1785 case NL80211_IFTYPE_ADHOC: 1786 sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid; 1787 ieee80211_ibss_setup_sdata(sdata); 1788 break; 1789 case NL80211_IFTYPE_MESH_POINT: 1790 if (ieee80211_vif_is_mesh(&sdata->vif)) 1791 ieee80211_mesh_init_sdata(sdata); 1792 break; 1793 case NL80211_IFTYPE_MONITOR: 1794 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP; 1795 sdata->dev->netdev_ops = &ieee80211_monitorif_ops; 1796 sdata->u.mntr.flags = MONITOR_FLAG_CONTROL | 1797 MONITOR_FLAG_OTHER_BSS; 1798 break; 1799 case NL80211_IFTYPE_NAN: 1800 idr_init(&sdata->u.nan.function_inst_ids); 1801 spin_lock_init(&sdata->u.nan.func_lock); 1802 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1803 break; 1804 case NL80211_IFTYPE_AP_VLAN: 1805 case NL80211_IFTYPE_P2P_DEVICE: 1806 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1807 break; 1808 case NL80211_IFTYPE_UNSPECIFIED: 1809 case NL80211_IFTYPE_WDS: 1810 case NUM_NL80211_IFTYPES: 1811 WARN_ON(1); 1812 break; 1813 } 1814 1815 /* need to do this after the switch so vif.type is correct */ 1816 ieee80211_link_setup(&sdata->deflink); 1817 1818 ieee80211_debugfs_recreate_netdev(sdata, false); 1819 } 1820 1821 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata, 1822 enum nl80211_iftype type) 1823 { 1824 struct ieee80211_local *local = sdata->local; 1825 int ret, err; 1826 enum nl80211_iftype internal_type = type; 1827 bool p2p = false; 1828 1829 ASSERT_RTNL(); 1830 1831 if (!local->ops->change_interface) 1832 return -EBUSY; 1833 1834 /* for now, don't support changing while links exist */ 1835 if (ieee80211_vif_is_mld(&sdata->vif)) 1836 return -EBUSY; 1837 1838 switch (sdata->vif.type) { 1839 case NL80211_IFTYPE_AP: 1840 if (!list_empty(&sdata->u.ap.vlans)) 1841 return -EBUSY; 1842 break; 1843 case NL80211_IFTYPE_STATION: 1844 case NL80211_IFTYPE_ADHOC: 1845 case NL80211_IFTYPE_OCB: 1846 /* 1847 * Could maybe also all others here? 1848 * Just not sure how that interacts 1849 * with the RX/config path e.g. for 1850 * mesh. 1851 */ 1852 break; 1853 default: 1854 return -EBUSY; 1855 } 1856 1857 switch (type) { 1858 case NL80211_IFTYPE_AP: 1859 case NL80211_IFTYPE_STATION: 1860 case NL80211_IFTYPE_ADHOC: 1861 case NL80211_IFTYPE_OCB: 1862 /* 1863 * Could probably support everything 1864 * but here. 1865 */ 1866 break; 1867 case NL80211_IFTYPE_P2P_CLIENT: 1868 p2p = true; 1869 internal_type = NL80211_IFTYPE_STATION; 1870 break; 1871 case NL80211_IFTYPE_P2P_GO: 1872 p2p = true; 1873 internal_type = NL80211_IFTYPE_AP; 1874 break; 1875 default: 1876 return -EBUSY; 1877 } 1878 1879 ret = ieee80211_check_concurrent_iface(sdata, internal_type); 1880 if (ret) 1881 return ret; 1882 1883 ieee80211_stop_vif_queues(local, sdata, 1884 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE); 1885 /* do_stop will synchronize_rcu() first thing */ 1886 ieee80211_do_stop(sdata, false); 1887 1888 ieee80211_teardown_sdata(sdata); 1889 1890 ieee80211_set_sdata_offload_flags(sdata); 1891 ret = drv_change_interface(local, sdata, internal_type, p2p); 1892 if (ret) 1893 type = ieee80211_vif_type_p2p(&sdata->vif); 1894 1895 /* 1896 * Ignore return value here, there's not much we can do since 1897 * the driver changed the interface type internally already. 1898 * The warnings will hopefully make driver authors fix it :-) 1899 */ 1900 ieee80211_check_queues(sdata, type); 1901 1902 ieee80211_setup_sdata(sdata, type); 1903 ieee80211_set_vif_encap_ops(sdata); 1904 1905 err = ieee80211_do_open(&sdata->wdev, false); 1906 WARN(err, "type change: do_open returned %d", err); 1907 1908 ieee80211_wake_vif_queues(local, sdata, 1909 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE); 1910 return ret; 1911 } 1912 1913 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata, 1914 enum nl80211_iftype type) 1915 { 1916 int ret; 1917 1918 ASSERT_RTNL(); 1919 1920 if (type == ieee80211_vif_type_p2p(&sdata->vif)) 1921 return 0; 1922 1923 if (ieee80211_sdata_running(sdata)) { 1924 ret = ieee80211_runtime_change_iftype(sdata, type); 1925 if (ret) 1926 return ret; 1927 } else { 1928 /* Purge and reset type-dependent state. */ 1929 ieee80211_teardown_sdata(sdata); 1930 ieee80211_setup_sdata(sdata, type); 1931 } 1932 1933 /* reset some values that shouldn't be kept across type changes */ 1934 if (type == NL80211_IFTYPE_STATION) 1935 sdata->u.mgd.use_4addr = false; 1936 1937 return 0; 1938 } 1939 1940 static void ieee80211_assign_perm_addr(struct ieee80211_local *local, 1941 u8 *perm_addr, enum nl80211_iftype type) 1942 { 1943 struct ieee80211_sub_if_data *sdata; 1944 u64 mask, start, addr, val, inc; 1945 u8 *m; 1946 u8 tmp_addr[ETH_ALEN]; 1947 int i; 1948 1949 lockdep_assert_wiphy(local->hw.wiphy); 1950 1951 /* default ... something at least */ 1952 memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN); 1953 1954 if (is_zero_ether_addr(local->hw.wiphy->addr_mask) && 1955 local->hw.wiphy->n_addresses <= 1) 1956 return; 1957 1958 switch (type) { 1959 case NL80211_IFTYPE_MONITOR: 1960 /* doesn't matter */ 1961 break; 1962 case NL80211_IFTYPE_AP_VLAN: 1963 /* match up with an AP interface */ 1964 list_for_each_entry(sdata, &local->interfaces, list) { 1965 if (sdata->vif.type != NL80211_IFTYPE_AP) 1966 continue; 1967 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN); 1968 break; 1969 } 1970 /* keep default if no AP interface present */ 1971 break; 1972 case NL80211_IFTYPE_P2P_CLIENT: 1973 case NL80211_IFTYPE_P2P_GO: 1974 if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) { 1975 list_for_each_entry(sdata, &local->interfaces, list) { 1976 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) 1977 continue; 1978 if (!ieee80211_sdata_running(sdata)) 1979 continue; 1980 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN); 1981 return; 1982 } 1983 } 1984 fallthrough; 1985 default: 1986 /* assign a new address if possible -- try n_addresses first */ 1987 for (i = 0; i < local->hw.wiphy->n_addresses; i++) { 1988 bool used = false; 1989 1990 list_for_each_entry(sdata, &local->interfaces, list) { 1991 if (ether_addr_equal(local->hw.wiphy->addresses[i].addr, 1992 sdata->vif.addr)) { 1993 used = true; 1994 break; 1995 } 1996 } 1997 1998 if (!used) { 1999 memcpy(perm_addr, 2000 local->hw.wiphy->addresses[i].addr, 2001 ETH_ALEN); 2002 break; 2003 } 2004 } 2005 2006 /* try mask if available */ 2007 if (is_zero_ether_addr(local->hw.wiphy->addr_mask)) 2008 break; 2009 2010 m = local->hw.wiphy->addr_mask; 2011 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | 2012 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | 2013 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); 2014 2015 if (__ffs64(mask) + hweight64(mask) != fls64(mask)) { 2016 /* not a contiguous mask ... not handled now! */ 2017 pr_info("not contiguous\n"); 2018 break; 2019 } 2020 2021 /* 2022 * Pick address of existing interface in case user changed 2023 * MAC address manually, default to perm_addr. 2024 */ 2025 m = local->hw.wiphy->perm_addr; 2026 list_for_each_entry(sdata, &local->interfaces, list) { 2027 if (sdata->vif.type == NL80211_IFTYPE_MONITOR) 2028 continue; 2029 m = sdata->vif.addr; 2030 break; 2031 } 2032 start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) | 2033 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) | 2034 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8); 2035 2036 inc = 1ULL<<__ffs64(mask); 2037 val = (start & mask); 2038 addr = (start & ~mask) | (val & mask); 2039 do { 2040 bool used = false; 2041 2042 tmp_addr[5] = addr >> 0*8; 2043 tmp_addr[4] = addr >> 1*8; 2044 tmp_addr[3] = addr >> 2*8; 2045 tmp_addr[2] = addr >> 3*8; 2046 tmp_addr[1] = addr >> 4*8; 2047 tmp_addr[0] = addr >> 5*8; 2048 2049 val += inc; 2050 2051 list_for_each_entry(sdata, &local->interfaces, list) { 2052 if (ether_addr_equal(tmp_addr, sdata->vif.addr)) { 2053 used = true; 2054 break; 2055 } 2056 } 2057 2058 if (!used) { 2059 memcpy(perm_addr, tmp_addr, ETH_ALEN); 2060 break; 2061 } 2062 addr = (start & ~mask) | (val & mask); 2063 } while (addr != start); 2064 2065 break; 2066 } 2067 } 2068 2069 int ieee80211_if_add(struct ieee80211_local *local, const char *name, 2070 unsigned char name_assign_type, 2071 struct wireless_dev **new_wdev, enum nl80211_iftype type, 2072 struct vif_params *params) 2073 { 2074 struct net_device *ndev = NULL; 2075 struct ieee80211_sub_if_data *sdata = NULL; 2076 struct txq_info *txqi; 2077 int ret, i; 2078 2079 ASSERT_RTNL(); 2080 lockdep_assert_wiphy(local->hw.wiphy); 2081 2082 if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) { 2083 struct wireless_dev *wdev; 2084 2085 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, 2086 GFP_KERNEL); 2087 if (!sdata) 2088 return -ENOMEM; 2089 wdev = &sdata->wdev; 2090 2091 sdata->dev = NULL; 2092 strscpy(sdata->name, name, IFNAMSIZ); 2093 ieee80211_assign_perm_addr(local, wdev->address, type); 2094 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN); 2095 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr); 2096 } else { 2097 int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size, 2098 sizeof(void *)); 2099 int txq_size = 0; 2100 2101 if (type != NL80211_IFTYPE_AP_VLAN && 2102 (type != NL80211_IFTYPE_MONITOR || 2103 (params->flags & MONITOR_FLAG_ACTIVE))) 2104 txq_size += sizeof(struct txq_info) + 2105 local->hw.txq_data_size; 2106 2107 ndev = alloc_netdev_mqs(size + txq_size, 2108 name, name_assign_type, 2109 ieee80211_if_setup, 1, 1); 2110 if (!ndev) 2111 return -ENOMEM; 2112 2113 dev_net_set(ndev, wiphy_net(local->hw.wiphy)); 2114 2115 ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 2116 2117 ndev->needed_headroom = local->tx_headroom + 2118 4*6 /* four MAC addresses */ 2119 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */ 2120 + 6 /* mesh */ 2121 + 8 /* rfc1042/bridge tunnel */ 2122 - ETH_HLEN /* ethernet hard_header_len */ 2123 + IEEE80211_ENCRYPT_HEADROOM; 2124 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM; 2125 2126 ret = dev_alloc_name(ndev, ndev->name); 2127 if (ret < 0) { 2128 free_netdev(ndev); 2129 return ret; 2130 } 2131 2132 ieee80211_assign_perm_addr(local, ndev->perm_addr, type); 2133 if (is_valid_ether_addr(params->macaddr)) 2134 eth_hw_addr_set(ndev, params->macaddr); 2135 else 2136 eth_hw_addr_set(ndev, ndev->perm_addr); 2137 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy)); 2138 2139 /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */ 2140 sdata = netdev_priv(ndev); 2141 ndev->ieee80211_ptr = &sdata->wdev; 2142 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN); 2143 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr); 2144 memcpy(sdata->name, ndev->name, IFNAMSIZ); 2145 2146 if (txq_size) { 2147 txqi = netdev_priv(ndev) + size; 2148 ieee80211_txq_init(sdata, NULL, txqi, 0); 2149 } 2150 2151 sdata->dev = ndev; 2152 } 2153 2154 /* initialise type-independent data */ 2155 sdata->wdev.wiphy = local->hw.wiphy; 2156 2157 ieee80211_sdata_init(local, sdata); 2158 2159 ieee80211_init_frag_cache(&sdata->frags); 2160 2161 INIT_LIST_HEAD(&sdata->key_list); 2162 2163 wiphy_delayed_work_init(&sdata->dec_tailroom_needed_wk, 2164 ieee80211_delayed_tailroom_dec); 2165 2166 for (i = 0; i < NUM_NL80211_BANDS; i++) { 2167 struct ieee80211_supported_band *sband; 2168 sband = local->hw.wiphy->bands[i]; 2169 sdata->rc_rateidx_mask[i] = 2170 sband ? (1 << sband->n_bitrates) - 1 : 0; 2171 if (sband) { 2172 __le16 cap; 2173 u16 *vht_rate_mask; 2174 2175 memcpy(sdata->rc_rateidx_mcs_mask[i], 2176 sband->ht_cap.mcs.rx_mask, 2177 sizeof(sdata->rc_rateidx_mcs_mask[i])); 2178 2179 cap = sband->vht_cap.vht_mcs.rx_mcs_map; 2180 vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i]; 2181 ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask); 2182 } else { 2183 memset(sdata->rc_rateidx_mcs_mask[i], 0, 2184 sizeof(sdata->rc_rateidx_mcs_mask[i])); 2185 memset(sdata->rc_rateidx_vht_mcs_mask[i], 0, 2186 sizeof(sdata->rc_rateidx_vht_mcs_mask[i])); 2187 } 2188 } 2189 2190 ieee80211_set_default_queues(sdata); 2191 2192 /* setup type-dependent data */ 2193 ieee80211_setup_sdata(sdata, type); 2194 2195 if (ndev) { 2196 ndev->ieee80211_ptr->use_4addr = params->use_4addr; 2197 if (type == NL80211_IFTYPE_STATION) 2198 sdata->u.mgd.use_4addr = params->use_4addr; 2199 2200 ndev->features |= local->hw.netdev_features; 2201 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 2202 ndev->hw_features |= ndev->features & 2203 MAC80211_SUPPORTED_FEATURES_TX; 2204 sdata->vif.netdev_features = local->hw.netdev_features; 2205 2206 netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops); 2207 2208 /* MTU range is normally 256 - 2304, where the upper limit is 2209 * the maximum MSDU size. Monitor interfaces send and receive 2210 * MPDU and A-MSDU frames which may be much larger so we do 2211 * not impose an upper limit in that case. 2212 */ 2213 ndev->min_mtu = 256; 2214 if (type == NL80211_IFTYPE_MONITOR) 2215 ndev->max_mtu = 0; 2216 else 2217 ndev->max_mtu = local->hw.max_mtu; 2218 2219 ret = cfg80211_register_netdevice(ndev); 2220 if (ret) { 2221 free_netdev(ndev); 2222 return ret; 2223 } 2224 } 2225 2226 mutex_lock(&local->iflist_mtx); 2227 list_add_tail_rcu(&sdata->list, &local->interfaces); 2228 mutex_unlock(&local->iflist_mtx); 2229 2230 if (new_wdev) 2231 *new_wdev = &sdata->wdev; 2232 2233 return 0; 2234 } 2235 2236 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata) 2237 { 2238 ASSERT_RTNL(); 2239 lockdep_assert_wiphy(sdata->local->hw.wiphy); 2240 2241 mutex_lock(&sdata->local->iflist_mtx); 2242 list_del_rcu(&sdata->list); 2243 mutex_unlock(&sdata->local->iflist_mtx); 2244 2245 if (sdata->vif.txq) 2246 ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq)); 2247 2248 synchronize_rcu(); 2249 2250 cfg80211_unregister_wdev(&sdata->wdev); 2251 2252 if (!sdata->dev) { 2253 ieee80211_teardown_sdata(sdata); 2254 kfree(sdata); 2255 } 2256 } 2257 2258 void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata) 2259 { 2260 if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state))) 2261 return; 2262 ieee80211_do_stop(sdata, true); 2263 } 2264 2265 void ieee80211_remove_interfaces(struct ieee80211_local *local) 2266 { 2267 struct ieee80211_sub_if_data *sdata, *tmp; 2268 LIST_HEAD(unreg_list); 2269 2270 ASSERT_RTNL(); 2271 2272 /* Before destroying the interfaces, make sure they're all stopped so 2273 * that the hardware is stopped. Otherwise, the driver might still be 2274 * iterating the interfaces during the shutdown, e.g. from a worker 2275 * or from RX processing or similar, and if it does so (using atomic 2276 * iteration) while we're manipulating the list, the iteration will 2277 * crash. 2278 * 2279 * After this, the hardware should be stopped and the driver should 2280 * have stopped all of its activities, so that we can do RCU-unaware 2281 * manipulations of the interface list below. 2282 */ 2283 cfg80211_shutdown_all_interfaces(local->hw.wiphy); 2284 2285 wiphy_lock(local->hw.wiphy); 2286 2287 WARN(local->open_count, "%s: open count remains %d\n", 2288 wiphy_name(local->hw.wiphy), local->open_count); 2289 2290 mutex_lock(&local->iflist_mtx); 2291 list_splice_init(&local->interfaces, &unreg_list); 2292 mutex_unlock(&local->iflist_mtx); 2293 2294 list_for_each_entry_safe(sdata, tmp, &unreg_list, list) { 2295 bool netdev = sdata->dev; 2296 2297 /* 2298 * Remove IP addresses explicitly, since the notifier will 2299 * skip the callbacks if wdev->registered is false, since 2300 * we can't acquire the wiphy_lock() again there if already 2301 * inside this locked section. 2302 */ 2303 sdata->vif.cfg.arp_addr_cnt = 0; 2304 if (sdata->vif.type == NL80211_IFTYPE_STATION && 2305 sdata->u.mgd.associated) 2306 ieee80211_vif_cfg_change_notify(sdata, 2307 BSS_CHANGED_ARP_FILTER); 2308 2309 list_del(&sdata->list); 2310 cfg80211_unregister_wdev(&sdata->wdev); 2311 2312 if (!netdev) 2313 kfree(sdata); 2314 } 2315 wiphy_unlock(local->hw.wiphy); 2316 } 2317 2318 static int netdev_notify(struct notifier_block *nb, 2319 unsigned long state, void *ptr) 2320 { 2321 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2322 struct ieee80211_sub_if_data *sdata; 2323 2324 if (state != NETDEV_CHANGENAME) 2325 return NOTIFY_DONE; 2326 2327 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy) 2328 return NOTIFY_DONE; 2329 2330 if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid) 2331 return NOTIFY_DONE; 2332 2333 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 2334 memcpy(sdata->name, dev->name, IFNAMSIZ); 2335 ieee80211_debugfs_rename_netdev(sdata); 2336 2337 return NOTIFY_OK; 2338 } 2339 2340 static struct notifier_block mac80211_netdev_notifier = { 2341 .notifier_call = netdev_notify, 2342 }; 2343 2344 int ieee80211_iface_init(void) 2345 { 2346 return register_netdevice_notifier(&mac80211_netdev_notifier); 2347 } 2348 2349 void ieee80211_iface_exit(void) 2350 { 2351 unregister_netdevice_notifier(&mac80211_netdev_notifier); 2352 } 2353 2354 void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata) 2355 { 2356 if (sdata->vif.type == NL80211_IFTYPE_AP) 2357 atomic_inc(&sdata->u.ap.num_mcast_sta); 2358 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 2359 atomic_inc(&sdata->u.vlan.num_mcast_sta); 2360 } 2361 2362 void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata) 2363 { 2364 if (sdata->vif.type == NL80211_IFTYPE_AP) 2365 atomic_dec(&sdata->u.ap.num_mcast_sta); 2366 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 2367 atomic_dec(&sdata->u.vlan.num_mcast_sta); 2368 } 2369 2370 void ieee80211_vif_block_queues_csa(struct ieee80211_sub_if_data *sdata) 2371 { 2372 struct ieee80211_local *local = sdata->local; 2373 2374 if (ieee80211_hw_check(&local->hw, HANDLES_QUIET_CSA)) 2375 return; 2376 2377 ieee80211_stop_vif_queues(local, sdata, 2378 IEEE80211_QUEUE_STOP_REASON_CSA); 2379 sdata->csa_blocked_queues = true; 2380 } 2381 2382 void ieee80211_vif_unblock_queues_csa(struct ieee80211_sub_if_data *sdata) 2383 { 2384 struct ieee80211_local *local = sdata->local; 2385 2386 if (sdata->csa_blocked_queues) { 2387 ieee80211_wake_vif_queues(local, sdata, 2388 IEEE80211_QUEUE_STOP_REASON_CSA); 2389 sdata->csa_blocked_queues = false; 2390 } 2391 } 2392