xref: /linux/net/mac80211/main.c (revision 730eeb17bbdd3c31f91e2a4fff35dd7e9c67d706)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright (C) 2017     Intel Deutschland GmbH
8  * Copyright (C) 2018-2023 Intel Corporation
9  */
10 
11 #include <net/mac80211.h>
12 #include <linux/module.h>
13 #include <linux/fips.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/bitmap.h>
23 #include <linux/inetdevice.h>
24 #include <net/net_namespace.h>
25 #include <net/dropreason.h>
26 #include <net/cfg80211.h>
27 #include <net/addrconf.h>
28 
29 #include "ieee80211_i.h"
30 #include "driver-ops.h"
31 #include "rate.h"
32 #include "mesh.h"
33 #include "wep.h"
34 #include "led.h"
35 #include "debugfs.h"
36 
37 void ieee80211_configure_filter(struct ieee80211_local *local)
38 {
39 	u64 mc;
40 	unsigned int changed_flags;
41 	unsigned int new_flags = 0;
42 
43 	if (atomic_read(&local->iff_allmultis))
44 		new_flags |= FIF_ALLMULTI;
45 
46 	if (local->monitors || test_bit(SCAN_SW_SCANNING, &local->scanning) ||
47 	    test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning))
48 		new_flags |= FIF_BCN_PRBRESP_PROMISC;
49 
50 	if (local->fif_probe_req || local->probe_req_reg)
51 		new_flags |= FIF_PROBE_REQ;
52 
53 	if (local->fif_fcsfail)
54 		new_flags |= FIF_FCSFAIL;
55 
56 	if (local->fif_plcpfail)
57 		new_flags |= FIF_PLCPFAIL;
58 
59 	if (local->fif_control)
60 		new_flags |= FIF_CONTROL;
61 
62 	if (local->fif_other_bss)
63 		new_flags |= FIF_OTHER_BSS;
64 
65 	if (local->fif_pspoll)
66 		new_flags |= FIF_PSPOLL;
67 
68 	if (local->rx_mcast_action_reg)
69 		new_flags |= FIF_MCAST_ACTION;
70 
71 	spin_lock_bh(&local->filter_lock);
72 	changed_flags = local->filter_flags ^ new_flags;
73 
74 	mc = drv_prepare_multicast(local, &local->mc_list);
75 	spin_unlock_bh(&local->filter_lock);
76 
77 	/* be a bit nasty */
78 	new_flags |= (1<<31);
79 
80 	drv_configure_filter(local, changed_flags, &new_flags, mc);
81 
82 	WARN_ON(new_flags & (1<<31));
83 
84 	local->filter_flags = new_flags & ~(1<<31);
85 }
86 
87 static void ieee80211_reconfig_filter(struct wiphy *wiphy,
88 				      struct wiphy_work *work)
89 {
90 	struct ieee80211_local *local =
91 		container_of(work, struct ieee80211_local, reconfig_filter);
92 
93 	ieee80211_configure_filter(local);
94 }
95 
96 static u32 ieee80211_hw_conf_chan(struct ieee80211_local *local)
97 {
98 	struct ieee80211_sub_if_data *sdata;
99 	struct cfg80211_chan_def chandef = {};
100 	u32 changed = 0;
101 	int power;
102 	u32 offchannel_flag;
103 
104 	offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
105 
106 	if (local->scan_chandef.chan) {
107 		chandef = local->scan_chandef;
108 	} else if (local->tmp_channel) {
109 		chandef.chan = local->tmp_channel;
110 		chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
111 		chandef.center_freq1 = chandef.chan->center_freq;
112 		chandef.freq1_offset = chandef.chan->freq_offset;
113 	} else
114 		chandef = local->_oper_chandef;
115 
116 	WARN(!cfg80211_chandef_valid(&chandef),
117 	     "control:%d.%03d MHz width:%d center: %d.%03d/%d MHz",
118 	     chandef.chan->center_freq, chandef.chan->freq_offset,
119 	     chandef.width, chandef.center_freq1, chandef.freq1_offset,
120 	     chandef.center_freq2);
121 
122 	if (!cfg80211_chandef_identical(&chandef, &local->_oper_chandef))
123 		local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
124 	else
125 		local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
126 
127 	offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
128 
129 	if (offchannel_flag ||
130 	    !cfg80211_chandef_identical(&local->hw.conf.chandef,
131 					&local->_oper_chandef)) {
132 		local->hw.conf.chandef = chandef;
133 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
134 	}
135 
136 	if (!conf_is_ht(&local->hw.conf)) {
137 		/*
138 		 * mac80211.h documents that this is only valid
139 		 * when the channel is set to an HT type, and
140 		 * that otherwise STATIC is used.
141 		 */
142 		local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
143 	} else if (local->hw.conf.smps_mode != local->smps_mode) {
144 		local->hw.conf.smps_mode = local->smps_mode;
145 		changed |= IEEE80211_CONF_CHANGE_SMPS;
146 	}
147 
148 	power = ieee80211_chandef_max_power(&chandef);
149 
150 	rcu_read_lock();
151 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
152 		if (!rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf))
153 			continue;
154 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
155 			continue;
156 		if (sdata->vif.bss_conf.txpower == INT_MIN)
157 			continue;
158 		power = min(power, sdata->vif.bss_conf.txpower);
159 	}
160 	rcu_read_unlock();
161 
162 	if (local->hw.conf.power_level != power) {
163 		changed |= IEEE80211_CONF_CHANGE_POWER;
164 		local->hw.conf.power_level = power;
165 	}
166 
167 	return changed;
168 }
169 
170 int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
171 {
172 	int ret = 0;
173 
174 	might_sleep();
175 
176 	if (!local->use_chanctx)
177 		changed |= ieee80211_hw_conf_chan(local);
178 	else
179 		changed &= ~(IEEE80211_CONF_CHANGE_CHANNEL |
180 			     IEEE80211_CONF_CHANGE_POWER |
181 			     IEEE80211_CONF_CHANGE_SMPS);
182 
183 	if (changed && local->open_count) {
184 		ret = drv_config(local, changed);
185 		/*
186 		 * Goal:
187 		 * HW reconfiguration should never fail, the driver has told
188 		 * us what it can support so it should live up to that promise.
189 		 *
190 		 * Current status:
191 		 * rfkill is not integrated with mac80211 and a
192 		 * configuration command can thus fail if hardware rfkill
193 		 * is enabled
194 		 *
195 		 * FIXME: integrate rfkill with mac80211 and then add this
196 		 * WARN_ON() back
197 		 *
198 		 */
199 		/* WARN_ON(ret); */
200 	}
201 
202 	return ret;
203 }
204 
205 #define BSS_CHANGED_VIF_CFG_FLAGS (BSS_CHANGED_ASSOC |\
206 				   BSS_CHANGED_IDLE |\
207 				   BSS_CHANGED_PS |\
208 				   BSS_CHANGED_IBSS |\
209 				   BSS_CHANGED_ARP_FILTER |\
210 				   BSS_CHANGED_SSID)
211 
212 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
213 				      u64 changed)
214 {
215 	struct ieee80211_local *local = sdata->local;
216 
217 	might_sleep();
218 
219 	if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
220 		return;
221 
222 	if (WARN_ON_ONCE(changed & (BSS_CHANGED_BEACON |
223 				    BSS_CHANGED_BEACON_ENABLED) &&
224 			 sdata->vif.type != NL80211_IFTYPE_AP &&
225 			 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
226 			 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
227 			 sdata->vif.type != NL80211_IFTYPE_OCB))
228 		return;
229 
230 	if (WARN_ON_ONCE(sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE ||
231 			 sdata->vif.type == NL80211_IFTYPE_NAN ||
232 			 (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
233 			  !sdata->vif.bss_conf.mu_mimo_owner &&
234 			  !(changed & BSS_CHANGED_TXPOWER))))
235 		return;
236 
237 	if (!check_sdata_in_driver(sdata))
238 		return;
239 
240 	if (changed & BSS_CHANGED_VIF_CFG_FLAGS) {
241 		u64 ch = changed & BSS_CHANGED_VIF_CFG_FLAGS;
242 
243 		trace_drv_vif_cfg_changed(local, sdata, changed);
244 		if (local->ops->vif_cfg_changed)
245 			local->ops->vif_cfg_changed(&local->hw, &sdata->vif, ch);
246 	}
247 
248 	if (changed & ~BSS_CHANGED_VIF_CFG_FLAGS) {
249 		u64 ch = changed & ~BSS_CHANGED_VIF_CFG_FLAGS;
250 
251 		/* FIXME: should be for each link */
252 		trace_drv_link_info_changed(local, sdata, &sdata->vif.bss_conf,
253 					    changed);
254 		if (local->ops->link_info_changed)
255 			local->ops->link_info_changed(&local->hw, &sdata->vif,
256 						      &sdata->vif.bss_conf, ch);
257 	}
258 
259 	if (local->ops->bss_info_changed)
260 		local->ops->bss_info_changed(&local->hw, &sdata->vif,
261 					     &sdata->vif.bss_conf, changed);
262 	trace_drv_return_void(local);
263 }
264 
265 void ieee80211_vif_cfg_change_notify(struct ieee80211_sub_if_data *sdata,
266 				     u64 changed)
267 {
268 	struct ieee80211_local *local = sdata->local;
269 
270 	WARN_ON_ONCE(changed & ~BSS_CHANGED_VIF_CFG_FLAGS);
271 
272 	if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
273 		return;
274 
275 	drv_vif_cfg_changed(local, sdata, changed);
276 }
277 
278 void ieee80211_link_info_change_notify(struct ieee80211_sub_if_data *sdata,
279 				       struct ieee80211_link_data *link,
280 				       u64 changed)
281 {
282 	struct ieee80211_local *local = sdata->local;
283 
284 	WARN_ON_ONCE(changed & BSS_CHANGED_VIF_CFG_FLAGS);
285 
286 	if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
287 		return;
288 
289 	if (!check_sdata_in_driver(sdata))
290 		return;
291 
292 	drv_link_info_changed(local, sdata, link->conf, link->link_id, changed);
293 }
294 
295 u64 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
296 {
297 	sdata->vif.bss_conf.use_cts_prot = false;
298 	sdata->vif.bss_conf.use_short_preamble = false;
299 	sdata->vif.bss_conf.use_short_slot = false;
300 	return BSS_CHANGED_ERP_CTS_PROT |
301 	       BSS_CHANGED_ERP_PREAMBLE |
302 	       BSS_CHANGED_ERP_SLOT;
303 }
304 
305 static void ieee80211_tasklet_handler(struct tasklet_struct *t)
306 {
307 	struct ieee80211_local *local = from_tasklet(local, t, tasklet);
308 	struct sk_buff *skb;
309 
310 	while ((skb = skb_dequeue(&local->skb_queue)) ||
311 	       (skb = skb_dequeue(&local->skb_queue_unreliable))) {
312 		switch (skb->pkt_type) {
313 		case IEEE80211_RX_MSG:
314 			/* Clear skb->pkt_type in order to not confuse kernel
315 			 * netstack. */
316 			skb->pkt_type = 0;
317 			ieee80211_rx(&local->hw, skb);
318 			break;
319 		case IEEE80211_TX_STATUS_MSG:
320 			skb->pkt_type = 0;
321 			ieee80211_tx_status(&local->hw, skb);
322 			break;
323 		default:
324 			WARN(1, "mac80211: Packet is of unknown type %d\n",
325 			     skb->pkt_type);
326 			dev_kfree_skb(skb);
327 			break;
328 		}
329 	}
330 }
331 
332 static void ieee80211_restart_work(struct work_struct *work)
333 {
334 	struct ieee80211_local *local =
335 		container_of(work, struct ieee80211_local, restart_work);
336 	struct ieee80211_sub_if_data *sdata;
337 	int ret;
338 
339 	flush_workqueue(local->workqueue);
340 
341 	rtnl_lock();
342 	/* we might do interface manipulations, so need both */
343 	wiphy_lock(local->hw.wiphy);
344 	wiphy_work_flush(local->hw.wiphy, NULL);
345 
346 	WARN(test_bit(SCAN_HW_SCANNING, &local->scanning),
347 	     "%s called with hardware scan in progress\n", __func__);
348 
349 	list_for_each_entry(sdata, &local->interfaces, list) {
350 		/*
351 		 * XXX: there may be more work for other vif types and even
352 		 * for station mode: a good thing would be to run most of
353 		 * the iface type's dependent _stop (ieee80211_mg_stop,
354 		 * ieee80211_ibss_stop) etc...
355 		 * For now, fix only the specific bug that was seen: race
356 		 * between csa_connection_drop_work and us.
357 		 */
358 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
359 			/*
360 			 * This worker is scheduled from the iface worker that
361 			 * runs on mac80211's workqueue, so we can't be
362 			 * scheduling this worker after the cancel right here.
363 			 * The exception is ieee80211_chswitch_done.
364 			 * Then we can have a race...
365 			 */
366 			wiphy_work_cancel(local->hw.wiphy,
367 					  &sdata->u.mgd.csa_connection_drop_work);
368 			if (sdata->vif.bss_conf.csa_active)
369 				ieee80211_sta_connection_lost(sdata,
370 							      WLAN_REASON_UNSPECIFIED,
371 							      false);
372 		}
373 		wiphy_delayed_work_flush(local->hw.wiphy,
374 					 &sdata->dec_tailroom_needed_wk);
375 	}
376 	ieee80211_scan_cancel(local);
377 
378 	/* make sure any new ROC will consider local->in_reconfig */
379 	wiphy_delayed_work_flush(local->hw.wiphy, &local->roc_work);
380 	wiphy_work_flush(local->hw.wiphy, &local->hw_roc_done);
381 
382 	/* wait for all packet processing to be done */
383 	synchronize_net();
384 
385 	ret = ieee80211_reconfig(local);
386 	wiphy_unlock(local->hw.wiphy);
387 
388 	if (ret)
389 		cfg80211_shutdown_all_interfaces(local->hw.wiphy);
390 
391 	rtnl_unlock();
392 }
393 
394 void ieee80211_restart_hw(struct ieee80211_hw *hw)
395 {
396 	struct ieee80211_local *local = hw_to_local(hw);
397 
398 	trace_api_restart_hw(local);
399 
400 	wiphy_info(hw->wiphy,
401 		   "Hardware restart was requested\n");
402 
403 	/* use this reason, ieee80211_reconfig will unblock it */
404 	ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
405 					IEEE80211_QUEUE_STOP_REASON_SUSPEND,
406 					false);
407 
408 	/*
409 	 * Stop all Rx during the reconfig. We don't want state changes
410 	 * or driver callbacks while this is in progress.
411 	 */
412 	local->in_reconfig = true;
413 	barrier();
414 
415 	queue_work(system_freezable_wq, &local->restart_work);
416 }
417 EXPORT_SYMBOL(ieee80211_restart_hw);
418 
419 #ifdef CONFIG_INET
420 static int ieee80211_ifa_changed(struct notifier_block *nb,
421 				 unsigned long data, void *arg)
422 {
423 	struct in_ifaddr *ifa = arg;
424 	struct ieee80211_local *local =
425 		container_of(nb, struct ieee80211_local,
426 			     ifa_notifier);
427 	struct net_device *ndev = ifa->ifa_dev->dev;
428 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
429 	struct in_device *idev;
430 	struct ieee80211_sub_if_data *sdata;
431 	struct ieee80211_vif_cfg *vif_cfg;
432 	struct ieee80211_if_managed *ifmgd;
433 	int c = 0;
434 
435 	/* Make sure it's our interface that got changed */
436 	if (!wdev)
437 		return NOTIFY_DONE;
438 
439 	if (wdev->wiphy != local->hw.wiphy || !wdev->registered)
440 		return NOTIFY_DONE;
441 
442 	sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
443 	vif_cfg = &sdata->vif.cfg;
444 
445 	/* ARP filtering is only supported in managed mode */
446 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
447 		return NOTIFY_DONE;
448 
449 	idev = __in_dev_get_rtnl(sdata->dev);
450 	if (!idev)
451 		return NOTIFY_DONE;
452 
453 	ifmgd = &sdata->u.mgd;
454 
455 	/*
456 	 * The nested here is needed to convince lockdep that this is
457 	 * all OK. Yes, we lock the wiphy mutex here while we already
458 	 * hold the notifier rwsem, that's the normal case. And yes,
459 	 * we also acquire the notifier rwsem again when unregistering
460 	 * a netdev while we already hold the wiphy mutex, so it does
461 	 * look like a typical ABBA deadlock.
462 	 *
463 	 * However, both of these things happen with the RTNL held
464 	 * already. Therefore, they can't actually happen, since the
465 	 * lock orders really are ABC and ACB, which is fine due to
466 	 * the RTNL (A).
467 	 *
468 	 * We still need to prevent recursion, which is accomplished
469 	 * by the !wdev->registered check above.
470 	 */
471 	mutex_lock_nested(&local->hw.wiphy->mtx, 1);
472 	__acquire(&local->hw.wiphy->mtx);
473 
474 	/* Copy the addresses to the vif config list */
475 	ifa = rtnl_dereference(idev->ifa_list);
476 	while (ifa) {
477 		if (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN)
478 			vif_cfg->arp_addr_list[c] = ifa->ifa_address;
479 		ifa = rtnl_dereference(ifa->ifa_next);
480 		c++;
481 	}
482 
483 	vif_cfg->arp_addr_cnt = c;
484 
485 	/* Configure driver only if associated (which also implies it is up) */
486 	if (ifmgd->associated)
487 		ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_ARP_FILTER);
488 
489 	wiphy_unlock(local->hw.wiphy);
490 
491 	return NOTIFY_OK;
492 }
493 #endif
494 
495 #if IS_ENABLED(CONFIG_IPV6)
496 static int ieee80211_ifa6_changed(struct notifier_block *nb,
497 				  unsigned long data, void *arg)
498 {
499 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)arg;
500 	struct inet6_dev *idev = ifa->idev;
501 	struct net_device *ndev = ifa->idev->dev;
502 	struct ieee80211_local *local =
503 		container_of(nb, struct ieee80211_local, ifa6_notifier);
504 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
505 	struct ieee80211_sub_if_data *sdata;
506 
507 	/* Make sure it's our interface that got changed */
508 	if (!wdev || wdev->wiphy != local->hw.wiphy)
509 		return NOTIFY_DONE;
510 
511 	sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
512 
513 	/*
514 	 * For now only support station mode. This is mostly because
515 	 * doing AP would have to handle AP_VLAN in some way ...
516 	 */
517 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
518 		return NOTIFY_DONE;
519 
520 	drv_ipv6_addr_change(local, sdata, idev);
521 
522 	return NOTIFY_OK;
523 }
524 #endif
525 
526 /* There isn't a lot of sense in it, but you can transmit anything you like */
527 static const struct ieee80211_txrx_stypes
528 ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
529 	[NL80211_IFTYPE_ADHOC] = {
530 		.tx = 0xffff,
531 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
532 			BIT(IEEE80211_STYPE_AUTH >> 4) |
533 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
534 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
535 	},
536 	[NL80211_IFTYPE_STATION] = {
537 		.tx = 0xffff,
538 		/*
539 		 * To support Pre Association Security Negotiation (PASN) while
540 		 * already associated to one AP, allow user space to register to
541 		 * Rx authentication frames, so that the user space logic would
542 		 * be able to receive/handle authentication frames from a
543 		 * different AP as part of PASN.
544 		 * It is expected that user space would intelligently register
545 		 * for Rx authentication frames, i.e., only when PASN is used
546 		 * and configure a match filter only for PASN authentication
547 		 * algorithm, as otherwise the MLME functionality of mac80211
548 		 * would be broken.
549 		 */
550 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
551 			BIT(IEEE80211_STYPE_AUTH >> 4) |
552 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
553 	},
554 	[NL80211_IFTYPE_AP] = {
555 		.tx = 0xffff,
556 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
557 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
558 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
559 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
560 			BIT(IEEE80211_STYPE_AUTH >> 4) |
561 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
562 			BIT(IEEE80211_STYPE_ACTION >> 4),
563 	},
564 	[NL80211_IFTYPE_AP_VLAN] = {
565 		/* copy AP */
566 		.tx = 0xffff,
567 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
568 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
569 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
570 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
571 			BIT(IEEE80211_STYPE_AUTH >> 4) |
572 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
573 			BIT(IEEE80211_STYPE_ACTION >> 4),
574 	},
575 	[NL80211_IFTYPE_P2P_CLIENT] = {
576 		.tx = 0xffff,
577 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
578 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
579 	},
580 	[NL80211_IFTYPE_P2P_GO] = {
581 		.tx = 0xffff,
582 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
583 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
584 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
585 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
586 			BIT(IEEE80211_STYPE_AUTH >> 4) |
587 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
588 			BIT(IEEE80211_STYPE_ACTION >> 4),
589 	},
590 	[NL80211_IFTYPE_MESH_POINT] = {
591 		.tx = 0xffff,
592 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
593 			BIT(IEEE80211_STYPE_AUTH >> 4) |
594 			BIT(IEEE80211_STYPE_DEAUTH >> 4),
595 	},
596 	[NL80211_IFTYPE_P2P_DEVICE] = {
597 		.tx = 0xffff,
598 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
599 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
600 	},
601 };
602 
603 static const struct ieee80211_ht_cap mac80211_ht_capa_mod_mask = {
604 	.ampdu_params_info = IEEE80211_HT_AMPDU_PARM_FACTOR |
605 			     IEEE80211_HT_AMPDU_PARM_DENSITY,
606 
607 	.cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
608 				IEEE80211_HT_CAP_MAX_AMSDU |
609 				IEEE80211_HT_CAP_SGI_20 |
610 				IEEE80211_HT_CAP_SGI_40 |
611 				IEEE80211_HT_CAP_TX_STBC |
612 				IEEE80211_HT_CAP_RX_STBC |
613 				IEEE80211_HT_CAP_LDPC_CODING |
614 				IEEE80211_HT_CAP_40MHZ_INTOLERANT),
615 	.mcs = {
616 		.rx_mask = { 0xff, 0xff, 0xff, 0xff, 0xff,
617 			     0xff, 0xff, 0xff, 0xff, 0xff, },
618 	},
619 };
620 
621 static const struct ieee80211_vht_cap mac80211_vht_capa_mod_mask = {
622 	.vht_cap_info =
623 		cpu_to_le32(IEEE80211_VHT_CAP_RXLDPC |
624 			    IEEE80211_VHT_CAP_SHORT_GI_80 |
625 			    IEEE80211_VHT_CAP_SHORT_GI_160 |
626 			    IEEE80211_VHT_CAP_RXSTBC_MASK |
627 			    IEEE80211_VHT_CAP_TXSTBC |
628 			    IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
629 			    IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
630 			    IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
631 			    IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
632 			    IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK),
633 	.supp_mcs = {
634 		.rx_mcs_map = cpu_to_le16(~0),
635 		.tx_mcs_map = cpu_to_le16(~0),
636 	},
637 };
638 
639 struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
640 					   const struct ieee80211_ops *ops,
641 					   const char *requested_name)
642 {
643 	struct ieee80211_local *local;
644 	int priv_size, i;
645 	struct wiphy *wiphy;
646 	bool use_chanctx;
647 
648 	if (WARN_ON(!ops->tx || !ops->start || !ops->stop || !ops->config ||
649 		    !ops->add_interface || !ops->remove_interface ||
650 		    !ops->configure_filter || !ops->wake_tx_queue))
651 		return NULL;
652 
653 	if (WARN_ON(ops->sta_state && (ops->sta_add || ops->sta_remove)))
654 		return NULL;
655 
656 	if (WARN_ON(!!ops->link_info_changed != !!ops->vif_cfg_changed ||
657 		    (ops->link_info_changed && ops->bss_info_changed)))
658 		return NULL;
659 
660 	/* check all or no channel context operations exist */
661 	i = !!ops->add_chanctx + !!ops->remove_chanctx +
662 	    !!ops->change_chanctx + !!ops->assign_vif_chanctx +
663 	    !!ops->unassign_vif_chanctx;
664 	if (WARN_ON(i != 0 && i != 5))
665 		return NULL;
666 	use_chanctx = i == 5;
667 
668 	/* Ensure 32-byte alignment of our private data and hw private data.
669 	 * We use the wiphy priv data for both our ieee80211_local and for
670 	 * the driver's private data
671 	 *
672 	 * In memory it'll be like this:
673 	 *
674 	 * +-------------------------+
675 	 * | struct wiphy	    |
676 	 * +-------------------------+
677 	 * | struct ieee80211_local  |
678 	 * +-------------------------+
679 	 * | driver's private data   |
680 	 * +-------------------------+
681 	 *
682 	 */
683 	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
684 
685 	wiphy = wiphy_new_nm(&mac80211_config_ops, priv_size, requested_name);
686 
687 	if (!wiphy)
688 		return NULL;
689 
690 	wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
691 
692 	wiphy->privid = mac80211_wiphy_privid;
693 
694 	wiphy->flags |= WIPHY_FLAG_NETNS_OK |
695 			WIPHY_FLAG_4ADDR_AP |
696 			WIPHY_FLAG_4ADDR_STATION |
697 			WIPHY_FLAG_REPORTS_OBSS |
698 			WIPHY_FLAG_OFFCHAN_TX;
699 
700 	if (!use_chanctx || ops->remain_on_channel)
701 		wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
702 
703 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS |
704 			   NL80211_FEATURE_SAE |
705 			   NL80211_FEATURE_HT_IBSS |
706 			   NL80211_FEATURE_VIF_TXPOWER |
707 			   NL80211_FEATURE_MAC_ON_CREATE |
708 			   NL80211_FEATURE_USERSPACE_MPM |
709 			   NL80211_FEATURE_FULL_AP_CLIENT_STATE;
710 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_STA);
711 	wiphy_ext_feature_set(wiphy,
712 			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211);
713 	wiphy_ext_feature_set(wiphy,
714 			      NL80211_EXT_FEATURE_CONTROL_PORT_NO_PREAUTH);
715 	wiphy_ext_feature_set(wiphy,
716 			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211_TX_STATUS);
717 	wiphy_ext_feature_set(wiphy,
718 			      NL80211_EXT_FEATURE_SCAN_FREQ_KHZ);
719 	wiphy_ext_feature_set(wiphy,
720 			      NL80211_EXT_FEATURE_POWERED_ADDR_CHANGE);
721 
722 	if (!ops->hw_scan) {
723 		wiphy->features |= NL80211_FEATURE_LOW_PRIORITY_SCAN |
724 				   NL80211_FEATURE_AP_SCAN;
725 		/*
726 		 * if the driver behaves correctly using the probe request
727 		 * (template) from mac80211, then both of these should be
728 		 * supported even with hw scan - but let drivers opt in.
729 		 */
730 		wiphy_ext_feature_set(wiphy,
731 				      NL80211_EXT_FEATURE_SCAN_RANDOM_SN);
732 		wiphy_ext_feature_set(wiphy,
733 				      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
734 	}
735 
736 	if (!ops->set_key)
737 		wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
738 
739 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_TXQS);
740 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_RRM);
741 
742 	wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
743 
744 	local = wiphy_priv(wiphy);
745 
746 	if (sta_info_init(local))
747 		goto err_free;
748 
749 	local->hw.wiphy = wiphy;
750 
751 	local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
752 
753 	local->ops = ops;
754 	local->use_chanctx = use_chanctx;
755 
756 	/*
757 	 * We need a bit of data queued to build aggregates properly, so
758 	 * instruct the TCP stack to allow more than a single ms of data
759 	 * to be queued in the stack. The value is a bit-shift of 1
760 	 * second, so 7 is ~8ms of queued data. Only affects local TCP
761 	 * sockets.
762 	 * This is the default, anyhow - drivers may need to override it
763 	 * for local reasons (longer buffers, longer completion time, or
764 	 * similar).
765 	 */
766 	local->hw.tx_sk_pacing_shift = 7;
767 
768 	/* set up some defaults */
769 	local->hw.queues = 1;
770 	local->hw.max_rates = 1;
771 	local->hw.max_report_rates = 0;
772 	local->hw.max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HT;
773 	local->hw.max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HT;
774 	local->hw.offchannel_tx_hw_queue = IEEE80211_INVAL_HW_QUEUE;
775 	local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
776 	local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
777 	local->hw.radiotap_mcs_details = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
778 					 IEEE80211_RADIOTAP_MCS_HAVE_GI |
779 					 IEEE80211_RADIOTAP_MCS_HAVE_BW;
780 	local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
781 					 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
782 	local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
783 	local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
784 	local->hw.max_mtu = IEEE80211_MAX_DATA_LEN;
785 	local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
786 	wiphy->ht_capa_mod_mask = &mac80211_ht_capa_mod_mask;
787 	wiphy->vht_capa_mod_mask = &mac80211_vht_capa_mod_mask;
788 
789 	local->ext_capa[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF;
790 
791 	wiphy->extended_capabilities = local->ext_capa;
792 	wiphy->extended_capabilities_mask = local->ext_capa;
793 	wiphy->extended_capabilities_len =
794 		ARRAY_SIZE(local->ext_capa);
795 
796 	INIT_LIST_HEAD(&local->interfaces);
797 	INIT_LIST_HEAD(&local->mon_list);
798 
799 	__hw_addr_init(&local->mc_list);
800 
801 	mutex_init(&local->iflist_mtx);
802 	spin_lock_init(&local->filter_lock);
803 	spin_lock_init(&local->rx_path_lock);
804 	spin_lock_init(&local->queue_stop_reason_lock);
805 
806 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
807 		INIT_LIST_HEAD(&local->active_txqs[i]);
808 		spin_lock_init(&local->active_txq_lock[i]);
809 		local->aql_txq_limit_low[i] = IEEE80211_DEFAULT_AQL_TXQ_LIMIT_L;
810 		local->aql_txq_limit_high[i] =
811 			IEEE80211_DEFAULT_AQL_TXQ_LIMIT_H;
812 		atomic_set(&local->aql_ac_pending_airtime[i], 0);
813 	}
814 
815 	local->airtime_flags = AIRTIME_USE_TX | AIRTIME_USE_RX;
816 	local->aql_threshold = IEEE80211_AQL_THRESHOLD;
817 	atomic_set(&local->aql_total_pending_airtime, 0);
818 
819 	spin_lock_init(&local->handle_wake_tx_queue_lock);
820 
821 	INIT_LIST_HEAD(&local->chanctx_list);
822 
823 	wiphy_delayed_work_init(&local->scan_work, ieee80211_scan_work);
824 
825 	INIT_WORK(&local->restart_work, ieee80211_restart_work);
826 
827 	wiphy_work_init(&local->radar_detected_work,
828 			ieee80211_dfs_radar_detected_work);
829 
830 	wiphy_work_init(&local->reconfig_filter, ieee80211_reconfig_filter);
831 	local->smps_mode = IEEE80211_SMPS_OFF;
832 
833 	wiphy_work_init(&local->dynamic_ps_enable_work,
834 			ieee80211_dynamic_ps_enable_work);
835 	wiphy_work_init(&local->dynamic_ps_disable_work,
836 			ieee80211_dynamic_ps_disable_work);
837 	timer_setup(&local->dynamic_ps_timer, ieee80211_dynamic_ps_timer, 0);
838 
839 	wiphy_work_init(&local->sched_scan_stopped_work,
840 			ieee80211_sched_scan_stopped_work);
841 
842 	spin_lock_init(&local->ack_status_lock);
843 	idr_init(&local->ack_status_frames);
844 
845 	for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
846 		skb_queue_head_init(&local->pending[i]);
847 		atomic_set(&local->agg_queue_stop[i], 0);
848 	}
849 	tasklet_setup(&local->tx_pending_tasklet, ieee80211_tx_pending);
850 	tasklet_setup(&local->wake_txqs_tasklet, ieee80211_wake_txqs);
851 	tasklet_setup(&local->tasklet, ieee80211_tasklet_handler);
852 
853 	skb_queue_head_init(&local->skb_queue);
854 	skb_queue_head_init(&local->skb_queue_unreliable);
855 
856 	ieee80211_alloc_led_names(local);
857 
858 	ieee80211_roc_setup(local);
859 
860 	local->hw.radiotap_timestamp.units_pos = -1;
861 	local->hw.radiotap_timestamp.accuracy = -1;
862 
863 	return &local->hw;
864  err_free:
865 	wiphy_free(wiphy);
866 	return NULL;
867 }
868 EXPORT_SYMBOL(ieee80211_alloc_hw_nm);
869 
870 static int ieee80211_init_cipher_suites(struct ieee80211_local *local)
871 {
872 	bool have_wep = !fips_enabled; /* FIPS does not permit the use of RC4 */
873 	bool have_mfp = ieee80211_hw_check(&local->hw, MFP_CAPABLE);
874 	int r = 0, w = 0;
875 	u32 *suites;
876 	static const u32 cipher_suites[] = {
877 		/* keep WEP first, it may be removed below */
878 		WLAN_CIPHER_SUITE_WEP40,
879 		WLAN_CIPHER_SUITE_WEP104,
880 		WLAN_CIPHER_SUITE_TKIP,
881 		WLAN_CIPHER_SUITE_CCMP,
882 		WLAN_CIPHER_SUITE_CCMP_256,
883 		WLAN_CIPHER_SUITE_GCMP,
884 		WLAN_CIPHER_SUITE_GCMP_256,
885 
886 		/* keep last -- depends on hw flags! */
887 		WLAN_CIPHER_SUITE_AES_CMAC,
888 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
889 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
890 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
891 	};
892 
893 	if (ieee80211_hw_check(&local->hw, SW_CRYPTO_CONTROL) ||
894 	    local->hw.wiphy->cipher_suites) {
895 		/* If the driver advertises, or doesn't support SW crypto,
896 		 * we only need to remove WEP if necessary.
897 		 */
898 		if (have_wep)
899 			return 0;
900 
901 		/* well if it has _no_ ciphers ... fine */
902 		if (!local->hw.wiphy->n_cipher_suites)
903 			return 0;
904 
905 		/* Driver provides cipher suites, but we need to exclude WEP */
906 		suites = kmemdup(local->hw.wiphy->cipher_suites,
907 				 sizeof(u32) * local->hw.wiphy->n_cipher_suites,
908 				 GFP_KERNEL);
909 		if (!suites)
910 			return -ENOMEM;
911 
912 		for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
913 			u32 suite = local->hw.wiphy->cipher_suites[r];
914 
915 			if (suite == WLAN_CIPHER_SUITE_WEP40 ||
916 			    suite == WLAN_CIPHER_SUITE_WEP104)
917 				continue;
918 			suites[w++] = suite;
919 		}
920 	} else {
921 		/* assign the (software supported and perhaps offloaded)
922 		 * cipher suites
923 		 */
924 		local->hw.wiphy->cipher_suites = cipher_suites;
925 		local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
926 
927 		if (!have_mfp)
928 			local->hw.wiphy->n_cipher_suites -= 4;
929 
930 		if (!have_wep) {
931 			local->hw.wiphy->cipher_suites += 2;
932 			local->hw.wiphy->n_cipher_suites -= 2;
933 		}
934 
935 		/* not dynamically allocated, so just return */
936 		return 0;
937 	}
938 
939 	local->hw.wiphy->cipher_suites = suites;
940 	local->hw.wiphy->n_cipher_suites = w;
941 	local->wiphy_ciphers_allocated = true;
942 
943 	return 0;
944 }
945 
946 int ieee80211_register_hw(struct ieee80211_hw *hw)
947 {
948 	struct ieee80211_local *local = hw_to_local(hw);
949 	int result, i;
950 	enum nl80211_band band;
951 	int channels, max_bitrates;
952 	bool supp_ht, supp_vht, supp_he, supp_eht;
953 	struct cfg80211_chan_def dflt_chandef = {};
954 
955 	if (ieee80211_hw_check(hw, QUEUE_CONTROL) &&
956 	    (local->hw.offchannel_tx_hw_queue == IEEE80211_INVAL_HW_QUEUE ||
957 	     local->hw.offchannel_tx_hw_queue >= local->hw.queues))
958 		return -EINVAL;
959 
960 	if ((hw->wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
961 	    (!local->ops->tdls_channel_switch ||
962 	     !local->ops->tdls_cancel_channel_switch ||
963 	     !local->ops->tdls_recv_channel_switch))
964 		return -EOPNOTSUPP;
965 
966 	if (WARN_ON(ieee80211_hw_check(hw, SUPPORTS_TX_FRAG) &&
967 		    !local->ops->set_frag_threshold))
968 		return -EINVAL;
969 
970 	if (WARN_ON(local->hw.wiphy->interface_modes &
971 			BIT(NL80211_IFTYPE_NAN) &&
972 		    (!local->ops->start_nan || !local->ops->stop_nan)))
973 		return -EINVAL;
974 
975 	if (hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO) {
976 		/*
977 		 * For drivers capable of doing MLO, assume modern driver
978 		 * or firmware facilities, so software doesn't have to do
979 		 * as much, e.g. monitoring beacons would be hard if we
980 		 * might not even know which link is active at which time.
981 		 */
982 		if (WARN_ON(!local->use_chanctx))
983 			return -EINVAL;
984 
985 		if (WARN_ON(!local->ops->link_info_changed))
986 			return -EINVAL;
987 
988 		if (WARN_ON(!ieee80211_hw_check(hw, HAS_RATE_CONTROL)))
989 			return -EINVAL;
990 
991 		if (WARN_ON(!ieee80211_hw_check(hw, AMPDU_AGGREGATION)))
992 			return -EINVAL;
993 
994 		if (WARN_ON(ieee80211_hw_check(hw, HOST_BROADCAST_PS_BUFFERING)))
995 			return -EINVAL;
996 
997 		if (WARN_ON(ieee80211_hw_check(hw, SUPPORTS_PS) &&
998 			    (!ieee80211_hw_check(hw, SUPPORTS_DYNAMIC_PS) ||
999 			     ieee80211_hw_check(hw, PS_NULLFUNC_STACK))))
1000 			return -EINVAL;
1001 
1002 		if (WARN_ON(!ieee80211_hw_check(hw, MFP_CAPABLE)))
1003 			return -EINVAL;
1004 
1005 		if (WARN_ON(!ieee80211_hw_check(hw, CONNECTION_MONITOR)))
1006 			return -EINVAL;
1007 
1008 		if (WARN_ON(ieee80211_hw_check(hw, NEED_DTIM_BEFORE_ASSOC)))
1009 			return -EINVAL;
1010 
1011 		if (WARN_ON(ieee80211_hw_check(hw, TIMING_BEACON_ONLY)))
1012 			return -EINVAL;
1013 
1014 		if (WARN_ON(!ieee80211_hw_check(hw, AP_LINK_PS)))
1015 			return -EINVAL;
1016 
1017 		if (WARN_ON(ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP)))
1018 			return -EINVAL;
1019 	}
1020 
1021 #ifdef CONFIG_PM
1022 	if (hw->wiphy->wowlan && (!local->ops->suspend || !local->ops->resume))
1023 		return -EINVAL;
1024 #endif
1025 
1026 	if (!local->use_chanctx) {
1027 		for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
1028 			const struct ieee80211_iface_combination *comb;
1029 
1030 			comb = &local->hw.wiphy->iface_combinations[i];
1031 
1032 			if (comb->num_different_channels > 1)
1033 				return -EINVAL;
1034 		}
1035 	} else {
1036 		/* DFS is not supported with multi-channel combinations yet */
1037 		for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
1038 			const struct ieee80211_iface_combination *comb;
1039 
1040 			comb = &local->hw.wiphy->iface_combinations[i];
1041 
1042 			if (comb->radar_detect_widths &&
1043 			    comb->num_different_channels > 1)
1044 				return -EINVAL;
1045 		}
1046 	}
1047 
1048 	/* Only HW csum features are currently compatible with mac80211 */
1049 	if (WARN_ON(hw->netdev_features & ~MAC80211_SUPPORTED_FEATURES))
1050 		return -EINVAL;
1051 
1052 	if (hw->max_report_rates == 0)
1053 		hw->max_report_rates = hw->max_rates;
1054 
1055 	local->rx_chains = 1;
1056 
1057 	/*
1058 	 * generic code guarantees at least one band,
1059 	 * set this very early because much code assumes
1060 	 * that hw.conf.channel is assigned
1061 	 */
1062 	channels = 0;
1063 	max_bitrates = 0;
1064 	supp_ht = false;
1065 	supp_vht = false;
1066 	supp_he = false;
1067 	supp_eht = false;
1068 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1069 		const struct ieee80211_sband_iftype_data *iftd;
1070 		struct ieee80211_supported_band *sband;
1071 
1072 		sband = local->hw.wiphy->bands[band];
1073 		if (!sband)
1074 			continue;
1075 
1076 		if (!dflt_chandef.chan) {
1077 			/*
1078 			 * Assign the first enabled channel to dflt_chandef
1079 			 * from the list of channels
1080 			 */
1081 			for (i = 0; i < sband->n_channels; i++)
1082 				if (!(sband->channels[i].flags &
1083 						IEEE80211_CHAN_DISABLED))
1084 					break;
1085 			/* if none found then use the first anyway */
1086 			if (i == sband->n_channels)
1087 				i = 0;
1088 			cfg80211_chandef_create(&dflt_chandef,
1089 						&sband->channels[i],
1090 						NL80211_CHAN_NO_HT);
1091 			/* init channel we're on */
1092 			if (!local->use_chanctx && !local->_oper_chandef.chan) {
1093 				local->hw.conf.chandef = dflt_chandef;
1094 				local->_oper_chandef = dflt_chandef;
1095 			}
1096 			local->monitor_chandef = dflt_chandef;
1097 		}
1098 
1099 		channels += sband->n_channels;
1100 
1101 		/*
1102 		 * Due to the way the aggregation code handles this and it
1103 		 * being an HT capability, we can't really support delayed
1104 		 * BA in MLO (yet).
1105 		 */
1106 		if (WARN_ON(sband->ht_cap.ht_supported &&
1107 			    (sband->ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA) &&
1108 			    hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
1109 			return -EINVAL;
1110 
1111 		if (max_bitrates < sband->n_bitrates)
1112 			max_bitrates = sband->n_bitrates;
1113 		supp_ht = supp_ht || sband->ht_cap.ht_supported;
1114 		supp_vht = supp_vht || sband->vht_cap.vht_supported;
1115 
1116 		for_each_sband_iftype_data(sband, i, iftd) {
1117 			supp_he = supp_he || iftd->he_cap.has_he;
1118 			supp_eht = supp_eht || iftd->eht_cap.has_eht;
1119 		}
1120 
1121 		/* HT, VHT, HE require QoS, thus >= 4 queues */
1122 		if (WARN_ON(local->hw.queues < IEEE80211_NUM_ACS &&
1123 			    (supp_ht || supp_vht || supp_he)))
1124 			return -EINVAL;
1125 
1126 		/* EHT requires HE support */
1127 		if (WARN_ON(supp_eht && !supp_he))
1128 			return -EINVAL;
1129 
1130 		if (!sband->ht_cap.ht_supported)
1131 			continue;
1132 
1133 		/* TODO: consider VHT for RX chains, hopefully it's the same */
1134 		local->rx_chains =
1135 			max(ieee80211_mcs_to_chains(&sband->ht_cap.mcs),
1136 			    local->rx_chains);
1137 
1138 		/* no need to mask, SM_PS_DISABLED has all bits set */
1139 		sband->ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
1140 			             IEEE80211_HT_CAP_SM_PS_SHIFT;
1141 	}
1142 
1143 	/* if low-level driver supports AP, we also support VLAN.
1144 	 * drivers advertising SW_CRYPTO_CONTROL should enable AP_VLAN
1145 	 * based on their support to transmit SW encrypted packets.
1146 	 */
1147 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP) &&
1148 	    !ieee80211_hw_check(&local->hw, SW_CRYPTO_CONTROL)) {
1149 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
1150 		hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
1151 	}
1152 
1153 	/* mac80211 always supports monitor */
1154 	hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
1155 	hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
1156 
1157 	/* mac80211 doesn't support more than one IBSS interface right now */
1158 	for (i = 0; i < hw->wiphy->n_iface_combinations; i++) {
1159 		const struct ieee80211_iface_combination *c;
1160 		int j;
1161 
1162 		c = &hw->wiphy->iface_combinations[i];
1163 
1164 		for (j = 0; j < c->n_limits; j++)
1165 			if ((c->limits[j].types & BIT(NL80211_IFTYPE_ADHOC)) &&
1166 			    c->limits[j].max > 1)
1167 				return -EINVAL;
1168 	}
1169 
1170 	local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
1171 				      sizeof(void *) * channels, GFP_KERNEL);
1172 	if (!local->int_scan_req)
1173 		return -ENOMEM;
1174 
1175 	eth_broadcast_addr(local->int_scan_req->bssid);
1176 
1177 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1178 		if (!local->hw.wiphy->bands[band])
1179 			continue;
1180 		local->int_scan_req->rates[band] = (u32) -1;
1181 	}
1182 
1183 #ifndef CONFIG_MAC80211_MESH
1184 	/* mesh depends on Kconfig, but drivers should set it if they want */
1185 	local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
1186 #endif
1187 
1188 	/* if the underlying driver supports mesh, mac80211 will (at least)
1189 	 * provide routing of mesh authentication frames to userspace */
1190 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
1191 		local->hw.wiphy->flags |= WIPHY_FLAG_MESH_AUTH;
1192 
1193 	/* mac80211 supports control port protocol changing */
1194 	local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
1195 
1196 	if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) {
1197 		local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1198 	} else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) {
1199 		local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1200 		if (hw->max_signal <= 0) {
1201 			result = -EINVAL;
1202 			goto fail_workqueue;
1203 		}
1204 	}
1205 
1206 	/* Mac80211 and therefore all drivers using SW crypto only
1207 	 * are able to handle PTK rekeys and Extended Key ID.
1208 	 */
1209 	if (!local->ops->set_key) {
1210 		wiphy_ext_feature_set(local->hw.wiphy,
1211 				      NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
1212 		wiphy_ext_feature_set(local->hw.wiphy,
1213 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
1214 	}
1215 
1216 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_ADHOC))
1217 		wiphy_ext_feature_set(local->hw.wiphy,
1218 				      NL80211_EXT_FEATURE_DEL_IBSS_STA);
1219 
1220 	/*
1221 	 * Calculate scan IE length -- we need this to alloc
1222 	 * memory and to subtract from the driver limit. It
1223 	 * includes the DS Params, (extended) supported rates, and HT
1224 	 * information -- SSID is the driver's responsibility.
1225 	 */
1226 	local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
1227 		3 /* DS Params */;
1228 	if (supp_ht)
1229 		local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
1230 
1231 	if (supp_vht)
1232 		local->scan_ies_len +=
1233 			2 + sizeof(struct ieee80211_vht_cap);
1234 
1235 	/*
1236 	 * HE cap element is variable in size - set len to allow max size */
1237 	if (supp_he) {
1238 		local->scan_ies_len +=
1239 			3 + sizeof(struct ieee80211_he_cap_elem) +
1240 			sizeof(struct ieee80211_he_mcs_nss_supp) +
1241 			IEEE80211_HE_PPE_THRES_MAX_LEN;
1242 
1243 		if (supp_eht)
1244 			local->scan_ies_len +=
1245 				3 + sizeof(struct ieee80211_eht_cap_elem) +
1246 				sizeof(struct ieee80211_eht_mcs_nss_supp) +
1247 				IEEE80211_EHT_PPE_THRES_MAX_LEN;
1248 	}
1249 
1250 	if (!local->ops->hw_scan) {
1251 		/* For hw_scan, driver needs to set these up. */
1252 		local->hw.wiphy->max_scan_ssids = 4;
1253 		local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
1254 	}
1255 
1256 	/*
1257 	 * If the driver supports any scan IEs, then assume the
1258 	 * limit includes the IEs mac80211 will add, otherwise
1259 	 * leave it at zero and let the driver sort it out; we
1260 	 * still pass our IEs to the driver but userspace will
1261 	 * not be allowed to in that case.
1262 	 */
1263 	if (local->hw.wiphy->max_scan_ie_len)
1264 		local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
1265 
1266 	result = ieee80211_init_cipher_suites(local);
1267 	if (result < 0)
1268 		goto fail_workqueue;
1269 
1270 	if (!local->ops->remain_on_channel)
1271 		local->hw.wiphy->max_remain_on_channel_duration = 5000;
1272 
1273 	/* mac80211 based drivers don't support internal TDLS setup */
1274 	if (local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS)
1275 		local->hw.wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
1276 
1277 	/* mac80211 supports eCSA, if the driver supports STA CSA at all */
1278 	if (ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA))
1279 		local->ext_capa[0] |= WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING;
1280 
1281 	/* mac80211 supports multi BSSID, if the driver supports it */
1282 	if (ieee80211_hw_check(&local->hw, SUPPORTS_MULTI_BSSID)) {
1283 		local->hw.wiphy->support_mbssid = true;
1284 		if (ieee80211_hw_check(&local->hw,
1285 				       SUPPORTS_ONLY_HE_MULTI_BSSID))
1286 			local->hw.wiphy->support_only_he_mbssid = true;
1287 		else
1288 			local->ext_capa[2] |=
1289 				WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
1290 	}
1291 
1292 	local->hw.wiphy->max_num_csa_counters = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
1293 
1294 	/*
1295 	 * We use the number of queues for feature tests (QoS, HT) internally
1296 	 * so restrict them appropriately.
1297 	 */
1298 	if (hw->queues > IEEE80211_MAX_QUEUES)
1299 		hw->queues = IEEE80211_MAX_QUEUES;
1300 
1301 	local->workqueue =
1302 		alloc_ordered_workqueue("%s", 0, wiphy_name(local->hw.wiphy));
1303 	if (!local->workqueue) {
1304 		result = -ENOMEM;
1305 		goto fail_workqueue;
1306 	}
1307 
1308 	/*
1309 	 * The hardware needs headroom for sending the frame,
1310 	 * and we need some headroom for passing the frame to monitor
1311 	 * interfaces, but never both at the same time.
1312 	 */
1313 	local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1314 				   IEEE80211_TX_STATUS_HEADROOM);
1315 
1316 	/*
1317 	 * if the driver doesn't specify a max listen interval we
1318 	 * use 5 which should be a safe default
1319 	 */
1320 	if (local->hw.max_listen_interval == 0)
1321 		local->hw.max_listen_interval = 5;
1322 
1323 	local->hw.conf.listen_interval = local->hw.max_listen_interval;
1324 
1325 	local->dynamic_ps_forced_timeout = -1;
1326 
1327 	if (!local->hw.max_nan_de_entries)
1328 		local->hw.max_nan_de_entries = IEEE80211_MAX_NAN_INSTANCE_ID;
1329 
1330 	if (!local->hw.weight_multiplier)
1331 		local->hw.weight_multiplier = 1;
1332 
1333 	ieee80211_wep_init(local);
1334 
1335 	local->hw.conf.flags = IEEE80211_CONF_IDLE;
1336 
1337 	ieee80211_led_init(local);
1338 
1339 	result = ieee80211_txq_setup_flows(local);
1340 	if (result)
1341 		goto fail_flows;
1342 
1343 	rtnl_lock();
1344 	result = ieee80211_init_rate_ctrl_alg(local,
1345 					      hw->rate_control_algorithm);
1346 	rtnl_unlock();
1347 	if (result < 0) {
1348 		wiphy_debug(local->hw.wiphy,
1349 			    "Failed to initialize rate control algorithm\n");
1350 		goto fail_rate;
1351 	}
1352 
1353 	if (local->rate_ctrl) {
1354 		clear_bit(IEEE80211_HW_SUPPORTS_VHT_EXT_NSS_BW, hw->flags);
1355 		if (local->rate_ctrl->ops->capa & RATE_CTRL_CAPA_VHT_EXT_NSS_BW)
1356 			ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
1357 	}
1358 
1359 	/*
1360 	 * If the VHT capabilities don't have IEEE80211_VHT_EXT_NSS_BW_CAPABLE,
1361 	 * or have it when we don't, copy the sband structure and set/clear it.
1362 	 * This is necessary because rate scaling algorithms could be switched
1363 	 * and have different support values.
1364 	 * Print a message so that in the common case the reallocation can be
1365 	 * avoided.
1366 	 */
1367 	BUILD_BUG_ON(NUM_NL80211_BANDS > 8 * sizeof(local->sband_allocated));
1368 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1369 		struct ieee80211_supported_band *sband;
1370 		bool local_cap, ie_cap;
1371 
1372 		local_cap = ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW);
1373 
1374 		sband = local->hw.wiphy->bands[band];
1375 		if (!sband || !sband->vht_cap.vht_supported)
1376 			continue;
1377 
1378 		ie_cap = !!(sband->vht_cap.vht_mcs.tx_highest &
1379 			    cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE));
1380 
1381 		if (local_cap == ie_cap)
1382 			continue;
1383 
1384 		sband = kmemdup(sband, sizeof(*sband), GFP_KERNEL);
1385 		if (!sband) {
1386 			result = -ENOMEM;
1387 			goto fail_rate;
1388 		}
1389 
1390 		wiphy_dbg(hw->wiphy, "copying sband (band %d) due to VHT EXT NSS BW flag\n",
1391 			  band);
1392 
1393 		sband->vht_cap.vht_mcs.tx_highest ^=
1394 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
1395 
1396 		local->hw.wiphy->bands[band] = sband;
1397 		local->sband_allocated |= BIT(band);
1398 	}
1399 
1400 	result = wiphy_register(local->hw.wiphy);
1401 	if (result < 0)
1402 		goto fail_wiphy_register;
1403 
1404 	debugfs_hw_add(local);
1405 	rate_control_add_debugfs(local);
1406 
1407 	rtnl_lock();
1408 	wiphy_lock(hw->wiphy);
1409 
1410 	/* add one default STA interface if supported */
1411 	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) &&
1412 	    !ieee80211_hw_check(hw, NO_AUTO_VIF)) {
1413 		struct vif_params params = {0};
1414 
1415 		result = ieee80211_if_add(local, "wlan%d", NET_NAME_ENUM, NULL,
1416 					  NL80211_IFTYPE_STATION, &params);
1417 		if (result)
1418 			wiphy_warn(local->hw.wiphy,
1419 				   "Failed to add default virtual iface\n");
1420 	}
1421 
1422 	wiphy_unlock(hw->wiphy);
1423 	rtnl_unlock();
1424 
1425 #ifdef CONFIG_INET
1426 	local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
1427 	result = register_inetaddr_notifier(&local->ifa_notifier);
1428 	if (result)
1429 		goto fail_ifa;
1430 #endif
1431 
1432 #if IS_ENABLED(CONFIG_IPV6)
1433 	local->ifa6_notifier.notifier_call = ieee80211_ifa6_changed;
1434 	result = register_inet6addr_notifier(&local->ifa6_notifier);
1435 	if (result)
1436 		goto fail_ifa6;
1437 #endif
1438 
1439 	return 0;
1440 
1441 #if IS_ENABLED(CONFIG_IPV6)
1442  fail_ifa6:
1443 #ifdef CONFIG_INET
1444 	unregister_inetaddr_notifier(&local->ifa_notifier);
1445 #endif
1446 #endif
1447 #if defined(CONFIG_INET) || defined(CONFIG_IPV6)
1448  fail_ifa:
1449 #endif
1450 	wiphy_unregister(local->hw.wiphy);
1451  fail_wiphy_register:
1452 	rtnl_lock();
1453 	rate_control_deinitialize(local);
1454 	ieee80211_remove_interfaces(local);
1455 	rtnl_unlock();
1456  fail_rate:
1457 	ieee80211_txq_teardown_flows(local);
1458  fail_flows:
1459 	ieee80211_led_exit(local);
1460 	destroy_workqueue(local->workqueue);
1461  fail_workqueue:
1462 	if (local->wiphy_ciphers_allocated) {
1463 		kfree(local->hw.wiphy->cipher_suites);
1464 		local->wiphy_ciphers_allocated = false;
1465 	}
1466 	kfree(local->int_scan_req);
1467 	return result;
1468 }
1469 EXPORT_SYMBOL(ieee80211_register_hw);
1470 
1471 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1472 {
1473 	struct ieee80211_local *local = hw_to_local(hw);
1474 
1475 	tasklet_kill(&local->tx_pending_tasklet);
1476 	tasklet_kill(&local->tasklet);
1477 
1478 #ifdef CONFIG_INET
1479 	unregister_inetaddr_notifier(&local->ifa_notifier);
1480 #endif
1481 #if IS_ENABLED(CONFIG_IPV6)
1482 	unregister_inet6addr_notifier(&local->ifa6_notifier);
1483 #endif
1484 
1485 	rtnl_lock();
1486 
1487 	/*
1488 	 * At this point, interface list manipulations are fine
1489 	 * because the driver cannot be handing us frames any
1490 	 * more and the tasklet is killed.
1491 	 */
1492 	ieee80211_remove_interfaces(local);
1493 
1494 	ieee80211_txq_teardown_flows(local);
1495 
1496 	wiphy_lock(local->hw.wiphy);
1497 	wiphy_delayed_work_cancel(local->hw.wiphy, &local->roc_work);
1498 	wiphy_work_cancel(local->hw.wiphy, &local->reconfig_filter);
1499 	wiphy_work_cancel(local->hw.wiphy, &local->sched_scan_stopped_work);
1500 	wiphy_work_cancel(local->hw.wiphy, &local->radar_detected_work);
1501 	wiphy_unlock(local->hw.wiphy);
1502 	rtnl_unlock();
1503 
1504 	cancel_work_sync(&local->restart_work);
1505 
1506 	ieee80211_clear_tx_pending(local);
1507 	rate_control_deinitialize(local);
1508 
1509 	if (skb_queue_len(&local->skb_queue) ||
1510 	    skb_queue_len(&local->skb_queue_unreliable))
1511 		wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
1512 	skb_queue_purge(&local->skb_queue);
1513 	skb_queue_purge(&local->skb_queue_unreliable);
1514 
1515 	wiphy_unregister(local->hw.wiphy);
1516 	destroy_workqueue(local->workqueue);
1517 	ieee80211_led_exit(local);
1518 	kfree(local->int_scan_req);
1519 }
1520 EXPORT_SYMBOL(ieee80211_unregister_hw);
1521 
1522 static int ieee80211_free_ack_frame(int id, void *p, void *data)
1523 {
1524 	WARN_ONCE(1, "Have pending ack frames!\n");
1525 	kfree_skb(p);
1526 	return 0;
1527 }
1528 
1529 void ieee80211_free_hw(struct ieee80211_hw *hw)
1530 {
1531 	struct ieee80211_local *local = hw_to_local(hw);
1532 	enum nl80211_band band;
1533 
1534 	mutex_destroy(&local->iflist_mtx);
1535 
1536 	if (local->wiphy_ciphers_allocated) {
1537 		kfree(local->hw.wiphy->cipher_suites);
1538 		local->wiphy_ciphers_allocated = false;
1539 	}
1540 
1541 	idr_for_each(&local->ack_status_frames,
1542 		     ieee80211_free_ack_frame, NULL);
1543 	idr_destroy(&local->ack_status_frames);
1544 
1545 	sta_info_stop(local);
1546 
1547 	ieee80211_free_led_names(local);
1548 
1549 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1550 		if (!(local->sband_allocated & BIT(band)))
1551 			continue;
1552 		kfree(local->hw.wiphy->bands[band]);
1553 	}
1554 
1555 	wiphy_free(local->hw.wiphy);
1556 }
1557 EXPORT_SYMBOL(ieee80211_free_hw);
1558 
1559 static const char * const drop_reasons_monitor[] = {
1560 #define V(x)	#x,
1561 	[0] = "RX_DROP_MONITOR",
1562 	MAC80211_DROP_REASONS_MONITOR(V)
1563 };
1564 
1565 static struct drop_reason_list drop_reason_list_monitor = {
1566 	.reasons = drop_reasons_monitor,
1567 	.n_reasons = ARRAY_SIZE(drop_reasons_monitor),
1568 };
1569 
1570 static const char * const drop_reasons_unusable[] = {
1571 	[0] = "RX_DROP_UNUSABLE",
1572 	MAC80211_DROP_REASONS_UNUSABLE(V)
1573 #undef V
1574 };
1575 
1576 static struct drop_reason_list drop_reason_list_unusable = {
1577 	.reasons = drop_reasons_unusable,
1578 	.n_reasons = ARRAY_SIZE(drop_reasons_unusable),
1579 };
1580 
1581 static int __init ieee80211_init(void)
1582 {
1583 	struct sk_buff *skb;
1584 	int ret;
1585 
1586 	BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
1587 	BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
1588 		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
1589 
1590 	ret = rc80211_minstrel_init();
1591 	if (ret)
1592 		return ret;
1593 
1594 	ret = ieee80211_iface_init();
1595 	if (ret)
1596 		goto err_netdev;
1597 
1598 	drop_reasons_register_subsys(SKB_DROP_REASON_SUBSYS_MAC80211_MONITOR,
1599 				     &drop_reason_list_monitor);
1600 	drop_reasons_register_subsys(SKB_DROP_REASON_SUBSYS_MAC80211_UNUSABLE,
1601 				     &drop_reason_list_unusable);
1602 
1603 	return 0;
1604  err_netdev:
1605 	rc80211_minstrel_exit();
1606 
1607 	return ret;
1608 }
1609 
1610 static void __exit ieee80211_exit(void)
1611 {
1612 	rc80211_minstrel_exit();
1613 
1614 	ieee80211s_stop();
1615 
1616 	ieee80211_iface_exit();
1617 
1618 	drop_reasons_unregister_subsys(SKB_DROP_REASON_SUBSYS_MAC80211_MONITOR);
1619 	drop_reasons_unregister_subsys(SKB_DROP_REASON_SUBSYS_MAC80211_UNUSABLE);
1620 
1621 	rcu_barrier();
1622 }
1623 
1624 
1625 subsys_initcall(ieee80211_init);
1626 module_exit(ieee80211_exit);
1627 
1628 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1629 MODULE_LICENSE("GPL");
1630