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