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