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