xref: /openbmc/linux/net/mac80211/iface.c (revision d699090510c3223641a23834b4710e2d4309a6ad)
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 	if (sdata->vif.txq)
686 		ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
687 
688 	sdata->bss = NULL;
689 
690 	if (local->open_count == 0)
691 		ieee80211_clear_tx_pending(local);
692 
693 	sdata->vif.bss_conf.beacon_int = 0;
694 
695 	/*
696 	 * If the interface goes down while suspended, presumably because
697 	 * the device was unplugged and that happens before our resume,
698 	 * then the driver is already unconfigured and the remainder of
699 	 * this function isn't needed.
700 	 * XXX: what about WoWLAN? If the device has software state, e.g.
701 	 *	memory allocated, it might expect teardown commands from
702 	 *	mac80211 here?
703 	 */
704 	if (local->suspended) {
705 		WARN_ON(local->wowlan);
706 		WARN_ON(rcu_access_pointer(local->monitor_sdata));
707 		return;
708 	}
709 
710 	switch (sdata->vif.type) {
711 	case NL80211_IFTYPE_AP_VLAN:
712 		break;
713 	case NL80211_IFTYPE_MONITOR:
714 		if (local->monitors == 0)
715 			ieee80211_del_virtual_monitor(local);
716 
717 		mutex_lock(&local->mtx);
718 		ieee80211_recalc_idle(local);
719 		mutex_unlock(&local->mtx);
720 
721 		if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
722 			break;
723 
724 		fallthrough;
725 	default:
726 		if (going_down)
727 			drv_remove_interface(local, sdata);
728 	}
729 
730 	ieee80211_recalc_ps(local);
731 
732 	if (cancel_scan)
733 		wiphy_delayed_work_flush(local->hw.wiphy, &local->scan_work);
734 
735 	if (local->open_count == 0) {
736 		ieee80211_stop_device(local);
737 
738 		/* no reconfiguring after stop! */
739 		return;
740 	}
741 
742 	/* do after stop to avoid reconfiguring when we stop anyway */
743 	ieee80211_configure_filter(local);
744 	ieee80211_hw_config(local, hw_reconf_flags);
745 
746 	if (local->monitors == local->open_count)
747 		ieee80211_add_virtual_monitor(local);
748 }
749 
ieee80211_stop_mbssid(struct ieee80211_sub_if_data * sdata)750 static void ieee80211_stop_mbssid(struct ieee80211_sub_if_data *sdata)
751 {
752 	struct ieee80211_sub_if_data *tx_sdata, *non_tx_sdata, *tmp_sdata;
753 	struct ieee80211_vif *tx_vif = sdata->vif.mbssid_tx_vif;
754 
755 	if (!tx_vif)
756 		return;
757 
758 	tx_sdata = vif_to_sdata(tx_vif);
759 	sdata->vif.mbssid_tx_vif = NULL;
760 
761 	list_for_each_entry_safe(non_tx_sdata, tmp_sdata,
762 				 &tx_sdata->local->interfaces, list) {
763 		if (non_tx_sdata != sdata && non_tx_sdata != tx_sdata &&
764 		    non_tx_sdata->vif.mbssid_tx_vif == tx_vif &&
765 		    ieee80211_sdata_running(non_tx_sdata)) {
766 			non_tx_sdata->vif.mbssid_tx_vif = NULL;
767 			dev_close(non_tx_sdata->wdev.netdev);
768 		}
769 	}
770 
771 	if (sdata != tx_sdata && ieee80211_sdata_running(tx_sdata)) {
772 		tx_sdata->vif.mbssid_tx_vif = NULL;
773 		dev_close(tx_sdata->wdev.netdev);
774 	}
775 }
776 
ieee80211_stop(struct net_device * dev)777 static int ieee80211_stop(struct net_device *dev)
778 {
779 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
780 
781 	/* close dependent VLAN and MBSSID interfaces before locking wiphy */
782 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
783 		struct ieee80211_sub_if_data *vlan, *tmpsdata;
784 
785 		list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
786 					 u.vlan.list)
787 			dev_close(vlan->dev);
788 
789 		ieee80211_stop_mbssid(sdata);
790 	}
791 
792 	cancel_work_sync(&sdata->activate_links_work);
793 
794 	wiphy_lock(sdata->local->hw.wiphy);
795 	ieee80211_do_stop(sdata, true);
796 	wiphy_unlock(sdata->local->hw.wiphy);
797 
798 	return 0;
799 }
800 
ieee80211_set_multicast_list(struct net_device * dev)801 static void ieee80211_set_multicast_list(struct net_device *dev)
802 {
803 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
804 	struct ieee80211_local *local = sdata->local;
805 	int allmulti, sdata_allmulti;
806 
807 	allmulti = !!(dev->flags & IFF_ALLMULTI);
808 	sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
809 
810 	if (allmulti != sdata_allmulti) {
811 		if (dev->flags & IFF_ALLMULTI)
812 			atomic_inc(&local->iff_allmultis);
813 		else
814 			atomic_dec(&local->iff_allmultis);
815 		sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
816 	}
817 
818 	spin_lock_bh(&local->filter_lock);
819 	__hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
820 	spin_unlock_bh(&local->filter_lock);
821 	ieee80211_queue_work(&local->hw, &local->reconfig_filter);
822 }
823 
824 /*
825  * Called when the netdev is removed or, by the code below, before
826  * the interface type changes.
827  */
ieee80211_teardown_sdata(struct ieee80211_sub_if_data * sdata)828 static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
829 {
830 	/* free extra data */
831 	ieee80211_free_keys(sdata, false);
832 
833 	ieee80211_debugfs_remove_netdev(sdata);
834 
835 	ieee80211_destroy_frag_cache(&sdata->frags);
836 
837 	if (ieee80211_vif_is_mesh(&sdata->vif))
838 		ieee80211_mesh_teardown_sdata(sdata);
839 
840 	ieee80211_vif_clear_links(sdata);
841 	ieee80211_link_stop(&sdata->deflink);
842 }
843 
ieee80211_uninit(struct net_device * dev)844 static void ieee80211_uninit(struct net_device *dev)
845 {
846 	ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
847 }
848 
849 static void
ieee80211_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)850 ieee80211_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
851 {
852 	dev_fetch_sw_netstats(stats, dev->tstats);
853 }
854 
ieee80211_netdev_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)855 static int ieee80211_netdev_setup_tc(struct net_device *dev,
856 				     enum tc_setup_type type, void *type_data)
857 {
858 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
859 	struct ieee80211_local *local = sdata->local;
860 
861 	return drv_net_setup_tc(local, sdata, dev, type, type_data);
862 }
863 
864 static const struct net_device_ops ieee80211_dataif_ops = {
865 	.ndo_open		= ieee80211_open,
866 	.ndo_stop		= ieee80211_stop,
867 	.ndo_uninit		= ieee80211_uninit,
868 	.ndo_start_xmit		= ieee80211_subif_start_xmit,
869 	.ndo_set_rx_mode	= ieee80211_set_multicast_list,
870 	.ndo_set_mac_address 	= ieee80211_change_mac,
871 	.ndo_get_stats64	= ieee80211_get_stats64,
872 	.ndo_setup_tc		= ieee80211_netdev_setup_tc,
873 };
874 
ieee80211_monitor_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)875 static u16 ieee80211_monitor_select_queue(struct net_device *dev,
876 					  struct sk_buff *skb,
877 					  struct net_device *sb_dev)
878 {
879 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
880 	struct ieee80211_local *local = sdata->local;
881 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
882 	struct ieee80211_hdr *hdr;
883 	int len_rthdr;
884 
885 	if (local->hw.queues < IEEE80211_NUM_ACS)
886 		return 0;
887 
888 	/* reset flags and info before parsing radiotap header */
889 	memset(info, 0, sizeof(*info));
890 
891 	if (!ieee80211_parse_tx_radiotap(skb, dev))
892 		return 0; /* doesn't matter, frame will be dropped */
893 
894 	len_rthdr = ieee80211_get_radiotap_len(skb->data);
895 	hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
896 	if (skb->len < len_rthdr + 2 ||
897 	    skb->len < len_rthdr + ieee80211_hdrlen(hdr->frame_control))
898 		return 0; /* doesn't matter, frame will be dropped */
899 
900 	return ieee80211_select_queue_80211(sdata, skb, hdr);
901 }
902 
903 static const struct net_device_ops ieee80211_monitorif_ops = {
904 	.ndo_open		= ieee80211_open,
905 	.ndo_stop		= ieee80211_stop,
906 	.ndo_uninit		= ieee80211_uninit,
907 	.ndo_start_xmit		= ieee80211_monitor_start_xmit,
908 	.ndo_set_rx_mode	= ieee80211_set_multicast_list,
909 	.ndo_set_mac_address 	= ieee80211_change_mac,
910 	.ndo_select_queue	= ieee80211_monitor_select_queue,
911 	.ndo_get_stats64	= ieee80211_get_stats64,
912 };
913 
ieee80211_netdev_fill_forward_path(struct net_device_path_ctx * ctx,struct net_device_path * path)914 static int ieee80211_netdev_fill_forward_path(struct net_device_path_ctx *ctx,
915 					      struct net_device_path *path)
916 {
917 	struct ieee80211_sub_if_data *sdata;
918 	struct ieee80211_local *local;
919 	struct sta_info *sta;
920 	int ret = -ENOENT;
921 
922 	sdata = IEEE80211_DEV_TO_SUB_IF(ctx->dev);
923 	local = sdata->local;
924 
925 	if (!local->ops->net_fill_forward_path)
926 		return -EOPNOTSUPP;
927 
928 	rcu_read_lock();
929 	switch (sdata->vif.type) {
930 	case NL80211_IFTYPE_AP_VLAN:
931 		sta = rcu_dereference(sdata->u.vlan.sta);
932 		if (sta)
933 			break;
934 		if (sdata->wdev.use_4addr)
935 			goto out;
936 		if (is_multicast_ether_addr(ctx->daddr))
937 			goto out;
938 		sta = sta_info_get_bss(sdata, ctx->daddr);
939 		break;
940 	case NL80211_IFTYPE_AP:
941 		if (is_multicast_ether_addr(ctx->daddr))
942 			goto out;
943 		sta = sta_info_get(sdata, ctx->daddr);
944 		break;
945 	case NL80211_IFTYPE_STATION:
946 		if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
947 			sta = sta_info_get(sdata, ctx->daddr);
948 			if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
949 				if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
950 					goto out;
951 
952 				break;
953 			}
954 		}
955 
956 		sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
957 		break;
958 	default:
959 		goto out;
960 	}
961 
962 	if (!sta)
963 		goto out;
964 
965 	ret = drv_net_fill_forward_path(local, sdata, &sta->sta, ctx, path);
966 out:
967 	rcu_read_unlock();
968 
969 	return ret;
970 }
971 
972 static const struct net_device_ops ieee80211_dataif_8023_ops = {
973 	.ndo_open		= ieee80211_open,
974 	.ndo_stop		= ieee80211_stop,
975 	.ndo_uninit		= ieee80211_uninit,
976 	.ndo_start_xmit		= ieee80211_subif_start_xmit_8023,
977 	.ndo_set_rx_mode	= ieee80211_set_multicast_list,
978 	.ndo_set_mac_address	= ieee80211_change_mac,
979 	.ndo_get_stats64	= ieee80211_get_stats64,
980 	.ndo_fill_forward_path	= ieee80211_netdev_fill_forward_path,
981 	.ndo_setup_tc		= ieee80211_netdev_setup_tc,
982 };
983 
ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)984 static bool ieee80211_iftype_supports_hdr_offload(enum nl80211_iftype iftype)
985 {
986 	switch (iftype) {
987 	/* P2P GO and client are mapped to AP/STATION types */
988 	case NL80211_IFTYPE_AP:
989 	case NL80211_IFTYPE_STATION:
990 		return true;
991 	default:
992 		return false;
993 	}
994 }
995 
ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data * sdata)996 static bool ieee80211_set_sdata_offload_flags(struct ieee80211_sub_if_data *sdata)
997 {
998 	struct ieee80211_local *local = sdata->local;
999 	u32 flags;
1000 
1001 	flags = sdata->vif.offload_flags;
1002 
1003 	if (ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) &&
1004 	    ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
1005 		flags |= IEEE80211_OFFLOAD_ENCAP_ENABLED;
1006 
1007 		if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG) &&
1008 		    local->hw.wiphy->frag_threshold != (u32)-1)
1009 			flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1010 
1011 		if (local->monitors)
1012 			flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1013 	} else {
1014 		flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
1015 	}
1016 
1017 	if (ieee80211_hw_check(&local->hw, SUPPORTS_RX_DECAP_OFFLOAD) &&
1018 	    ieee80211_iftype_supports_hdr_offload(sdata->vif.type)) {
1019 		flags |= IEEE80211_OFFLOAD_DECAP_ENABLED;
1020 
1021 		if (local->monitors &&
1022 		    !ieee80211_hw_check(&local->hw, SUPPORTS_CONC_MON_RX_DECAP))
1023 			flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1024 	} else {
1025 		flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
1026 	}
1027 
1028 	if (sdata->vif.offload_flags == flags)
1029 		return false;
1030 
1031 	sdata->vif.offload_flags = flags;
1032 	ieee80211_check_fast_rx_iface(sdata);
1033 	return true;
1034 }
1035 
ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data * sdata)1036 static void ieee80211_set_vif_encap_ops(struct ieee80211_sub_if_data *sdata)
1037 {
1038 	struct ieee80211_local *local = sdata->local;
1039 	struct ieee80211_sub_if_data *bss = sdata;
1040 	bool enabled;
1041 
1042 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1043 		if (!sdata->bss)
1044 			return;
1045 
1046 		bss = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1047 	}
1048 
1049 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD) ||
1050 	    !ieee80211_iftype_supports_hdr_offload(bss->vif.type))
1051 		return;
1052 
1053 	enabled = bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED;
1054 	if (sdata->wdev.use_4addr &&
1055 	    !(bss->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_4ADDR))
1056 		enabled = false;
1057 
1058 	sdata->dev->netdev_ops = enabled ? &ieee80211_dataif_8023_ops :
1059 					   &ieee80211_dataif_ops;
1060 }
1061 
ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data * sdata)1062 static void ieee80211_recalc_sdata_offload(struct ieee80211_sub_if_data *sdata)
1063 {
1064 	struct ieee80211_local *local = sdata->local;
1065 	struct ieee80211_sub_if_data *vsdata;
1066 
1067 	if (ieee80211_set_sdata_offload_flags(sdata)) {
1068 		drv_update_vif_offload(local, sdata);
1069 		ieee80211_set_vif_encap_ops(sdata);
1070 	}
1071 
1072 	list_for_each_entry(vsdata, &local->interfaces, list) {
1073 		if (vsdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
1074 		    vsdata->bss != &sdata->u.ap)
1075 			continue;
1076 
1077 		ieee80211_set_vif_encap_ops(vsdata);
1078 	}
1079 }
1080 
ieee80211_recalc_offload(struct ieee80211_local * local)1081 void ieee80211_recalc_offload(struct ieee80211_local *local)
1082 {
1083 	struct ieee80211_sub_if_data *sdata;
1084 
1085 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_TX_ENCAP_OFFLOAD))
1086 		return;
1087 
1088 	mutex_lock(&local->iflist_mtx);
1089 
1090 	list_for_each_entry(sdata, &local->interfaces, list) {
1091 		if (!ieee80211_sdata_running(sdata))
1092 			continue;
1093 
1094 		ieee80211_recalc_sdata_offload(sdata);
1095 	}
1096 
1097 	mutex_unlock(&local->iflist_mtx);
1098 }
1099 
ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data * sdata,const int offset)1100 void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
1101 				    const int offset)
1102 {
1103 	struct ieee80211_local *local = sdata->local;
1104 	u32 flags = sdata->u.mntr.flags;
1105 
1106 #define ADJUST(_f, _s)	do {					\
1107 	if (flags & MONITOR_FLAG_##_f)				\
1108 		local->fif_##_s += offset;			\
1109 	} while (0)
1110 
1111 	ADJUST(FCSFAIL, fcsfail);
1112 	ADJUST(PLCPFAIL, plcpfail);
1113 	ADJUST(CONTROL, control);
1114 	ADJUST(CONTROL, pspoll);
1115 	ADJUST(OTHER_BSS, other_bss);
1116 
1117 #undef ADJUST
1118 }
1119 
ieee80211_set_default_queues(struct ieee80211_sub_if_data * sdata)1120 static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
1121 {
1122 	struct ieee80211_local *local = sdata->local;
1123 	int i;
1124 
1125 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
1126 		if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1127 			sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
1128 		else if (local->hw.queues >= IEEE80211_NUM_ACS)
1129 			sdata->vif.hw_queue[i] = i;
1130 		else
1131 			sdata->vif.hw_queue[i] = 0;
1132 	}
1133 	sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
1134 }
1135 
ieee80211_sdata_init(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)1136 static void ieee80211_sdata_init(struct ieee80211_local *local,
1137 				 struct ieee80211_sub_if_data *sdata)
1138 {
1139 	sdata->local = local;
1140 
1141 	/*
1142 	 * Initialize the default link, so we can use link_id 0 for non-MLD,
1143 	 * and that continues to work for non-MLD-aware drivers that use just
1144 	 * vif.bss_conf instead of vif.link_conf.
1145 	 *
1146 	 * Note that we never change this, so if link ID 0 isn't used in an
1147 	 * MLD connection, we get a separate allocation for it.
1148 	 */
1149 	ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf);
1150 }
1151 
ieee80211_add_virtual_monitor(struct ieee80211_local * local)1152 int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
1153 {
1154 	struct ieee80211_sub_if_data *sdata;
1155 	int ret;
1156 
1157 	if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1158 		return 0;
1159 
1160 	ASSERT_RTNL();
1161 	lockdep_assert_wiphy(local->hw.wiphy);
1162 
1163 	if (local->monitor_sdata)
1164 		return 0;
1165 
1166 	sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
1167 	if (!sdata)
1168 		return -ENOMEM;
1169 
1170 	/* set up data */
1171 	sdata->vif.type = NL80211_IFTYPE_MONITOR;
1172 	snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
1173 		 wiphy_name(local->hw.wiphy));
1174 	sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
1175 	mutex_init(&sdata->wdev.mtx);
1176 
1177 	ieee80211_sdata_init(local, sdata);
1178 
1179 	ieee80211_set_default_queues(sdata);
1180 
1181 	ret = drv_add_interface(local, sdata);
1182 	if (WARN_ON(ret)) {
1183 		/* ok .. stupid driver, it asked for this! */
1184 		kfree(sdata);
1185 		return ret;
1186 	}
1187 
1188 	set_bit(SDATA_STATE_RUNNING, &sdata->state);
1189 
1190 	ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
1191 	if (ret) {
1192 		kfree(sdata);
1193 		return ret;
1194 	}
1195 
1196 	mutex_lock(&local->iflist_mtx);
1197 	rcu_assign_pointer(local->monitor_sdata, sdata);
1198 	mutex_unlock(&local->iflist_mtx);
1199 
1200 	sdata_lock(sdata);
1201 	mutex_lock(&local->mtx);
1202 	ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chandef,
1203 					 IEEE80211_CHANCTX_EXCLUSIVE);
1204 	mutex_unlock(&local->mtx);
1205 	sdata_unlock(sdata);
1206 	if (ret) {
1207 		mutex_lock(&local->iflist_mtx);
1208 		RCU_INIT_POINTER(local->monitor_sdata, NULL);
1209 		mutex_unlock(&local->iflist_mtx);
1210 		synchronize_net();
1211 		drv_remove_interface(local, sdata);
1212 		mutex_destroy(&sdata->wdev.mtx);
1213 		kfree(sdata);
1214 		return ret;
1215 	}
1216 
1217 	skb_queue_head_init(&sdata->skb_queue);
1218 	skb_queue_head_init(&sdata->status_queue);
1219 	wiphy_work_init(&sdata->work, ieee80211_iface_work);
1220 
1221 	return 0;
1222 }
1223 
ieee80211_del_virtual_monitor(struct ieee80211_local * local)1224 void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
1225 {
1226 	struct ieee80211_sub_if_data *sdata;
1227 
1228 	if (!ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1229 		return;
1230 
1231 	ASSERT_RTNL();
1232 	lockdep_assert_wiphy(local->hw.wiphy);
1233 
1234 	mutex_lock(&local->iflist_mtx);
1235 
1236 	sdata = rcu_dereference_protected(local->monitor_sdata,
1237 					  lockdep_is_held(&local->iflist_mtx));
1238 	if (!sdata) {
1239 		mutex_unlock(&local->iflist_mtx);
1240 		return;
1241 	}
1242 
1243 	RCU_INIT_POINTER(local->monitor_sdata, NULL);
1244 	mutex_unlock(&local->iflist_mtx);
1245 
1246 	synchronize_net();
1247 
1248 	sdata_lock(sdata);
1249 	mutex_lock(&local->mtx);
1250 	ieee80211_link_release_channel(&sdata->deflink);
1251 	mutex_unlock(&local->mtx);
1252 	sdata_unlock(sdata);
1253 
1254 	drv_remove_interface(local, sdata);
1255 
1256 	mutex_destroy(&sdata->wdev.mtx);
1257 	kfree(sdata);
1258 }
1259 
1260 /*
1261  * NOTE: Be very careful when changing this function, it must NOT return
1262  * an error on interface type changes that have been pre-checked, so most
1263  * checks should be in ieee80211_check_concurrent_iface.
1264  */
ieee80211_do_open(struct wireless_dev * wdev,bool coming_up)1265 int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
1266 {
1267 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1268 	struct net_device *dev = wdev->netdev;
1269 	struct ieee80211_local *local = sdata->local;
1270 	u64 changed = 0;
1271 	int res;
1272 	u32 hw_reconf_flags = 0;
1273 
1274 	switch (sdata->vif.type) {
1275 	case NL80211_IFTYPE_AP_VLAN: {
1276 		struct ieee80211_sub_if_data *master;
1277 
1278 		if (!sdata->bss)
1279 			return -ENOLINK;
1280 
1281 		mutex_lock(&local->mtx);
1282 		list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
1283 		mutex_unlock(&local->mtx);
1284 
1285 		master = container_of(sdata->bss,
1286 				      struct ieee80211_sub_if_data, u.ap);
1287 		sdata->control_port_protocol =
1288 			master->control_port_protocol;
1289 		sdata->control_port_no_encrypt =
1290 			master->control_port_no_encrypt;
1291 		sdata->control_port_over_nl80211 =
1292 			master->control_port_over_nl80211;
1293 		sdata->control_port_no_preauth =
1294 			master->control_port_no_preauth;
1295 		sdata->vif.cab_queue = master->vif.cab_queue;
1296 		memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
1297 		       sizeof(sdata->vif.hw_queue));
1298 		sdata->vif.bss_conf.chandef = master->vif.bss_conf.chandef;
1299 
1300 		mutex_lock(&local->key_mtx);
1301 		sdata->crypto_tx_tailroom_needed_cnt +=
1302 			master->crypto_tx_tailroom_needed_cnt;
1303 		mutex_unlock(&local->key_mtx);
1304 
1305 		break;
1306 		}
1307 	case NL80211_IFTYPE_AP:
1308 		sdata->bss = &sdata->u.ap;
1309 		break;
1310 	case NL80211_IFTYPE_MESH_POINT:
1311 	case NL80211_IFTYPE_STATION:
1312 	case NL80211_IFTYPE_MONITOR:
1313 	case NL80211_IFTYPE_ADHOC:
1314 	case NL80211_IFTYPE_P2P_DEVICE:
1315 	case NL80211_IFTYPE_OCB:
1316 	case NL80211_IFTYPE_NAN:
1317 		/* no special treatment */
1318 		break;
1319 	case NL80211_IFTYPE_UNSPECIFIED:
1320 	case NUM_NL80211_IFTYPES:
1321 	case NL80211_IFTYPE_P2P_CLIENT:
1322 	case NL80211_IFTYPE_P2P_GO:
1323 	case NL80211_IFTYPE_WDS:
1324 		/* cannot happen */
1325 		WARN_ON(1);
1326 		break;
1327 	}
1328 
1329 	if (local->open_count == 0) {
1330 		/* here we can consider everything in good order (again) */
1331 		local->reconfig_failure = false;
1332 
1333 		res = drv_start(local);
1334 		if (res)
1335 			goto err_del_bss;
1336 		/* we're brought up, everything changes */
1337 		hw_reconf_flags = ~0;
1338 		ieee80211_led_radio(local, true);
1339 		ieee80211_mod_tpt_led_trig(local,
1340 					   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1341 	}
1342 
1343 	/*
1344 	 * Copy the hopefully now-present MAC address to
1345 	 * this interface, if it has the special null one.
1346 	 */
1347 	if (dev && is_zero_ether_addr(dev->dev_addr)) {
1348 		eth_hw_addr_set(dev, local->hw.wiphy->perm_addr);
1349 		memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
1350 
1351 		if (!is_valid_ether_addr(dev->dev_addr)) {
1352 			res = -EADDRNOTAVAIL;
1353 			goto err_stop;
1354 		}
1355 	}
1356 
1357 	switch (sdata->vif.type) {
1358 	case NL80211_IFTYPE_AP_VLAN:
1359 		/* no need to tell driver, but set carrier and chanctx */
1360 		if (sdata->bss->active) {
1361 			ieee80211_link_vlan_copy_chanctx(&sdata->deflink);
1362 			netif_carrier_on(dev);
1363 			ieee80211_set_vif_encap_ops(sdata);
1364 		} else {
1365 			netif_carrier_off(dev);
1366 		}
1367 		break;
1368 	case NL80211_IFTYPE_MONITOR:
1369 		if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
1370 			local->cooked_mntrs++;
1371 			break;
1372 		}
1373 
1374 		if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1375 			res = drv_add_interface(local, sdata);
1376 			if (res)
1377 				goto err_stop;
1378 		} else if (local->monitors == 0 && local->open_count == 0) {
1379 			res = ieee80211_add_virtual_monitor(local);
1380 			if (res)
1381 				goto err_stop;
1382 		}
1383 
1384 		/* must be before the call to ieee80211_configure_filter */
1385 		local->monitors++;
1386 		if (local->monitors == 1) {
1387 			local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
1388 			hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
1389 		}
1390 
1391 		ieee80211_adjust_monitor_flags(sdata, 1);
1392 		ieee80211_configure_filter(local);
1393 		ieee80211_recalc_offload(local);
1394 		mutex_lock(&local->mtx);
1395 		ieee80211_recalc_idle(local);
1396 		mutex_unlock(&local->mtx);
1397 
1398 		netif_carrier_on(dev);
1399 		break;
1400 	default:
1401 		if (coming_up) {
1402 			ieee80211_del_virtual_monitor(local);
1403 			ieee80211_set_sdata_offload_flags(sdata);
1404 
1405 			res = drv_add_interface(local, sdata);
1406 			if (res)
1407 				goto err_stop;
1408 
1409 			ieee80211_set_vif_encap_ops(sdata);
1410 			res = ieee80211_check_queues(sdata,
1411 				ieee80211_vif_type_p2p(&sdata->vif));
1412 			if (res)
1413 				goto err_del_interface;
1414 		}
1415 
1416 		if (sdata->vif.type == NL80211_IFTYPE_AP) {
1417 			local->fif_pspoll++;
1418 			local->fif_probe_req++;
1419 
1420 			ieee80211_configure_filter(local);
1421 		} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1422 			local->fif_probe_req++;
1423 		}
1424 
1425 		if (sdata->vif.probe_req_reg)
1426 			drv_config_iface_filter(local, sdata,
1427 						FIF_PROBE_REQ,
1428 						FIF_PROBE_REQ);
1429 
1430 		if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1431 		    sdata->vif.type != NL80211_IFTYPE_NAN)
1432 			changed |= ieee80211_reset_erp_info(sdata);
1433 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1434 						  changed);
1435 
1436 		switch (sdata->vif.type) {
1437 		case NL80211_IFTYPE_STATION:
1438 		case NL80211_IFTYPE_ADHOC:
1439 		case NL80211_IFTYPE_AP:
1440 		case NL80211_IFTYPE_MESH_POINT:
1441 		case NL80211_IFTYPE_OCB:
1442 			netif_carrier_off(dev);
1443 			break;
1444 		case NL80211_IFTYPE_P2P_DEVICE:
1445 		case NL80211_IFTYPE_NAN:
1446 			break;
1447 		default:
1448 			/* not reached */
1449 			WARN_ON(1);
1450 		}
1451 
1452 		/*
1453 		 * Set default queue parameters so drivers don't
1454 		 * need to initialise the hardware if the hardware
1455 		 * doesn't start up with sane defaults.
1456 		 * Enable QoS for anything but station interfaces.
1457 		 */
1458 		ieee80211_set_wmm_default(&sdata->deflink, true,
1459 			sdata->vif.type != NL80211_IFTYPE_STATION);
1460 	}
1461 
1462 	switch (sdata->vif.type) {
1463 	case NL80211_IFTYPE_P2P_DEVICE:
1464 		rcu_assign_pointer(local->p2p_sdata, sdata);
1465 		break;
1466 	case NL80211_IFTYPE_MONITOR:
1467 		if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
1468 			break;
1469 		list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list);
1470 		break;
1471 	default:
1472 		break;
1473 	}
1474 
1475 	/*
1476 	 * set_multicast_list will be invoked by the networking core
1477 	 * which will check whether any increments here were done in
1478 	 * error and sync them down to the hardware as filter flags.
1479 	 */
1480 	if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
1481 		atomic_inc(&local->iff_allmultis);
1482 
1483 	if (coming_up)
1484 		local->open_count++;
1485 
1486 	if (hw_reconf_flags)
1487 		ieee80211_hw_config(local, hw_reconf_flags);
1488 
1489 	ieee80211_recalc_ps(local);
1490 
1491 	set_bit(SDATA_STATE_RUNNING, &sdata->state);
1492 
1493 	return 0;
1494  err_del_interface:
1495 	drv_remove_interface(local, sdata);
1496  err_stop:
1497 	if (!local->open_count)
1498 		drv_stop(local);
1499  err_del_bss:
1500 	sdata->bss = NULL;
1501 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1502 		mutex_lock(&local->mtx);
1503 		list_del(&sdata->u.vlan.list);
1504 		mutex_unlock(&local->mtx);
1505 	}
1506 	/* might already be clear but that doesn't matter */
1507 	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1508 	return res;
1509 }
1510 
ieee80211_if_free(struct net_device * dev)1511 static void ieee80211_if_free(struct net_device *dev)
1512 {
1513 	free_percpu(dev->tstats);
1514 }
1515 
ieee80211_if_setup(struct net_device * dev)1516 static void ieee80211_if_setup(struct net_device *dev)
1517 {
1518 	ether_setup(dev);
1519 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1520 	dev->priv_flags |= IFF_NO_QUEUE;
1521 	dev->netdev_ops = &ieee80211_dataif_ops;
1522 	dev->needs_free_netdev = true;
1523 	dev->priv_destructor = ieee80211_if_free;
1524 }
1525 
ieee80211_iface_process_skb(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1526 static void ieee80211_iface_process_skb(struct ieee80211_local *local,
1527 					struct ieee80211_sub_if_data *sdata,
1528 					struct sk_buff *skb)
1529 {
1530 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1531 
1532 	if (ieee80211_is_action(mgmt->frame_control) &&
1533 	    mgmt->u.action.category == WLAN_CATEGORY_BACK) {
1534 		struct sta_info *sta;
1535 		int len = skb->len;
1536 
1537 		mutex_lock(&local->sta_mtx);
1538 		sta = sta_info_get_bss(sdata, mgmt->sa);
1539 		if (sta) {
1540 			switch (mgmt->u.action.u.addba_req.action_code) {
1541 			case WLAN_ACTION_ADDBA_REQ:
1542 				ieee80211_process_addba_request(local, sta,
1543 								mgmt, len);
1544 				break;
1545 			case WLAN_ACTION_ADDBA_RESP:
1546 				ieee80211_process_addba_resp(local, sta,
1547 							     mgmt, len);
1548 				break;
1549 			case WLAN_ACTION_DELBA:
1550 				ieee80211_process_delba(sdata, sta,
1551 							mgmt, len);
1552 				break;
1553 			default:
1554 				WARN_ON(1);
1555 				break;
1556 			}
1557 		}
1558 		mutex_unlock(&local->sta_mtx);
1559 	} else if (ieee80211_is_action(mgmt->frame_control) &&
1560 		   mgmt->u.action.category == WLAN_CATEGORY_VHT) {
1561 		switch (mgmt->u.action.u.vht_group_notif.action_code) {
1562 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
1563 			struct ieee80211_rx_status *status;
1564 			enum nl80211_band band;
1565 			struct sta_info *sta;
1566 			u8 opmode;
1567 
1568 			status = IEEE80211_SKB_RXCB(skb);
1569 			band = status->band;
1570 			opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
1571 
1572 			mutex_lock(&local->sta_mtx);
1573 			sta = sta_info_get_bss(sdata, mgmt->sa);
1574 
1575 			if (sta)
1576 				ieee80211_vht_handle_opmode(sdata,
1577 							    &sta->deflink,
1578 							    opmode, band);
1579 
1580 			mutex_unlock(&local->sta_mtx);
1581 			break;
1582 		}
1583 		case WLAN_VHT_ACTION_GROUPID_MGMT:
1584 			ieee80211_process_mu_groups(sdata, &sdata->deflink,
1585 						    mgmt);
1586 			break;
1587 		default:
1588 			WARN_ON(1);
1589 			break;
1590 		}
1591 	} else if (ieee80211_is_action(mgmt->frame_control) &&
1592 		   mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1593 		switch (mgmt->u.action.u.s1g.action_code) {
1594 		case WLAN_S1G_TWT_TEARDOWN:
1595 		case WLAN_S1G_TWT_SETUP:
1596 			ieee80211_s1g_rx_twt_action(sdata, skb);
1597 			break;
1598 		default:
1599 			break;
1600 		}
1601 	} else if (ieee80211_is_ext(mgmt->frame_control)) {
1602 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
1603 			ieee80211_sta_rx_queued_ext(sdata, skb);
1604 		else
1605 			WARN_ON(1);
1606 	} else if (ieee80211_is_data_qos(mgmt->frame_control)) {
1607 		struct ieee80211_hdr *hdr = (void *)mgmt;
1608 		struct sta_info *sta;
1609 
1610 		/*
1611 		 * So the frame isn't mgmt, but frame_control
1612 		 * is at the right place anyway, of course, so
1613 		 * the if statement is correct.
1614 		 *
1615 		 * Warn if we have other data frame types here,
1616 		 * they must not get here.
1617 		 */
1618 		WARN_ON(hdr->frame_control &
1619 				cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
1620 		WARN_ON(!(hdr->seq_ctrl &
1621 				cpu_to_le16(IEEE80211_SCTL_FRAG)));
1622 		/*
1623 		 * This was a fragment of a frame, received while
1624 		 * a block-ack session was active. That cannot be
1625 		 * right, so terminate the session.
1626 		 */
1627 		mutex_lock(&local->sta_mtx);
1628 		sta = sta_info_get_bss(sdata, mgmt->sa);
1629 		if (sta) {
1630 			u16 tid = ieee80211_get_tid(hdr);
1631 
1632 			__ieee80211_stop_rx_ba_session(
1633 				sta, tid, WLAN_BACK_RECIPIENT,
1634 				WLAN_REASON_QSTA_REQUIRE_SETUP,
1635 				true);
1636 		}
1637 		mutex_unlock(&local->sta_mtx);
1638 	} else switch (sdata->vif.type) {
1639 	case NL80211_IFTYPE_STATION:
1640 		ieee80211_sta_rx_queued_mgmt(sdata, skb);
1641 		break;
1642 	case NL80211_IFTYPE_ADHOC:
1643 		ieee80211_ibss_rx_queued_mgmt(sdata, skb);
1644 		break;
1645 	case NL80211_IFTYPE_MESH_POINT:
1646 		if (!ieee80211_vif_is_mesh(&sdata->vif))
1647 			break;
1648 		ieee80211_mesh_rx_queued_mgmt(sdata, skb);
1649 		break;
1650 	default:
1651 		WARN(1, "frame for unexpected interface type");
1652 		break;
1653 	}
1654 }
1655 
ieee80211_iface_process_status(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1656 static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata,
1657 					   struct sk_buff *skb)
1658 {
1659 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1660 
1661 	if (ieee80211_is_action(mgmt->frame_control) &&
1662 	    mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1663 		switch (mgmt->u.action.u.s1g.action_code) {
1664 		case WLAN_S1G_TWT_TEARDOWN:
1665 		case WLAN_S1G_TWT_SETUP:
1666 			ieee80211_s1g_status_twt_action(sdata, skb);
1667 			break;
1668 		default:
1669 			break;
1670 		}
1671 	}
1672 }
1673 
ieee80211_iface_work(struct wiphy * wiphy,struct wiphy_work * work)1674 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work)
1675 {
1676 	struct ieee80211_sub_if_data *sdata =
1677 		container_of(work, struct ieee80211_sub_if_data, work);
1678 	struct ieee80211_local *local = sdata->local;
1679 	struct sk_buff *skb;
1680 
1681 	if (!ieee80211_sdata_running(sdata))
1682 		return;
1683 
1684 	if (test_bit(SCAN_SW_SCANNING, &local->scanning))
1685 		return;
1686 
1687 	if (!ieee80211_can_run_worker(local))
1688 		return;
1689 
1690 	/* first process frames */
1691 	while ((skb = skb_dequeue(&sdata->skb_queue))) {
1692 		kcov_remote_start_common(skb_get_kcov_handle(skb));
1693 
1694 		if (skb->protocol == cpu_to_be16(ETH_P_TDLS))
1695 			ieee80211_process_tdls_channel_switch(sdata, skb);
1696 		else
1697 			ieee80211_iface_process_skb(local, sdata, skb);
1698 
1699 		kfree_skb(skb);
1700 		kcov_remote_stop();
1701 	}
1702 
1703 	/* process status queue */
1704 	while ((skb = skb_dequeue(&sdata->status_queue))) {
1705 		kcov_remote_start_common(skb_get_kcov_handle(skb));
1706 
1707 		ieee80211_iface_process_status(sdata, skb);
1708 		kfree_skb(skb);
1709 
1710 		kcov_remote_stop();
1711 	}
1712 
1713 	/* then other type-dependent work */
1714 	switch (sdata->vif.type) {
1715 	case NL80211_IFTYPE_STATION:
1716 		ieee80211_sta_work(sdata);
1717 		break;
1718 	case NL80211_IFTYPE_ADHOC:
1719 		ieee80211_ibss_work(sdata);
1720 		break;
1721 	case NL80211_IFTYPE_MESH_POINT:
1722 		if (!ieee80211_vif_is_mesh(&sdata->vif))
1723 			break;
1724 		ieee80211_mesh_work(sdata);
1725 		break;
1726 	case NL80211_IFTYPE_OCB:
1727 		ieee80211_ocb_work(sdata);
1728 		break;
1729 	default:
1730 		break;
1731 	}
1732 }
1733 
ieee80211_recalc_smps_work(struct work_struct * work)1734 static void ieee80211_recalc_smps_work(struct work_struct *work)
1735 {
1736 	struct ieee80211_sub_if_data *sdata =
1737 		container_of(work, struct ieee80211_sub_if_data, recalc_smps);
1738 
1739 	ieee80211_recalc_smps(sdata, &sdata->deflink);
1740 }
1741 
ieee80211_activate_links_work(struct work_struct * work)1742 static void ieee80211_activate_links_work(struct work_struct *work)
1743 {
1744 	struct ieee80211_sub_if_data *sdata =
1745 		container_of(work, struct ieee80211_sub_if_data,
1746 			     activate_links_work);
1747 
1748 	ieee80211_set_active_links(&sdata->vif, sdata->desired_active_links);
1749 }
1750 
1751 /*
1752  * Helper function to initialise an interface to a specific type.
1753  */
ieee80211_setup_sdata(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1754 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1755 				  enum nl80211_iftype type)
1756 {
1757 	static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
1758 						    0xff, 0xff, 0xff};
1759 
1760 	/* clear type-dependent unions */
1761 	memset(&sdata->u, 0, sizeof(sdata->u));
1762 	memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u));
1763 
1764 	/* and set some type-dependent values */
1765 	sdata->vif.type = type;
1766 	sdata->vif.p2p = false;
1767 	sdata->wdev.iftype = type;
1768 
1769 	sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
1770 	sdata->control_port_no_encrypt = false;
1771 	sdata->control_port_over_nl80211 = false;
1772 	sdata->control_port_no_preauth = false;
1773 	sdata->vif.cfg.idle = true;
1774 	sdata->vif.bss_conf.txpower = INT_MIN; /* unset */
1775 
1776 	sdata->noack_map = 0;
1777 
1778 	/* only monitor/p2p-device differ */
1779 	if (sdata->dev) {
1780 		sdata->dev->netdev_ops = &ieee80211_dataif_ops;
1781 		sdata->dev->type = ARPHRD_ETHER;
1782 	}
1783 
1784 	skb_queue_head_init(&sdata->skb_queue);
1785 	skb_queue_head_init(&sdata->status_queue);
1786 	wiphy_work_init(&sdata->work, ieee80211_iface_work);
1787 	INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
1788 	INIT_WORK(&sdata->activate_links_work, ieee80211_activate_links_work);
1789 
1790 	switch (type) {
1791 	case NL80211_IFTYPE_P2P_GO:
1792 		type = NL80211_IFTYPE_AP;
1793 		sdata->vif.type = type;
1794 		sdata->vif.p2p = true;
1795 		fallthrough;
1796 	case NL80211_IFTYPE_AP:
1797 		skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1798 		INIT_LIST_HEAD(&sdata->u.ap.vlans);
1799 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1800 		break;
1801 	case NL80211_IFTYPE_P2P_CLIENT:
1802 		type = NL80211_IFTYPE_STATION;
1803 		sdata->vif.type = type;
1804 		sdata->vif.p2p = true;
1805 		fallthrough;
1806 	case NL80211_IFTYPE_STATION:
1807 		sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid;
1808 		ieee80211_sta_setup_sdata(sdata);
1809 		break;
1810 	case NL80211_IFTYPE_OCB:
1811 		sdata->vif.bss_conf.bssid = bssid_wildcard;
1812 		ieee80211_ocb_setup_sdata(sdata);
1813 		break;
1814 	case NL80211_IFTYPE_ADHOC:
1815 		sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
1816 		ieee80211_ibss_setup_sdata(sdata);
1817 		break;
1818 	case NL80211_IFTYPE_MESH_POINT:
1819 		if (ieee80211_vif_is_mesh(&sdata->vif))
1820 			ieee80211_mesh_init_sdata(sdata);
1821 		break;
1822 	case NL80211_IFTYPE_MONITOR:
1823 		sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
1824 		sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
1825 		sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
1826 				      MONITOR_FLAG_OTHER_BSS;
1827 		break;
1828 	case NL80211_IFTYPE_NAN:
1829 		idr_init(&sdata->u.nan.function_inst_ids);
1830 		spin_lock_init(&sdata->u.nan.func_lock);
1831 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1832 		break;
1833 	case NL80211_IFTYPE_AP_VLAN:
1834 	case NL80211_IFTYPE_P2P_DEVICE:
1835 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1836 		break;
1837 	case NL80211_IFTYPE_UNSPECIFIED:
1838 	case NL80211_IFTYPE_WDS:
1839 	case NUM_NL80211_IFTYPES:
1840 		WARN_ON(1);
1841 		break;
1842 	}
1843 
1844 	/* need to do this after the switch so vif.type is correct */
1845 	ieee80211_link_setup(&sdata->deflink);
1846 
1847 	ieee80211_debugfs_add_netdev(sdata);
1848 }
1849 
ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1850 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
1851 					   enum nl80211_iftype type)
1852 {
1853 	struct ieee80211_local *local = sdata->local;
1854 	int ret, err;
1855 	enum nl80211_iftype internal_type = type;
1856 	bool p2p = false;
1857 
1858 	ASSERT_RTNL();
1859 
1860 	if (!local->ops->change_interface)
1861 		return -EBUSY;
1862 
1863 	/* for now, don't support changing while links exist */
1864 	if (ieee80211_vif_is_mld(&sdata->vif))
1865 		return -EBUSY;
1866 
1867 	switch (sdata->vif.type) {
1868 	case NL80211_IFTYPE_AP:
1869 		if (!list_empty(&sdata->u.ap.vlans))
1870 			return -EBUSY;
1871 		break;
1872 	case NL80211_IFTYPE_STATION:
1873 	case NL80211_IFTYPE_ADHOC:
1874 	case NL80211_IFTYPE_OCB:
1875 		/*
1876 		 * Could maybe also all others here?
1877 		 * Just not sure how that interacts
1878 		 * with the RX/config path e.g. for
1879 		 * mesh.
1880 		 */
1881 		break;
1882 	default:
1883 		return -EBUSY;
1884 	}
1885 
1886 	switch (type) {
1887 	case NL80211_IFTYPE_AP:
1888 	case NL80211_IFTYPE_STATION:
1889 	case NL80211_IFTYPE_ADHOC:
1890 	case NL80211_IFTYPE_OCB:
1891 		/*
1892 		 * Could probably support everything
1893 		 * but here.
1894 		 */
1895 		break;
1896 	case NL80211_IFTYPE_P2P_CLIENT:
1897 		p2p = true;
1898 		internal_type = NL80211_IFTYPE_STATION;
1899 		break;
1900 	case NL80211_IFTYPE_P2P_GO:
1901 		p2p = true;
1902 		internal_type = NL80211_IFTYPE_AP;
1903 		break;
1904 	default:
1905 		return -EBUSY;
1906 	}
1907 
1908 	ret = ieee80211_check_concurrent_iface(sdata, internal_type);
1909 	if (ret)
1910 		return ret;
1911 
1912 	ieee80211_stop_vif_queues(local, sdata,
1913 				  IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1914 	/* do_stop will synchronize_rcu() first thing */
1915 	ieee80211_do_stop(sdata, false);
1916 
1917 	ieee80211_teardown_sdata(sdata);
1918 
1919 	ieee80211_set_sdata_offload_flags(sdata);
1920 	ret = drv_change_interface(local, sdata, internal_type, p2p);
1921 	if (ret)
1922 		type = ieee80211_vif_type_p2p(&sdata->vif);
1923 
1924 	/*
1925 	 * Ignore return value here, there's not much we can do since
1926 	 * the driver changed the interface type internally already.
1927 	 * The warnings will hopefully make driver authors fix it :-)
1928 	 */
1929 	ieee80211_check_queues(sdata, type);
1930 
1931 	ieee80211_setup_sdata(sdata, type);
1932 	ieee80211_set_vif_encap_ops(sdata);
1933 
1934 	err = ieee80211_do_open(&sdata->wdev, false);
1935 	WARN(err, "type change: do_open returned %d", err);
1936 
1937 	ieee80211_wake_vif_queues(local, sdata,
1938 				  IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1939 	return ret;
1940 }
1941 
ieee80211_if_change_type(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1942 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
1943 			     enum nl80211_iftype type)
1944 {
1945 	int ret;
1946 
1947 	ASSERT_RTNL();
1948 
1949 	if (type == ieee80211_vif_type_p2p(&sdata->vif))
1950 		return 0;
1951 
1952 	if (ieee80211_sdata_running(sdata)) {
1953 		ret = ieee80211_runtime_change_iftype(sdata, type);
1954 		if (ret)
1955 			return ret;
1956 	} else {
1957 		/* Purge and reset type-dependent state. */
1958 		ieee80211_teardown_sdata(sdata);
1959 		ieee80211_setup_sdata(sdata, type);
1960 	}
1961 
1962 	/* reset some values that shouldn't be kept across type changes */
1963 	if (type == NL80211_IFTYPE_STATION)
1964 		sdata->u.mgd.use_4addr = false;
1965 
1966 	return 0;
1967 }
1968 
ieee80211_assign_perm_addr(struct ieee80211_local * local,u8 * perm_addr,enum nl80211_iftype type)1969 static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
1970 				       u8 *perm_addr, enum nl80211_iftype type)
1971 {
1972 	struct ieee80211_sub_if_data *sdata;
1973 	u64 mask, start, addr, val, inc;
1974 	u8 *m;
1975 	u8 tmp_addr[ETH_ALEN];
1976 	int i;
1977 
1978 	/* default ... something at least */
1979 	memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1980 
1981 	if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
1982 	    local->hw.wiphy->n_addresses <= 1)
1983 		return;
1984 
1985 	mutex_lock(&local->iflist_mtx);
1986 
1987 	switch (type) {
1988 	case NL80211_IFTYPE_MONITOR:
1989 		/* doesn't matter */
1990 		break;
1991 	case NL80211_IFTYPE_AP_VLAN:
1992 		/* match up with an AP interface */
1993 		list_for_each_entry(sdata, &local->interfaces, list) {
1994 			if (sdata->vif.type != NL80211_IFTYPE_AP)
1995 				continue;
1996 			memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
1997 			break;
1998 		}
1999 		/* keep default if no AP interface present */
2000 		break;
2001 	case NL80211_IFTYPE_P2P_CLIENT:
2002 	case NL80211_IFTYPE_P2P_GO:
2003 		if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
2004 			list_for_each_entry(sdata, &local->interfaces, list) {
2005 				if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
2006 					continue;
2007 				if (!ieee80211_sdata_running(sdata))
2008 					continue;
2009 				memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
2010 				goto out_unlock;
2011 			}
2012 		}
2013 		fallthrough;
2014 	default:
2015 		/* assign a new address if possible -- try n_addresses first */
2016 		for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
2017 			bool used = false;
2018 
2019 			list_for_each_entry(sdata, &local->interfaces, list) {
2020 				if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
2021 						     sdata->vif.addr)) {
2022 					used = true;
2023 					break;
2024 				}
2025 			}
2026 
2027 			if (!used) {
2028 				memcpy(perm_addr,
2029 				       local->hw.wiphy->addresses[i].addr,
2030 				       ETH_ALEN);
2031 				break;
2032 			}
2033 		}
2034 
2035 		/* try mask if available */
2036 		if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
2037 			break;
2038 
2039 		m = local->hw.wiphy->addr_mask;
2040 		mask =	((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2041 			((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2042 			((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2043 
2044 		if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
2045 			/* not a contiguous mask ... not handled now! */
2046 			pr_info("not contiguous\n");
2047 			break;
2048 		}
2049 
2050 		/*
2051 		 * Pick address of existing interface in case user changed
2052 		 * MAC address manually, default to perm_addr.
2053 		 */
2054 		m = local->hw.wiphy->perm_addr;
2055 		list_for_each_entry(sdata, &local->interfaces, list) {
2056 			if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2057 				continue;
2058 			m = sdata->vif.addr;
2059 			break;
2060 		}
2061 		start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2062 			((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2063 			((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2064 
2065 		inc = 1ULL<<__ffs64(mask);
2066 		val = (start & mask);
2067 		addr = (start & ~mask) | (val & mask);
2068 		do {
2069 			bool used = false;
2070 
2071 			tmp_addr[5] = addr >> 0*8;
2072 			tmp_addr[4] = addr >> 1*8;
2073 			tmp_addr[3] = addr >> 2*8;
2074 			tmp_addr[2] = addr >> 3*8;
2075 			tmp_addr[1] = addr >> 4*8;
2076 			tmp_addr[0] = addr >> 5*8;
2077 
2078 			val += inc;
2079 
2080 			list_for_each_entry(sdata, &local->interfaces, list) {
2081 				if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
2082 					used = true;
2083 					break;
2084 				}
2085 			}
2086 
2087 			if (!used) {
2088 				memcpy(perm_addr, tmp_addr, ETH_ALEN);
2089 				break;
2090 			}
2091 			addr = (start & ~mask) | (val & mask);
2092 		} while (addr != start);
2093 
2094 		break;
2095 	}
2096 
2097  out_unlock:
2098 	mutex_unlock(&local->iflist_mtx);
2099 }
2100 
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)2101 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
2102 		     unsigned char name_assign_type,
2103 		     struct wireless_dev **new_wdev, enum nl80211_iftype type,
2104 		     struct vif_params *params)
2105 {
2106 	struct net_device *ndev = NULL;
2107 	struct ieee80211_sub_if_data *sdata = NULL;
2108 	struct txq_info *txqi;
2109 	int ret, i;
2110 
2111 	ASSERT_RTNL();
2112 
2113 	if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
2114 		struct wireless_dev *wdev;
2115 
2116 		sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
2117 				GFP_KERNEL);
2118 		if (!sdata)
2119 			return -ENOMEM;
2120 		wdev = &sdata->wdev;
2121 
2122 		sdata->dev = NULL;
2123 		strscpy(sdata->name, name, IFNAMSIZ);
2124 		ieee80211_assign_perm_addr(local, wdev->address, type);
2125 		memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
2126 		ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2127 	} else {
2128 		int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
2129 				 sizeof(void *));
2130 		int txq_size = 0;
2131 
2132 		if (type != NL80211_IFTYPE_AP_VLAN &&
2133 		    (type != NL80211_IFTYPE_MONITOR ||
2134 		     (params->flags & MONITOR_FLAG_ACTIVE)))
2135 			txq_size += sizeof(struct txq_info) +
2136 				    local->hw.txq_data_size;
2137 
2138 		ndev = alloc_netdev_mqs(size + txq_size,
2139 					name, name_assign_type,
2140 					ieee80211_if_setup, 1, 1);
2141 		if (!ndev)
2142 			return -ENOMEM;
2143 
2144 		dev_net_set(ndev, wiphy_net(local->hw.wiphy));
2145 
2146 		ndev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2147 		if (!ndev->tstats) {
2148 			free_netdev(ndev);
2149 			return -ENOMEM;
2150 		}
2151 
2152 		ndev->needed_headroom = local->tx_headroom +
2153 					4*6 /* four MAC addresses */
2154 					+ 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
2155 					+ 6 /* mesh */
2156 					+ 8 /* rfc1042/bridge tunnel */
2157 					- ETH_HLEN /* ethernet hard_header_len */
2158 					+ IEEE80211_ENCRYPT_HEADROOM;
2159 		ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
2160 
2161 		ret = dev_alloc_name(ndev, ndev->name);
2162 		if (ret < 0) {
2163 			ieee80211_if_free(ndev);
2164 			free_netdev(ndev);
2165 			return ret;
2166 		}
2167 
2168 		ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
2169 		if (is_valid_ether_addr(params->macaddr))
2170 			eth_hw_addr_set(ndev, params->macaddr);
2171 		else
2172 			eth_hw_addr_set(ndev, ndev->perm_addr);
2173 		SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
2174 
2175 		/* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
2176 		sdata = netdev_priv(ndev);
2177 		ndev->ieee80211_ptr = &sdata->wdev;
2178 		memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
2179 		ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2180 		memcpy(sdata->name, ndev->name, IFNAMSIZ);
2181 
2182 		if (txq_size) {
2183 			txqi = netdev_priv(ndev) + size;
2184 			ieee80211_txq_init(sdata, NULL, txqi, 0);
2185 		}
2186 
2187 		sdata->dev = ndev;
2188 	}
2189 
2190 	/* initialise type-independent data */
2191 	sdata->wdev.wiphy = local->hw.wiphy;
2192 
2193 	ieee80211_sdata_init(local, sdata);
2194 
2195 	ieee80211_init_frag_cache(&sdata->frags);
2196 
2197 	INIT_LIST_HEAD(&sdata->key_list);
2198 
2199 	INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
2200 			  ieee80211_delayed_tailroom_dec);
2201 
2202 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
2203 		struct ieee80211_supported_band *sband;
2204 		sband = local->hw.wiphy->bands[i];
2205 		sdata->rc_rateidx_mask[i] =
2206 			sband ? (1 << sband->n_bitrates) - 1 : 0;
2207 		if (sband) {
2208 			__le16 cap;
2209 			u16 *vht_rate_mask;
2210 
2211 			memcpy(sdata->rc_rateidx_mcs_mask[i],
2212 			       sband->ht_cap.mcs.rx_mask,
2213 			       sizeof(sdata->rc_rateidx_mcs_mask[i]));
2214 
2215 			cap = sband->vht_cap.vht_mcs.rx_mcs_map;
2216 			vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
2217 			ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
2218 		} else {
2219 			memset(sdata->rc_rateidx_mcs_mask[i], 0,
2220 			       sizeof(sdata->rc_rateidx_mcs_mask[i]));
2221 			memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
2222 			       sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
2223 		}
2224 	}
2225 
2226 	ieee80211_set_default_queues(sdata);
2227 
2228 	sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2229 	sdata->deflink.user_power_level = local->user_power_level;
2230 
2231 	/* setup type-dependent data */
2232 	ieee80211_setup_sdata(sdata, type);
2233 
2234 	if (ndev) {
2235 		ndev->ieee80211_ptr->use_4addr = params->use_4addr;
2236 		if (type == NL80211_IFTYPE_STATION)
2237 			sdata->u.mgd.use_4addr = params->use_4addr;
2238 
2239 		ndev->features |= local->hw.netdev_features;
2240 		ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2241 		ndev->hw_features |= ndev->features &
2242 					MAC80211_SUPPORTED_FEATURES_TX;
2243 		sdata->vif.netdev_features = local->hw.netdev_features;
2244 
2245 		netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
2246 
2247 		/* MTU range is normally 256 - 2304, where the upper limit is
2248 		 * the maximum MSDU size. Monitor interfaces send and receive
2249 		 * MPDU and A-MSDU frames which may be much larger so we do
2250 		 * not impose an upper limit in that case.
2251 		 */
2252 		ndev->min_mtu = 256;
2253 		if (type == NL80211_IFTYPE_MONITOR)
2254 			ndev->max_mtu = 0;
2255 		else
2256 			ndev->max_mtu = local->hw.max_mtu;
2257 
2258 		ret = cfg80211_register_netdevice(ndev);
2259 		if (ret) {
2260 			free_netdev(ndev);
2261 			return ret;
2262 		}
2263 	}
2264 
2265 	mutex_lock(&local->iflist_mtx);
2266 	list_add_tail_rcu(&sdata->list, &local->interfaces);
2267 	mutex_unlock(&local->iflist_mtx);
2268 
2269 	if (new_wdev)
2270 		*new_wdev = &sdata->wdev;
2271 
2272 	return 0;
2273 }
2274 
ieee80211_if_remove(struct ieee80211_sub_if_data * sdata)2275 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
2276 {
2277 	ASSERT_RTNL();
2278 
2279 	mutex_lock(&sdata->local->iflist_mtx);
2280 	list_del_rcu(&sdata->list);
2281 	mutex_unlock(&sdata->local->iflist_mtx);
2282 
2283 	if (sdata->vif.txq)
2284 		ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
2285 
2286 	synchronize_rcu();
2287 
2288 	cfg80211_unregister_wdev(&sdata->wdev);
2289 
2290 	if (!sdata->dev) {
2291 		ieee80211_teardown_sdata(sdata);
2292 		kfree(sdata);
2293 	}
2294 }
2295 
ieee80211_sdata_stop(struct ieee80211_sub_if_data * sdata)2296 void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
2297 {
2298 	if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
2299 		return;
2300 	ieee80211_do_stop(sdata, true);
2301 }
2302 
ieee80211_remove_interfaces(struct ieee80211_local * local)2303 void ieee80211_remove_interfaces(struct ieee80211_local *local)
2304 {
2305 	struct ieee80211_sub_if_data *sdata, *tmp;
2306 	LIST_HEAD(unreg_list);
2307 
2308 	ASSERT_RTNL();
2309 
2310 	/* Before destroying the interfaces, make sure they're all stopped so
2311 	 * that the hardware is stopped. Otherwise, the driver might still be
2312 	 * iterating the interfaces during the shutdown, e.g. from a worker
2313 	 * or from RX processing or similar, and if it does so (using atomic
2314 	 * iteration) while we're manipulating the list, the iteration will
2315 	 * crash.
2316 	 *
2317 	 * After this, the hardware should be stopped and the driver should
2318 	 * have stopped all of its activities, so that we can do RCU-unaware
2319 	 * manipulations of the interface list below.
2320 	 */
2321 	cfg80211_shutdown_all_interfaces(local->hw.wiphy);
2322 
2323 	WARN(local->open_count, "%s: open count remains %d\n",
2324 	     wiphy_name(local->hw.wiphy), local->open_count);
2325 
2326 	ieee80211_txq_teardown_flows(local);
2327 
2328 	mutex_lock(&local->iflist_mtx);
2329 	list_splice_init(&local->interfaces, &unreg_list);
2330 	mutex_unlock(&local->iflist_mtx);
2331 
2332 	wiphy_lock(local->hw.wiphy);
2333 	list_for_each_entry_safe(sdata, tmp, &unreg_list, list) {
2334 		bool netdev = sdata->dev;
2335 
2336 		/*
2337 		 * Remove IP addresses explicitly, since the notifier will
2338 		 * skip the callbacks if wdev->registered is false, since
2339 		 * we can't acquire the wiphy_lock() again there if already
2340 		 * inside this locked section.
2341 		 */
2342 		sdata_lock(sdata);
2343 		sdata->vif.cfg.arp_addr_cnt = 0;
2344 		if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2345 		    sdata->u.mgd.associated)
2346 			ieee80211_vif_cfg_change_notify(sdata,
2347 							BSS_CHANGED_ARP_FILTER);
2348 		sdata_unlock(sdata);
2349 
2350 		list_del(&sdata->list);
2351 		cfg80211_unregister_wdev(&sdata->wdev);
2352 
2353 		if (!netdev)
2354 			kfree(sdata);
2355 	}
2356 	wiphy_unlock(local->hw.wiphy);
2357 }
2358 
netdev_notify(struct notifier_block * nb,unsigned long state,void * ptr)2359 static int netdev_notify(struct notifier_block *nb,
2360 			 unsigned long state, void *ptr)
2361 {
2362 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2363 	struct ieee80211_sub_if_data *sdata;
2364 
2365 	if (state != NETDEV_CHANGENAME)
2366 		return NOTIFY_DONE;
2367 
2368 	if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
2369 		return NOTIFY_DONE;
2370 
2371 	if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
2372 		return NOTIFY_DONE;
2373 
2374 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2375 	memcpy(sdata->name, dev->name, IFNAMSIZ);
2376 	ieee80211_debugfs_rename_netdev(sdata);
2377 
2378 	return NOTIFY_OK;
2379 }
2380 
2381 static struct notifier_block mac80211_netdev_notifier = {
2382 	.notifier_call = netdev_notify,
2383 };
2384 
ieee80211_iface_init(void)2385 int ieee80211_iface_init(void)
2386 {
2387 	return register_netdevice_notifier(&mac80211_netdev_notifier);
2388 }
2389 
ieee80211_iface_exit(void)2390 void ieee80211_iface_exit(void)
2391 {
2392 	unregister_netdevice_notifier(&mac80211_netdev_notifier);
2393 }
2394 
ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data * sdata)2395 void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
2396 {
2397 	if (sdata->vif.type == NL80211_IFTYPE_AP)
2398 		atomic_inc(&sdata->u.ap.num_mcast_sta);
2399 	else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2400 		atomic_inc(&sdata->u.vlan.num_mcast_sta);
2401 }
2402 
ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data * sdata)2403 void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
2404 {
2405 	if (sdata->vif.type == NL80211_IFTYPE_AP)
2406 		atomic_dec(&sdata->u.ap.num_mcast_sta);
2407 	else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2408 		atomic_dec(&sdata->u.vlan.num_mcast_sta);
2409 }
2410