main.c 25 KB

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