main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <net/mac80211.h>
  12. #include <linux/module.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. spin_lock_bh(&local->filter_lock);
  56. changed_flags = local->filter_flags ^ new_flags;
  57. mc = drv_prepare_multicast(local, &local->mc_list);
  58. spin_unlock_bh(&local->filter_lock);
  59. /* be a bit nasty */
  60. new_flags |= (1<<31);
  61. drv_configure_filter(local, changed_flags, &new_flags, mc);
  62. WARN_ON(new_flags & (1<<31));
  63. local->filter_flags = new_flags & ~(1<<31);
  64. }
  65. static void ieee80211_reconfig_filter(struct work_struct *work)
  66. {
  67. struct ieee80211_local *local =
  68. container_of(work, struct ieee80211_local, reconfig_filter);
  69. ieee80211_configure_filter(local);
  70. }
  71. static u32 ieee80211_hw_conf_chan(struct ieee80211_local *local)
  72. {
  73. struct ieee80211_sub_if_data *sdata;
  74. struct cfg80211_chan_def chandef = {};
  75. u32 changed = 0;
  76. int power;
  77. u32 offchannel_flag;
  78. offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  79. if (local->scan_chandef.chan) {
  80. chandef = local->scan_chandef;
  81. } else if (local->tmp_channel) {
  82. chandef.chan = local->tmp_channel;
  83. chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  84. chandef.center_freq1 = chandef.chan->center_freq;
  85. } else
  86. chandef = local->_oper_chandef;
  87. WARN(!cfg80211_chandef_valid(&chandef),
  88. "control:%d MHz width:%d center: %d/%d MHz",
  89. chandef.chan->center_freq, chandef.width,
  90. chandef.center_freq1, chandef.center_freq2);
  91. if (!cfg80211_chandef_identical(&chandef, &local->_oper_chandef))
  92. local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
  93. else
  94. local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
  95. offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  96. if (offchannel_flag ||
  97. !cfg80211_chandef_identical(&local->hw.conf.chandef,
  98. &local->_oper_chandef)) {
  99. local->hw.conf.chandef = chandef;
  100. changed |= IEEE80211_CONF_CHANGE_CHANNEL;
  101. }
  102. if (!conf_is_ht(&local->hw.conf)) {
  103. /*
  104. * mac80211.h documents that this is only valid
  105. * when the channel is set to an HT type, and
  106. * that otherwise STATIC is used.
  107. */
  108. local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
  109. } else if (local->hw.conf.smps_mode != local->smps_mode) {
  110. local->hw.conf.smps_mode = local->smps_mode;
  111. changed |= IEEE80211_CONF_CHANGE_SMPS;
  112. }
  113. power = ieee80211_chandef_max_power(&chandef);
  114. rcu_read_lock();
  115. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  116. if (!rcu_access_pointer(sdata->vif.chanctx_conf))
  117. continue;
  118. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  119. continue;
  120. power = min(power, sdata->vif.bss_conf.txpower);
  121. }
  122. rcu_read_unlock();
  123. if (local->hw.conf.power_level != power) {
  124. changed |= IEEE80211_CONF_CHANGE_POWER;
  125. local->hw.conf.power_level = power;
  126. }
  127. return changed;
  128. }
  129. int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
  130. {
  131. int ret = 0;
  132. might_sleep();
  133. if (!local->use_chanctx)
  134. changed |= ieee80211_hw_conf_chan(local);
  135. else
  136. changed &= ~(IEEE80211_CONF_CHANGE_CHANNEL |
  137. IEEE80211_CONF_CHANGE_POWER);
  138. if (changed && local->open_count) {
  139. ret = drv_config(local, changed);
  140. /*
  141. * Goal:
  142. * HW reconfiguration should never fail, the driver has told
  143. * us what it can support so it should live up to that promise.
  144. *
  145. * Current status:
  146. * rfkill is not integrated with mac80211 and a
  147. * configuration command can thus fail if hardware rfkill
  148. * is enabled
  149. *
  150. * FIXME: integrate rfkill with mac80211 and then add this
  151. * WARN_ON() back
  152. *
  153. */
  154. /* WARN_ON(ret); */
  155. }
  156. return ret;
  157. }
  158. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  159. u32 changed)
  160. {
  161. struct ieee80211_local *local = sdata->local;
  162. if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  163. return;
  164. drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
  165. }
  166. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  167. {
  168. sdata->vif.bss_conf.use_cts_prot = false;
  169. sdata->vif.bss_conf.use_short_preamble = false;
  170. sdata->vif.bss_conf.use_short_slot = false;
  171. return BSS_CHANGED_ERP_CTS_PROT |
  172. BSS_CHANGED_ERP_PREAMBLE |
  173. BSS_CHANGED_ERP_SLOT;
  174. }
  175. static void ieee80211_tasklet_handler(unsigned long data)
  176. {
  177. struct ieee80211_local *local = (struct ieee80211_local *) data;
  178. struct sk_buff *skb;
  179. while ((skb = skb_dequeue(&local->skb_queue)) ||
  180. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  181. switch (skb->pkt_type) {
  182. case IEEE80211_RX_MSG:
  183. /* Clear skb->pkt_type in order to not confuse kernel
  184. * netstack. */
  185. skb->pkt_type = 0;
  186. ieee80211_rx(&local->hw, skb);
  187. break;
  188. case IEEE80211_TX_STATUS_MSG:
  189. skb->pkt_type = 0;
  190. ieee80211_tx_status(&local->hw, skb);
  191. break;
  192. default:
  193. WARN(1, "mac80211: Packet is of unknown type %d\n",
  194. skb->pkt_type);
  195. dev_kfree_skb(skb);
  196. break;
  197. }
  198. }
  199. }
  200. static void ieee80211_restart_work(struct work_struct *work)
  201. {
  202. struct ieee80211_local *local =
  203. container_of(work, struct ieee80211_local, restart_work);
  204. struct ieee80211_sub_if_data *sdata;
  205. /* wait for scan work complete */
  206. flush_workqueue(local->workqueue);
  207. flush_work(&local->sched_scan_stopped_work);
  208. WARN(test_bit(SCAN_HW_SCANNING, &local->scanning),
  209. "%s called with hardware scan in progress\n", __func__);
  210. flush_work(&local->radar_detected_work);
  211. rtnl_lock();
  212. list_for_each_entry(sdata, &local->interfaces, list) {
  213. /*
  214. * XXX: there may be more work for other vif types and even
  215. * for station mode: a good thing would be to run most of
  216. * the iface type's dependent _stop (ieee80211_mg_stop,
  217. * ieee80211_ibss_stop) etc...
  218. * For now, fix only the specific bug that was seen: race
  219. * between csa_connection_drop_work and us.
  220. */
  221. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  222. /*
  223. * This worker is scheduled from the iface worker that
  224. * runs on mac80211's workqueue, so we can't be
  225. * scheduling this worker after the cancel right here.
  226. * The exception is ieee80211_chswitch_done.
  227. * Then we can have a race...
  228. */
  229. cancel_work_sync(&sdata->u.mgd.csa_connection_drop_work);
  230. }
  231. flush_delayed_work(&sdata->dec_tailroom_needed_wk);
  232. }
  233. ieee80211_scan_cancel(local);
  234. /* make sure any new ROC will consider local->in_reconfig */
  235. flush_delayed_work(&local->roc_work);
  236. flush_work(&local->hw_roc_done);
  237. ieee80211_reconfig(local);
  238. rtnl_unlock();
  239. }
  240. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  241. {
  242. struct ieee80211_local *local = hw_to_local(hw);
  243. trace_api_restart_hw(local);
  244. wiphy_info(hw->wiphy,
  245. "Hardware restart was requested\n");
  246. /* use this reason, ieee80211_reconfig will unblock it */
  247. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  248. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  249. false);
  250. /*
  251. * Stop all Rx during the reconfig. We don't want state changes
  252. * or driver callbacks while this is in progress.
  253. */
  254. local->in_reconfig = true;
  255. barrier();
  256. queue_work(system_freezable_wq, &local->restart_work);
  257. }
  258. EXPORT_SYMBOL(ieee80211_restart_hw);
  259. #ifdef CONFIG_INET
  260. static int ieee80211_ifa_changed(struct notifier_block *nb,
  261. unsigned long data, void *arg)
  262. {
  263. struct in_ifaddr *ifa = arg;
  264. struct ieee80211_local *local =
  265. container_of(nb, struct ieee80211_local,
  266. ifa_notifier);
  267. struct net_device *ndev = ifa->ifa_dev->dev;
  268. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  269. struct in_device *idev;
  270. struct ieee80211_sub_if_data *sdata;
  271. struct ieee80211_bss_conf *bss_conf;
  272. struct ieee80211_if_managed *ifmgd;
  273. int c = 0;
  274. /* Make sure it's our interface that got changed */
  275. if (!wdev)
  276. return NOTIFY_DONE;
  277. if (wdev->wiphy != local->hw.wiphy)
  278. return NOTIFY_DONE;
  279. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  280. bss_conf = &sdata->vif.bss_conf;
  281. /* ARP filtering is only supported in managed mode */
  282. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  283. return NOTIFY_DONE;
  284. idev = __in_dev_get_rtnl(sdata->dev);
  285. if (!idev)
  286. return NOTIFY_DONE;
  287. ifmgd = &sdata->u.mgd;
  288. sdata_lock(sdata);
  289. /* Copy the addresses to the bss_conf list */
  290. ifa = idev->ifa_list;
  291. while (ifa) {
  292. if (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN)
  293. bss_conf->arp_addr_list[c] = ifa->ifa_address;
  294. ifa = ifa->ifa_next;
  295. c++;
  296. }
  297. bss_conf->arp_addr_cnt = c;
  298. /* Configure driver only if associated (which also implies it is up) */
  299. if (ifmgd->associated)
  300. ieee80211_bss_info_change_notify(sdata,
  301. BSS_CHANGED_ARP_FILTER);
  302. sdata_unlock(sdata);
  303. return NOTIFY_OK;
  304. }
  305. #endif
  306. #if IS_ENABLED(CONFIG_IPV6)
  307. static int ieee80211_ifa6_changed(struct notifier_block *nb,
  308. unsigned long data, void *arg)
  309. {
  310. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)arg;
  311. struct inet6_dev *idev = ifa->idev;
  312. struct net_device *ndev = ifa->idev->dev;
  313. struct ieee80211_local *local =
  314. container_of(nb, struct ieee80211_local, ifa6_notifier);
  315. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  316. struct ieee80211_sub_if_data *sdata;
  317. /* Make sure it's our interface that got changed */
  318. if (!wdev || wdev->wiphy != local->hw.wiphy)
  319. return NOTIFY_DONE;
  320. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  321. /*
  322. * For now only support station mode. This is mostly because
  323. * doing AP would have to handle AP_VLAN in some way ...
  324. */
  325. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  326. return NOTIFY_DONE;
  327. drv_ipv6_addr_change(local, sdata, idev);
  328. return NOTIFY_OK;
  329. }
  330. #endif
  331. /* There isn't a lot of sense in it, but you can transmit anything you like */
  332. static const struct ieee80211_txrx_stypes
  333. ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  334. [NL80211_IFTYPE_ADHOC] = {
  335. .tx = 0xffff,
  336. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  337. BIT(IEEE80211_STYPE_AUTH >> 4) |
  338. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  339. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  340. },
  341. [NL80211_IFTYPE_STATION] = {
  342. .tx = 0xffff,
  343. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  344. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  345. },
  346. [NL80211_IFTYPE_AP] = {
  347. .tx = 0xffff,
  348. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  349. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  350. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  351. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  352. BIT(IEEE80211_STYPE_AUTH >> 4) |
  353. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  354. BIT(IEEE80211_STYPE_ACTION >> 4),
  355. },
  356. [NL80211_IFTYPE_AP_VLAN] = {
  357. /* copy AP */
  358. .tx = 0xffff,
  359. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  360. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  361. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  362. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  363. BIT(IEEE80211_STYPE_AUTH >> 4) |
  364. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  365. BIT(IEEE80211_STYPE_ACTION >> 4),
  366. },
  367. [NL80211_IFTYPE_P2P_CLIENT] = {
  368. .tx = 0xffff,
  369. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  370. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  371. },
  372. [NL80211_IFTYPE_P2P_GO] = {
  373. .tx = 0xffff,
  374. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  375. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  376. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  377. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  378. BIT(IEEE80211_STYPE_AUTH >> 4) |
  379. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  380. BIT(IEEE80211_STYPE_ACTION >> 4),
  381. },
  382. [NL80211_IFTYPE_MESH_POINT] = {
  383. .tx = 0xffff,
  384. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  385. BIT(IEEE80211_STYPE_AUTH >> 4) |
  386. BIT(IEEE80211_STYPE_DEAUTH >> 4),
  387. },
  388. [NL80211_IFTYPE_P2P_DEVICE] = {
  389. .tx = 0xffff,
  390. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  391. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  392. },
  393. };
  394. static const struct ieee80211_ht_cap mac80211_ht_capa_mod_mask = {
  395. .ampdu_params_info = IEEE80211_HT_AMPDU_PARM_FACTOR |
  396. IEEE80211_HT_AMPDU_PARM_DENSITY,
  397. .cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  398. IEEE80211_HT_CAP_MAX_AMSDU |
  399. IEEE80211_HT_CAP_SGI_20 |
  400. IEEE80211_HT_CAP_SGI_40 |
  401. IEEE80211_HT_CAP_LDPC_CODING |
  402. IEEE80211_HT_CAP_40MHZ_INTOLERANT),
  403. .mcs = {
  404. .rx_mask = { 0xff, 0xff, 0xff, 0xff, 0xff,
  405. 0xff, 0xff, 0xff, 0xff, 0xff, },
  406. },
  407. };
  408. static const struct ieee80211_vht_cap mac80211_vht_capa_mod_mask = {
  409. .vht_cap_info =
  410. cpu_to_le32(IEEE80211_VHT_CAP_RXLDPC |
  411. IEEE80211_VHT_CAP_SHORT_GI_80 |
  412. IEEE80211_VHT_CAP_SHORT_GI_160 |
  413. IEEE80211_VHT_CAP_RXSTBC_MASK |
  414. IEEE80211_VHT_CAP_TXSTBC |
  415. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  416. IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  417. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
  418. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
  419. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK),
  420. .supp_mcs = {
  421. .rx_mcs_map = cpu_to_le16(~0),
  422. .tx_mcs_map = cpu_to_le16(~0),
  423. },
  424. };
  425. struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
  426. const struct ieee80211_ops *ops,
  427. const char *requested_name)
  428. {
  429. struct ieee80211_local *local;
  430. int priv_size, i;
  431. struct wiphy *wiphy;
  432. bool use_chanctx;
  433. if (WARN_ON(!ops->tx || !ops->start || !ops->stop || !ops->config ||
  434. !ops->add_interface || !ops->remove_interface ||
  435. !ops->configure_filter))
  436. return NULL;
  437. if (WARN_ON(ops->sta_state && (ops->sta_add || ops->sta_remove)))
  438. return NULL;
  439. /* check all or no channel context operations exist */
  440. i = !!ops->add_chanctx + !!ops->remove_chanctx +
  441. !!ops->change_chanctx + !!ops->assign_vif_chanctx +
  442. !!ops->unassign_vif_chanctx;
  443. if (WARN_ON(i != 0 && i != 5))
  444. return NULL;
  445. use_chanctx = i == 5;
  446. /* Ensure 32-byte alignment of our private data and hw private data.
  447. * We use the wiphy priv data for both our ieee80211_local and for
  448. * the driver's private data
  449. *
  450. * In memory it'll be like this:
  451. *
  452. * +-------------------------+
  453. * | struct wiphy |
  454. * +-------------------------+
  455. * | struct ieee80211_local |
  456. * +-------------------------+
  457. * | driver's private data |
  458. * +-------------------------+
  459. *
  460. */
  461. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  462. wiphy = wiphy_new_nm(&mac80211_config_ops, priv_size, requested_name);
  463. if (!wiphy)
  464. return NULL;
  465. wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
  466. wiphy->privid = mac80211_wiphy_privid;
  467. wiphy->flags |= WIPHY_FLAG_NETNS_OK |
  468. WIPHY_FLAG_4ADDR_AP |
  469. WIPHY_FLAG_4ADDR_STATION |
  470. WIPHY_FLAG_REPORTS_OBSS |
  471. WIPHY_FLAG_OFFCHAN_TX;
  472. if (ops->remain_on_channel)
  473. wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  474. wiphy->features |= NL80211_FEATURE_SK_TX_STATUS |
  475. NL80211_FEATURE_SAE |
  476. NL80211_FEATURE_HT_IBSS |
  477. NL80211_FEATURE_VIF_TXPOWER |
  478. NL80211_FEATURE_MAC_ON_CREATE |
  479. NL80211_FEATURE_USERSPACE_MPM |
  480. NL80211_FEATURE_FULL_AP_CLIENT_STATE;
  481. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_STA);
  482. if (!ops->hw_scan)
  483. wiphy->features |= NL80211_FEATURE_LOW_PRIORITY_SCAN |
  484. NL80211_FEATURE_AP_SCAN;
  485. if (!ops->set_key)
  486. wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  487. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_RRM);
  488. wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
  489. local = wiphy_priv(wiphy);
  490. if (sta_info_init(local))
  491. goto err_free;
  492. local->hw.wiphy = wiphy;
  493. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  494. local->ops = ops;
  495. local->use_chanctx = use_chanctx;
  496. /* set up some defaults */
  497. local->hw.queues = 1;
  498. local->hw.max_rates = 1;
  499. local->hw.max_report_rates = 0;
  500. local->hw.max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
  501. local->hw.max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
  502. local->hw.offchannel_tx_hw_queue = IEEE80211_INVAL_HW_QUEUE;
  503. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  504. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  505. local->hw.radiotap_mcs_details = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  506. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  507. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  508. local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
  509. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
  510. local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  511. local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  512. local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
  513. wiphy->ht_capa_mod_mask = &mac80211_ht_capa_mod_mask;
  514. wiphy->vht_capa_mod_mask = &mac80211_vht_capa_mod_mask;
  515. local->ext_capa[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF;
  516. wiphy->extended_capabilities = local->ext_capa;
  517. wiphy->extended_capabilities_mask = local->ext_capa;
  518. wiphy->extended_capabilities_len =
  519. ARRAY_SIZE(local->ext_capa);
  520. INIT_LIST_HEAD(&local->interfaces);
  521. INIT_LIST_HEAD(&local->mon_list);
  522. __hw_addr_init(&local->mc_list);
  523. mutex_init(&local->iflist_mtx);
  524. mutex_init(&local->mtx);
  525. mutex_init(&local->key_mtx);
  526. spin_lock_init(&local->filter_lock);
  527. spin_lock_init(&local->rx_path_lock);
  528. spin_lock_init(&local->queue_stop_reason_lock);
  529. INIT_LIST_HEAD(&local->chanctx_list);
  530. mutex_init(&local->chanctx_mtx);
  531. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  532. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  533. INIT_WORK(&local->radar_detected_work,
  534. ieee80211_dfs_radar_detected_work);
  535. INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
  536. local->smps_mode = IEEE80211_SMPS_OFF;
  537. INIT_WORK(&local->dynamic_ps_enable_work,
  538. ieee80211_dynamic_ps_enable_work);
  539. INIT_WORK(&local->dynamic_ps_disable_work,
  540. ieee80211_dynamic_ps_disable_work);
  541. setup_timer(&local->dynamic_ps_timer,
  542. ieee80211_dynamic_ps_timer, (unsigned long) local);
  543. INIT_WORK(&local->sched_scan_stopped_work,
  544. ieee80211_sched_scan_stopped_work);
  545. INIT_WORK(&local->tdls_chsw_work, ieee80211_tdls_chsw_work);
  546. spin_lock_init(&local->ack_status_lock);
  547. idr_init(&local->ack_status_frames);
  548. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  549. skb_queue_head_init(&local->pending[i]);
  550. atomic_set(&local->agg_queue_stop[i], 0);
  551. }
  552. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  553. (unsigned long)local);
  554. tasklet_init(&local->tasklet,
  555. ieee80211_tasklet_handler,
  556. (unsigned long) local);
  557. skb_queue_head_init(&local->skb_queue);
  558. skb_queue_head_init(&local->skb_queue_unreliable);
  559. skb_queue_head_init(&local->skb_queue_tdls_chsw);
  560. ieee80211_alloc_led_names(local);
  561. ieee80211_roc_setup(local);
  562. local->hw.radiotap_timestamp.units_pos = -1;
  563. local->hw.radiotap_timestamp.accuracy = -1;
  564. return &local->hw;
  565. err_free:
  566. wiphy_free(wiphy);
  567. return NULL;
  568. }
  569. EXPORT_SYMBOL(ieee80211_alloc_hw_nm);
  570. static int ieee80211_init_cipher_suites(struct ieee80211_local *local)
  571. {
  572. bool have_wep = !(IS_ERR(local->wep_tx_tfm) ||
  573. IS_ERR(local->wep_rx_tfm));
  574. bool have_mfp = ieee80211_hw_check(&local->hw, MFP_CAPABLE);
  575. int n_suites = 0, r = 0, w = 0;
  576. u32 *suites;
  577. static const u32 cipher_suites[] = {
  578. /* keep WEP first, it may be removed below */
  579. WLAN_CIPHER_SUITE_WEP40,
  580. WLAN_CIPHER_SUITE_WEP104,
  581. WLAN_CIPHER_SUITE_TKIP,
  582. WLAN_CIPHER_SUITE_CCMP,
  583. WLAN_CIPHER_SUITE_CCMP_256,
  584. WLAN_CIPHER_SUITE_GCMP,
  585. WLAN_CIPHER_SUITE_GCMP_256,
  586. /* keep last -- depends on hw flags! */
  587. WLAN_CIPHER_SUITE_AES_CMAC,
  588. WLAN_CIPHER_SUITE_BIP_CMAC_256,
  589. WLAN_CIPHER_SUITE_BIP_GMAC_128,
  590. WLAN_CIPHER_SUITE_BIP_GMAC_256,
  591. };
  592. if (ieee80211_hw_check(&local->hw, SW_CRYPTO_CONTROL) ||
  593. local->hw.wiphy->cipher_suites) {
  594. /* If the driver advertises, or doesn't support SW crypto,
  595. * we only need to remove WEP if necessary.
  596. */
  597. if (have_wep)
  598. return 0;
  599. /* well if it has _no_ ciphers ... fine */
  600. if (!local->hw.wiphy->n_cipher_suites)
  601. return 0;
  602. /* Driver provides cipher suites, but we need to exclude WEP */
  603. suites = kmemdup(local->hw.wiphy->cipher_suites,
  604. sizeof(u32) * local->hw.wiphy->n_cipher_suites,
  605. GFP_KERNEL);
  606. if (!suites)
  607. return -ENOMEM;
  608. for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
  609. u32 suite = local->hw.wiphy->cipher_suites[r];
  610. if (suite == WLAN_CIPHER_SUITE_WEP40 ||
  611. suite == WLAN_CIPHER_SUITE_WEP104)
  612. continue;
  613. suites[w++] = suite;
  614. }
  615. } else if (!local->hw.cipher_schemes) {
  616. /* If the driver doesn't have cipher schemes, there's nothing
  617. * else to do other than assign the (software supported and
  618. * perhaps offloaded) cipher suites.
  619. */
  620. local->hw.wiphy->cipher_suites = cipher_suites;
  621. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  622. if (!have_mfp)
  623. local->hw.wiphy->n_cipher_suites -= 4;
  624. if (!have_wep) {
  625. local->hw.wiphy->cipher_suites += 2;
  626. local->hw.wiphy->n_cipher_suites -= 2;
  627. }
  628. /* not dynamically allocated, so just return */
  629. return 0;
  630. } else {
  631. const struct ieee80211_cipher_scheme *cs;
  632. cs = local->hw.cipher_schemes;
  633. /* Driver specifies cipher schemes only (but not cipher suites
  634. * including the schemes)
  635. *
  636. * We start counting ciphers defined by schemes, TKIP, CCMP,
  637. * CCMP-256, GCMP, and GCMP-256
  638. */
  639. n_suites = local->hw.n_cipher_schemes + 5;
  640. /* check if we have WEP40 and WEP104 */
  641. if (have_wep)
  642. n_suites += 2;
  643. /* check if we have AES_CMAC, BIP-CMAC-256, BIP-GMAC-128,
  644. * BIP-GMAC-256
  645. */
  646. if (have_mfp)
  647. n_suites += 4;
  648. suites = kmalloc(sizeof(u32) * n_suites, GFP_KERNEL);
  649. if (!suites)
  650. return -ENOMEM;
  651. suites[w++] = WLAN_CIPHER_SUITE_CCMP;
  652. suites[w++] = WLAN_CIPHER_SUITE_CCMP_256;
  653. suites[w++] = WLAN_CIPHER_SUITE_TKIP;
  654. suites[w++] = WLAN_CIPHER_SUITE_GCMP;
  655. suites[w++] = WLAN_CIPHER_SUITE_GCMP_256;
  656. if (have_wep) {
  657. suites[w++] = WLAN_CIPHER_SUITE_WEP40;
  658. suites[w++] = WLAN_CIPHER_SUITE_WEP104;
  659. }
  660. if (have_mfp) {
  661. suites[w++] = WLAN_CIPHER_SUITE_AES_CMAC;
  662. suites[w++] = WLAN_CIPHER_SUITE_BIP_CMAC_256;
  663. suites[w++] = WLAN_CIPHER_SUITE_BIP_GMAC_128;
  664. suites[w++] = WLAN_CIPHER_SUITE_BIP_GMAC_256;
  665. }
  666. for (r = 0; r < local->hw.n_cipher_schemes; r++) {
  667. suites[w++] = cs[r].cipher;
  668. if (WARN_ON(cs[r].pn_len > IEEE80211_MAX_PN_LEN)) {
  669. kfree(suites);
  670. return -EINVAL;
  671. }
  672. }
  673. }
  674. local->hw.wiphy->cipher_suites = suites;
  675. local->hw.wiphy->n_cipher_suites = w;
  676. local->wiphy_ciphers_allocated = true;
  677. return 0;
  678. }
  679. int ieee80211_register_hw(struct ieee80211_hw *hw)
  680. {
  681. struct ieee80211_local *local = hw_to_local(hw);
  682. int result, i;
  683. enum nl80211_band band;
  684. int channels, max_bitrates;
  685. bool supp_ht, supp_vht;
  686. netdev_features_t feature_whitelist;
  687. struct cfg80211_chan_def dflt_chandef = {};
  688. if (ieee80211_hw_check(hw, QUEUE_CONTROL) &&
  689. (local->hw.offchannel_tx_hw_queue == IEEE80211_INVAL_HW_QUEUE ||
  690. local->hw.offchannel_tx_hw_queue >= local->hw.queues))
  691. return -EINVAL;
  692. if ((hw->wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) &&
  693. (!local->ops->tdls_channel_switch ||
  694. !local->ops->tdls_cancel_channel_switch ||
  695. !local->ops->tdls_recv_channel_switch))
  696. return -EOPNOTSUPP;
  697. if (WARN_ON(ieee80211_hw_check(hw, SUPPORTS_TX_FRAG) &&
  698. !local->ops->set_frag_threshold))
  699. return -EINVAL;
  700. if (WARN_ON(local->hw.wiphy->interface_modes &
  701. BIT(NL80211_IFTYPE_NAN) &&
  702. (!local->ops->start_nan || !local->ops->stop_nan)))
  703. return -EINVAL;
  704. #ifdef CONFIG_PM
  705. if (hw->wiphy->wowlan && (!local->ops->suspend || !local->ops->resume))
  706. return -EINVAL;
  707. #endif
  708. if (!local->use_chanctx) {
  709. for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
  710. const struct ieee80211_iface_combination *comb;
  711. comb = &local->hw.wiphy->iface_combinations[i];
  712. if (comb->num_different_channels > 1)
  713. return -EINVAL;
  714. }
  715. } else {
  716. /*
  717. * WDS is currently prohibited when channel contexts are used
  718. * because there's no clear definition of which channel WDS
  719. * type interfaces use
  720. */
  721. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_WDS))
  722. return -EINVAL;
  723. /* DFS is not supported with multi-channel combinations yet */
  724. for (i = 0; i < local->hw.wiphy->n_iface_combinations; i++) {
  725. const struct ieee80211_iface_combination *comb;
  726. comb = &local->hw.wiphy->iface_combinations[i];
  727. if (comb->radar_detect_widths &&
  728. comb->num_different_channels > 1)
  729. return -EINVAL;
  730. }
  731. }
  732. /* Only HW csum features are currently compatible with mac80211 */
  733. feature_whitelist = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  734. NETIF_F_HW_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA |
  735. NETIF_F_GSO_SOFTWARE | NETIF_F_RXCSUM;
  736. if (WARN_ON(hw->netdev_features & ~feature_whitelist))
  737. return -EINVAL;
  738. if (hw->max_report_rates == 0)
  739. hw->max_report_rates = hw->max_rates;
  740. local->rx_chains = 1;
  741. /*
  742. * generic code guarantees at least one band,
  743. * set this very early because much code assumes
  744. * that hw.conf.channel is assigned
  745. */
  746. channels = 0;
  747. max_bitrates = 0;
  748. supp_ht = false;
  749. supp_vht = false;
  750. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  751. struct ieee80211_supported_band *sband;
  752. sband = local->hw.wiphy->bands[band];
  753. if (!sband)
  754. continue;
  755. if (!dflt_chandef.chan) {
  756. /*
  757. * Assign the first enabled channel to dflt_chandef
  758. * from the list of channels
  759. */
  760. for (i = 0; i < sband->n_channels; i++)
  761. if (!(sband->channels[i].flags &
  762. IEEE80211_CHAN_DISABLED))
  763. break;
  764. /* if none found then use the first anyway */
  765. if (i == sband->n_channels)
  766. i = 0;
  767. cfg80211_chandef_create(&dflt_chandef,
  768. &sband->channels[i],
  769. NL80211_CHAN_NO_HT);
  770. /* init channel we're on */
  771. if (!local->use_chanctx && !local->_oper_chandef.chan) {
  772. local->hw.conf.chandef = dflt_chandef;
  773. local->_oper_chandef = dflt_chandef;
  774. }
  775. local->monitor_chandef = dflt_chandef;
  776. }
  777. channels += sband->n_channels;
  778. if (max_bitrates < sband->n_bitrates)
  779. max_bitrates = sband->n_bitrates;
  780. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  781. supp_vht = supp_vht || sband->vht_cap.vht_supported;
  782. if (!sband->ht_cap.ht_supported)
  783. continue;
  784. /* TODO: consider VHT for RX chains, hopefully it's the same */
  785. local->rx_chains =
  786. max(ieee80211_mcs_to_chains(&sband->ht_cap.mcs),
  787. local->rx_chains);
  788. /* no need to mask, SM_PS_DISABLED has all bits set */
  789. sband->ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  790. IEEE80211_HT_CAP_SM_PS_SHIFT;
  791. }
  792. /* if low-level driver supports AP, we also support VLAN */
  793. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  794. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  795. hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
  796. }
  797. /* mac80211 always supports monitor */
  798. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  799. hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
  800. /* mac80211 doesn't support more than one IBSS interface right now */
  801. for (i = 0; i < hw->wiphy->n_iface_combinations; i++) {
  802. const struct ieee80211_iface_combination *c;
  803. int j;
  804. c = &hw->wiphy->iface_combinations[i];
  805. for (j = 0; j < c->n_limits; j++)
  806. if ((c->limits[j].types & BIT(NL80211_IFTYPE_ADHOC)) &&
  807. c->limits[j].max > 1)
  808. return -EINVAL;
  809. }
  810. local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
  811. sizeof(void *) * channels, GFP_KERNEL);
  812. if (!local->int_scan_req)
  813. return -ENOMEM;
  814. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  815. if (!local->hw.wiphy->bands[band])
  816. continue;
  817. local->int_scan_req->rates[band] = (u32) -1;
  818. }
  819. #ifndef CONFIG_MAC80211_MESH
  820. /* mesh depends on Kconfig, but drivers should set it if they want */
  821. local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
  822. #endif
  823. /* if the underlying driver supports mesh, mac80211 will (at least)
  824. * provide routing of mesh authentication frames to userspace */
  825. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
  826. local->hw.wiphy->flags |= WIPHY_FLAG_MESH_AUTH;
  827. /* mac80211 supports control port protocol changing */
  828. local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
  829. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) {
  830. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  831. } else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) {
  832. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  833. if (hw->max_signal <= 0) {
  834. result = -EINVAL;
  835. goto fail_wiphy_register;
  836. }
  837. }
  838. /*
  839. * Calculate scan IE length -- we need this to alloc
  840. * memory and to subtract from the driver limit. It
  841. * includes the DS Params, (extended) supported rates, and HT
  842. * information -- SSID is the driver's responsibility.
  843. */
  844. local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
  845. 3 /* DS Params */;
  846. if (supp_ht)
  847. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  848. if (supp_vht)
  849. local->scan_ies_len +=
  850. 2 + sizeof(struct ieee80211_vht_cap);
  851. if (!local->ops->hw_scan) {
  852. /* For hw_scan, driver needs to set these up. */
  853. local->hw.wiphy->max_scan_ssids = 4;
  854. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  855. }
  856. /*
  857. * If the driver supports any scan IEs, then assume the
  858. * limit includes the IEs mac80211 will add, otherwise
  859. * leave it at zero and let the driver sort it out; we
  860. * still pass our IEs to the driver but userspace will
  861. * not be allowed to in that case.
  862. */
  863. if (local->hw.wiphy->max_scan_ie_len)
  864. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  865. if (WARN_ON(!ieee80211_cs_list_valid(local->hw.cipher_schemes,
  866. local->hw.n_cipher_schemes))) {
  867. result = -EINVAL;
  868. goto fail_workqueue;
  869. }
  870. result = ieee80211_init_cipher_suites(local);
  871. if (result < 0)
  872. goto fail_wiphy_register;
  873. if (!local->ops->remain_on_channel)
  874. local->hw.wiphy->max_remain_on_channel_duration = 5000;
  875. /* mac80211 based drivers don't support internal TDLS setup */
  876. if (local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS)
  877. local->hw.wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
  878. /* mac80211 supports eCSA, if the driver supports STA CSA at all */
  879. if (ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA))
  880. local->ext_capa[0] |= WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING;
  881. local->hw.wiphy->max_num_csa_counters = IEEE80211_MAX_CSA_COUNTERS_NUM;
  882. result = wiphy_register(local->hw.wiphy);
  883. if (result < 0)
  884. goto fail_wiphy_register;
  885. /*
  886. * We use the number of queues for feature tests (QoS, HT) internally
  887. * so restrict them appropriately.
  888. */
  889. if (hw->queues > IEEE80211_MAX_QUEUES)
  890. hw->queues = IEEE80211_MAX_QUEUES;
  891. local->workqueue =
  892. alloc_ordered_workqueue("%s", 0, wiphy_name(local->hw.wiphy));
  893. if (!local->workqueue) {
  894. result = -ENOMEM;
  895. goto fail_workqueue;
  896. }
  897. /*
  898. * The hardware needs headroom for sending the frame,
  899. * and we need some headroom for passing the frame to monitor
  900. * interfaces, but never both at the same time.
  901. */
  902. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  903. IEEE80211_TX_STATUS_HEADROOM);
  904. debugfs_hw_add(local);
  905. /*
  906. * if the driver doesn't specify a max listen interval we
  907. * use 5 which should be a safe default
  908. */
  909. if (local->hw.max_listen_interval == 0)
  910. local->hw.max_listen_interval = 5;
  911. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  912. local->dynamic_ps_forced_timeout = -1;
  913. if (!local->hw.max_nan_de_entries)
  914. local->hw.max_nan_de_entries = IEEE80211_MAX_NAN_INSTANCE_ID;
  915. result = ieee80211_wep_init(local);
  916. if (result < 0)
  917. wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
  918. result);
  919. local->hw.conf.flags = IEEE80211_CONF_IDLE;
  920. ieee80211_led_init(local);
  921. rtnl_lock();
  922. result = ieee80211_init_rate_ctrl_alg(local,
  923. hw->rate_control_algorithm);
  924. if (result < 0) {
  925. wiphy_debug(local->hw.wiphy,
  926. "Failed to initialize rate control algorithm\n");
  927. goto fail_rate;
  928. }
  929. /* add one default STA interface if supported */
  930. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) &&
  931. !ieee80211_hw_check(hw, NO_AUTO_VIF)) {
  932. result = ieee80211_if_add(local, "wlan%d", NET_NAME_ENUM, NULL,
  933. NL80211_IFTYPE_STATION, NULL);
  934. if (result)
  935. wiphy_warn(local->hw.wiphy,
  936. "Failed to add default virtual iface\n");
  937. }
  938. rtnl_unlock();
  939. result = ieee80211_txq_setup_flows(local);
  940. if (result)
  941. goto fail_flows;
  942. #ifdef CONFIG_INET
  943. local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
  944. result = register_inetaddr_notifier(&local->ifa_notifier);
  945. if (result)
  946. goto fail_ifa;
  947. #endif
  948. #if IS_ENABLED(CONFIG_IPV6)
  949. local->ifa6_notifier.notifier_call = ieee80211_ifa6_changed;
  950. result = register_inet6addr_notifier(&local->ifa6_notifier);
  951. if (result)
  952. goto fail_ifa6;
  953. #endif
  954. return 0;
  955. #if IS_ENABLED(CONFIG_IPV6)
  956. fail_ifa6:
  957. #ifdef CONFIG_INET
  958. unregister_inetaddr_notifier(&local->ifa_notifier);
  959. #endif
  960. #endif
  961. #if defined(CONFIG_INET) || defined(CONFIG_IPV6)
  962. fail_ifa:
  963. #endif
  964. ieee80211_txq_teardown_flows(local);
  965. fail_flows:
  966. rtnl_lock();
  967. rate_control_deinitialize(local);
  968. ieee80211_remove_interfaces(local);
  969. fail_rate:
  970. rtnl_unlock();
  971. ieee80211_led_exit(local);
  972. ieee80211_wep_free(local);
  973. destroy_workqueue(local->workqueue);
  974. fail_workqueue:
  975. wiphy_unregister(local->hw.wiphy);
  976. fail_wiphy_register:
  977. if (local->wiphy_ciphers_allocated)
  978. kfree(local->hw.wiphy->cipher_suites);
  979. kfree(local->int_scan_req);
  980. return result;
  981. }
  982. EXPORT_SYMBOL(ieee80211_register_hw);
  983. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  984. {
  985. struct ieee80211_local *local = hw_to_local(hw);
  986. tasklet_kill(&local->tx_pending_tasklet);
  987. tasklet_kill(&local->tasklet);
  988. #ifdef CONFIG_INET
  989. unregister_inetaddr_notifier(&local->ifa_notifier);
  990. #endif
  991. #if IS_ENABLED(CONFIG_IPV6)
  992. unregister_inet6addr_notifier(&local->ifa6_notifier);
  993. #endif
  994. ieee80211_txq_teardown_flows(local);
  995. rtnl_lock();
  996. /*
  997. * At this point, interface list manipulations are fine
  998. * because the driver cannot be handing us frames any
  999. * more and the tasklet is killed.
  1000. */
  1001. ieee80211_remove_interfaces(local);
  1002. rtnl_unlock();
  1003. cancel_delayed_work_sync(&local->roc_work);
  1004. cancel_work_sync(&local->restart_work);
  1005. cancel_work_sync(&local->reconfig_filter);
  1006. cancel_work_sync(&local->tdls_chsw_work);
  1007. flush_work(&local->sched_scan_stopped_work);
  1008. flush_work(&local->radar_detected_work);
  1009. ieee80211_clear_tx_pending(local);
  1010. rate_control_deinitialize(local);
  1011. if (skb_queue_len(&local->skb_queue) ||
  1012. skb_queue_len(&local->skb_queue_unreliable))
  1013. wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
  1014. skb_queue_purge(&local->skb_queue);
  1015. skb_queue_purge(&local->skb_queue_unreliable);
  1016. skb_queue_purge(&local->skb_queue_tdls_chsw);
  1017. destroy_workqueue(local->workqueue);
  1018. wiphy_unregister(local->hw.wiphy);
  1019. ieee80211_wep_free(local);
  1020. ieee80211_led_exit(local);
  1021. kfree(local->int_scan_req);
  1022. }
  1023. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1024. static int ieee80211_free_ack_frame(int id, void *p, void *data)
  1025. {
  1026. WARN_ONCE(1, "Have pending ack frames!\n");
  1027. kfree_skb(p);
  1028. return 0;
  1029. }
  1030. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1031. {
  1032. struct ieee80211_local *local = hw_to_local(hw);
  1033. mutex_destroy(&local->iflist_mtx);
  1034. mutex_destroy(&local->mtx);
  1035. if (local->wiphy_ciphers_allocated)
  1036. kfree(local->hw.wiphy->cipher_suites);
  1037. idr_for_each(&local->ack_status_frames,
  1038. ieee80211_free_ack_frame, NULL);
  1039. idr_destroy(&local->ack_status_frames);
  1040. sta_info_stop(local);
  1041. ieee80211_free_led_names(local);
  1042. wiphy_free(local->hw.wiphy);
  1043. }
  1044. EXPORT_SYMBOL(ieee80211_free_hw);
  1045. static int __init ieee80211_init(void)
  1046. {
  1047. struct sk_buff *skb;
  1048. int ret;
  1049. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  1050. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  1051. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  1052. ret = rc80211_minstrel_init();
  1053. if (ret)
  1054. return ret;
  1055. ret = rc80211_minstrel_ht_init();
  1056. if (ret)
  1057. goto err_minstrel;
  1058. ret = ieee80211_iface_init();
  1059. if (ret)
  1060. goto err_netdev;
  1061. return 0;
  1062. err_netdev:
  1063. rc80211_minstrel_ht_exit();
  1064. err_minstrel:
  1065. rc80211_minstrel_exit();
  1066. return ret;
  1067. }
  1068. static void __exit ieee80211_exit(void)
  1069. {
  1070. rc80211_minstrel_ht_exit();
  1071. rc80211_minstrel_exit();
  1072. ieee80211s_stop();
  1073. ieee80211_iface_exit();
  1074. rcu_barrier();
  1075. }
  1076. subsys_initcall(ieee80211_init);
  1077. module_exit(ieee80211_exit);
  1078. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1079. MODULE_LICENSE("GPL");