main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/pm_qos_params.h>
  22. #include <linux/inetdevice.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "driver-ops.h"
  27. #include "rate.h"
  28. #include "mesh.h"
  29. #include "wep.h"
  30. #include "led.h"
  31. #include "cfg.h"
  32. #include "debugfs.h"
  33. bool ieee80211_disable_40mhz_24ghz;
  34. module_param(ieee80211_disable_40mhz_24ghz, bool, 0644);
  35. MODULE_PARM_DESC(ieee80211_disable_40mhz_24ghz,
  36. "Disable 40MHz support in the 2.4GHz band");
  37. static struct lock_class_key ieee80211_rx_skb_queue_class;
  38. void ieee80211_configure_filter(struct ieee80211_local *local)
  39. {
  40. u64 mc;
  41. unsigned int changed_flags;
  42. unsigned int new_flags = 0;
  43. if (atomic_read(&local->iff_promiscs))
  44. new_flags |= FIF_PROMISC_IN_BSS;
  45. if (atomic_read(&local->iff_allmultis))
  46. new_flags |= FIF_ALLMULTI;
  47. if (local->monitors || local->scanning)
  48. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  49. if (local->fif_probe_req || local->probe_req_reg)
  50. new_flags |= FIF_PROBE_REQ;
  51. if (local->fif_fcsfail)
  52. new_flags |= FIF_FCSFAIL;
  53. if (local->fif_plcpfail)
  54. new_flags |= FIF_PLCPFAIL;
  55. if (local->fif_control)
  56. new_flags |= FIF_CONTROL;
  57. if (local->fif_other_bss)
  58. new_flags |= FIF_OTHER_BSS;
  59. if (local->fif_pspoll)
  60. new_flags |= FIF_PSPOLL;
  61. spin_lock_bh(&local->filter_lock);
  62. changed_flags = local->filter_flags ^ new_flags;
  63. mc = drv_prepare_multicast(local, &local->mc_list);
  64. spin_unlock_bh(&local->filter_lock);
  65. /* be a bit nasty */
  66. new_flags |= (1<<31);
  67. drv_configure_filter(local, changed_flags, &new_flags, mc);
  68. WARN_ON(new_flags & (1<<31));
  69. local->filter_flags = new_flags & ~(1<<31);
  70. }
  71. static void ieee80211_reconfig_filter(struct work_struct *work)
  72. {
  73. struct ieee80211_local *local =
  74. container_of(work, struct ieee80211_local, reconfig_filter);
  75. ieee80211_configure_filter(local);
  76. }
  77. int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
  78. {
  79. struct ieee80211_channel *chan, *scan_chan;
  80. int ret = 0;
  81. int power;
  82. enum nl80211_channel_type channel_type;
  83. u32 offchannel_flag;
  84. might_sleep();
  85. scan_chan = local->scan_channel;
  86. offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  87. if (scan_chan) {
  88. chan = scan_chan;
  89. channel_type = NL80211_CHAN_NO_HT;
  90. local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
  91. } else if (local->tmp_channel &&
  92. local->oper_channel != local->tmp_channel) {
  93. chan = scan_chan = local->tmp_channel;
  94. channel_type = local->tmp_channel_type;
  95. local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
  96. } else {
  97. chan = local->oper_channel;
  98. channel_type = local->_oper_channel_type;
  99. local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
  100. }
  101. offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  102. if (offchannel_flag || chan != local->hw.conf.channel ||
  103. channel_type != local->hw.conf.channel_type) {
  104. local->hw.conf.channel = chan;
  105. local->hw.conf.channel_type = channel_type;
  106. changed |= IEEE80211_CONF_CHANGE_CHANNEL;
  107. }
  108. if (!conf_is_ht(&local->hw.conf)) {
  109. /*
  110. * mac80211.h documents that this is only valid
  111. * when the channel is set to an HT type, and
  112. * that otherwise STATIC is used.
  113. */
  114. local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
  115. } else if (local->hw.conf.smps_mode != local->smps_mode) {
  116. local->hw.conf.smps_mode = local->smps_mode;
  117. changed |= IEEE80211_CONF_CHANGE_SMPS;
  118. }
  119. if (scan_chan)
  120. power = chan->max_power;
  121. else
  122. power = local->power_constr_level ?
  123. (chan->max_power - local->power_constr_level) :
  124. chan->max_power;
  125. if (local->user_power_level >= 0)
  126. power = min(power, local->user_power_level);
  127. if (local->hw.conf.power_level != power) {
  128. changed |= IEEE80211_CONF_CHANGE_POWER;
  129. local->hw.conf.power_level = power;
  130. }
  131. if (changed && local->open_count) {
  132. ret = drv_config(local, changed);
  133. /*
  134. * Goal:
  135. * HW reconfiguration should never fail, the driver has told
  136. * us what it can support so it should live up to that promise.
  137. *
  138. * Current status:
  139. * rfkill is not integrated with mac80211 and a
  140. * configuration command can thus fail if hardware rfkill
  141. * is enabled
  142. *
  143. * FIXME: integrate rfkill with mac80211 and then add this
  144. * WARN_ON() back
  145. *
  146. */
  147. /* WARN_ON(ret); */
  148. }
  149. return ret;
  150. }
  151. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  152. u32 changed)
  153. {
  154. struct ieee80211_local *local = sdata->local;
  155. static const u8 zero[ETH_ALEN] = { 0 };
  156. if (!changed)
  157. return;
  158. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  159. /*
  160. * While not associated, claim a BSSID of all-zeroes
  161. * so that drivers don't do any weird things with the
  162. * BSSID at that time.
  163. */
  164. if (sdata->vif.bss_conf.assoc)
  165. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  166. else
  167. sdata->vif.bss_conf.bssid = zero;
  168. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  169. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  170. else if (sdata->vif.type == NL80211_IFTYPE_AP)
  171. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  172. else if (sdata->vif.type == NL80211_IFTYPE_WDS)
  173. sdata->vif.bss_conf.bssid = NULL;
  174. else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  175. sdata->vif.bss_conf.bssid = zero;
  176. } else {
  177. WARN_ON(1);
  178. return;
  179. }
  180. switch (sdata->vif.type) {
  181. case NL80211_IFTYPE_AP:
  182. case NL80211_IFTYPE_ADHOC:
  183. case NL80211_IFTYPE_WDS:
  184. case NL80211_IFTYPE_MESH_POINT:
  185. break;
  186. default:
  187. /* do not warn to simplify caller in scan.c */
  188. changed &= ~BSS_CHANGED_BEACON_ENABLED;
  189. if (WARN_ON(changed & BSS_CHANGED_BEACON))
  190. return;
  191. break;
  192. }
  193. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  194. if (local->quiescing || !ieee80211_sdata_running(sdata) ||
  195. test_bit(SCAN_SW_SCANNING, &local->scanning)) {
  196. sdata->vif.bss_conf.enable_beacon = false;
  197. } else {
  198. /*
  199. * Beacon should be enabled, but AP mode must
  200. * check whether there is a beacon configured.
  201. */
  202. switch (sdata->vif.type) {
  203. case NL80211_IFTYPE_AP:
  204. sdata->vif.bss_conf.enable_beacon =
  205. !!sdata->u.ap.beacon;
  206. break;
  207. case NL80211_IFTYPE_ADHOC:
  208. sdata->vif.bss_conf.enable_beacon =
  209. !!sdata->u.ibss.presp;
  210. break;
  211. #ifdef CONFIG_MAC80211_MESH
  212. case NL80211_IFTYPE_MESH_POINT:
  213. sdata->vif.bss_conf.enable_beacon =
  214. !!sdata->u.mesh.mesh_id_len;
  215. break;
  216. #endif
  217. default:
  218. /* not reached */
  219. WARN_ON(1);
  220. break;
  221. }
  222. }
  223. }
  224. drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
  225. }
  226. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  227. {
  228. sdata->vif.bss_conf.use_cts_prot = false;
  229. sdata->vif.bss_conf.use_short_preamble = false;
  230. sdata->vif.bss_conf.use_short_slot = false;
  231. return BSS_CHANGED_ERP_CTS_PROT |
  232. BSS_CHANGED_ERP_PREAMBLE |
  233. BSS_CHANGED_ERP_SLOT;
  234. }
  235. static void ieee80211_tasklet_handler(unsigned long data)
  236. {
  237. struct ieee80211_local *local = (struct ieee80211_local *) data;
  238. struct sk_buff *skb;
  239. while ((skb = skb_dequeue(&local->skb_queue)) ||
  240. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  241. switch (skb->pkt_type) {
  242. case IEEE80211_RX_MSG:
  243. /* Clear skb->pkt_type in order to not confuse kernel
  244. * netstack. */
  245. skb->pkt_type = 0;
  246. ieee80211_rx(local_to_hw(local), skb);
  247. break;
  248. case IEEE80211_TX_STATUS_MSG:
  249. skb->pkt_type = 0;
  250. ieee80211_tx_status(local_to_hw(local), skb);
  251. break;
  252. default:
  253. WARN(1, "mac80211: Packet is of unknown type %d\n",
  254. skb->pkt_type);
  255. dev_kfree_skb(skb);
  256. break;
  257. }
  258. }
  259. }
  260. static void ieee80211_restart_work(struct work_struct *work)
  261. {
  262. struct ieee80211_local *local =
  263. container_of(work, struct ieee80211_local, restart_work);
  264. /* wait for scan work complete */
  265. flush_workqueue(local->workqueue);
  266. mutex_lock(&local->mtx);
  267. WARN(test_bit(SCAN_HW_SCANNING, &local->scanning),
  268. "%s called with hardware scan in progress\n", __func__);
  269. mutex_unlock(&local->mtx);
  270. rtnl_lock();
  271. ieee80211_scan_cancel(local);
  272. ieee80211_reconfig(local);
  273. rtnl_unlock();
  274. }
  275. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. trace_api_restart_hw(local);
  279. /* use this reason, ieee80211_reconfig will unblock it */
  280. ieee80211_stop_queues_by_reason(hw,
  281. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  282. schedule_work(&local->restart_work);
  283. }
  284. EXPORT_SYMBOL(ieee80211_restart_hw);
  285. static void ieee80211_recalc_smps_work(struct work_struct *work)
  286. {
  287. struct ieee80211_local *local =
  288. container_of(work, struct ieee80211_local, recalc_smps);
  289. mutex_lock(&local->iflist_mtx);
  290. ieee80211_recalc_smps(local);
  291. mutex_unlock(&local->iflist_mtx);
  292. }
  293. #ifdef CONFIG_INET
  294. static int ieee80211_ifa_changed(struct notifier_block *nb,
  295. unsigned long data, void *arg)
  296. {
  297. struct in_ifaddr *ifa = arg;
  298. struct ieee80211_local *local =
  299. container_of(nb, struct ieee80211_local,
  300. ifa_notifier);
  301. struct net_device *ndev = ifa->ifa_dev->dev;
  302. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  303. struct in_device *idev;
  304. struct ieee80211_sub_if_data *sdata;
  305. struct ieee80211_bss_conf *bss_conf;
  306. struct ieee80211_if_managed *ifmgd;
  307. int c = 0;
  308. /* Make sure it's our interface that got changed */
  309. if (!wdev)
  310. return NOTIFY_DONE;
  311. if (wdev->wiphy != local->hw.wiphy)
  312. return NOTIFY_DONE;
  313. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  314. bss_conf = &sdata->vif.bss_conf;
  315. if (!ieee80211_sdata_running(sdata))
  316. return NOTIFY_DONE;
  317. /* ARP filtering is only supported in managed mode */
  318. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  319. return NOTIFY_DONE;
  320. idev = __in_dev_get_rtnl(sdata->dev);
  321. if (!idev)
  322. return NOTIFY_DONE;
  323. ifmgd = &sdata->u.mgd;
  324. mutex_lock(&ifmgd->mtx);
  325. /* Copy the addresses to the bss_conf list */
  326. ifa = idev->ifa_list;
  327. while (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN && ifa) {
  328. bss_conf->arp_addr_list[c] = ifa->ifa_address;
  329. ifa = ifa->ifa_next;
  330. c++;
  331. }
  332. /* If not all addresses fit the list, disable filtering */
  333. if (ifa) {
  334. sdata->arp_filter_state = false;
  335. c = 0;
  336. } else {
  337. sdata->arp_filter_state = true;
  338. }
  339. bss_conf->arp_addr_cnt = c;
  340. /* Configure driver only if associated */
  341. if (ifmgd->associated) {
  342. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  343. ieee80211_bss_info_change_notify(sdata,
  344. BSS_CHANGED_ARP_FILTER);
  345. }
  346. mutex_unlock(&ifmgd->mtx);
  347. return NOTIFY_DONE;
  348. }
  349. #endif
  350. static int ieee80211_napi_poll(struct napi_struct *napi, int budget)
  351. {
  352. struct ieee80211_local *local =
  353. container_of(napi, struct ieee80211_local, napi);
  354. return local->ops->napi_poll(&local->hw, budget);
  355. }
  356. void ieee80211_napi_schedule(struct ieee80211_hw *hw)
  357. {
  358. struct ieee80211_local *local = hw_to_local(hw);
  359. napi_schedule(&local->napi);
  360. }
  361. EXPORT_SYMBOL(ieee80211_napi_schedule);
  362. void ieee80211_napi_complete(struct ieee80211_hw *hw)
  363. {
  364. struct ieee80211_local *local = hw_to_local(hw);
  365. napi_complete(&local->napi);
  366. }
  367. EXPORT_SYMBOL(ieee80211_napi_complete);
  368. /* There isn't a lot of sense in it, but you can transmit anything you like */
  369. static const struct ieee80211_txrx_stypes
  370. ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  371. [NL80211_IFTYPE_ADHOC] = {
  372. .tx = 0xffff,
  373. .rx = BIT(IEEE80211_STYPE_ACTION >> 4),
  374. },
  375. [NL80211_IFTYPE_STATION] = {
  376. .tx = 0xffff,
  377. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  378. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  379. },
  380. [NL80211_IFTYPE_AP] = {
  381. .tx = 0xffff,
  382. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  383. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  384. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  385. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  386. BIT(IEEE80211_STYPE_AUTH >> 4) |
  387. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  388. BIT(IEEE80211_STYPE_ACTION >> 4),
  389. },
  390. [NL80211_IFTYPE_AP_VLAN] = {
  391. /* copy AP */
  392. .tx = 0xffff,
  393. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  394. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  395. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  396. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  397. BIT(IEEE80211_STYPE_AUTH >> 4) |
  398. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  399. BIT(IEEE80211_STYPE_ACTION >> 4),
  400. },
  401. [NL80211_IFTYPE_P2P_CLIENT] = {
  402. .tx = 0xffff,
  403. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  404. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  405. },
  406. [NL80211_IFTYPE_P2P_GO] = {
  407. .tx = 0xffff,
  408. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  409. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  410. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  411. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  412. BIT(IEEE80211_STYPE_AUTH >> 4) |
  413. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  414. BIT(IEEE80211_STYPE_ACTION >> 4),
  415. },
  416. [NL80211_IFTYPE_MESH_POINT] = {
  417. .tx = 0xffff,
  418. .rx = BIT(IEEE80211_STYPE_ACTION >> 4),
  419. },
  420. };
  421. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  422. const struct ieee80211_ops *ops)
  423. {
  424. struct ieee80211_local *local;
  425. int priv_size, i;
  426. struct wiphy *wiphy;
  427. /* Ensure 32-byte alignment of our private data and hw private data.
  428. * We use the wiphy priv data for both our ieee80211_local and for
  429. * the driver's private data
  430. *
  431. * In memory it'll be like this:
  432. *
  433. * +-------------------------+
  434. * | struct wiphy |
  435. * +-------------------------+
  436. * | struct ieee80211_local |
  437. * +-------------------------+
  438. * | driver's private data |
  439. * +-------------------------+
  440. *
  441. */
  442. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  443. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  444. if (!wiphy)
  445. return NULL;
  446. wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
  447. wiphy->privid = mac80211_wiphy_privid;
  448. wiphy->flags |= WIPHY_FLAG_NETNS_OK |
  449. WIPHY_FLAG_4ADDR_AP |
  450. WIPHY_FLAG_4ADDR_STATION |
  451. WIPHY_FLAG_SUPPORTS_SEPARATE_DEFAULT_KEYS;
  452. if (!ops->set_key)
  453. wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  454. wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
  455. local = wiphy_priv(wiphy);
  456. local->hw.wiphy = wiphy;
  457. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  458. BUG_ON(!ops->tx);
  459. BUG_ON(!ops->start);
  460. BUG_ON(!ops->stop);
  461. BUG_ON(!ops->config);
  462. BUG_ON(!ops->add_interface);
  463. BUG_ON(!ops->remove_interface);
  464. BUG_ON(!ops->configure_filter);
  465. local->ops = ops;
  466. /* set up some defaults */
  467. local->hw.queues = 1;
  468. local->hw.max_rates = 1;
  469. local->hw.max_report_rates = 0;
  470. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  471. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  472. local->user_power_level = -1;
  473. local->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  474. local->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  475. INIT_LIST_HEAD(&local->interfaces);
  476. __hw_addr_init(&local->mc_list);
  477. mutex_init(&local->iflist_mtx);
  478. mutex_init(&local->mtx);
  479. mutex_init(&local->key_mtx);
  480. spin_lock_init(&local->filter_lock);
  481. spin_lock_init(&local->queue_stop_reason_lock);
  482. /*
  483. * The rx_skb_queue is only accessed from tasklets,
  484. * but other SKB queues are used from within IRQ
  485. * context. Therefore, this one needs a different
  486. * locking class so our direct, non-irq-safe use of
  487. * the queue's lock doesn't throw lockdep warnings.
  488. */
  489. skb_queue_head_init_class(&local->rx_skb_queue,
  490. &ieee80211_rx_skb_queue_class);
  491. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  492. ieee80211_work_init(local);
  493. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  494. INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
  495. INIT_WORK(&local->recalc_smps, ieee80211_recalc_smps_work);
  496. local->smps_mode = IEEE80211_SMPS_OFF;
  497. INIT_WORK(&local->dynamic_ps_enable_work,
  498. ieee80211_dynamic_ps_enable_work);
  499. INIT_WORK(&local->dynamic_ps_disable_work,
  500. ieee80211_dynamic_ps_disable_work);
  501. setup_timer(&local->dynamic_ps_timer,
  502. ieee80211_dynamic_ps_timer, (unsigned long) local);
  503. sta_info_init(local);
  504. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  505. skb_queue_head_init(&local->pending[i]);
  506. atomic_set(&local->agg_queue_stop[i], 0);
  507. }
  508. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  509. (unsigned long)local);
  510. tasklet_init(&local->tasklet,
  511. ieee80211_tasklet_handler,
  512. (unsigned long) local);
  513. skb_queue_head_init(&local->skb_queue);
  514. skb_queue_head_init(&local->skb_queue_unreliable);
  515. /* init dummy netdev for use w/ NAPI */
  516. init_dummy_netdev(&local->napi_dev);
  517. ieee80211_led_names(local);
  518. ieee80211_hw_roc_setup(local);
  519. return local_to_hw(local);
  520. }
  521. EXPORT_SYMBOL(ieee80211_alloc_hw);
  522. int ieee80211_register_hw(struct ieee80211_hw *hw)
  523. {
  524. struct ieee80211_local *local = hw_to_local(hw);
  525. int result;
  526. enum ieee80211_band band;
  527. int channels, max_bitrates;
  528. bool supp_ht;
  529. static const u32 cipher_suites[] = {
  530. /* keep WEP first, it may be removed below */
  531. WLAN_CIPHER_SUITE_WEP40,
  532. WLAN_CIPHER_SUITE_WEP104,
  533. WLAN_CIPHER_SUITE_TKIP,
  534. WLAN_CIPHER_SUITE_CCMP,
  535. /* keep last -- depends on hw flags! */
  536. WLAN_CIPHER_SUITE_AES_CMAC
  537. };
  538. if (hw->max_report_rates == 0)
  539. hw->max_report_rates = hw->max_rates;
  540. /*
  541. * generic code guarantees at least one band,
  542. * set this very early because much code assumes
  543. * that hw.conf.channel is assigned
  544. */
  545. channels = 0;
  546. max_bitrates = 0;
  547. supp_ht = false;
  548. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  549. struct ieee80211_supported_band *sband;
  550. sband = local->hw.wiphy->bands[band];
  551. if (!sband)
  552. continue;
  553. if (!local->oper_channel) {
  554. /* init channel we're on */
  555. local->hw.conf.channel =
  556. local->oper_channel = &sband->channels[0];
  557. local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
  558. }
  559. channels += sband->n_channels;
  560. if (max_bitrates < sband->n_bitrates)
  561. max_bitrates = sband->n_bitrates;
  562. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  563. }
  564. local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
  565. sizeof(void *) * channels, GFP_KERNEL);
  566. if (!local->int_scan_req)
  567. return -ENOMEM;
  568. /* if low-level driver supports AP, we also support VLAN */
  569. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  570. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  571. /* mac80211 always supports monitor */
  572. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  573. #ifndef CONFIG_MAC80211_MESH
  574. /* mesh depends on Kconfig, but drivers should set it if they want */
  575. local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
  576. #endif
  577. /* mac80211 supports control port protocol changing */
  578. local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
  579. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  580. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  581. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  582. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  583. WARN((local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)
  584. && (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK),
  585. "U-APSD not supported with HW_PS_NULLFUNC_STACK\n");
  586. /*
  587. * Calculate scan IE length -- we need this to alloc
  588. * memory and to subtract from the driver limit. It
  589. * includes the DS Params, (extended) supported rates, and HT
  590. * information -- SSID is the driver's responsibility.
  591. */
  592. local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
  593. 3 /* DS Params */;
  594. if (supp_ht)
  595. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  596. if (!local->ops->hw_scan) {
  597. /* For hw_scan, driver needs to set these up. */
  598. local->hw.wiphy->max_scan_ssids = 4;
  599. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  600. }
  601. /*
  602. * If the driver supports any scan IEs, then assume the
  603. * limit includes the IEs mac80211 will add, otherwise
  604. * leave it at zero and let the driver sort it out; we
  605. * still pass our IEs to the driver but userspace will
  606. * not be allowed to in that case.
  607. */
  608. if (local->hw.wiphy->max_scan_ie_len)
  609. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  610. /* Set up cipher suites unless driver already did */
  611. if (!local->hw.wiphy->cipher_suites) {
  612. local->hw.wiphy->cipher_suites = cipher_suites;
  613. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  614. if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
  615. local->hw.wiphy->n_cipher_suites--;
  616. }
  617. if (IS_ERR(local->wep_tx_tfm) || IS_ERR(local->wep_rx_tfm)) {
  618. if (local->hw.wiphy->cipher_suites == cipher_suites) {
  619. local->hw.wiphy->cipher_suites += 2;
  620. local->hw.wiphy->n_cipher_suites -= 2;
  621. } else {
  622. u32 *suites;
  623. int r, w = 0;
  624. /* Filter out WEP */
  625. suites = kmemdup(
  626. local->hw.wiphy->cipher_suites,
  627. sizeof(u32) * local->hw.wiphy->n_cipher_suites,
  628. GFP_KERNEL);
  629. if (!suites)
  630. return -ENOMEM;
  631. for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
  632. u32 suite = local->hw.wiphy->cipher_suites[r];
  633. if (suite == WLAN_CIPHER_SUITE_WEP40 ||
  634. suite == WLAN_CIPHER_SUITE_WEP104)
  635. continue;
  636. suites[w++] = suite;
  637. }
  638. local->hw.wiphy->cipher_suites = suites;
  639. local->hw.wiphy->n_cipher_suites = w;
  640. local->wiphy_ciphers_allocated = true;
  641. }
  642. }
  643. if (!local->ops->remain_on_channel)
  644. local->hw.wiphy->max_remain_on_channel_duration = 5000;
  645. result = wiphy_register(local->hw.wiphy);
  646. if (result < 0)
  647. goto fail_wiphy_register;
  648. /*
  649. * We use the number of queues for feature tests (QoS, HT) internally
  650. * so restrict them appropriately.
  651. */
  652. if (hw->queues > IEEE80211_MAX_QUEUES)
  653. hw->queues = IEEE80211_MAX_QUEUES;
  654. local->workqueue =
  655. alloc_ordered_workqueue(wiphy_name(local->hw.wiphy), 0);
  656. if (!local->workqueue) {
  657. result = -ENOMEM;
  658. goto fail_workqueue;
  659. }
  660. /*
  661. * The hardware needs headroom for sending the frame,
  662. * and we need some headroom for passing the frame to monitor
  663. * interfaces, but never both at the same time.
  664. */
  665. BUILD_BUG_ON(IEEE80211_TX_STATUS_HEADROOM !=
  666. sizeof(struct ieee80211_tx_status_rtap_hdr));
  667. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  668. sizeof(struct ieee80211_tx_status_rtap_hdr));
  669. debugfs_hw_add(local);
  670. /*
  671. * if the driver doesn't specify a max listen interval we
  672. * use 5 which should be a safe default
  673. */
  674. if (local->hw.max_listen_interval == 0)
  675. local->hw.max_listen_interval = 5;
  676. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  677. local->dynamic_ps_forced_timeout = -1;
  678. result = sta_info_start(local);
  679. if (result < 0)
  680. goto fail_sta_info;
  681. result = ieee80211_wep_init(local);
  682. if (result < 0)
  683. wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
  684. result);
  685. rtnl_lock();
  686. result = ieee80211_init_rate_ctrl_alg(local,
  687. hw->rate_control_algorithm);
  688. if (result < 0) {
  689. wiphy_debug(local->hw.wiphy,
  690. "Failed to initialize rate control algorithm\n");
  691. goto fail_rate;
  692. }
  693. /* add one default STA interface if supported */
  694. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  695. result = ieee80211_if_add(local, "wlan%d", NULL,
  696. NL80211_IFTYPE_STATION, NULL);
  697. if (result)
  698. wiphy_warn(local->hw.wiphy,
  699. "Failed to add default virtual iface\n");
  700. }
  701. rtnl_unlock();
  702. ieee80211_led_init(local);
  703. local->network_latency_notifier.notifier_call =
  704. ieee80211_max_network_latency;
  705. result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
  706. &local->network_latency_notifier);
  707. if (result) {
  708. rtnl_lock();
  709. goto fail_pm_qos;
  710. }
  711. #ifdef CONFIG_INET
  712. local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
  713. result = register_inetaddr_notifier(&local->ifa_notifier);
  714. if (result)
  715. goto fail_ifa;
  716. #endif
  717. netif_napi_add(&local->napi_dev, &local->napi, ieee80211_napi_poll,
  718. local->hw.napi_weight);
  719. return 0;
  720. #ifdef CONFIG_INET
  721. fail_ifa:
  722. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  723. &local->network_latency_notifier);
  724. rtnl_lock();
  725. #endif
  726. fail_pm_qos:
  727. ieee80211_led_exit(local);
  728. ieee80211_remove_interfaces(local);
  729. fail_rate:
  730. rtnl_unlock();
  731. ieee80211_wep_free(local);
  732. sta_info_stop(local);
  733. fail_sta_info:
  734. destroy_workqueue(local->workqueue);
  735. fail_workqueue:
  736. wiphy_unregister(local->hw.wiphy);
  737. fail_wiphy_register:
  738. if (local->wiphy_ciphers_allocated)
  739. kfree(local->hw.wiphy->cipher_suites);
  740. kfree(local->int_scan_req);
  741. return result;
  742. }
  743. EXPORT_SYMBOL(ieee80211_register_hw);
  744. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  745. {
  746. struct ieee80211_local *local = hw_to_local(hw);
  747. tasklet_kill(&local->tx_pending_tasklet);
  748. tasklet_kill(&local->tasklet);
  749. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  750. &local->network_latency_notifier);
  751. #ifdef CONFIG_INET
  752. unregister_inetaddr_notifier(&local->ifa_notifier);
  753. #endif
  754. rtnl_lock();
  755. /*
  756. * At this point, interface list manipulations are fine
  757. * because the driver cannot be handing us frames any
  758. * more and the tasklet is killed.
  759. */
  760. ieee80211_remove_interfaces(local);
  761. rtnl_unlock();
  762. /*
  763. * Now all work items will be gone, but the
  764. * timer might still be armed, so delete it
  765. */
  766. del_timer_sync(&local->work_timer);
  767. cancel_work_sync(&local->restart_work);
  768. cancel_work_sync(&local->reconfig_filter);
  769. ieee80211_clear_tx_pending(local);
  770. sta_info_stop(local);
  771. rate_control_deinitialize(local);
  772. if (skb_queue_len(&local->skb_queue) ||
  773. skb_queue_len(&local->skb_queue_unreliable))
  774. wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
  775. skb_queue_purge(&local->skb_queue);
  776. skb_queue_purge(&local->skb_queue_unreliable);
  777. skb_queue_purge(&local->rx_skb_queue);
  778. destroy_workqueue(local->workqueue);
  779. wiphy_unregister(local->hw.wiphy);
  780. ieee80211_wep_free(local);
  781. ieee80211_led_exit(local);
  782. kfree(local->int_scan_req);
  783. }
  784. EXPORT_SYMBOL(ieee80211_unregister_hw);
  785. void ieee80211_free_hw(struct ieee80211_hw *hw)
  786. {
  787. struct ieee80211_local *local = hw_to_local(hw);
  788. mutex_destroy(&local->iflist_mtx);
  789. mutex_destroy(&local->mtx);
  790. if (local->wiphy_ciphers_allocated)
  791. kfree(local->hw.wiphy->cipher_suites);
  792. wiphy_free(local->hw.wiphy);
  793. }
  794. EXPORT_SYMBOL(ieee80211_free_hw);
  795. static int __init ieee80211_init(void)
  796. {
  797. struct sk_buff *skb;
  798. int ret;
  799. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  800. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  801. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  802. ret = rc80211_minstrel_init();
  803. if (ret)
  804. return ret;
  805. ret = rc80211_minstrel_ht_init();
  806. if (ret)
  807. goto err_minstrel;
  808. ret = rc80211_pid_init();
  809. if (ret)
  810. goto err_pid;
  811. ret = ieee80211_iface_init();
  812. if (ret)
  813. goto err_netdev;
  814. return 0;
  815. err_netdev:
  816. rc80211_pid_exit();
  817. err_pid:
  818. rc80211_minstrel_ht_exit();
  819. err_minstrel:
  820. rc80211_minstrel_exit();
  821. return ret;
  822. }
  823. static void __exit ieee80211_exit(void)
  824. {
  825. rc80211_pid_exit();
  826. rc80211_minstrel_ht_exit();
  827. rc80211_minstrel_exit();
  828. if (mesh_allocated)
  829. ieee80211s_stop();
  830. ieee80211_iface_exit();
  831. }
  832. subsys_initcall(ieee80211_init);
  833. module_exit(ieee80211_exit);
  834. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  835. MODULE_LICENSE("GPL");