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