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