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