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