main.c 27 KB

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