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