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