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