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