xref: /linux/net/mac80211/iface.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
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 	/*
1154 	 * Initialize the default link, so we can use link_id 0 for non-MLD,
1155 	 * and that continues to work for non-MLD-aware drivers that use just
1156 	 * vif.bss_conf instead of vif.link_conf.
1157 	 *
1158 	 * Note that we never change this, so if link ID 0 isn't used in an
1159 	 * MLD connection, we get a separate allocation for it.
1160 	 */
1161 	ieee80211_link_init(sdata, -1, &sdata->deflink, &sdata->vif.bss_conf);
1162 }
1163 
ieee80211_add_virtual_monitor(struct ieee80211_local * local)1164 int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
1165 {
1166 	struct ieee80211_sub_if_data *sdata;
1167 	int ret;
1168 
1169 	ASSERT_RTNL();
1170 	lockdep_assert_wiphy(local->hw.wiphy);
1171 
1172 	if (local->monitor_sdata ||
1173 	    ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
1174 		return 0;
1175 
1176 	sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
1177 	if (!sdata)
1178 		return -ENOMEM;
1179 
1180 	/* set up data */
1181 	sdata->vif.type = NL80211_IFTYPE_MONITOR;
1182 	snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
1183 		 wiphy_name(local->hw.wiphy));
1184 	sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
1185 	sdata->wdev.wiphy = local->hw.wiphy;
1186 
1187 	ieee80211_sdata_init(local, sdata);
1188 
1189 	ieee80211_set_default_queues(sdata);
1190 
1191 	if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
1192 		ret = drv_add_interface(local, sdata);
1193 		if (WARN_ON(ret)) {
1194 			/* ok .. stupid driver, it asked for this! */
1195 			kfree(sdata);
1196 			return ret;
1197 		}
1198 	}
1199 
1200 	set_bit(SDATA_STATE_RUNNING, &sdata->state);
1201 
1202 	ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
1203 	if (ret) {
1204 		kfree(sdata);
1205 		return ret;
1206 	}
1207 
1208 	mutex_lock(&local->iflist_mtx);
1209 	rcu_assign_pointer(local->monitor_sdata, sdata);
1210 	mutex_unlock(&local->iflist_mtx);
1211 
1212 	ret = ieee80211_link_use_channel(&sdata->deflink, &local->monitor_chanreq,
1213 					 IEEE80211_CHANCTX_EXCLUSIVE);
1214 	if (ret) {
1215 		mutex_lock(&local->iflist_mtx);
1216 		RCU_INIT_POINTER(local->monitor_sdata, NULL);
1217 		mutex_unlock(&local->iflist_mtx);
1218 		synchronize_net();
1219 		drv_remove_interface(local, sdata);
1220 		kfree(sdata);
1221 		return ret;
1222 	}
1223 
1224 	skb_queue_head_init(&sdata->skb_queue);
1225 	skb_queue_head_init(&sdata->status_queue);
1226 	wiphy_work_init(&sdata->work, ieee80211_iface_work);
1227 
1228 	return 0;
1229 }
1230 
ieee80211_del_virtual_monitor(struct ieee80211_local * local)1231 void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
1232 {
1233 	struct ieee80211_sub_if_data *sdata;
1234 
1235 	if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
1236 		return;
1237 
1238 	ASSERT_RTNL();
1239 	lockdep_assert_wiphy(local->hw.wiphy);
1240 
1241 	mutex_lock(&local->iflist_mtx);
1242 
1243 	sdata = rcu_dereference_protected(local->monitor_sdata,
1244 					  lockdep_is_held(&local->iflist_mtx));
1245 	if (!sdata) {
1246 		mutex_unlock(&local->iflist_mtx);
1247 		return;
1248 	}
1249 
1250 	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1251 	ieee80211_link_release_channel(&sdata->deflink);
1252 
1253 	if (ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
1254 		drv_remove_interface(local, sdata);
1255 
1256 	RCU_INIT_POINTER(local->monitor_sdata, NULL);
1257 	mutex_unlock(&local->iflist_mtx);
1258 
1259 	synchronize_net();
1260 
1261 	kfree(sdata);
1262 }
1263 
1264 /*
1265  * NOTE: Be very careful when changing this function, it must NOT return
1266  * an error on interface type changes that have been pre-checked, so most
1267  * checks should be in ieee80211_check_concurrent_iface.
1268  */
ieee80211_do_open(struct wireless_dev * wdev,bool coming_up)1269 int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
1270 {
1271 	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1272 	struct net_device *dev = wdev->netdev;
1273 	struct ieee80211_local *local = sdata->local;
1274 	u64 changed = 0;
1275 	int res;
1276 	u32 hw_reconf_flags = 0;
1277 
1278 	lockdep_assert_wiphy(local->hw.wiphy);
1279 
1280 	switch (sdata->vif.type) {
1281 	case NL80211_IFTYPE_AP_VLAN: {
1282 		struct ieee80211_sub_if_data *master;
1283 
1284 		if (!sdata->bss)
1285 			return -ENOLINK;
1286 
1287 		list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
1288 
1289 		master = container_of(sdata->bss,
1290 				      struct ieee80211_sub_if_data, u.ap);
1291 		sdata->control_port_protocol =
1292 			master->control_port_protocol;
1293 		sdata->control_port_no_encrypt =
1294 			master->control_port_no_encrypt;
1295 		sdata->control_port_over_nl80211 =
1296 			master->control_port_over_nl80211;
1297 		sdata->control_port_no_preauth =
1298 			master->control_port_no_preauth;
1299 		sdata->vif.cab_queue = master->vif.cab_queue;
1300 		memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
1301 		       sizeof(sdata->vif.hw_queue));
1302 		sdata->vif.bss_conf.chanreq = master->vif.bss_conf.chanreq;
1303 
1304 		sdata->crypto_tx_tailroom_needed_cnt +=
1305 			master->crypto_tx_tailroom_needed_cnt;
1306 
1307 		ieee80211_apvlan_link_setup(sdata);
1308 
1309 		break;
1310 		}
1311 	case NL80211_IFTYPE_AP:
1312 		sdata->bss = &sdata->u.ap;
1313 		break;
1314 	case NL80211_IFTYPE_MESH_POINT:
1315 	case NL80211_IFTYPE_STATION:
1316 	case NL80211_IFTYPE_MONITOR:
1317 	case NL80211_IFTYPE_ADHOC:
1318 	case NL80211_IFTYPE_P2P_DEVICE:
1319 	case NL80211_IFTYPE_OCB:
1320 	case NL80211_IFTYPE_NAN:
1321 		/* no special treatment */
1322 		break;
1323 	case NL80211_IFTYPE_UNSPECIFIED:
1324 	case NUM_NL80211_IFTYPES:
1325 	case NL80211_IFTYPE_P2P_CLIENT:
1326 	case NL80211_IFTYPE_P2P_GO:
1327 	case NL80211_IFTYPE_WDS:
1328 		/* cannot happen */
1329 		WARN_ON(1);
1330 		break;
1331 	}
1332 
1333 	if (local->open_count == 0) {
1334 		/* here we can consider everything in good order (again) */
1335 		local->reconfig_failure = false;
1336 
1337 		res = drv_start(local);
1338 		if (res)
1339 			goto err_del_bss;
1340 		ieee80211_led_radio(local, true);
1341 		ieee80211_mod_tpt_led_trig(local,
1342 					   IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1343 	}
1344 
1345 	/*
1346 	 * Copy the hopefully now-present MAC address to
1347 	 * this interface, if it has the special null one.
1348 	 */
1349 	if (dev && is_zero_ether_addr(dev->dev_addr)) {
1350 		eth_hw_addr_set(dev, local->hw.wiphy->perm_addr);
1351 		memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
1352 
1353 		if (!is_valid_ether_addr(dev->dev_addr)) {
1354 			res = -EADDRNOTAVAIL;
1355 			goto err_stop;
1356 		}
1357 	}
1358 
1359 	sdata->vif.addr_valid = sdata->vif.type != NL80211_IFTYPE_MONITOR ||
1360 				(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE);
1361 	switch (sdata->vif.type) {
1362 	case NL80211_IFTYPE_AP_VLAN:
1363 		/* no need to tell driver, but set carrier and chanctx */
1364 		if (sdata->bss->active) {
1365 			struct ieee80211_link_data *link;
1366 
1367 			for_each_link_data(sdata, link) {
1368 				ieee80211_link_vlan_copy_chanctx(link);
1369 			}
1370 
1371 			netif_carrier_on(dev);
1372 			ieee80211_set_vif_encap_ops(sdata);
1373 		} else {
1374 			netif_carrier_off(dev);
1375 		}
1376 		break;
1377 	case NL80211_IFTYPE_MONITOR:
1378 		if ((sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) ||
1379 		    ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
1380 			res = drv_add_interface(local, sdata);
1381 			if (res)
1382 				goto err_stop;
1383 		} else {
1384 			if (local->virt_monitors == 0 && local->open_count == 0) {
1385 				res = ieee80211_add_virtual_monitor(local);
1386 				if (res)
1387 					goto err_stop;
1388 			}
1389 			local->virt_monitors++;
1390 
1391 			/* must be before the call to ieee80211_configure_filter */
1392 			if (local->virt_monitors == 1) {
1393 				local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
1394 				hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
1395 			}
1396 		}
1397 
1398 		local->monitors++;
1399 
1400 		ieee80211_adjust_monitor_flags(sdata, 1);
1401 		ieee80211_configure_filter(local);
1402 		ieee80211_recalc_offload(local);
1403 		ieee80211_recalc_idle(local);
1404 
1405 		netif_carrier_on(dev);
1406 		break;
1407 	default:
1408 		if (coming_up) {
1409 			ieee80211_del_virtual_monitor(local);
1410 			ieee80211_set_sdata_offload_flags(sdata);
1411 
1412 			res = drv_add_interface(local, sdata);
1413 			if (res)
1414 				goto err_stop;
1415 
1416 			ieee80211_set_vif_encap_ops(sdata);
1417 			res = ieee80211_check_queues(sdata,
1418 				ieee80211_vif_type_p2p(&sdata->vif));
1419 			if (res)
1420 				goto err_del_interface;
1421 		}
1422 
1423 		if (sdata->vif.type == NL80211_IFTYPE_AP) {
1424 			local->fif_pspoll++;
1425 			local->fif_probe_req++;
1426 
1427 			ieee80211_configure_filter(local);
1428 		} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1429 			local->fif_probe_req++;
1430 		}
1431 
1432 		if (sdata->vif.probe_req_reg)
1433 			drv_config_iface_filter(local, sdata,
1434 						FIF_PROBE_REQ,
1435 						FIF_PROBE_REQ);
1436 
1437 		if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1438 		    sdata->vif.type != NL80211_IFTYPE_NAN)
1439 			changed |= ieee80211_reset_erp_info(sdata);
1440 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1441 						  changed);
1442 
1443 		switch (sdata->vif.type) {
1444 		case NL80211_IFTYPE_STATION:
1445 		case NL80211_IFTYPE_ADHOC:
1446 		case NL80211_IFTYPE_AP:
1447 		case NL80211_IFTYPE_MESH_POINT:
1448 		case NL80211_IFTYPE_OCB:
1449 			netif_carrier_off(dev);
1450 			break;
1451 		case NL80211_IFTYPE_P2P_DEVICE:
1452 		case NL80211_IFTYPE_NAN:
1453 			break;
1454 		default:
1455 			/* not reached */
1456 			WARN_ON(1);
1457 		}
1458 
1459 		/*
1460 		 * Set default queue parameters so drivers don't
1461 		 * need to initialise the hardware if the hardware
1462 		 * doesn't start up with sane defaults.
1463 		 * Enable QoS for anything but station interfaces.
1464 		 */
1465 		ieee80211_set_wmm_default(&sdata->deflink, true,
1466 			sdata->vif.type != NL80211_IFTYPE_STATION);
1467 	}
1468 
1469 	switch (sdata->vif.type) {
1470 	case NL80211_IFTYPE_P2P_DEVICE:
1471 		rcu_assign_pointer(local->p2p_sdata, sdata);
1472 		break;
1473 	case NL80211_IFTYPE_MONITOR:
1474 		list_add_tail_rcu(&sdata->u.mntr.list, &local->mon_list);
1475 		break;
1476 	default:
1477 		break;
1478 	}
1479 
1480 	/*
1481 	 * set_multicast_list will be invoked by the networking core
1482 	 * which will check whether any increments here were done in
1483 	 * error and sync them down to the hardware as filter flags.
1484 	 */
1485 	if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
1486 		atomic_inc(&local->iff_allmultis);
1487 
1488 	if (coming_up)
1489 		local->open_count++;
1490 
1491 	if (local->open_count == 1)
1492 		ieee80211_hw_conf_init(local);
1493 	else if (hw_reconf_flags)
1494 		ieee80211_hw_config(local, hw_reconf_flags);
1495 
1496 	ieee80211_recalc_ps(local);
1497 
1498 	set_bit(SDATA_STATE_RUNNING, &sdata->state);
1499 
1500 	return 0;
1501  err_del_interface:
1502 	drv_remove_interface(local, sdata);
1503  err_stop:
1504 	if (!local->open_count)
1505 		drv_stop(local, false);
1506  err_del_bss:
1507 	sdata->bss = NULL;
1508 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1509 		list_del(&sdata->u.vlan.list);
1510 	/* might already be clear but that doesn't matter */
1511 	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
1512 	return res;
1513 }
1514 
ieee80211_if_setup(struct net_device * dev)1515 static void ieee80211_if_setup(struct net_device *dev)
1516 {
1517 	ether_setup(dev);
1518 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1519 	dev->priv_flags |= IFF_NO_QUEUE;
1520 	dev->netdev_ops = &ieee80211_dataif_ops;
1521 	dev->needs_free_netdev = true;
1522 }
1523 
ieee80211_iface_process_skb(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1524 static void ieee80211_iface_process_skb(struct ieee80211_local *local,
1525 					struct ieee80211_sub_if_data *sdata,
1526 					struct sk_buff *skb)
1527 {
1528 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1529 
1530 	lockdep_assert_wiphy(local->hw.wiphy);
1531 
1532 	if (ieee80211_is_action(mgmt->frame_control) &&
1533 	    mgmt->u.action.category == WLAN_CATEGORY_BACK) {
1534 		struct sta_info *sta;
1535 		int len = skb->len;
1536 
1537 		sta = sta_info_get_bss(sdata, mgmt->sa);
1538 		if (sta) {
1539 			switch (mgmt->u.action.u.addba_req.action_code) {
1540 			case WLAN_ACTION_ADDBA_REQ:
1541 				ieee80211_process_addba_request(local, sta,
1542 								mgmt, len);
1543 				break;
1544 			case WLAN_ACTION_ADDBA_RESP:
1545 				ieee80211_process_addba_resp(local, sta,
1546 							     mgmt, len);
1547 				break;
1548 			case WLAN_ACTION_DELBA:
1549 				ieee80211_process_delba(sdata, sta,
1550 							mgmt, len);
1551 				break;
1552 			default:
1553 				WARN_ON(1);
1554 				break;
1555 			}
1556 		}
1557 	} else if (ieee80211_is_action(mgmt->frame_control) &&
1558 		   mgmt->u.action.category == WLAN_CATEGORY_VHT) {
1559 		switch (mgmt->u.action.u.vht_group_notif.action_code) {
1560 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
1561 			struct ieee80211_rx_status *status;
1562 			enum nl80211_band band;
1563 			struct sta_info *sta;
1564 			u8 opmode;
1565 
1566 			status = IEEE80211_SKB_RXCB(skb);
1567 			band = status->band;
1568 			opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
1569 
1570 			sta = sta_info_get_bss(sdata, mgmt->sa);
1571 
1572 			if (sta)
1573 				ieee80211_vht_handle_opmode(sdata,
1574 							    &sta->deflink,
1575 							    opmode, band);
1576 
1577 			break;
1578 		}
1579 		case WLAN_VHT_ACTION_GROUPID_MGMT:
1580 			ieee80211_process_mu_groups(sdata, &sdata->deflink,
1581 						    mgmt);
1582 			break;
1583 		default:
1584 			WARN_ON(1);
1585 			break;
1586 		}
1587 	} else if (ieee80211_is_action(mgmt->frame_control) &&
1588 		   mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1589 		switch (mgmt->u.action.u.s1g.action_code) {
1590 		case WLAN_S1G_TWT_TEARDOWN:
1591 		case WLAN_S1G_TWT_SETUP:
1592 			ieee80211_s1g_rx_twt_action(sdata, skb);
1593 			break;
1594 		default:
1595 			break;
1596 		}
1597 	} else if (ieee80211_is_action(mgmt->frame_control) &&
1598 		   mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_EHT) {
1599 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1600 			switch (mgmt->u.action.u.ttlm_req.action_code) {
1601 			case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
1602 				ieee80211_process_neg_ttlm_req(sdata, mgmt,
1603 							       skb->len);
1604 				break;
1605 			case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
1606 				ieee80211_process_neg_ttlm_res(sdata, mgmt,
1607 							       skb->len);
1608 				break;
1609 			case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
1610 				ieee80211_process_ttlm_teardown(sdata);
1611 				break;
1612 			case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
1613 				ieee80211_process_ml_reconf_resp(sdata, mgmt,
1614 								 skb->len);
1615 				break;
1616 			case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
1617 				ieee80211_process_epcs_ena_resp(sdata, mgmt,
1618 								skb->len);
1619 				break;
1620 			case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
1621 				ieee80211_process_epcs_teardown(sdata, mgmt,
1622 								skb->len);
1623 				break;
1624 			default:
1625 				break;
1626 			}
1627 		}
1628 	} else if (ieee80211_is_ext(mgmt->frame_control)) {
1629 		if (sdata->vif.type == NL80211_IFTYPE_STATION)
1630 			ieee80211_sta_rx_queued_ext(sdata, skb);
1631 		else
1632 			WARN_ON(1);
1633 	} else if (ieee80211_is_data_qos(mgmt->frame_control)) {
1634 		struct ieee80211_hdr *hdr = (void *)mgmt;
1635 		struct sta_info *sta;
1636 
1637 		/*
1638 		 * So the frame isn't mgmt, but frame_control
1639 		 * is at the right place anyway, of course, so
1640 		 * the if statement is correct.
1641 		 *
1642 		 * Warn if we have other data frame types here,
1643 		 * they must not get here.
1644 		 */
1645 		WARN_ON(hdr->frame_control &
1646 				cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
1647 		WARN_ON(!(hdr->seq_ctrl &
1648 				cpu_to_le16(IEEE80211_SCTL_FRAG)));
1649 		/*
1650 		 * This was a fragment of a frame, received while
1651 		 * a block-ack session was active. That cannot be
1652 		 * right, so terminate the session.
1653 		 */
1654 		sta = sta_info_get_bss(sdata, mgmt->sa);
1655 		if (sta) {
1656 			u16 tid = ieee80211_get_tid(hdr);
1657 
1658 			__ieee80211_stop_rx_ba_session(
1659 				sta, tid, WLAN_BACK_RECIPIENT,
1660 				WLAN_REASON_QSTA_REQUIRE_SETUP,
1661 				true);
1662 		}
1663 	} else switch (sdata->vif.type) {
1664 	case NL80211_IFTYPE_STATION:
1665 		ieee80211_sta_rx_queued_mgmt(sdata, skb);
1666 		break;
1667 	case NL80211_IFTYPE_ADHOC:
1668 		ieee80211_ibss_rx_queued_mgmt(sdata, skb);
1669 		break;
1670 	case NL80211_IFTYPE_MESH_POINT:
1671 		if (!ieee80211_vif_is_mesh(&sdata->vif))
1672 			break;
1673 		ieee80211_mesh_rx_queued_mgmt(sdata, skb);
1674 		break;
1675 	default:
1676 		WARN(1, "frame for unexpected interface type");
1677 		break;
1678 	}
1679 }
1680 
ieee80211_iface_process_status(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1681 static void ieee80211_iface_process_status(struct ieee80211_sub_if_data *sdata,
1682 					   struct sk_buff *skb)
1683 {
1684 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1685 
1686 	if (ieee80211_is_action(mgmt->frame_control) &&
1687 	    mgmt->u.action.category == WLAN_CATEGORY_S1G) {
1688 		switch (mgmt->u.action.u.s1g.action_code) {
1689 		case WLAN_S1G_TWT_TEARDOWN:
1690 		case WLAN_S1G_TWT_SETUP:
1691 			ieee80211_s1g_status_twt_action(sdata, skb);
1692 			break;
1693 		default:
1694 			break;
1695 		}
1696 	}
1697 }
1698 
ieee80211_iface_work(struct wiphy * wiphy,struct wiphy_work * work)1699 static void ieee80211_iface_work(struct wiphy *wiphy, struct wiphy_work *work)
1700 {
1701 	struct ieee80211_sub_if_data *sdata =
1702 		container_of(work, struct ieee80211_sub_if_data, work);
1703 	struct ieee80211_local *local = sdata->local;
1704 	struct sk_buff *skb;
1705 
1706 	if (!ieee80211_sdata_running(sdata))
1707 		return;
1708 
1709 	if (test_bit(SCAN_SW_SCANNING, &local->scanning))
1710 		return;
1711 
1712 	if (!ieee80211_can_run_worker(local))
1713 		return;
1714 
1715 	/* first process frames */
1716 	while ((skb = skb_dequeue(&sdata->skb_queue))) {
1717 		kcov_remote_start_common(skb_get_kcov_handle(skb));
1718 
1719 		if (skb->protocol == cpu_to_be16(ETH_P_TDLS))
1720 			ieee80211_process_tdls_channel_switch(sdata, skb);
1721 		else
1722 			ieee80211_iface_process_skb(local, sdata, skb);
1723 
1724 		kfree_skb(skb);
1725 		kcov_remote_stop();
1726 	}
1727 
1728 	/* process status queue */
1729 	while ((skb = skb_dequeue(&sdata->status_queue))) {
1730 		kcov_remote_start_common(skb_get_kcov_handle(skb));
1731 
1732 		ieee80211_iface_process_status(sdata, skb);
1733 		kfree_skb(skb);
1734 
1735 		kcov_remote_stop();
1736 	}
1737 
1738 	/* then other type-dependent work */
1739 	switch (sdata->vif.type) {
1740 	case NL80211_IFTYPE_STATION:
1741 		ieee80211_sta_work(sdata);
1742 		break;
1743 	case NL80211_IFTYPE_ADHOC:
1744 		ieee80211_ibss_work(sdata);
1745 		break;
1746 	case NL80211_IFTYPE_MESH_POINT:
1747 		if (!ieee80211_vif_is_mesh(&sdata->vif))
1748 			break;
1749 		ieee80211_mesh_work(sdata);
1750 		break;
1751 	case NL80211_IFTYPE_OCB:
1752 		ieee80211_ocb_work(sdata);
1753 		break;
1754 	default:
1755 		break;
1756 	}
1757 }
1758 
ieee80211_activate_links_work(struct wiphy * wiphy,struct wiphy_work * work)1759 static void ieee80211_activate_links_work(struct wiphy *wiphy,
1760 					  struct wiphy_work *work)
1761 {
1762 	struct ieee80211_sub_if_data *sdata =
1763 		container_of(work, struct ieee80211_sub_if_data,
1764 			     activate_links_work);
1765 	struct ieee80211_local *local = wiphy_priv(wiphy);
1766 
1767 	if (local->in_reconfig)
1768 		return;
1769 
1770 	ieee80211_set_active_links(&sdata->vif, sdata->desired_active_links);
1771 	sdata->desired_active_links = 0;
1772 }
1773 
1774 /*
1775  * Helper function to initialise an interface to a specific type.
1776  */
ieee80211_setup_sdata(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1777 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1778 				  enum nl80211_iftype type)
1779 {
1780 	static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
1781 						    0xff, 0xff, 0xff};
1782 
1783 	/* clear type-dependent unions */
1784 	memset(&sdata->u, 0, sizeof(sdata->u));
1785 	memset(&sdata->deflink.u, 0, sizeof(sdata->deflink.u));
1786 
1787 	/* and set some type-dependent values */
1788 	sdata->vif.type = type;
1789 	sdata->vif.p2p = false;
1790 	sdata->wdev.iftype = type;
1791 
1792 	sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
1793 	sdata->control_port_no_encrypt = false;
1794 	sdata->control_port_over_nl80211 = false;
1795 	sdata->control_port_no_preauth = false;
1796 	sdata->vif.cfg.idle = true;
1797 	sdata->vif.bss_conf.txpower = INT_MIN; /* unset */
1798 
1799 	sdata->noack_map = 0;
1800 
1801 	/* only monitor/p2p-device differ */
1802 	if (sdata->dev) {
1803 		sdata->dev->netdev_ops = &ieee80211_dataif_ops;
1804 		sdata->dev->type = ARPHRD_ETHER;
1805 	}
1806 
1807 	skb_queue_head_init(&sdata->skb_queue);
1808 	skb_queue_head_init(&sdata->status_queue);
1809 	wiphy_work_init(&sdata->work, ieee80211_iface_work);
1810 	wiphy_work_init(&sdata->activate_links_work,
1811 			ieee80211_activate_links_work);
1812 
1813 	switch (type) {
1814 	case NL80211_IFTYPE_P2P_GO:
1815 		type = NL80211_IFTYPE_AP;
1816 		sdata->vif.type = type;
1817 		sdata->vif.p2p = true;
1818 		fallthrough;
1819 	case NL80211_IFTYPE_AP:
1820 		skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1821 		INIT_LIST_HEAD(&sdata->u.ap.vlans);
1822 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1823 		break;
1824 	case NL80211_IFTYPE_P2P_CLIENT:
1825 		type = NL80211_IFTYPE_STATION;
1826 		sdata->vif.type = type;
1827 		sdata->vif.p2p = true;
1828 		fallthrough;
1829 	case NL80211_IFTYPE_STATION:
1830 		sdata->vif.bss_conf.bssid = sdata->deflink.u.mgd.bssid;
1831 		ieee80211_sta_setup_sdata(sdata);
1832 		break;
1833 	case NL80211_IFTYPE_OCB:
1834 		sdata->vif.bss_conf.bssid = bssid_wildcard;
1835 		ieee80211_ocb_setup_sdata(sdata);
1836 		break;
1837 	case NL80211_IFTYPE_ADHOC:
1838 		sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
1839 		ieee80211_ibss_setup_sdata(sdata);
1840 		break;
1841 	case NL80211_IFTYPE_MESH_POINT:
1842 		if (ieee80211_vif_is_mesh(&sdata->vif))
1843 			ieee80211_mesh_init_sdata(sdata);
1844 		break;
1845 	case NL80211_IFTYPE_MONITOR:
1846 		sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
1847 		sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
1848 		sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
1849 				      MONITOR_FLAG_OTHER_BSS;
1850 		break;
1851 	case NL80211_IFTYPE_NAN:
1852 		idr_init(&sdata->u.nan.function_inst_ids);
1853 		spin_lock_init(&sdata->u.nan.func_lock);
1854 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1855 		break;
1856 	case NL80211_IFTYPE_AP_VLAN:
1857 	case NL80211_IFTYPE_P2P_DEVICE:
1858 		sdata->vif.bss_conf.bssid = sdata->vif.addr;
1859 		break;
1860 	case NL80211_IFTYPE_UNSPECIFIED:
1861 	case NL80211_IFTYPE_WDS:
1862 	case NUM_NL80211_IFTYPES:
1863 		WARN_ON(1);
1864 		break;
1865 	}
1866 
1867 	/* need to do this after the switch so vif.type is correct */
1868 	ieee80211_link_setup(&sdata->deflink);
1869 
1870 	ieee80211_debugfs_recreate_netdev(sdata, false);
1871 }
1872 
ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1873 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
1874 					   enum nl80211_iftype type)
1875 {
1876 	struct ieee80211_local *local = sdata->local;
1877 	int ret, err;
1878 	enum nl80211_iftype internal_type = type;
1879 	bool p2p = false;
1880 
1881 	ASSERT_RTNL();
1882 
1883 	if (!local->ops->change_interface)
1884 		return -EBUSY;
1885 
1886 	/* for now, don't support changing while links exist */
1887 	if (ieee80211_vif_is_mld(&sdata->vif))
1888 		return -EBUSY;
1889 
1890 	switch (sdata->vif.type) {
1891 	case NL80211_IFTYPE_AP:
1892 		if (!list_empty(&sdata->u.ap.vlans))
1893 			return -EBUSY;
1894 		break;
1895 	case NL80211_IFTYPE_STATION:
1896 	case NL80211_IFTYPE_ADHOC:
1897 	case NL80211_IFTYPE_OCB:
1898 		/*
1899 		 * Could maybe also all others here?
1900 		 * Just not sure how that interacts
1901 		 * with the RX/config path e.g. for
1902 		 * mesh.
1903 		 */
1904 		break;
1905 	default:
1906 		return -EBUSY;
1907 	}
1908 
1909 	switch (type) {
1910 	case NL80211_IFTYPE_AP:
1911 	case NL80211_IFTYPE_STATION:
1912 	case NL80211_IFTYPE_ADHOC:
1913 	case NL80211_IFTYPE_OCB:
1914 		/*
1915 		 * Could probably support everything
1916 		 * but here.
1917 		 */
1918 		break;
1919 	case NL80211_IFTYPE_P2P_CLIENT:
1920 		p2p = true;
1921 		internal_type = NL80211_IFTYPE_STATION;
1922 		break;
1923 	case NL80211_IFTYPE_P2P_GO:
1924 		p2p = true;
1925 		internal_type = NL80211_IFTYPE_AP;
1926 		break;
1927 	default:
1928 		return -EBUSY;
1929 	}
1930 
1931 	ret = ieee80211_check_concurrent_iface(sdata, internal_type);
1932 	if (ret)
1933 		return ret;
1934 
1935 	ieee80211_stop_vif_queues(local, sdata,
1936 				  IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1937 	/* do_stop will synchronize_rcu() first thing */
1938 	ieee80211_do_stop(sdata, false);
1939 
1940 	ieee80211_teardown_sdata(sdata);
1941 
1942 	ieee80211_set_sdata_offload_flags(sdata);
1943 	ret = drv_change_interface(local, sdata, internal_type, p2p);
1944 	if (ret)
1945 		type = ieee80211_vif_type_p2p(&sdata->vif);
1946 
1947 	/*
1948 	 * Ignore return value here, there's not much we can do since
1949 	 * the driver changed the interface type internally already.
1950 	 * The warnings will hopefully make driver authors fix it :-)
1951 	 */
1952 	ieee80211_check_queues(sdata, type);
1953 
1954 	ieee80211_setup_sdata(sdata, type);
1955 	ieee80211_set_vif_encap_ops(sdata);
1956 
1957 	err = ieee80211_do_open(&sdata->wdev, false);
1958 	WARN(err, "type change: do_open returned %d", err);
1959 
1960 	ieee80211_wake_vif_queues(local, sdata,
1961 				  IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
1962 	return ret;
1963 }
1964 
ieee80211_if_change_type(struct ieee80211_sub_if_data * sdata,enum nl80211_iftype type)1965 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
1966 			     enum nl80211_iftype type)
1967 {
1968 	int ret;
1969 
1970 	ASSERT_RTNL();
1971 
1972 	if (type == ieee80211_vif_type_p2p(&sdata->vif))
1973 		return 0;
1974 
1975 	if (ieee80211_sdata_running(sdata)) {
1976 		ret = ieee80211_runtime_change_iftype(sdata, type);
1977 		if (ret)
1978 			return ret;
1979 	} else {
1980 		/* Purge and reset type-dependent state. */
1981 		ieee80211_teardown_sdata(sdata);
1982 		ieee80211_setup_sdata(sdata, type);
1983 	}
1984 
1985 	/* reset some values that shouldn't be kept across type changes */
1986 	if (type == NL80211_IFTYPE_STATION)
1987 		sdata->u.mgd.use_4addr = false;
1988 
1989 	return 0;
1990 }
1991 
ieee80211_assign_perm_addr(struct ieee80211_local * local,u8 * perm_addr,enum nl80211_iftype type)1992 static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
1993 				       u8 *perm_addr, enum nl80211_iftype type)
1994 {
1995 	struct ieee80211_sub_if_data *sdata;
1996 	u64 mask, start, addr, val, inc;
1997 	u8 *m;
1998 	u8 tmp_addr[ETH_ALEN];
1999 	int i;
2000 
2001 	lockdep_assert_wiphy(local->hw.wiphy);
2002 
2003 	/* default ... something at least */
2004 	memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
2005 
2006 	if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
2007 	    local->hw.wiphy->n_addresses <= 1)
2008 		return;
2009 
2010 	switch (type) {
2011 	case NL80211_IFTYPE_MONITOR:
2012 		/* doesn't matter */
2013 		break;
2014 	case NL80211_IFTYPE_AP_VLAN:
2015 		/* match up with an AP interface */
2016 		list_for_each_entry(sdata, &local->interfaces, list) {
2017 			if (sdata->vif.type != NL80211_IFTYPE_AP)
2018 				continue;
2019 			memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
2020 			break;
2021 		}
2022 		/* keep default if no AP interface present */
2023 		break;
2024 	case NL80211_IFTYPE_P2P_CLIENT:
2025 	case NL80211_IFTYPE_P2P_GO:
2026 		if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
2027 			list_for_each_entry(sdata, &local->interfaces, list) {
2028 				if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
2029 					continue;
2030 				if (!ieee80211_sdata_running(sdata))
2031 					continue;
2032 				memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
2033 				return;
2034 			}
2035 		}
2036 		fallthrough;
2037 	default:
2038 		/* assign a new address if possible -- try n_addresses first */
2039 		for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
2040 			bool used = false;
2041 
2042 			list_for_each_entry(sdata, &local->interfaces, list) {
2043 				if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
2044 						     sdata->vif.addr)) {
2045 					used = true;
2046 					break;
2047 				}
2048 			}
2049 
2050 			if (!used) {
2051 				memcpy(perm_addr,
2052 				       local->hw.wiphy->addresses[i].addr,
2053 				       ETH_ALEN);
2054 				break;
2055 			}
2056 		}
2057 
2058 		/* try mask if available */
2059 		if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
2060 			break;
2061 
2062 		m = local->hw.wiphy->addr_mask;
2063 		mask =	((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2064 			((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2065 			((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2066 
2067 		if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
2068 			/* not a contiguous mask ... not handled now! */
2069 			pr_info("not contiguous\n");
2070 			break;
2071 		}
2072 
2073 		/*
2074 		 * Pick address of existing interface in case user changed
2075 		 * MAC address manually, default to perm_addr.
2076 		 */
2077 		m = local->hw.wiphy->perm_addr;
2078 		list_for_each_entry(sdata, &local->interfaces, list) {
2079 			if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2080 				continue;
2081 			m = sdata->vif.addr;
2082 			break;
2083 		}
2084 		start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
2085 			((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
2086 			((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
2087 
2088 		inc = 1ULL<<__ffs64(mask);
2089 		val = (start & mask);
2090 		addr = (start & ~mask) | (val & mask);
2091 		do {
2092 			bool used = false;
2093 
2094 			tmp_addr[5] = addr >> 0*8;
2095 			tmp_addr[4] = addr >> 1*8;
2096 			tmp_addr[3] = addr >> 2*8;
2097 			tmp_addr[2] = addr >> 3*8;
2098 			tmp_addr[1] = addr >> 4*8;
2099 			tmp_addr[0] = addr >> 5*8;
2100 
2101 			val += inc;
2102 
2103 			list_for_each_entry(sdata, &local->interfaces, list) {
2104 				if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
2105 					used = true;
2106 					break;
2107 				}
2108 			}
2109 
2110 			if (!used) {
2111 				memcpy(perm_addr, tmp_addr, ETH_ALEN);
2112 				break;
2113 			}
2114 			addr = (start & ~mask) | (val & mask);
2115 		} while (addr != start);
2116 
2117 		break;
2118 	}
2119 }
2120 
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)2121 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
2122 		     unsigned char name_assign_type,
2123 		     struct wireless_dev **new_wdev, enum nl80211_iftype type,
2124 		     struct vif_params *params)
2125 {
2126 	struct net_device *ndev = NULL;
2127 	struct ieee80211_sub_if_data *sdata = NULL;
2128 	struct txq_info *txqi;
2129 	int ret, i;
2130 
2131 	ASSERT_RTNL();
2132 	lockdep_assert_wiphy(local->hw.wiphy);
2133 
2134 	if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
2135 		struct wireless_dev *wdev;
2136 
2137 		sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
2138 				GFP_KERNEL);
2139 		if (!sdata)
2140 			return -ENOMEM;
2141 		wdev = &sdata->wdev;
2142 
2143 		sdata->dev = NULL;
2144 		strscpy(sdata->name, name, IFNAMSIZ);
2145 		ieee80211_assign_perm_addr(local, wdev->address, type);
2146 		memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
2147 		ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2148 	} else {
2149 		int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
2150 				 sizeof(void *));
2151 		int txq_size = 0;
2152 
2153 		if (type != NL80211_IFTYPE_AP_VLAN &&
2154 		    (type != NL80211_IFTYPE_MONITOR ||
2155 		     (params->flags & MONITOR_FLAG_ACTIVE)))
2156 			txq_size += sizeof(struct txq_info) +
2157 				    local->hw.txq_data_size;
2158 
2159 		ndev = alloc_netdev_mqs(size + txq_size,
2160 					name, name_assign_type,
2161 					ieee80211_if_setup, 1, 1);
2162 		if (!ndev)
2163 			return -ENOMEM;
2164 
2165 		dev_net_set(ndev, wiphy_net(local->hw.wiphy));
2166 
2167 		ndev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2168 
2169 		ndev->needed_headroom = local->tx_headroom +
2170 					4*6 /* four MAC addresses */
2171 					+ 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
2172 					+ 6 /* mesh */
2173 					+ 8 /* rfc1042/bridge tunnel */
2174 					- ETH_HLEN /* ethernet hard_header_len */
2175 					+ IEEE80211_ENCRYPT_HEADROOM;
2176 		ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
2177 
2178 		ret = dev_alloc_name(ndev, ndev->name);
2179 		if (ret < 0) {
2180 			free_netdev(ndev);
2181 			return ret;
2182 		}
2183 
2184 		ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
2185 		if (is_valid_ether_addr(params->macaddr))
2186 			eth_hw_addr_set(ndev, params->macaddr);
2187 		else
2188 			eth_hw_addr_set(ndev, ndev->perm_addr);
2189 		SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
2190 
2191 		/* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
2192 		sdata = netdev_priv(ndev);
2193 		ndev->ieee80211_ptr = &sdata->wdev;
2194 		memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
2195 		ether_addr_copy(sdata->vif.bss_conf.addr, sdata->vif.addr);
2196 		memcpy(sdata->name, ndev->name, IFNAMSIZ);
2197 
2198 		if (txq_size) {
2199 			txqi = netdev_priv(ndev) + size;
2200 			ieee80211_txq_init(sdata, NULL, txqi, 0);
2201 		}
2202 
2203 		sdata->dev = ndev;
2204 	}
2205 
2206 	/* initialise type-independent data */
2207 	sdata->wdev.wiphy = local->hw.wiphy;
2208 
2209 	ieee80211_sdata_init(local, sdata);
2210 
2211 	ieee80211_init_frag_cache(&sdata->frags);
2212 
2213 	INIT_LIST_HEAD(&sdata->key_list);
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