1 // SPDX-License-Identifier: GPL-2.0-only
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-2022 Intel Corporation
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"
23 #include "debugfs_netdev.h"
26 #include "driver-ops.h"
31 * DOC: Interface list locking
33 * The interface list in each struct ieee80211_local is protected
36 * (1) modifications may only be done under the RTNL
37 * (2) modifications and readers are protected against each other by
39 * (3) modifications are done in an RCU manner so atomic readers
40 * can traverse the list in RCU-safe blocks.
42 * As a consequence, reads (traversals) of the list can be protected
43 * by either the RTNL, the iflist_mtx or RCU.
46 static void ieee80211_iface_work(struct work_struct *work);
48 bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
50 struct ieee80211_chanctx_conf *chanctx_conf;
54 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
60 power = ieee80211_chandef_max_power(&chanctx_conf->def);
63 if (sdata->deflink.user_power_level != IEEE80211_UNSET_POWER_LEVEL)
64 power = min(power, sdata->deflink.user_power_level);
66 if (sdata->deflink.ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
67 power = min(power, sdata->deflink.ap_power_level);
69 if (power != sdata->vif.bss_conf.txpower) {
70 sdata->vif.bss_conf.txpower = power;
71 ieee80211_hw_config(sdata->local, 0);
78 void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata,
81 if (__ieee80211_recalc_txpower(sdata) ||
82 (update_bss && ieee80211_sdata_running(sdata)))
83 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
87 static u32 __ieee80211_idle_off(struct ieee80211_local *local)
89 if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
92 local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
93 return IEEE80211_CONF_CHANGE_IDLE;
96 static u32 __ieee80211_idle_on(struct ieee80211_local *local)
98 if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
101 ieee80211_flush_queues(local, NULL, false);
103 local->hw.conf.flags |= IEEE80211_CONF_IDLE;
104 return IEEE80211_CONF_CHANGE_IDLE;
107 static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
110 bool working, scanning, active;
111 unsigned int led_trig_start = 0, led_trig_stop = 0;
113 lockdep_assert_held(&local->mtx);
115 active = force_active ||
116 !list_empty(&local->chanctx_list) ||
119 working = !local->ops->remain_on_channel &&
120 !list_empty(&local->roc_list);
122 scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
123 test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
125 if (working || scanning)
126 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
128 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
131 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
133 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
135 ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
137 if (working || scanning || active)
138 return __ieee80211_idle_off(local);
139 return __ieee80211_idle_on(local);
142 u32 ieee80211_idle_off(struct ieee80211_local *local)
144 return __ieee80211_recalc_idle(local, true);
147 void ieee80211_recalc_idle(struct ieee80211_local *local)
149 u32 change = __ieee80211_recalc_idle(local, false);
151 ieee80211_hw_config(local, change);
154 static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr,
157 struct ieee80211_local *local = sdata->local;
158 struct ieee80211_sub_if_data *iter;
163 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
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);
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);
179 mutex_lock(&local->iflist_mtx);
180 list_for_each_entry(iter, &local->interfaces, list) {
184 if (iter->vif.type == NL80211_IFTYPE_MONITOR &&
185 !(iter->u.mntr.flags & MONITOR_FLAG_ACTIVE))
189 tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
190 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
191 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
193 if ((new & ~mask) != (tmp & ~mask)) {
198 mutex_unlock(&local->iflist_mtx);
203 static int ieee80211_change_mac(struct net_device *dev, void *addr)
205 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
206 struct sockaddr *sa = addr;
207 bool check_dup = true;
210 if (ieee80211_sdata_running(sdata))
213 if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
214 !(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
217 ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup);
221 ret = eth_mac_addr(dev, sa);
224 memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
225 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
231 static inline int identical_mac_addr_allowed(int type1, int type2)
233 return type1 == NL80211_IFTYPE_MONITOR ||
234 type2 == NL80211_IFTYPE_MONITOR ||
235 type1 == NL80211_IFTYPE_P2P_DEVICE ||
236 type2 == NL80211_IFTYPE_P2P_DEVICE ||
237 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
238 (type1 == NL80211_IFTYPE_AP_VLAN &&
239 (type2 == NL80211_IFTYPE_AP ||
240 type2 == NL80211_IFTYPE_AP_VLAN));
243 static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
244 enum nl80211_iftype iftype)
246 struct ieee80211_local *local = sdata->local;
247 struct ieee80211_sub_if_data *nsdata;
252 /* we hold the RTNL here so can safely walk the list */
253 list_for_each_entry(nsdata, &local->interfaces, list) {
254 if (nsdata != sdata && ieee80211_sdata_running(nsdata)) {
256 * Only OCB and monitor mode may coexist
258 if ((sdata->vif.type == NL80211_IFTYPE_OCB &&
259 nsdata->vif.type != NL80211_IFTYPE_MONITOR) ||
260 (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
261 nsdata->vif.type == NL80211_IFTYPE_OCB))
265 * Allow only a single IBSS interface to be up at any
266 * time. This is restricted because beacon distribution
267 * cannot work properly if both are in the same IBSS.
269 * To remove this restriction we'd have to disallow them
270 * from setting the same SSID on different IBSS interfaces
271 * belonging to the same hardware. Then, however, we're
272 * faced with having to adopt two different TSF timers...
274 if (iftype == NL80211_IFTYPE_ADHOC &&
275 nsdata->vif.type == NL80211_IFTYPE_ADHOC)
278 * will not add another interface while any channel
281 if (nsdata->vif.bss_conf.csa_active)
285 * The remaining checks are only performed for interfaces
286 * with the same MAC address.
288 if (!ether_addr_equal(sdata->vif.addr,
293 * check whether it may have the same address
295 if (!identical_mac_addr_allowed(iftype,
300 * can only add VLANs to enabled APs
302 if (iftype == NL80211_IFTYPE_AP_VLAN &&
303 nsdata->vif.type == NL80211_IFTYPE_AP)
304 sdata->bss = &nsdata->u.ap;
308 mutex_lock(&local->chanctx_mtx);
309 ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
310 mutex_unlock(&local->chanctx_mtx);
314 static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata,
315 enum nl80211_iftype iftype)
317 int n_queues = sdata->local->hw.queues;
320 if (iftype == NL80211_IFTYPE_NAN)
323 if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
324 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
325 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
326 IEEE80211_INVAL_HW_QUEUE))
328 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
334 if ((iftype != NL80211_IFTYPE_AP &&
335 iftype != NL80211_IFTYPE_P2P_GO &&
336 iftype != NL80211_IFTYPE_MESH_POINT) ||
337 !ieee80211_hw_check(&sdata->local->hw, QUEUE_CONTROL)) {
338 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
342 if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
345 if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
351 static int ieee80211_open(struct net_device *dev)
353 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
356 /* fail early if user set an invalid address */
357 if (!is_valid_ether_addr(dev->dev_addr))
358 return -EADDRNOTAVAIL;
360 err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
364 wiphy_lock(sdata->local->hw.wiphy);
365 err = ieee80211_do_open(&sdata->wdev, true);
366 wiphy_unlock(sdata->local->hw.wiphy);
371 static void ieee80211_link_setup(struct ieee80211_link_data *link)
373 if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
374 ieee80211_mgd_setup_link(link);
377 static void ieee80211_link_init(struct ieee80211_sub_if_data *sdata,
379 struct ieee80211_link_data *link,
380 struct ieee80211_bss_conf *link_conf)
382 bool deflink = link_id < 0;
387 rcu_assign_pointer(sdata->vif.link_conf[link_id], link_conf);
388 rcu_assign_pointer(sdata->link[link_id], link);
391 link->link_id = link_id;
392 link->conf = link_conf;
393 link_conf->link_id = link_id;
395 INIT_WORK(&link->csa_finalize_work,
396 ieee80211_csa_finalize_work);
397 INIT_WORK(&link->color_change_finalize_work,
398 ieee80211_color_change_finalize_work);
399 INIT_LIST_HEAD(&link->assigned_chanctx_list);
400 INIT_LIST_HEAD(&link->reserved_chanctx_list);
401 INIT_DELAYED_WORK(&link->dfs_cac_timer_work,
402 ieee80211_dfs_cac_timer_work);
405 switch (sdata->vif.type) {
406 case NL80211_IFTYPE_AP:
407 ether_addr_copy(link_conf->addr,
408 sdata->wdev.links[link_id].addr);
409 WARN_ON(!(sdata->wdev.valid_links & BIT(link_id)));
411 case NL80211_IFTYPE_STATION:
419 static void ieee80211_link_stop(struct ieee80211_link_data *link)
421 if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
422 ieee80211_mgd_stop_link(link);
424 ieee80211_link_release_channel(link);
427 struct link_container {
428 struct ieee80211_link_data data;
429 struct ieee80211_bss_conf conf;
432 static void ieee80211_free_links(struct ieee80211_sub_if_data *sdata,
433 struct link_container **links)
435 unsigned int link_id;
439 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
442 ieee80211_link_stop(&links[link_id]->data);
443 kfree(links[link_id]);
447 static int ieee80211_check_dup_link_addrs(struct ieee80211_sub_if_data *sdata)
451 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
452 struct ieee80211_link_data *link1;
454 link1 = sdata_dereference(sdata->link[i], sdata);
457 for (j = i + 1; j < IEEE80211_MLD_MAX_NUM_LINKS; j++) {
458 struct ieee80211_link_data *link2;
460 link2 = sdata_dereference(sdata->link[j], sdata);
464 if (ether_addr_equal(link1->conf->addr,
473 static int ieee80211_vif_update_links(struct ieee80211_sub_if_data *sdata,
474 struct link_container **to_free,
477 u16 old_links = sdata->vif.valid_links;
478 unsigned long add = new_links & ~old_links;
479 unsigned long rem = old_links & ~new_links;
480 unsigned int link_id;
482 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *link;
483 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS];
484 struct ieee80211_link_data *old_data[IEEE80211_MLD_MAX_NUM_LINKS];
485 bool use_deflink = old_links == 0; /* set for error case */
487 sdata_assert_lock(sdata);
489 memset(to_free, 0, sizeof(links));
491 if (old_links == new_links)
494 /* if there were no old links, need to clear the pointers to deflink */
498 /* allocate new link structures first */
499 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
500 link = kzalloc(sizeof(*link), GFP_KERNEL);
505 links[link_id] = link;
508 /* keep track of the old pointers for the driver */
509 BUILD_BUG_ON(sizeof(old) != sizeof(sdata->vif.link_conf));
510 memcpy(old, sdata->vif.link_conf, sizeof(old));
511 /* and for us in error cases */
512 BUILD_BUG_ON(sizeof(old_data) != sizeof(sdata->link));
513 memcpy(old_data, sdata->link, sizeof(old_data));
515 /* grab old links to free later */
516 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) {
517 if (rcu_access_pointer(sdata->link[link_id]) != &sdata->deflink) {
519 * we must have allocated the data through this path so
520 * we know we can free both at the same time
522 to_free[link_id] = container_of(rcu_access_pointer(sdata->link[link_id]),
523 typeof(*links[link_id]),
527 RCU_INIT_POINTER(sdata->link[link_id], NULL);
528 RCU_INIT_POINTER(sdata->vif.link_conf[link_id], NULL);
531 /* link them into data structures */
532 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) {
533 WARN_ON(!use_deflink &&
534 rcu_access_pointer(sdata->link[link_id]) == &sdata->deflink);
536 link = links[link_id];
537 ieee80211_link_init(sdata, link_id, &link->data, &link->conf);
538 ieee80211_link_setup(&link->data);
542 ieee80211_link_init(sdata, -1, &sdata->deflink,
543 &sdata->vif.bss_conf);
545 sdata->vif.valid_links = new_links;
547 ret = ieee80211_check_dup_link_addrs(sdata);
549 /* tell the driver */
550 ret = drv_change_vif_links(sdata->local, sdata,
551 old_links, new_links,
557 memcpy(sdata->link, old_data, sizeof(old_data));
558 memcpy(sdata->vif.link_conf, old, sizeof(old));
559 sdata->vif.valid_links = old_links;
560 /* and free (only) the newly allocated links */
561 memset(to_free, 0, sizeof(links));
565 /* use deflink/bss_conf again if and only if there are no more links */
566 use_deflink = new_links == 0;
570 /* if we failed during allocation, only free all */
571 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
572 kfree(links[link_id]);
573 links[link_id] = NULL;
577 ieee80211_link_init(sdata, -1, &sdata->deflink,
578 &sdata->vif.bss_conf);
582 int ieee80211_vif_set_links(struct ieee80211_sub_if_data *sdata,
585 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
588 ret = ieee80211_vif_update_links(sdata, links, new_links);
589 ieee80211_free_links(sdata, links);
594 static void ieee80211_vif_clear_links(struct ieee80211_sub_if_data *sdata)
596 struct link_container *links[IEEE80211_MLD_MAX_NUM_LINKS];
599 * The locking here is different because when we free links
600 * in the station case we need to be able to cancel_work_sync()
601 * something that also takes the lock.
605 ieee80211_vif_update_links(sdata, links, 0);
608 ieee80211_free_links(sdata, links);
611 static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_down)
613 struct ieee80211_local *local = sdata->local;
615 struct sk_buff *skb, *tmp;
616 u32 hw_reconf_flags = 0;
619 struct cfg80211_chan_def chandef;
621 struct cfg80211_nan_func *func;
623 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
624 synchronize_rcu(); /* flush _ieee80211_wake_txqs() */
626 cancel_scan = rcu_access_pointer(local->scan_sdata) == sdata;
628 ieee80211_scan_cancel(local);
631 * Stop TX on this interface first.
634 netif_tx_stop_all_queues(sdata->dev);
636 ieee80211_roc_purge(local, sdata);
638 switch (sdata->vif.type) {
639 case NL80211_IFTYPE_STATION:
640 ieee80211_mgd_stop(sdata);
642 case NL80211_IFTYPE_ADHOC:
643 ieee80211_ibss_stop(sdata);
645 case NL80211_IFTYPE_MONITOR:
646 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
648 list_del_rcu(&sdata->u.mntr.list);
655 * Remove all stations associated with this interface.
657 * This must be done before calling ops->remove_interface()
658 * because otherwise we can later invoke ops->sta_notify()
659 * whenever the STAs are removed, and that invalidates driver
660 * assumptions about always getting a vif pointer that is valid
661 * (because if we remove a STA after ops->remove_interface()
662 * the driver will have removed the vif info already!)
664 * For AP_VLANs stations may exist since there's nothing else that
665 * would have removed them, but in other modes there shouldn't
668 flushed = sta_info_flush(sdata);
669 WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP_VLAN && flushed > 0);
671 /* don't count this interface for allmulti while it is down */
672 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
673 atomic_dec(&local->iff_allmultis);
675 if (sdata->vif.type == NL80211_IFTYPE_AP) {
677 local->fif_probe_req--;
678 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
679 local->fif_probe_req--;
683 netif_addr_lock_bh(sdata->dev);
684 spin_lock_bh(&local->filter_lock);
685 __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
686 sdata->dev->addr_len);
687 spin_unlock_bh(&local->filter_lock);
688 netif_addr_unlock_bh(sdata->dev);
691 del_timer_sync(&local->dynamic_ps_timer);
692 cancel_work_sync(&local->dynamic_ps_enable_work);
694 cancel_work_sync(&sdata->recalc_smps);
697 WARN(sdata->vif.valid_links,
698 "destroying interface with valid links 0x%04x\n",
699 sdata->vif.valid_links);
701 mutex_lock(&local->mtx);
702 sdata->vif.bss_conf.csa_active = false;
703 if (sdata->vif.type == NL80211_IFTYPE_STATION)
704 sdata->deflink.u.mgd.csa_waiting_bcn = false;
705 if (sdata->deflink.csa_block_tx) {
706 ieee80211_wake_vif_queues(local, sdata,
707 IEEE80211_QUEUE_STOP_REASON_CSA);
708 sdata->deflink.csa_block_tx = false;
710 mutex_unlock(&local->mtx);
713 cancel_work_sync(&sdata->deflink.csa_finalize_work);
714 cancel_work_sync(&sdata->deflink.color_change_finalize_work);
716 cancel_delayed_work_sync(&sdata->deflink.dfs_cac_timer_work);
718 if (sdata->wdev.cac_started) {
719 chandef = sdata->vif.bss_conf.chandef;
720 WARN_ON(local->suspended);
721 mutex_lock(&local->mtx);
722 ieee80211_link_release_channel(&sdata->deflink);
723 mutex_unlock(&local->mtx);
724 cfg80211_cac_event(sdata->dev, &chandef,
725 NL80211_RADAR_CAC_ABORTED,
729 if (sdata->vif.type == NL80211_IFTYPE_AP) {
730 WARN_ON(!list_empty(&sdata->u.ap.vlans));
731 } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
732 /* remove all packets in parent bc_buf pointing to this dev */
733 ps = &sdata->bss->ps;
735 spin_lock_irqsave(&ps->bc_buf.lock, flags);
736 skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
737 if (skb->dev == sdata->dev) {
738 __skb_unlink(skb, &ps->bc_buf);
739 local->total_ps_buffered--;
740 ieee80211_free_txskb(&local->hw, skb);
743 spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
749 switch (sdata->vif.type) {
750 case NL80211_IFTYPE_AP_VLAN:
751 mutex_lock(&local->mtx);
752 list_del(&sdata->u.vlan.list);
753 mutex_unlock(&local->mtx);
754 RCU_INIT_POINTER(sdata->vif.bss_conf.chanctx_conf, NULL);
755 /* see comment in the default case below */
756 ieee80211_free_keys(sdata, true);
757 /* no need to tell driver */
759 case NL80211_IFTYPE_MONITOR:
760 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
761 local->cooked_mntrs--;
766 if (local->monitors == 0) {
767 local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
768 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
771 ieee80211_adjust_monitor_flags(sdata, -1);
773 case NL80211_IFTYPE_NAN:
774 /* clean all the functions */
775 spin_lock_bh(&sdata->u.nan.func_lock);
777 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, i) {
778 idr_remove(&sdata->u.nan.function_inst_ids, i);
779 cfg80211_free_nan_func(func);
781 idr_destroy(&sdata->u.nan.function_inst_ids);
783 spin_unlock_bh(&sdata->u.nan.func_lock);
785 case NL80211_IFTYPE_P2P_DEVICE:
786 /* relies on synchronize_rcu() below */
787 RCU_INIT_POINTER(local->p2p_sdata, NULL);
790 cancel_work_sync(&sdata->work);
792 * When we get here, the interface is marked down.
793 * Free the remaining keys, if there are any
794 * (which can happen in AP mode if userspace sets
795 * keys before the interface is operating)
797 * Force the key freeing to always synchronize_net()
798 * to wait for the RX path in case it is using this
799 * interface enqueuing frames at this very time on
802 ieee80211_free_keys(sdata, true);
803 skb_queue_purge(&sdata->skb_queue);
804 skb_queue_purge(&sdata->status_queue);
807 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
808 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
809 skb_queue_walk_safe(&local->pending[i], skb, tmp) {
810 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
811 if (info->control.vif == &sdata->vif) {
812 __skb_unlink(skb, &local->pending[i]);
813 ieee80211_free_txskb(&local->hw, skb);
817 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
819 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
820 ieee80211_txq_remove_vlan(local, sdata);
824 if (local->open_count == 0)
825 ieee80211_clear_tx_pending(local);
827 sdata->vif.bss_conf.beacon_int = 0;
830 * If the interface goes down while suspended, presumably because
831 * the device was unplugged and that happens before our resume,
832 * then the driver is already unconfigured and the remainder of
833 * this function isn't needed.
834 * XXX: what about WoWLAN? If the device has software state, e.g.
835 * memory allocated, it might expect teardown commands from
838 if (local->suspended) {
839 WARN_ON(local->wowlan);
840 WARN_ON(rcu_access_pointer(local->monitor_sdata));
844 switch (sdata->vif.type) {
845 case NL80211_IFTYPE_AP_VLAN:
847 case NL80211_IFTYPE_MONITOR:
848 if (local->monitors == 0)
849 ieee80211_del_virtual_monitor(local);
851 mutex_lock(&local->mtx);
852 ieee80211_recalc_idle(local);
853 mutex_unlock(&local->mtx);
855 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
861 drv_remove_interface(local, sdata);
864 ieee80211_recalc_ps(local);
867 flush_delayed_work(&local->scan_work);
869 if (local->open_count == 0) {
870 ieee80211_stop_device(local);
872 /* no reconfiguring after stop! */
876 /* do after stop to avoid reconfiguring when we stop anyway */
877 ieee80211_configure_filter(local);
878 ieee80211_hw_config(local, hw_reconf_flags);
880 if (local->monitors == local->open_count)
881 ieee80211_add_virtual_monitor(local);
884 static void ieee80211_stop_mbssid(struct ieee80211_sub_if_data *sdata)
886 struct ieee80211_sub_if_data *tx_sdata, *non_tx_sdata, *tmp_sdata;
887 struct ieee80211_vif *tx_vif = sdata->vif.mbssid_tx_vif;
892 tx_sdata = vif_to_sdata(tx_vif);
893 sdata->vif.mbssid_tx_vif = NULL;
895 list_for_each_entry_safe(non_tx_sdata, tmp_sdata,
896 &tx_sdata->local->interfaces, list) {
897 if (non_tx_sdata != sdata && non_tx_sdata != tx_sdata &&
898 non_tx_sdata->vif.mbssid_tx_vif == tx_vif &&
899 ieee80211_sdata_running(non_tx_sdata)) {
900 non_tx_sdata->vif.mbssid_tx_vif = NULL;
901 dev_close(non_tx_sdata->wdev.netdev);
905 if (sdata != tx_sdata && ieee80211_sdata_running(tx_sdata)) {
906 tx_sdata->vif.mbssid_tx_vif = NULL;
907 dev_close(tx_sdata->wdev.netdev);
911 static int ieee80211_stop(struct net_device *dev)
913 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
915 /* close dependent VLAN and MBSSID interfaces before locking wiphy */
916 if (sdata->vif.type == NL80211_IFTYPE_AP) {
917 struct ieee80211_sub_if_data *vlan, *tmpsdata;
919 list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
921 dev_close(vlan->dev);
923 ieee80211_stop_mbssid(sdata);
926 wiphy_lock(sdata->local->hw.wiphy);
927 ieee80211_do_stop(sdata, true);
928 wiphy_unlock(sdata->local->hw.wiphy);
933 static void ieee80211_set_multicast_list(struct net_device *dev)
935 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
936 struct ieee80211_local *local = sdata->local;
937 int allmulti, sdata_allmulti;
939 allmulti = !!(dev->flags & IFF_ALLMULTI);
940 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
942 if (allmulti != sdata_allmulti) {
943 if (dev->flags & IFF_ALLMULTI)
944 atomic_inc(&local->iff_allmultis);
946 atomic_dec(&local->iff_allmultis);
947 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
950 spin_lock_bh(&local->filter_lock);
951 __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
952 spin_unlock_bh(&local->filter_lock);
953 ieee80211_queue_work(&local->hw, &local->reconfig_filter);
957 * Called when the netdev is removed or, by the code below, before
958 * the interface type changes.
960 static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
962 /* free extra data */
963 ieee80211_free_keys(sdata, false);
965 ieee80211_debugfs_remove_netdev(sdata);
967 ieee80211_destroy_frag_cache(&sdata->frags);
969 if (ieee80211_vif_is_mesh(&sdata->vif))
970 ieee80211_mesh_teardown_sdata(sdata);
972 ieee80211_vif_clear_links(sdata);
973 ieee80211_link_stop(&sdata->deflink);
976 static void ieee80211_uninit(struct net_device *dev)
978 ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
981 static u16 ieee80211_netdev_select_queue(struct net_device *dev,
983 struct net_device *sb_dev)
985 return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
989 ieee80211_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
991 dev_fetch_sw_netstats(stats, dev->tstats);
994 static const struct net_device_ops ieee80211_dataif_ops = {
995 .ndo_open = ieee80211_open,
996 .ndo_stop = ieee80211_stop,
997 .ndo_uninit = ieee80211_uninit,
998 .ndo_start_xmit = ieee80211_subif_start_xmit,
999 .ndo_set_rx_mode = ieee80211_set_multicast_list,
1000 .ndo_set_mac_address = ieee80211_change_mac,
1001 .ndo_select_queue = ieee80211_netdev_select_queue,
1002 .ndo_get_stats64 = ieee80211_get_stats64,
1005 static u16 ieee80211_monitor_select_queue(struct net_device *dev,
1006 struct sk_buff *skb,
1007 struct net_device *sb_dev)
1009 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1010 struct ieee80211_local *local = sdata->local;
1011 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1012 struct ieee80211_hdr *hdr;
1015 if (local->hw.queues < IEEE80211_NUM_ACS)
1018 /* reset flags and info before parsing radiotap header */
1019 memset(info, 0, sizeof(*info));
1021 if (!ieee80211_parse_tx_radiotap(skb, dev))
1022 return 0; /* doesn't matter, frame will be dropped */
1024 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1025 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1026 if (skb->len < len_rthdr + 2 ||
1027 skb->len < len_rthdr + ieee80211_hdrlen(hdr->frame_control))
1028 return 0; /* doesn't matter, frame will be dropped */
1030 return ieee80211_select_queue_80211(sdata, skb, hdr);
1033 static const struct net_device_ops ieee80211_monitorif_ops = {
1034 .ndo_open = ieee80211_open,
1035 .ndo_stop = ieee80211_stop,
1036 .ndo_uninit = ieee80211_uninit,
1037 .ndo_start_xmit = ieee80211_monitor_start_xmit,
1038 .ndo_set_rx_mode = ieee80211_set_multicast_list,
1039 .ndo_set_mac_address = ieee80211_change_mac,
1040 .ndo_select_queue = ieee80211_monitor_select_queue,
1041 .ndo_get_stats64 = ieee80211_get_stats64,
1044 static int ieee80211_netdev_fill_forward_path(struct net_device_path_ctx *ctx,
1045 struct net_device_path *path)
1047 struct ieee80211_sub_if_data *sdata;
1048 struct ieee80211_local *local;
1049 struct sta_info *sta;
1052 sdata = IEEE80211_DEV_TO_SUB_IF(ctx->dev);
1053 local = sdata->local;
1055 if (!local->ops->net_fill_forward_path)
1059 switch (sdata->vif.type) {
1060 case NL80211_IFTYPE_AP_VLAN:
1061 sta = rcu_dereference(sdata->u.vlan.sta);
1064 if (sdata->wdev.use_4addr)
1066 if (is_multicast_ether_addr(ctx->daddr))
1068 sta = sta_info_get_bss(sdata, ctx->daddr);
1070 case NL80211_IFTYPE_AP:
1071 if (is_multicast_ether_addr(ctx->daddr))
1073 sta = sta_info_get(sdata, ctx->daddr);
1075 case NL80211_IFTYPE_STATION:
1076 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1077 sta = sta_info_get(sdata, ctx->daddr);
1078 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1079 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
1086 sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
1095 ret = drv_net_fill_forward_path(local, sdata, &sta->sta, ctx, path);
1102 static const struct net_device_ops ieee80211_dataif_8023_ops = {
1103 .ndo_open = ieee80211_open,
1104 .ndo_stop = ieee80211_stop,
1105 .ndo_uninit = ieee80211_uninit,
1106 .ndo_start_xmit = ieee80211_subif_start_xmit_8023,
1107 .ndo_set_rx_mode = ieee80211_set_multicast_list,
1108 .ndo_set_mac_address = ieee80211_change_mac,
1109 .ndo_select_queue = ieee80211_netdev_select_queue,
1110 .ndo_get_stats64 = ieee80211_get_stats64,
1111 .ndo_fill_forward_path = ieee80211_netdev_fill_forward_path,
1114 static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)
1117 /* P2P GO and client are mapped to AP/STATION types */
1118 case NL80211_IFTYPE_AP:
1119 case NL80211_IFTYPE_STATION:
1126 static bool ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data *sdata)
1128 struct ieee80211_local *local = sdata->local;
1131 flags = sdata->vif.offload_flags;
1133 if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) &&
1134 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
1135 flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED;
1137 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) &&
1138 local->hw.wiphy->frag_threshold != (u32)-1)
1139 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1141 if (local->monitors)
1142 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1144 flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1147 if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) &&
1148 ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
1149 flags |= IEEE80211_OFFLOAD_DECAP_ENABLED;
1151 if (local->monitors &&
1152 !ieee80211_hw_check(&local->hw, SUPPORTS_CONC_MON_RX_DECAP))
1153 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1155 flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1158 if (sdata->vif.offload_flags == flags)
1161 sdata->vif.offload_flags = flags;
1162 ieee80211_check_fast_rx_iface(sdata);
1166 static void ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data *sdata)
1168 struct ieee80211_local *local = sdata->local;
1169 struct ieee80211_sub_if_data *bss = sdata;
1172 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1176 bss = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1179 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) ||
1180 !ieee80211_iftype_supports_hdr_offload(bss->vif.type))
1183 enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED;
1184 if (sdata->wdev.use_4addr &&
1185 !(bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_4ADDR))
1188 sdata->dev->netdev_ops = enabled ? &ieee80211_dataif_8023_ops :
1189 &ieee80211_dataif_ops;
1192 static void ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data *sdata)
1194 struct ieee80211_local *local = sdata->local;
1195 struct ieee80211_sub_if_data *vsdata;
1197 if (ieee80211_set_sdata_offload_flags(sdata)) {
1198 drv_update_vif_offload(local, sdata);
1199 ieee80211_set_vif_encap_ops(sdata);
1202 list_for_each_entry(vsdata, &local->interfaces, list) {
1203 if (vsdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
1204 vsdata->bss != &sdata->u.ap)
1207 ieee80211_set_vif_encap_ops(vsdata);
1211 void ieee80211_recalc_offload(struct ieee80211_local *local)
1213 struct ieee80211_sub_if_data *sdata;
1215 if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD))
1218 mutex_lock(&local->iflist_mtx);
1220 list_for_each_entry(sdata, &local->interfaces, list) {
1221 if (!ieee80211_sdata_running(sdata))
1224 ieee80211_recalc_sdata_offload(sdata);
1227 mutex_unlock(&local->iflist_mtx);
1230 void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
1233 struct ieee80211_local *local = sdata->local;
1234 u32 flags = sdata->u.mntr.flags;
1236 #define ADJUST(_f, _s) do { \
1237 if (flags & MONITOR_FLAG_##_f) \
1238 local->fif_##_s += offset; \
1241 ADJUST(FCSFAIL, fcsfail);
1242 ADJUST(PLCPFAIL, plcpfail);
1243 ADJUST(CONTROL, control);
1244 ADJUST(CONTROL, pspoll);
1245 ADJUST(OTHER_BSS, other_bss);
1250 static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
1252 struct ieee80211_local *local = sdata->local;
1255 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
1256 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1257 sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
1258 else if (local->hw.queues >= IEEE80211_NUM_ACS)
1259 sdata->vif.hw_queue[i] = i;
1261 sdata->vif.hw_queue[i] = 0;
1263 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
1266 static void ieee80211_sdata_init(struct ieee80211_local *local,
1267 struct ieee80211_sub_if_data *sdata)
1269 sdata->local = local;
1272 * Initialize the default link, so we can use link_id 0 for non-MLD,
1273 * and that continues to work for non-MLD-aware drivers that use just
1274 * vif.bss_conf instead of vif.link_conf.
1276 * Note that we never change this, so if link ID 0 isn't used in an
1277 * MLD connection, we get a separate allocation for it.
1279 ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf);
1282 int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
1284 struct ieee80211_sub_if_data *sdata;
1287 if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1291 lockdep_assert_wiphy(local->hw.wiphy);
1293 if (local->monitor_sdata)
1296 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
1301 sdata->vif.type = NL80211_IFTYPE_MONITOR;
1302 snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
1303 wiphy_name(local->hw.wiphy));
1304 sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
1306 ieee80211_sdata_init(local, sdata);
1308 ieee80211_set_default_queues(sdata);
1310 ret = drv_add_interface(local, sdata);
1312 /* ok .. stupid driver, it asked for this! */
1317 set_bit(SDATA_STATE_RUNNING, &sdata->state);
1319 ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
1325 mutex_lock(&local->iflist_mtx);
1326 rcu_assign_pointer(local->monitor_sdata, sdata);
1327 mutex_unlock(&local->iflist_mtx);
1329 mutex_lock(&local->mtx);
1330 ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chandef,
1331 IEEE80211_CHANCTX_EXCLUSIVE);
1332 mutex_unlock(&local->mtx);
1334 mutex_lock(&local->iflist_mtx);
1335 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1336 mutex_unlock(&local->iflist_mtx);
1338 drv_remove_interface(local, sdata);
1343 skb_queue_head_init(&sdata->skb_queue);
1344 skb_queue_head_init(&sdata->status_queue);
1345 INIT_WORK(&sdata->work, ieee80211_iface_work);
1350 void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
1352 struct ieee80211_sub_if_data *sdata;
1354 if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1358 lockdep_assert_wiphy(local->hw.wiphy);
1360 mutex_lock(&local->iflist_mtx);
1362 sdata = rcu_dereference_protected(local->monitor_sdata,
1363 lockdep_is_held(&local->iflist_mtx));
1365 mutex_unlock(&local->iflist_mtx);
1369 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1370 mutex_unlock(&local->iflist_mtx);
1374 mutex_lock(&local->mtx);
1375 ieee80211_link_release_channel(&sdata->deflink);
1376 mutex_unlock(&local->mtx);
1378 drv_remove_interface(local, sdata);
1384 * NOTE: Be very careful when changing this function, it must NOT return
1385 * an error on interface type changes that have been pre-checked, so most
1386 * checks should be in ieee80211_check_concurrent_iface.
1388 int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
1390 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1391 struct net_device *dev = wdev->netdev;
1392 struct ieee80211_local *local = sdata->local;
1395 u32 hw_reconf_flags = 0;
1397 switch (sdata->vif.type) {
1398 case NL80211_IFTYPE_AP_VLAN: {
1399 struct ieee80211_sub_if_data *master;
1404 mutex_lock(&local->mtx);
1405 list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
1406 mutex_unlock(&local->mtx);
1408 master = container_of(sdata->bss,
1409 struct ieee80211_sub_if_data, u.ap);
1410 sdata->control_port_protocol =
1411 master->control_port_protocol;
1412 sdata->control_port_no_encrypt =
1413 master->control_port_no_encrypt;
1414 sdata->control_port_over_nl80211 =
1415 master->control_port_over_nl80211;
1416 sdata->control_port_no_preauth =
1417 master->control_port_no_preauth;
1418 sdata->vif.cab_queue = master->vif.cab_queue;
1419 memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
1420 sizeof(sdata->vif.hw_queue));
1421 sdata->vif.bss_conf.chandef = master->vif.bss_conf.chandef;
1423 mutex_lock(&local->key_mtx);
1424 sdata->crypto_tx_tailroom_needed_cnt +=
1425 master->crypto_tx_tailroom_needed_cnt;
1426 mutex_unlock(&local->key_mtx);
1430 case NL80211_IFTYPE_AP:
1431 sdata->bss = &sdata->u.ap;
1433 case NL80211_IFTYPE_MESH_POINT:
1434 case NL80211_IFTYPE_STATION:
1435 case NL80211_IFTYPE_MONITOR:
1436 case NL80211_IFTYPE_ADHOC:
1437 case NL80211_IFTYPE_P2P_DEVICE:
1438 case NL80211_IFTYPE_OCB:
1439 case NL80211_IFTYPE_NAN:
1440 /* no special treatment */
1442 case NL80211_IFTYPE_UNSPECIFIED:
1443 case NUM_NL80211_IFTYPES:
1444 case NL80211_IFTYPE_P2P_CLIENT:
1445 case NL80211_IFTYPE_P2P_GO:
1446 case NL80211_IFTYPE_WDS:
1452 if (local->open_count == 0) {
1453 res = drv_start(local);
1456 /* we're brought up, everything changes */
1457 hw_reconf_flags = ~0;
1458 ieee80211_led_radio(local, true);
1459 ieee80211_mod_tpt_led_trig(local,
1460 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1464 * Copy the hopefully now-present MAC address to
1465 * this interface, if it has the special null one.
1467 if (dev && is_zero_ether_addr(dev->dev_addr)) {
1468 eth_hw_addr_set(dev, local->hw.wiphy->perm_addr);
1469 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
1471 if (!is_valid_ether_addr(dev->dev_addr)) {
1472 res = -EADDRNOTAVAIL;
1477 switch (sdata->vif.type) {
1478 case NL80211_IFTYPE_AP_VLAN:
1479 /* no need to tell driver, but set carrier and chanctx */
1480 if (sdata->bss->active) {
1481 ieee80211_link_vlan_copy_chanctx(&sdata->deflink);
1482 netif_carrier_on(dev);
1483 ieee80211_set_vif_encap_ops(sdata);
1485 netif_carrier_off(dev);
1488 case NL80211_IFTYPE_MONITOR:
1489 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
1490 local->cooked_mntrs++;
1494 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1495 res = drv_add_interface(local, sdata);
1498 } else if (local->monitors == 0 && local->open_count == 0) {
1499 res = ieee80211_add_virtual_monitor(local);
1504 /* must be before the call to ieee80211_configure_filter */
1506 if (local->monitors == 1) {
1507 local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
1508 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
1511 ieee80211_adjust_monitor_flags(sdata, 1);
1512 ieee80211_configure_filter(local);
1513 ieee80211_recalc_offload(local);
1514 mutex_lock(&local->mtx);
1515 ieee80211_recalc_idle(local);
1516 mutex_unlock(&local->mtx);
1518 netif_carrier_on(dev);
1522 ieee80211_del_virtual_monitor(local);
1523 ieee80211_set_sdata_offload_flags(sdata);
1525 res = drv_add_interface(local, sdata);
1529 ieee80211_set_vif_encap_ops(sdata);
1530 res = ieee80211_check_queues(sdata,
1531 ieee80211_vif_type_p2p(&sdata->vif));
1533 goto err_del_interface;
1536 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1537 local->fif_pspoll++;
1538 local->fif_probe_req++;
1540 ieee80211_configure_filter(local);
1541 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1542 local->fif_probe_req++;
1545 if (sdata->vif.probe_req_reg)
1546 drv_config_iface_filter(local, sdata,
1550 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1551 sdata->vif.type != NL80211_IFTYPE_NAN)
1552 changed |= ieee80211_reset_erp_info(sdata);
1553 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1556 switch (sdata->vif.type) {
1557 case NL80211_IFTYPE_STATION:
1558 case NL80211_IFTYPE_ADHOC:
1559 case NL80211_IFTYPE_AP:
1560 case NL80211_IFTYPE_MESH_POINT:
1561 case NL80211_IFTYPE_OCB:
1562 netif_carrier_off(dev);
1564 case NL80211_IFTYPE_P2P_DEVICE:
1565 case NL80211_IFTYPE_NAN:
1573 * Set default queue parameters so drivers don't
1574 * need to initialise the hardware if the hardware
1575 * doesn't start up with sane defaults.
1576 * Enable QoS for anything but station interfaces.
1578 ieee80211_set_wmm_default(&sdata->deflink, true,
1579 sdata->vif.type != NL80211_IFTYPE_STATION);
1582 set_bit(SDATA_STATE_RUNNING, &sdata->state);
1584 switch (sdata->vif.type) {
1585 case NL80211_IFTYPE_P2P_DEVICE:
1586 rcu_assign_pointer(local->p2p_sdata, sdata);
1588 case NL80211_IFTYPE_MONITOR:
1589 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
1591 list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list);
1598 * set_multicast_list will be invoked by the networking core
1599 * which will check whether any increments here were done in
1600 * error and sync them down to the hardware as filter flags.
1602 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
1603 atomic_inc(&local->iff_allmultis);
1606 local->open_count++;
1608 if (hw_reconf_flags)
1609 ieee80211_hw_config(local, hw_reconf_flags);
1611 ieee80211_recalc_ps(local);
1613 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1614 sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1615 local->ops->wake_tx_queue) {
1616 /* XXX: for AP_VLAN, actually track AP queues */
1618 netif_tx_start_all_queues(dev);
1620 unsigned long flags;
1621 int n_acs = IEEE80211_NUM_ACS;
1624 if (local->hw.queues < IEEE80211_NUM_ACS)
1627 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1628 if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE ||
1629 (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 &&
1630 skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) {
1631 for (ac = 0; ac < n_acs; ac++) {
1632 int ac_queue = sdata->vif.hw_queue[ac];
1634 if (local->queue_stop_reasons[ac_queue] == 0 &&
1635 skb_queue_empty(&local->pending[ac_queue]))
1636 netif_start_subqueue(dev, ac);
1639 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1644 drv_remove_interface(local, sdata);
1646 if (!local->open_count)
1650 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1651 mutex_lock(&local->mtx);
1652 list_del(&sdata->u.vlan.list);
1653 mutex_unlock(&local->mtx);
1655 /* might already be clear but that doesn't matter */
1656 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1660 static void ieee80211_if_free(struct net_device *dev)
1662 free_percpu(dev->tstats);
1665 static void ieee80211_if_setup(struct net_device *dev)
1668 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1669 dev->netdev_ops = &ieee80211_dataif_ops;
1670 dev->needs_free_netdev = true;
1671 dev->priv_destructor = ieee80211_if_free;
1674 static void ieee80211_if_setup_no_queue(struct net_device *dev)
1676 ieee80211_if_setup(dev);
1677 dev->priv_flags |= IFF_NO_QUEUE;
1680 static void ieee80211_iface_process_skb(struct ieee80211_local *local,
1681 struct ieee80211_sub_if_data *sdata,
1682 struct sk_buff *skb)
1684 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1686 if (ieee80211_is_action(mgmt->frame_control) &&
1687 mgmt->u.action.category == WLAN_CATEGORY_BACK) {
1688 struct sta_info *sta;
1691 mutex_lock(&local->sta_mtx);
1692 sta = sta_info_get_bss(sdata, mgmt->sa);
1694 switch (mgmt->u.action.u.addba_req.action_code) {
1695 case WLAN_ACTION_ADDBA_REQ:
1696 ieee80211_process_addba_request(local, sta,
1699 case WLAN_ACTION_ADDBA_RESP:
1700 ieee80211_process_addba_resp(local, sta,
1703 case WLAN_ACTION_DELBA:
1704 ieee80211_process_delba(sdata, sta,
1712 mutex_unlock(&local->sta_mtx);
1713 } else if (ieee80211_is_action(mgmt->frame_control) &&
1714 mgmt->u.action.category == WLAN_CATEGORY_VHT) {
1715 switch (mgmt->u.action.u.vht_group_notif.action_code) {
1716 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
1717 struct ieee80211_rx_status *status;
1718 enum nl80211_band band;
1719 struct sta_info *sta;
1722 status = IEEE80211_SKB_RXCB(skb);
1723 band = status->band;
1724 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
1726 mutex_lock(&local->sta_mtx);
1727 sta = sta_info_get_bss(sdata, mgmt->sa);
1730 ieee80211_vht_handle_opmode(sdata,
1734 mutex_unlock(&local->sta_mtx);
1737 case WLAN_VHT_ACTION_GROUPID_MGMT:
1738 ieee80211_process_mu_groups(sdata, &sdata->deflink,
1745 } else if (ieee80211_is_action(mgmt->frame_control) &&
1746 mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1747 switch (mgmt->u.action.u.s1g.action_code) {
1748 case WLAN_S1G_TWT_TEARDOWN:
1749 case WLAN_S1G_TWT_SETUP:
1750 ieee80211_s1g_rx_twt_action(sdata, skb);
1755 } else if (ieee80211_is_ext(mgmt->frame_control)) {
1756 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1757 ieee80211_sta_rx_queued_ext(sdata, skb);
1760 } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
1761 struct ieee80211_hdr *hdr = (void *)mgmt;
1762 struct sta_info *sta;
1765 * So the frame isn't mgmt, but frame_control
1766 * is at the right place anyway, of course, so
1767 * the if statement is correct.
1769 * Warn if we have other data frame types here,
1770 * they must not get here.
1772 WARN_ON(hdr->frame_control &
1773 cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
1774 WARN_ON(!(hdr->seq_ctrl &
1775 cpu_to_le16(IEEE80211_SCTL_FRAG)));
1777 * This was a fragment of a frame, received while
1778 * a block-ack session was active. That cannot be
1779 * right, so terminate the session.
1781 mutex_lock(&local->sta_mtx);
1782 sta = sta_info_get_bss(sdata, mgmt->sa);
1784 u16 tid = ieee80211_get_tid(hdr);
1786 __ieee80211_stop_rx_ba_session(
1787 sta, tid, WLAN_BACK_RECIPIENT,
1788 WLAN_REASON_QSTA_REQUIRE_SETUP,
1791 mutex_unlock(&local->sta_mtx);
1792 } else switch (sdata->vif.type) {
1793 case NL80211_IFTYPE_STATION:
1794 ieee80211_sta_rx_queued_mgmt(sdata, skb);
1796 case NL80211_IFTYPE_ADHOC:
1797 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
1799 case NL80211_IFTYPE_MESH_POINT:
1800 if (!ieee80211_vif_is_mesh(&sdata->vif))
1802 ieee80211_mesh_rx_queued_mgmt(sdata, skb);
1805 WARN(1, "frame for unexpected interface type");
1810 static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata,
1811 struct sk_buff *skb)
1813 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1815 if (ieee80211_is_action(mgmt->frame_control) &&
1816 mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1817 switch (mgmt->u.action.u.s1g.action_code) {
1818 case WLAN_S1G_TWT_TEARDOWN:
1819 case WLAN_S1G_TWT_SETUP:
1820 ieee80211_s1g_status_twt_action(sdata, skb);
1828 static void ieee80211_iface_work(struct work_struct *work)
1830 struct ieee80211_sub_if_data *sdata =
1831 container_of(work, struct ieee80211_sub_if_data, work);
1832 struct ieee80211_local *local = sdata->local;
1833 struct sk_buff *skb;
1835 if (!ieee80211_sdata_running(sdata))
1838 if (test_bit(SCAN_SW_SCANNING, &local->scanning))
1841 if (!ieee80211_can_run_worker(local))
1844 /* first process frames */
1845 while ((skb = skb_dequeue(&sdata->skb_queue))) {
1846 kcov_remote_start_common(skb_get_kcov_handle(skb));
1848 if (skb->protocol == cpu_to_be16(ETH_P_TDLS))
1849 ieee80211_process_tdls_channel_switch(sdata, skb);
1851 ieee80211_iface_process_skb(local, sdata, skb);
1857 /* process status queue */
1858 while ((skb = skb_dequeue(&sdata->status_queue))) {
1859 kcov_remote_start_common(skb_get_kcov_handle(skb));
1861 ieee80211_iface_process_status(sdata, skb);
1867 /* then other type-dependent work */
1868 switch (sdata->vif.type) {
1869 case NL80211_IFTYPE_STATION:
1870 ieee80211_sta_work(sdata);
1872 case NL80211_IFTYPE_ADHOC:
1873 ieee80211_ibss_work(sdata);
1875 case NL80211_IFTYPE_MESH_POINT:
1876 if (!ieee80211_vif_is_mesh(&sdata->vif))
1878 ieee80211_mesh_work(sdata);
1880 case NL80211_IFTYPE_OCB:
1881 ieee80211_ocb_work(sdata);
1888 static void ieee80211_recalc_smps_work(struct work_struct *work)
1890 struct ieee80211_sub_if_data *sdata =
1891 container_of(work, struct ieee80211_sub_if_data, recalc_smps);
1893 ieee80211_recalc_smps(sdata, &sdata->deflink);
1897 * Helper function to initialise an interface to a specific type.
1899 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1900 enum nl80211_iftype type)
1902 static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
1905 /* clear type-dependent unions */
1906 memset(&sdata->u, 0, sizeof(sdata->u));
1907 memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u));
1909 /* and set some type-dependent values */
1910 sdata->vif.type = type;
1911 sdata->vif.p2p = false;
1912 sdata->wdev.iftype = type;
1914 sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
1915 sdata->control_port_no_encrypt = false;
1916 sdata->control_port_over_nl80211 = false;
1917 sdata->control_port_no_preauth = false;
1918 sdata->vif.cfg.idle = true;
1919 sdata->vif.bss_conf.txpower = INT_MIN; /* unset */
1921 sdata->noack_map = 0;
1923 /* only monitor/p2p-device differ */
1925 sdata->dev->netdev_ops = &ieee80211_dataif_ops;
1926 sdata->dev->type = ARPHRD_ETHER;
1929 skb_queue_head_init(&sdata->skb_queue);
1930 skb_queue_head_init(&sdata->status_queue);
1931 INIT_WORK(&sdata->work, ieee80211_iface_work);
1932 INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
1935 case NL80211_IFTYPE_P2P_GO:
1936 type = NL80211_IFTYPE_AP;
1937 sdata->vif.type = type;
1938 sdata->vif.p2p = true;
1940 case NL80211_IFTYPE_AP:
1941 skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1942 INIT_LIST_HEAD(&sdata->u.ap.vlans);
1943 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1945 case NL80211_IFTYPE_P2P_CLIENT:
1946 type = NL80211_IFTYPE_STATION;
1947 sdata->vif.type = type;
1948 sdata->vif.p2p = true;
1950 case NL80211_IFTYPE_STATION:
1951 sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid;
1952 ieee80211_sta_setup_sdata(sdata);
1954 case NL80211_IFTYPE_OCB:
1955 sdata->vif.bss_conf.bssid = bssid_wildcard;
1956 ieee80211_ocb_setup_sdata(sdata);
1958 case NL80211_IFTYPE_ADHOC:
1959 sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
1960 ieee80211_ibss_setup_sdata(sdata);
1962 case NL80211_IFTYPE_MESH_POINT:
1963 if (ieee80211_vif_is_mesh(&sdata->vif))
1964 ieee80211_mesh_init_sdata(sdata);
1966 case NL80211_IFTYPE_MONITOR:
1967 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
1968 sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
1969 sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
1970 MONITOR_FLAG_OTHER_BSS;
1972 case NL80211_IFTYPE_NAN:
1973 idr_init(&sdata->u.nan.function_inst_ids);
1974 spin_lock_init(&sdata->u.nan.func_lock);
1975 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1977 case NL80211_IFTYPE_AP_VLAN:
1978 case NL80211_IFTYPE_P2P_DEVICE:
1979 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1981 case NL80211_IFTYPE_UNSPECIFIED:
1982 case NL80211_IFTYPE_WDS:
1983 case NUM_NL80211_IFTYPES:
1988 /* need to do this after the switch so vif.type is correct */
1989 ieee80211_link_setup(&sdata->deflink);
1991 ieee80211_debugfs_add_netdev(sdata);
1994 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
1995 enum nl80211_iftype type)
1997 struct ieee80211_local *local = sdata->local;
1999 enum nl80211_iftype internal_type = type;
2004 if (!local->ops->change_interface)
2007 /* for now, don't support changing while links exist */
2008 if (sdata->vif.valid_links)
2011 switch (sdata->vif.type) {
2012 case NL80211_IFTYPE_AP:
2013 if (!list_empty(&sdata->u.ap.vlans))
2016 case NL80211_IFTYPE_STATION:
2017 case NL80211_IFTYPE_ADHOC:
2018 case NL80211_IFTYPE_OCB:
2020 * Could maybe also all others here?
2021 * Just not sure how that interacts
2022 * with the RX/config path e.g. for
2031 case NL80211_IFTYPE_AP:
2032 case NL80211_IFTYPE_STATION:
2033 case NL80211_IFTYPE_ADHOC:
2034 case NL80211_IFTYPE_OCB:
2036 * Could probably support everything
2040 case NL80211_IFTYPE_P2P_CLIENT:
2042 internal_type = NL80211_IFTYPE_STATION;
2044 case NL80211_IFTYPE_P2P_GO:
2046 internal_type = NL80211_IFTYPE_AP;
2052 ret = ieee80211_check_concurrent_iface(sdata, internal_type);
2056 ieee80211_stop_vif_queues(local, sdata,
2057 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
2060 ieee80211_do_stop(sdata, false);
2062 ieee80211_teardown_sdata(sdata);
2064 ieee80211_set_sdata_offload_flags(sdata);
2065 ret = drv_change_interface(local, sdata, internal_type, p2p);
2067 type = ieee80211_vif_type_p2p(&sdata->vif);
2070 * Ignore return value here, there's not much we can do since
2071 * the driver changed the interface type internally already.
2072 * The warnings will hopefully make driver authors fix it :-)
2074 ieee80211_check_queues(sdata, type);
2076 ieee80211_setup_sdata(sdata, type);
2077 ieee80211_set_vif_encap_ops(sdata);
2079 err = ieee80211_do_open(&sdata->wdev, false);
2080 WARN(err, "type change: do_open returned %d", err);
2082 ieee80211_wake_vif_queues(local, sdata,
2083 IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
2087 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
2088 enum nl80211_iftype type)
2094 if (type == ieee80211_vif_type_p2p(&sdata->vif))
2097 if (ieee80211_sdata_running(sdata)) {
2098 ret = ieee80211_runtime_change_iftype(sdata, type);
2102 /* Purge and reset type-dependent state. */
2103 ieee80211_teardown_sdata(sdata);
2104 ieee80211_setup_sdata(sdata, type);
2107 /* reset some values that shouldn't be kept across type changes */
2108 if (type == NL80211_IFTYPE_STATION)
2109 sdata->u.mgd.use_4addr = false;
2114 static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
2115 u8 *perm_addr, enum nl80211_iftype type)
2117 struct ieee80211_sub_if_data *sdata;
2118 u64 mask, start, addr, val, inc;
2120 u8 tmp_addr[ETH_ALEN];
2123 /* default ... something at least */
2124 memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
2126 if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
2127 local->hw.wiphy->n_addresses <= 1)
2130 mutex_lock(&local->iflist_mtx);
2133 case NL80211_IFTYPE_MONITOR:
2134 /* doesn't matter */
2136 case NL80211_IFTYPE_AP_VLAN:
2137 /* match up with an AP interface */
2138 list_for_each_entry(sdata, &local->interfaces, list) {
2139 if (sdata->vif.type != NL80211_IFTYPE_AP)
2141 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
2144 /* keep default if no AP interface present */
2146 case NL80211_IFTYPE_P2P_CLIENT:
2147 case NL80211_IFTYPE_P2P_GO:
2148 if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
2149 list_for_each_entry(sdata, &local->interfaces, list) {
2150 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
2152 if (!ieee80211_sdata_running(sdata))
2154 memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
2160 /* assign a new address if possible -- try n_addresses first */
2161 for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
2164 list_for_each_entry(sdata, &local->interfaces, list) {
2165 if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
2174 local->hw.wiphy->addresses[i].addr,
2180 /* try mask if available */
2181 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
2184 m = local->hw.wiphy->addr_mask;
2185 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2186 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2187 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2189 if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
2190 /* not a contiguous mask ... not handled now! */
2191 pr_info("not contiguous\n");
2196 * Pick address of existing interface in case user changed
2197 * MAC address manually, default to perm_addr.
2199 m = local->hw.wiphy->perm_addr;
2200 list_for_each_entry(sdata, &local->interfaces, list) {
2201 if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2203 m = sdata->vif.addr;
2206 start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2207 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2208 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2210 inc = 1ULL<<__ffs64(mask);
2211 val = (start & mask);
2212 addr = (start & ~mask) | (val & mask);
2216 tmp_addr[5] = addr >> 0*8;
2217 tmp_addr[4] = addr >> 1*8;
2218 tmp_addr[3] = addr >> 2*8;
2219 tmp_addr[2] = addr >> 3*8;
2220 tmp_addr[1] = addr >> 4*8;
2221 tmp_addr[0] = addr >> 5*8;
2225 list_for_each_entry(sdata, &local->interfaces, list) {
2226 if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
2233 memcpy(perm_addr, tmp_addr, ETH_ALEN);
2236 addr = (start & ~mask) | (val & mask);
2237 } while (addr != start);
2243 mutex_unlock(&local->iflist_mtx);
2246 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
2247 unsigned char name_assign_type,
2248 struct wireless_dev **new_wdev, enum nl80211_iftype type,
2249 struct vif_params *params)
2251 struct net_device *ndev = NULL;
2252 struct ieee80211_sub_if_data *sdata = NULL;
2253 struct txq_info *txqi;
2254 void (*if_setup)(struct net_device *dev);
2260 if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
2261 struct wireless_dev *wdev;
2263 sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
2267 wdev = &sdata->wdev;
2270 strlcpy(sdata->name, name, IFNAMSIZ);
2271 ieee80211_assign_perm_addr(local, wdev->address, type);
2272 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
2273 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2275 int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
2279 if (local->ops->wake_tx_queue &&
2280 type != NL80211_IFTYPE_AP_VLAN &&
2281 (type != NL80211_IFTYPE_MONITOR ||
2282 (params->flags & MONITOR_FLAG_ACTIVE)))
2283 txq_size += sizeof(struct txq_info) +
2284 local->hw.txq_data_size;
2286 if (local->ops->wake_tx_queue) {
2287 if_setup = ieee80211_if_setup_no_queue;
2289 if_setup = ieee80211_if_setup;
2290 if (local->hw.queues >= IEEE80211_NUM_ACS)
2291 txqs = IEEE80211_NUM_ACS;
2294 ndev = alloc_netdev_mqs(size + txq_size,
2295 name, name_assign_type,
2300 if (!local->ops->wake_tx_queue && local->hw.wiphy->tx_queue_len)
2301 ndev->tx_queue_len = local->hw.wiphy->tx_queue_len;
2303 dev_net_set(ndev, wiphy_net(local->hw.wiphy));
2305 ndev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2306 if (!ndev->tstats) {
2311 ndev->needed_headroom = local->tx_headroom +
2312 4*6 /* four MAC addresses */
2313 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
2315 + 8 /* rfc1042/bridge tunnel */
2316 - ETH_HLEN /* ethernet hard_header_len */
2317 + IEEE80211_ENCRYPT_HEADROOM;
2318 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
2320 ret = dev_alloc_name(ndev, ndev->name);
2322 ieee80211_if_free(ndev);
2327 ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
2328 if (is_valid_ether_addr(params->macaddr))
2329 eth_hw_addr_set(ndev, params->macaddr);
2331 eth_hw_addr_set(ndev, ndev->perm_addr);
2332 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
2334 /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
2335 sdata = netdev_priv(ndev);
2336 ndev->ieee80211_ptr = &sdata->wdev;
2337 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
2338 ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2339 memcpy(sdata->name, ndev->name, IFNAMSIZ);
2342 txqi = netdev_priv(ndev) + size;
2343 ieee80211_txq_init(sdata, NULL, txqi, 0);
2349 /* initialise type-independent data */
2350 sdata->wdev.wiphy = local->hw.wiphy;
2352 ieee80211_sdata_init(local, sdata);
2354 ieee80211_init_frag_cache(&sdata->frags);
2356 INIT_LIST_HEAD(&sdata->key_list);
2358 INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
2359 ieee80211_delayed_tailroom_dec);
2361 for (i = 0; i < NUM_NL80211_BANDS; i++) {
2362 struct ieee80211_supported_band *sband;
2363 sband = local->hw.wiphy->bands[i];
2364 sdata->rc_rateidx_mask[i] =
2365 sband ? (1 << sband->n_bitrates) - 1 : 0;
2370 memcpy(sdata->rc_rateidx_mcs_mask[i],
2371 sband->ht_cap.mcs.rx_mask,
2372 sizeof(sdata->rc_rateidx_mcs_mask[i]));
2374 cap = sband->vht_cap.vht_mcs.rx_mcs_map;
2375 vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
2376 ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
2378 memset(sdata->rc_rateidx_mcs_mask[i], 0,
2379 sizeof(sdata->rc_rateidx_mcs_mask[i]));
2380 memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
2381 sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
2385 ieee80211_set_default_queues(sdata);
2387 sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2388 sdata->deflink.user_power_level = local->user_power_level;
2390 /* setup type-dependent data */
2391 ieee80211_setup_sdata(sdata, type);
2394 ndev->ieee80211_ptr->use_4addr = params->use_4addr;
2395 if (type == NL80211_IFTYPE_STATION)
2396 sdata->u.mgd.use_4addr = params->use_4addr;
2398 ndev->features |= local->hw.netdev_features;
2399 ndev->hw_features |= ndev->features &
2400 MAC80211_SUPPORTED_FEATURES_TX;
2402 netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
2404 /* MTU range is normally 256 - 2304, where the upper limit is
2405 * the maximum MSDU size. Monitor interfaces send and receive
2406 * MPDU and A-MSDU frames which may be much larger so we do
2407 * not impose an upper limit in that case.
2409 ndev->min_mtu = 256;
2410 if (type == NL80211_IFTYPE_MONITOR)
2413 ndev->max_mtu = local->hw.max_mtu;
2415 ret = cfg80211_register_netdevice(ndev);
2422 mutex_lock(&local->iflist_mtx);
2423 list_add_tail_rcu(&sdata->list, &local->interfaces);
2424 mutex_unlock(&local->iflist_mtx);
2427 *new_wdev = &sdata->wdev;
2432 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
2436 mutex_lock(&sdata->local->iflist_mtx);
2437 list_del_rcu(&sdata->list);
2438 mutex_unlock(&sdata->local->iflist_mtx);
2441 ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
2445 cfg80211_unregister_wdev(&sdata->wdev);
2448 ieee80211_teardown_sdata(sdata);
2453 void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
2455 if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
2457 ieee80211_do_stop(sdata, true);
2460 void ieee80211_remove_interfaces(struct ieee80211_local *local)
2462 struct ieee80211_sub_if_data *sdata, *tmp;
2463 LIST_HEAD(unreg_list);
2464 LIST_HEAD(wdev_list);
2468 /* Before destroying the interfaces, make sure they're all stopped so
2469 * that the hardware is stopped. Otherwise, the driver might still be
2470 * iterating the interfaces during the shutdown, e.g. from a worker
2471 * or from RX processing or similar, and if it does so (using atomic
2472 * iteration) while we're manipulating the list, the iteration will
2475 * After this, the hardware should be stopped and the driver should
2476 * have stopped all of its activities, so that we can do RCU-unaware
2477 * manipulations of the interface list below.
2479 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
2481 WARN(local->open_count, "%s: open count remains %d\n",
2482 wiphy_name(local->hw.wiphy), local->open_count);
2484 ieee80211_txq_teardown_flows(local);
2486 mutex_lock(&local->iflist_mtx);
2487 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
2488 list_del(&sdata->list);
2491 unregister_netdevice_queue(sdata->dev, &unreg_list);
2493 list_add(&sdata->list, &wdev_list);
2495 mutex_unlock(&local->iflist_mtx);
2497 unregister_netdevice_many(&unreg_list);
2499 wiphy_lock(local->hw.wiphy);
2500 list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
2501 list_del(&sdata->list);
2502 cfg80211_unregister_wdev(&sdata->wdev);
2505 wiphy_unlock(local->hw.wiphy);
2508 static int netdev_notify(struct notifier_block *nb,
2509 unsigned long state, void *ptr)
2511 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2512 struct ieee80211_sub_if_data *sdata;
2514 if (state != NETDEV_CHANGENAME)
2517 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
2520 if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
2523 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2524 memcpy(sdata->name, dev->name, IFNAMSIZ);
2525 ieee80211_debugfs_rename_netdev(sdata);
2530 static struct notifier_block mac80211_netdev_notifier = {
2531 .notifier_call = netdev_notify,
2534 int ieee80211_iface_init(void)
2536 return register_netdevice_notifier(&mac80211_netdev_notifier);
2539 void ieee80211_iface_exit(void)
2541 unregister_netdevice_notifier(&mac80211_netdev_notifier);
2544 void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
2546 if (sdata->vif.type == NL80211_IFTYPE_AP)
2547 atomic_inc(&sdata->u.ap.num_mcast_sta);
2548 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2549 atomic_inc(&sdata->u.vlan.num_mcast_sta);
2552 void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
2554 if (sdata->vif.type == NL80211_IFTYPE_AP)
2555 atomic_dec(&sdata->u.ap.num_mcast_sta);
2556 else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2557 atomic_dec(&sdata->u.vlan.num_mcast_sta);