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