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