main.c 44 KB

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