main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. /* wait for scan work complete */
  256. flush_workqueue(local->workqueue);
  257. mutex_lock(&local->mtx);
  258. WARN(test_bit(SCAN_HW_SCANNING, &local->scanning),
  259. "%s called with hardware scan in progress\n", __func__);
  260. mutex_unlock(&local->mtx);
  261. rtnl_lock();
  262. if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)))
  263. ieee80211_scan_cancel(local);
  264. ieee80211_reconfig(local);
  265. rtnl_unlock();
  266. }
  267. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  268. {
  269. struct ieee80211_local *local = hw_to_local(hw);
  270. trace_api_restart_hw(local);
  271. /* use this reason, ieee80211_reconfig will unblock it */
  272. ieee80211_stop_queues_by_reason(hw,
  273. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  274. schedule_work(&local->restart_work);
  275. }
  276. EXPORT_SYMBOL(ieee80211_restart_hw);
  277. static void ieee80211_recalc_smps_work(struct work_struct *work)
  278. {
  279. struct ieee80211_local *local =
  280. container_of(work, struct ieee80211_local, recalc_smps);
  281. mutex_lock(&local->iflist_mtx);
  282. ieee80211_recalc_smps(local, NULL);
  283. mutex_unlock(&local->iflist_mtx);
  284. }
  285. #ifdef CONFIG_INET
  286. static int ieee80211_ifa_changed(struct notifier_block *nb,
  287. unsigned long data, void *arg)
  288. {
  289. struct in_ifaddr *ifa = arg;
  290. struct ieee80211_local *local =
  291. container_of(nb, struct ieee80211_local,
  292. ifa_notifier);
  293. struct net_device *ndev = ifa->ifa_dev->dev;
  294. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  295. struct in_device *idev;
  296. struct ieee80211_sub_if_data *sdata;
  297. struct ieee80211_bss_conf *bss_conf;
  298. struct ieee80211_if_managed *ifmgd;
  299. int c = 0;
  300. /* Make sure it's our interface that got changed */
  301. if (!wdev)
  302. return NOTIFY_DONE;
  303. if (wdev->wiphy != local->hw.wiphy)
  304. return NOTIFY_DONE;
  305. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  306. bss_conf = &sdata->vif.bss_conf;
  307. if (!ieee80211_sdata_running(sdata))
  308. return NOTIFY_DONE;
  309. /* ARP filtering is only supported in managed mode */
  310. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  311. return NOTIFY_DONE;
  312. idev = sdata->dev->ip_ptr;
  313. if (!idev)
  314. return NOTIFY_DONE;
  315. ifmgd = &sdata->u.mgd;
  316. mutex_lock(&ifmgd->mtx);
  317. /* Copy the addresses to the bss_conf list */
  318. ifa = idev->ifa_list;
  319. while (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN && ifa) {
  320. bss_conf->arp_addr_list[c] = ifa->ifa_address;
  321. ifa = ifa->ifa_next;
  322. c++;
  323. }
  324. /* If not all addresses fit the list, disable filtering */
  325. if (ifa) {
  326. sdata->arp_filter_state = false;
  327. c = 0;
  328. } else {
  329. sdata->arp_filter_state = true;
  330. }
  331. bss_conf->arp_addr_cnt = c;
  332. /* Configure driver only if associated */
  333. if (ifmgd->associated) {
  334. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  335. ieee80211_bss_info_change_notify(sdata,
  336. BSS_CHANGED_ARP_FILTER);
  337. }
  338. mutex_unlock(&ifmgd->mtx);
  339. return NOTIFY_DONE;
  340. }
  341. #endif
  342. static int ieee80211_napi_poll(struct napi_struct *napi, int budget)
  343. {
  344. struct ieee80211_local *local =
  345. container_of(napi, struct ieee80211_local, napi);
  346. return local->ops->napi_poll(&local->hw, budget);
  347. }
  348. void ieee80211_napi_schedule(struct ieee80211_hw *hw)
  349. {
  350. struct ieee80211_local *local = hw_to_local(hw);
  351. napi_schedule(&local->napi);
  352. }
  353. EXPORT_SYMBOL(ieee80211_napi_schedule);
  354. void ieee80211_napi_complete(struct ieee80211_hw *hw)
  355. {
  356. struct ieee80211_local *local = hw_to_local(hw);
  357. napi_complete(&local->napi);
  358. }
  359. EXPORT_SYMBOL(ieee80211_napi_complete);
  360. /* There isn't a lot of sense in it, but you can transmit anything you like */
  361. static const struct ieee80211_txrx_stypes
  362. ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  363. [NL80211_IFTYPE_ADHOC] = {
  364. .tx = 0xffff,
  365. .rx = BIT(IEEE80211_STYPE_ACTION >> 4),
  366. },
  367. [NL80211_IFTYPE_STATION] = {
  368. .tx = 0xffff,
  369. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  370. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  371. },
  372. [NL80211_IFTYPE_AP] = {
  373. .tx = 0xffff,
  374. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  375. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  376. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  377. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  378. BIT(IEEE80211_STYPE_AUTH >> 4) |
  379. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  380. BIT(IEEE80211_STYPE_ACTION >> 4),
  381. },
  382. [NL80211_IFTYPE_AP_VLAN] = {
  383. /* copy AP */
  384. .tx = 0xffff,
  385. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  386. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  387. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  388. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  389. BIT(IEEE80211_STYPE_AUTH >> 4) |
  390. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  391. BIT(IEEE80211_STYPE_ACTION >> 4),
  392. },
  393. [NL80211_IFTYPE_P2P_CLIENT] = {
  394. .tx = 0xffff,
  395. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  396. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  397. },
  398. [NL80211_IFTYPE_P2P_GO] = {
  399. .tx = 0xffff,
  400. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  401. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  402. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  403. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  404. BIT(IEEE80211_STYPE_AUTH >> 4) |
  405. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  406. BIT(IEEE80211_STYPE_ACTION >> 4),
  407. },
  408. };
  409. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  410. const struct ieee80211_ops *ops)
  411. {
  412. struct ieee80211_local *local;
  413. int priv_size, i;
  414. struct wiphy *wiphy;
  415. /* Ensure 32-byte alignment of our private data and hw private data.
  416. * We use the wiphy priv data for both our ieee80211_local and for
  417. * the driver's private data
  418. *
  419. * In memory it'll be like this:
  420. *
  421. * +-------------------------+
  422. * | struct wiphy |
  423. * +-------------------------+
  424. * | struct ieee80211_local |
  425. * +-------------------------+
  426. * | driver's private data |
  427. * +-------------------------+
  428. *
  429. */
  430. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  431. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  432. if (!wiphy)
  433. return NULL;
  434. wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
  435. wiphy->flags |= WIPHY_FLAG_NETNS_OK |
  436. WIPHY_FLAG_4ADDR_AP |
  437. WIPHY_FLAG_4ADDR_STATION;
  438. wiphy->privid = mac80211_wiphy_privid;
  439. wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
  440. local = wiphy_priv(wiphy);
  441. local->hw.wiphy = wiphy;
  442. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  443. BUG_ON(!ops->tx);
  444. BUG_ON(!ops->start);
  445. BUG_ON(!ops->stop);
  446. BUG_ON(!ops->config);
  447. BUG_ON(!ops->add_interface);
  448. BUG_ON(!ops->remove_interface);
  449. BUG_ON(!ops->configure_filter);
  450. local->ops = ops;
  451. /* set up some defaults */
  452. local->hw.queues = 1;
  453. local->hw.max_rates = 1;
  454. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  455. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  456. local->user_power_level = -1;
  457. local->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  458. local->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  459. INIT_LIST_HEAD(&local->interfaces);
  460. __hw_addr_init(&local->mc_list);
  461. mutex_init(&local->iflist_mtx);
  462. mutex_init(&local->mtx);
  463. mutex_init(&local->key_mtx);
  464. spin_lock_init(&local->filter_lock);
  465. spin_lock_init(&local->queue_stop_reason_lock);
  466. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  467. ieee80211_work_init(local);
  468. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  469. INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
  470. INIT_WORK(&local->recalc_smps, ieee80211_recalc_smps_work);
  471. local->smps_mode = IEEE80211_SMPS_OFF;
  472. INIT_WORK(&local->dynamic_ps_enable_work,
  473. ieee80211_dynamic_ps_enable_work);
  474. INIT_WORK(&local->dynamic_ps_disable_work,
  475. ieee80211_dynamic_ps_disable_work);
  476. setup_timer(&local->dynamic_ps_timer,
  477. ieee80211_dynamic_ps_timer, (unsigned long) local);
  478. sta_info_init(local);
  479. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  480. skb_queue_head_init(&local->pending[i]);
  481. atomic_set(&local->agg_queue_stop[i], 0);
  482. }
  483. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  484. (unsigned long)local);
  485. tasklet_init(&local->tasklet,
  486. ieee80211_tasklet_handler,
  487. (unsigned long) local);
  488. skb_queue_head_init(&local->skb_queue);
  489. skb_queue_head_init(&local->skb_queue_unreliable);
  490. /* init dummy netdev for use w/ NAPI */
  491. init_dummy_netdev(&local->napi_dev);
  492. return local_to_hw(local);
  493. }
  494. EXPORT_SYMBOL(ieee80211_alloc_hw);
  495. int ieee80211_register_hw(struct ieee80211_hw *hw)
  496. {
  497. struct ieee80211_local *local = hw_to_local(hw);
  498. int result;
  499. enum ieee80211_band band;
  500. int channels, max_bitrates;
  501. bool supp_ht;
  502. static const u32 cipher_suites[] = {
  503. /* keep WEP first, it may be removed below */
  504. WLAN_CIPHER_SUITE_WEP40,
  505. WLAN_CIPHER_SUITE_WEP104,
  506. WLAN_CIPHER_SUITE_TKIP,
  507. WLAN_CIPHER_SUITE_CCMP,
  508. /* keep last -- depends on hw flags! */
  509. WLAN_CIPHER_SUITE_AES_CMAC
  510. };
  511. /*
  512. * generic code guarantees at least one band,
  513. * set this very early because much code assumes
  514. * that hw.conf.channel is assigned
  515. */
  516. channels = 0;
  517. max_bitrates = 0;
  518. supp_ht = false;
  519. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  520. struct ieee80211_supported_band *sband;
  521. sband = local->hw.wiphy->bands[band];
  522. if (!sband)
  523. continue;
  524. if (!local->oper_channel) {
  525. /* init channel we're on */
  526. local->hw.conf.channel =
  527. local->oper_channel = &sband->channels[0];
  528. local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
  529. }
  530. channels += sband->n_channels;
  531. if (max_bitrates < sband->n_bitrates)
  532. max_bitrates = sband->n_bitrates;
  533. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  534. }
  535. local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
  536. sizeof(void *) * channels, GFP_KERNEL);
  537. if (!local->int_scan_req)
  538. return -ENOMEM;
  539. /* if low-level driver supports AP, we also support VLAN */
  540. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  541. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  542. /* mac80211 always supports monitor */
  543. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  544. #ifndef CONFIG_MAC80211_MESH
  545. /* mesh depends on Kconfig, but drivers should set it if they want */
  546. local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
  547. #endif
  548. /* mac80211 supports control port protocol changing */
  549. local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
  550. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  551. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  552. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  553. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  554. WARN((local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)
  555. && (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK),
  556. "U-APSD not supported with HW_PS_NULLFUNC_STACK\n");
  557. /*
  558. * Calculate scan IE length -- we need this to alloc
  559. * memory and to subtract from the driver limit. It
  560. * includes the (extended) supported rates and HT
  561. * information -- SSID is the driver's responsibility.
  562. */
  563. local->scan_ies_len = 4 + max_bitrates; /* (ext) supp rates */
  564. if (supp_ht)
  565. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  566. if (!local->ops->hw_scan) {
  567. /* For hw_scan, driver needs to set these up. */
  568. local->hw.wiphy->max_scan_ssids = 4;
  569. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  570. }
  571. /*
  572. * If the driver supports any scan IEs, then assume the
  573. * limit includes the IEs mac80211 will add, otherwise
  574. * leave it at zero and let the driver sort it out; we
  575. * still pass our IEs to the driver but userspace will
  576. * not be allowed to in that case.
  577. */
  578. if (local->hw.wiphy->max_scan_ie_len)
  579. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  580. /* Set up cipher suites unless driver already did */
  581. if (!local->hw.wiphy->cipher_suites) {
  582. local->hw.wiphy->cipher_suites = cipher_suites;
  583. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  584. if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
  585. local->hw.wiphy->n_cipher_suites--;
  586. }
  587. if (IS_ERR(local->wep_tx_tfm) || IS_ERR(local->wep_rx_tfm)) {
  588. if (local->hw.wiphy->cipher_suites == cipher_suites) {
  589. local->hw.wiphy->cipher_suites += 2;
  590. local->hw.wiphy->n_cipher_suites -= 2;
  591. } else {
  592. u32 *suites;
  593. int r, w = 0;
  594. /* Filter out WEP */
  595. suites = kmemdup(
  596. local->hw.wiphy->cipher_suites,
  597. sizeof(u32) * local->hw.wiphy->n_cipher_suites,
  598. GFP_KERNEL);
  599. if (!suites)
  600. return -ENOMEM;
  601. for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
  602. u32 suite = local->hw.wiphy->cipher_suites[r];
  603. if (suite == WLAN_CIPHER_SUITE_WEP40 ||
  604. suite == WLAN_CIPHER_SUITE_WEP104)
  605. continue;
  606. suites[w++] = suite;
  607. }
  608. local->hw.wiphy->cipher_suites = suites;
  609. local->hw.wiphy->n_cipher_suites = w;
  610. local->wiphy_ciphers_allocated = true;
  611. }
  612. }
  613. result = wiphy_register(local->hw.wiphy);
  614. if (result < 0)
  615. goto fail_wiphy_register;
  616. /*
  617. * We use the number of queues for feature tests (QoS, HT) internally
  618. * so restrict them appropriately.
  619. */
  620. if (hw->queues > IEEE80211_MAX_QUEUES)
  621. hw->queues = IEEE80211_MAX_QUEUES;
  622. local->workqueue =
  623. create_singlethread_workqueue(wiphy_name(local->hw.wiphy));
  624. if (!local->workqueue) {
  625. result = -ENOMEM;
  626. goto fail_workqueue;
  627. }
  628. /*
  629. * The hardware needs headroom for sending the frame,
  630. * and we need some headroom for passing the frame to monitor
  631. * interfaces, but never both at the same time.
  632. */
  633. BUILD_BUG_ON(IEEE80211_TX_STATUS_HEADROOM !=
  634. sizeof(struct ieee80211_tx_status_rtap_hdr));
  635. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  636. sizeof(struct ieee80211_tx_status_rtap_hdr));
  637. debugfs_hw_add(local);
  638. /*
  639. * if the driver doesn't specify a max listen interval we
  640. * use 5 which should be a safe default
  641. */
  642. if (local->hw.max_listen_interval == 0)
  643. local->hw.max_listen_interval = 5;
  644. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  645. local->dynamic_ps_forced_timeout = -1;
  646. result = sta_info_start(local);
  647. if (result < 0)
  648. goto fail_sta_info;
  649. result = ieee80211_wep_init(local);
  650. if (result < 0)
  651. wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
  652. result);
  653. rtnl_lock();
  654. result = ieee80211_init_rate_ctrl_alg(local,
  655. hw->rate_control_algorithm);
  656. if (result < 0) {
  657. wiphy_debug(local->hw.wiphy,
  658. "Failed to initialize rate control algorithm\n");
  659. goto fail_rate;
  660. }
  661. /* add one default STA interface if supported */
  662. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  663. result = ieee80211_if_add(local, "wlan%d", NULL,
  664. NL80211_IFTYPE_STATION, NULL);
  665. if (result)
  666. wiphy_warn(local->hw.wiphy,
  667. "Failed to add default virtual iface\n");
  668. }
  669. rtnl_unlock();
  670. ieee80211_led_init(local);
  671. local->network_latency_notifier.notifier_call =
  672. ieee80211_max_network_latency;
  673. result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
  674. &local->network_latency_notifier);
  675. if (result) {
  676. rtnl_lock();
  677. goto fail_pm_qos;
  678. }
  679. #ifdef CONFIG_INET
  680. local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
  681. result = register_inetaddr_notifier(&local->ifa_notifier);
  682. if (result)
  683. goto fail_ifa;
  684. #endif
  685. netif_napi_add(&local->napi_dev, &local->napi, ieee80211_napi_poll,
  686. local->hw.napi_weight);
  687. return 0;
  688. #ifdef CONFIG_INET
  689. fail_ifa:
  690. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  691. &local->network_latency_notifier);
  692. rtnl_lock();
  693. #endif
  694. fail_pm_qos:
  695. ieee80211_led_exit(local);
  696. ieee80211_remove_interfaces(local);
  697. fail_rate:
  698. rtnl_unlock();
  699. ieee80211_wep_free(local);
  700. sta_info_stop(local);
  701. fail_sta_info:
  702. destroy_workqueue(local->workqueue);
  703. fail_workqueue:
  704. wiphy_unregister(local->hw.wiphy);
  705. fail_wiphy_register:
  706. if (local->wiphy_ciphers_allocated)
  707. kfree(local->hw.wiphy->cipher_suites);
  708. kfree(local->int_scan_req);
  709. return result;
  710. }
  711. EXPORT_SYMBOL(ieee80211_register_hw);
  712. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  713. {
  714. struct ieee80211_local *local = hw_to_local(hw);
  715. tasklet_kill(&local->tx_pending_tasklet);
  716. tasklet_kill(&local->tasklet);
  717. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  718. &local->network_latency_notifier);
  719. #ifdef CONFIG_INET
  720. unregister_inetaddr_notifier(&local->ifa_notifier);
  721. #endif
  722. rtnl_lock();
  723. /*
  724. * At this point, interface list manipulations are fine
  725. * because the driver cannot be handing us frames any
  726. * more and the tasklet is killed.
  727. */
  728. ieee80211_remove_interfaces(local);
  729. rtnl_unlock();
  730. /*
  731. * Now all work items will be gone, but the
  732. * timer might still be armed, so delete it
  733. */
  734. del_timer_sync(&local->work_timer);
  735. cancel_work_sync(&local->restart_work);
  736. cancel_work_sync(&local->reconfig_filter);
  737. ieee80211_clear_tx_pending(local);
  738. sta_info_stop(local);
  739. rate_control_deinitialize(local);
  740. if (skb_queue_len(&local->skb_queue) ||
  741. skb_queue_len(&local->skb_queue_unreliable))
  742. wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
  743. skb_queue_purge(&local->skb_queue);
  744. skb_queue_purge(&local->skb_queue_unreliable);
  745. destroy_workqueue(local->workqueue);
  746. wiphy_unregister(local->hw.wiphy);
  747. ieee80211_wep_free(local);
  748. ieee80211_led_exit(local);
  749. kfree(local->int_scan_req);
  750. }
  751. EXPORT_SYMBOL(ieee80211_unregister_hw);
  752. void ieee80211_free_hw(struct ieee80211_hw *hw)
  753. {
  754. struct ieee80211_local *local = hw_to_local(hw);
  755. mutex_destroy(&local->iflist_mtx);
  756. mutex_destroy(&local->mtx);
  757. if (local->wiphy_ciphers_allocated)
  758. kfree(local->hw.wiphy->cipher_suites);
  759. wiphy_free(local->hw.wiphy);
  760. }
  761. EXPORT_SYMBOL(ieee80211_free_hw);
  762. static int __init ieee80211_init(void)
  763. {
  764. struct sk_buff *skb;
  765. int ret;
  766. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  767. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  768. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  769. ret = rc80211_minstrel_init();
  770. if (ret)
  771. return ret;
  772. ret = rc80211_minstrel_ht_init();
  773. if (ret)
  774. goto err_minstrel;
  775. ret = rc80211_pid_init();
  776. if (ret)
  777. goto err_pid;
  778. ret = ieee80211_iface_init();
  779. if (ret)
  780. goto err_netdev;
  781. return 0;
  782. err_netdev:
  783. rc80211_pid_exit();
  784. err_pid:
  785. rc80211_minstrel_ht_exit();
  786. err_minstrel:
  787. rc80211_minstrel_exit();
  788. return ret;
  789. }
  790. static void __exit ieee80211_exit(void)
  791. {
  792. rc80211_pid_exit();
  793. rc80211_minstrel_ht_exit();
  794. rc80211_minstrel_exit();
  795. /*
  796. * For key todo, it'll be empty by now but the work
  797. * might still be scheduled.
  798. */
  799. flush_scheduled_work();
  800. if (mesh_allocated)
  801. ieee80211s_stop();
  802. ieee80211_iface_exit();
  803. }
  804. subsys_initcall(ieee80211_init);
  805. module_exit(ieee80211_exit);
  806. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  807. MODULE_LICENSE("GPL");