main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/pm_qos_params.h>
  24. #include <net/net_namespace.h>
  25. #include <net/cfg80211.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wep.h"
  31. #include "wme.h"
  32. #include "aes_ccm.h"
  33. #include "led.h"
  34. #include "cfg.h"
  35. #include "debugfs.h"
  36. #include "debugfs_netdev.h"
  37. /*
  38. * For seeing transmitted packets on monitor interfaces
  39. * we have a radiotap header too.
  40. */
  41. struct ieee80211_tx_status_rtap_hdr {
  42. struct ieee80211_radiotap_header hdr;
  43. u8 rate;
  44. u8 padding_for_rate;
  45. __le16 tx_flags;
  46. u8 data_retries;
  47. } __attribute__ ((packed));
  48. void ieee80211_configure_filter(struct ieee80211_local *local)
  49. {
  50. u64 mc;
  51. unsigned int changed_flags;
  52. unsigned int new_flags = 0;
  53. if (atomic_read(&local->iff_promiscs))
  54. new_flags |= FIF_PROMISC_IN_BSS;
  55. if (atomic_read(&local->iff_allmultis))
  56. new_flags |= FIF_ALLMULTI;
  57. if (local->monitors || local->scanning)
  58. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  59. if (local->fif_fcsfail)
  60. new_flags |= FIF_FCSFAIL;
  61. if (local->fif_plcpfail)
  62. new_flags |= FIF_PLCPFAIL;
  63. if (local->fif_control)
  64. new_flags |= FIF_CONTROL;
  65. if (local->fif_other_bss)
  66. new_flags |= FIF_OTHER_BSS;
  67. if (local->fif_pspoll)
  68. new_flags |= FIF_PSPOLL;
  69. spin_lock_bh(&local->filter_lock);
  70. changed_flags = local->filter_flags ^ new_flags;
  71. mc = drv_prepare_multicast(local, local->mc_count, local->mc_list);
  72. spin_unlock_bh(&local->filter_lock);
  73. /* be a bit nasty */
  74. new_flags |= (1<<31);
  75. drv_configure_filter(local, changed_flags, &new_flags, mc);
  76. WARN_ON(new_flags & (1<<31));
  77. local->filter_flags = new_flags & ~(1<<31);
  78. }
  79. static void ieee80211_reconfig_filter(struct work_struct *work)
  80. {
  81. struct ieee80211_local *local =
  82. container_of(work, struct ieee80211_local, reconfig_filter);
  83. ieee80211_configure_filter(local);
  84. }
  85. int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
  86. {
  87. struct ieee80211_channel *chan, *scan_chan;
  88. int ret = 0;
  89. int power;
  90. enum nl80211_channel_type channel_type;
  91. might_sleep();
  92. scan_chan = local->scan_channel;
  93. if (scan_chan) {
  94. chan = scan_chan;
  95. channel_type = NL80211_CHAN_NO_HT;
  96. } else {
  97. chan = local->oper_channel;
  98. channel_type = local->oper_channel_type;
  99. }
  100. if (chan != local->hw.conf.channel ||
  101. channel_type != local->hw.conf.channel_type) {
  102. local->hw.conf.channel = chan;
  103. local->hw.conf.channel_type = channel_type;
  104. changed |= IEEE80211_CONF_CHANGE_CHANNEL;
  105. }
  106. if (scan_chan)
  107. power = chan->max_power;
  108. else
  109. power = local->power_constr_level ?
  110. (chan->max_power - local->power_constr_level) :
  111. chan->max_power;
  112. if (local->user_power_level >= 0)
  113. power = min(power, local->user_power_level);
  114. if (local->hw.conf.power_level != power) {
  115. changed |= IEEE80211_CONF_CHANGE_POWER;
  116. local->hw.conf.power_level = power;
  117. }
  118. if (changed && local->open_count) {
  119. ret = drv_config(local, changed);
  120. /*
  121. * Goal:
  122. * HW reconfiguration should never fail, the driver has told
  123. * us what it can support so it should live up to that promise.
  124. *
  125. * Current status:
  126. * rfkill is not integrated with mac80211 and a
  127. * configuration command can thus fail if hardware rfkill
  128. * is enabled
  129. *
  130. * FIXME: integrate rfkill with mac80211 and then add this
  131. * WARN_ON() back
  132. *
  133. */
  134. /* WARN_ON(ret); */
  135. }
  136. return ret;
  137. }
  138. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  139. u32 changed)
  140. {
  141. struct ieee80211_local *local = sdata->local;
  142. static const u8 zero[ETH_ALEN] = { 0 };
  143. if (!changed)
  144. return;
  145. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  146. /*
  147. * While not associated, claim a BSSID of all-zeroes
  148. * so that drivers don't do any weird things with the
  149. * BSSID at that time.
  150. */
  151. if (sdata->vif.bss_conf.assoc)
  152. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  153. else
  154. sdata->vif.bss_conf.bssid = zero;
  155. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  156. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  157. else if (sdata->vif.type == NL80211_IFTYPE_AP)
  158. sdata->vif.bss_conf.bssid = sdata->dev->dev_addr;
  159. else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  160. sdata->vif.bss_conf.bssid = zero;
  161. } else {
  162. WARN_ON(1);
  163. return;
  164. }
  165. switch (sdata->vif.type) {
  166. case NL80211_IFTYPE_AP:
  167. case NL80211_IFTYPE_ADHOC:
  168. case NL80211_IFTYPE_MESH_POINT:
  169. break;
  170. default:
  171. /* do not warn to simplify caller in scan.c */
  172. changed &= ~BSS_CHANGED_BEACON_ENABLED;
  173. if (WARN_ON(changed & BSS_CHANGED_BEACON))
  174. return;
  175. break;
  176. }
  177. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  178. if (local->quiescing || !netif_running(sdata->dev) ||
  179. test_bit(SCAN_SW_SCANNING, &local->scanning)) {
  180. sdata->vif.bss_conf.enable_beacon = false;
  181. } else {
  182. /*
  183. * Beacon should be enabled, but AP mode must
  184. * check whether there is a beacon configured.
  185. */
  186. switch (sdata->vif.type) {
  187. case NL80211_IFTYPE_AP:
  188. sdata->vif.bss_conf.enable_beacon =
  189. !!rcu_dereference(sdata->u.ap.beacon);
  190. break;
  191. case NL80211_IFTYPE_ADHOC:
  192. sdata->vif.bss_conf.enable_beacon =
  193. !!rcu_dereference(sdata->u.ibss.presp);
  194. break;
  195. case NL80211_IFTYPE_MESH_POINT:
  196. sdata->vif.bss_conf.enable_beacon = true;
  197. break;
  198. default:
  199. /* not reached */
  200. WARN_ON(1);
  201. break;
  202. }
  203. }
  204. }
  205. drv_bss_info_changed(local, &sdata->vif,
  206. &sdata->vif.bss_conf, changed);
  207. /* DEPRECATED */
  208. local->hw.conf.beacon_int = sdata->vif.bss_conf.beacon_int;
  209. }
  210. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  211. {
  212. sdata->vif.bss_conf.use_cts_prot = false;
  213. sdata->vif.bss_conf.use_short_preamble = false;
  214. sdata->vif.bss_conf.use_short_slot = false;
  215. return BSS_CHANGED_ERP_CTS_PROT |
  216. BSS_CHANGED_ERP_PREAMBLE |
  217. BSS_CHANGED_ERP_SLOT;
  218. }
  219. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  220. struct sk_buff *skb)
  221. {
  222. struct ieee80211_local *local = hw_to_local(hw);
  223. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  224. int tmp;
  225. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  226. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  227. &local->skb_queue : &local->skb_queue_unreliable, skb);
  228. tmp = skb_queue_len(&local->skb_queue) +
  229. skb_queue_len(&local->skb_queue_unreliable);
  230. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  231. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  232. dev_kfree_skb_irq(skb);
  233. tmp--;
  234. I802_DEBUG_INC(local->tx_status_drop);
  235. }
  236. tasklet_schedule(&local->tasklet);
  237. }
  238. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  239. static void ieee80211_tasklet_handler(unsigned long data)
  240. {
  241. struct ieee80211_local *local = (struct ieee80211_local *) data;
  242. struct sk_buff *skb;
  243. struct ieee80211_ra_tid *ra_tid;
  244. while ((skb = skb_dequeue(&local->skb_queue)) ||
  245. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  246. switch (skb->pkt_type) {
  247. case IEEE80211_RX_MSG:
  248. /* Clear skb->pkt_type in order to not confuse kernel
  249. * netstack. */
  250. skb->pkt_type = 0;
  251. ieee80211_rx(local_to_hw(local), skb);
  252. break;
  253. case IEEE80211_TX_STATUS_MSG:
  254. skb->pkt_type = 0;
  255. ieee80211_tx_status(local_to_hw(local), skb);
  256. break;
  257. case IEEE80211_DELBA_MSG:
  258. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  259. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  260. ra_tid->ra, ra_tid->tid);
  261. dev_kfree_skb(skb);
  262. break;
  263. case IEEE80211_ADDBA_MSG:
  264. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  265. ieee80211_start_tx_ba_cb(local_to_hw(local),
  266. ra_tid->ra, ra_tid->tid);
  267. dev_kfree_skb(skb);
  268. break ;
  269. default:
  270. WARN(1, "mac80211: Packet is of unknown type %d\n",
  271. skb->pkt_type);
  272. dev_kfree_skb(skb);
  273. break;
  274. }
  275. }
  276. }
  277. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  278. struct sta_info *sta,
  279. struct sk_buff *skb)
  280. {
  281. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  282. /*
  283. * XXX: This is temporary!
  284. *
  285. * The problem here is that when we get here, the driver will
  286. * quite likely have pretty much overwritten info->control by
  287. * using info->driver_data or info->rate_driver_data. Thus,
  288. * when passing out the frame to the driver again, we would be
  289. * passing completely bogus data since the driver would then
  290. * expect a properly filled info->control. In mac80211 itself
  291. * the same problem occurs, since we need info->control.vif
  292. * internally.
  293. *
  294. * To fix this, we should send the frame through TX processing
  295. * again. However, it's not that simple, since the frame will
  296. * have been software-encrypted (if applicable) already, and
  297. * encrypting it again doesn't do much good. So to properly do
  298. * that, we not only have to skip the actual 'raw' encryption
  299. * (key selection etc. still has to be done!) but also the
  300. * sequence number assignment since that impacts the crypto
  301. * encapsulation, of course.
  302. *
  303. * Hence, for now, fix the bug by just dropping the frame.
  304. */
  305. goto drop;
  306. sta->tx_filtered_count++;
  307. /*
  308. * Clear the TX filter mask for this STA when sending the next
  309. * packet. If the STA went to power save mode, this will happen
  310. * when it wakes up for the next time.
  311. */
  312. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  313. /*
  314. * This code races in the following way:
  315. *
  316. * (1) STA sends frame indicating it will go to sleep and does so
  317. * (2) hardware/firmware adds STA to filter list, passes frame up
  318. * (3) hardware/firmware processes TX fifo and suppresses a frame
  319. * (4) we get TX status before having processed the frame and
  320. * knowing that the STA has gone to sleep.
  321. *
  322. * This is actually quite unlikely even when both those events are
  323. * processed from interrupts coming in quickly after one another or
  324. * even at the same time because we queue both TX status events and
  325. * RX frames to be processed by a tasklet and process them in the
  326. * same order that they were received or TX status last. Hence, there
  327. * is no race as long as the frame RX is processed before the next TX
  328. * status, which drivers can ensure, see below.
  329. *
  330. * Note that this can only happen if the hardware or firmware can
  331. * actually add STAs to the filter list, if this is done by the
  332. * driver in response to set_tim() (which will only reduce the race
  333. * this whole filtering tries to solve, not completely solve it)
  334. * this situation cannot happen.
  335. *
  336. * To completely solve this race drivers need to make sure that they
  337. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  338. * functions and
  339. * (b) always process RX events before TX status events if ordering
  340. * can be unknown, for example with different interrupt status
  341. * bits.
  342. */
  343. if (test_sta_flags(sta, WLAN_STA_PS) &&
  344. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  345. skb_queue_tail(&sta->tx_filtered, skb);
  346. return;
  347. }
  348. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  349. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  350. /* Software retry the packet once */
  351. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  352. ieee80211_add_pending_skb(local, skb);
  353. return;
  354. }
  355. drop:
  356. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  357. if (net_ratelimit())
  358. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  359. "queue_len=%d PS=%d @%lu\n",
  360. wiphy_name(local->hw.wiphy),
  361. skb_queue_len(&sta->tx_filtered),
  362. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  363. #endif
  364. dev_kfree_skb(skb);
  365. }
  366. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  367. {
  368. struct sk_buff *skb2;
  369. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  370. struct ieee80211_local *local = hw_to_local(hw);
  371. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  372. u16 frag, type;
  373. __le16 fc;
  374. struct ieee80211_supported_band *sband;
  375. struct ieee80211_tx_status_rtap_hdr *rthdr;
  376. struct ieee80211_sub_if_data *sdata;
  377. struct net_device *prev_dev = NULL;
  378. struct sta_info *sta;
  379. int retry_count = -1, i;
  380. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  381. /* the HW cannot have attempted that rate */
  382. if (i >= hw->max_rates) {
  383. info->status.rates[i].idx = -1;
  384. info->status.rates[i].count = 0;
  385. }
  386. retry_count += info->status.rates[i].count;
  387. }
  388. if (retry_count < 0)
  389. retry_count = 0;
  390. rcu_read_lock();
  391. sband = local->hw.wiphy->bands[info->band];
  392. sta = sta_info_get(local, hdr->addr1);
  393. if (sta) {
  394. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  395. test_sta_flags(sta, WLAN_STA_PS)) {
  396. /*
  397. * The STA is in power save mode, so assume
  398. * that this TX packet failed because of that.
  399. */
  400. ieee80211_handle_filtered_frame(local, sta, skb);
  401. rcu_read_unlock();
  402. return;
  403. }
  404. fc = hdr->frame_control;
  405. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  406. (ieee80211_is_data_qos(fc))) {
  407. u16 tid, ssn;
  408. u8 *qc;
  409. qc = ieee80211_get_qos_ctl(hdr);
  410. tid = qc[0] & 0xf;
  411. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  412. & IEEE80211_SCTL_SEQ);
  413. ieee80211_send_bar(sta->sdata, hdr->addr1,
  414. tid, ssn);
  415. }
  416. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  417. ieee80211_handle_filtered_frame(local, sta, skb);
  418. rcu_read_unlock();
  419. return;
  420. } else {
  421. if (!(info->flags & IEEE80211_TX_STAT_ACK))
  422. sta->tx_retry_failed++;
  423. sta->tx_retry_count += retry_count;
  424. }
  425. rate_control_tx_status(local, sband, sta, skb);
  426. }
  427. rcu_read_unlock();
  428. ieee80211_led_tx(local, 0);
  429. /* SNMP counters
  430. * Fragments are passed to low-level drivers as separate skbs, so these
  431. * are actually fragments, not frames. Update frame counters only for
  432. * the first fragment of the frame. */
  433. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  434. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  435. if (info->flags & IEEE80211_TX_STAT_ACK) {
  436. if (frag == 0) {
  437. local->dot11TransmittedFrameCount++;
  438. if (is_multicast_ether_addr(hdr->addr1))
  439. local->dot11MulticastTransmittedFrameCount++;
  440. if (retry_count > 0)
  441. local->dot11RetryCount++;
  442. if (retry_count > 1)
  443. local->dot11MultipleRetryCount++;
  444. }
  445. /* This counter shall be incremented for an acknowledged MPDU
  446. * with an individual address in the address 1 field or an MPDU
  447. * with a multicast address in the address 1 field of type Data
  448. * or Management. */
  449. if (!is_multicast_ether_addr(hdr->addr1) ||
  450. type == IEEE80211_FTYPE_DATA ||
  451. type == IEEE80211_FTYPE_MGMT)
  452. local->dot11TransmittedFragmentCount++;
  453. } else {
  454. if (frag == 0)
  455. local->dot11FailedCount++;
  456. }
  457. /* this was a transmitted frame, but now we want to reuse it */
  458. skb_orphan(skb);
  459. /*
  460. * This is a bit racy but we can avoid a lot of work
  461. * with this test...
  462. */
  463. if (!local->monitors && !local->cooked_mntrs) {
  464. dev_kfree_skb(skb);
  465. return;
  466. }
  467. /* send frame to monitor interfaces now */
  468. if (skb_headroom(skb) < sizeof(*rthdr)) {
  469. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  470. dev_kfree_skb(skb);
  471. return;
  472. }
  473. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  474. skb_push(skb, sizeof(*rthdr));
  475. memset(rthdr, 0, sizeof(*rthdr));
  476. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  477. rthdr->hdr.it_present =
  478. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  479. (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
  480. (1 << IEEE80211_RADIOTAP_RATE));
  481. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  482. !is_multicast_ether_addr(hdr->addr1))
  483. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  484. /*
  485. * XXX: Once radiotap gets the bitmap reset thing the vendor
  486. * extensions proposal contains, we can actually report
  487. * the whole set of tries we did.
  488. */
  489. if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  490. (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  491. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  492. else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  493. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  494. if (info->status.rates[0].idx >= 0 &&
  495. !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
  496. rthdr->rate = sband->bitrates[
  497. info->status.rates[0].idx].bitrate / 5;
  498. /* for now report the total retry_count */
  499. rthdr->data_retries = retry_count;
  500. /* XXX: is this sufficient for BPF? */
  501. skb_set_mac_header(skb, 0);
  502. skb->ip_summed = CHECKSUM_UNNECESSARY;
  503. skb->pkt_type = PACKET_OTHERHOST;
  504. skb->protocol = htons(ETH_P_802_2);
  505. memset(skb->cb, 0, sizeof(skb->cb));
  506. rcu_read_lock();
  507. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  508. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  509. if (!netif_running(sdata->dev))
  510. continue;
  511. if (prev_dev) {
  512. skb2 = skb_clone(skb, GFP_ATOMIC);
  513. if (skb2) {
  514. skb2->dev = prev_dev;
  515. netif_rx(skb2);
  516. }
  517. }
  518. prev_dev = sdata->dev;
  519. }
  520. }
  521. if (prev_dev) {
  522. skb->dev = prev_dev;
  523. netif_rx(skb);
  524. skb = NULL;
  525. }
  526. rcu_read_unlock();
  527. dev_kfree_skb(skb);
  528. }
  529. EXPORT_SYMBOL(ieee80211_tx_status);
  530. static void ieee80211_restart_work(struct work_struct *work)
  531. {
  532. struct ieee80211_local *local =
  533. container_of(work, struct ieee80211_local, restart_work);
  534. rtnl_lock();
  535. ieee80211_reconfig(local);
  536. rtnl_unlock();
  537. }
  538. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  539. {
  540. struct ieee80211_local *local = hw_to_local(hw);
  541. /* use this reason, __ieee80211_resume will unblock it */
  542. ieee80211_stop_queues_by_reason(hw,
  543. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  544. schedule_work(&local->restart_work);
  545. }
  546. EXPORT_SYMBOL(ieee80211_restart_hw);
  547. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  548. const struct ieee80211_ops *ops)
  549. {
  550. struct ieee80211_local *local;
  551. int priv_size, i;
  552. struct wiphy *wiphy;
  553. /* Ensure 32-byte alignment of our private data and hw private data.
  554. * We use the wiphy priv data for both our ieee80211_local and for
  555. * the driver's private data
  556. *
  557. * In memory it'll be like this:
  558. *
  559. * +-------------------------+
  560. * | struct wiphy |
  561. * +-------------------------+
  562. * | struct ieee80211_local |
  563. * +-------------------------+
  564. * | driver's private data |
  565. * +-------------------------+
  566. *
  567. */
  568. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  569. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  570. if (!wiphy)
  571. return NULL;
  572. wiphy->netnsok = true;
  573. wiphy->privid = mac80211_wiphy_privid;
  574. /* Yes, putting cfg80211_bss into ieee80211_bss is a hack */
  575. wiphy->bss_priv_size = sizeof(struct ieee80211_bss) -
  576. sizeof(struct cfg80211_bss);
  577. local = wiphy_priv(wiphy);
  578. local->hw.wiphy = wiphy;
  579. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  580. BUG_ON(!ops->tx);
  581. BUG_ON(!ops->start);
  582. BUG_ON(!ops->stop);
  583. BUG_ON(!ops->config);
  584. BUG_ON(!ops->add_interface);
  585. BUG_ON(!ops->remove_interface);
  586. BUG_ON(!ops->configure_filter);
  587. local->ops = ops;
  588. /* set up some defaults */
  589. local->hw.queues = 1;
  590. local->hw.max_rates = 1;
  591. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  592. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  593. local->hw.conf.radio_enabled = true;
  594. local->user_power_level = -1;
  595. INIT_LIST_HEAD(&local->interfaces);
  596. mutex_init(&local->iflist_mtx);
  597. mutex_init(&local->scan_mtx);
  598. spin_lock_init(&local->key_lock);
  599. spin_lock_init(&local->filter_lock);
  600. spin_lock_init(&local->queue_stop_reason_lock);
  601. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  602. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  603. INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
  604. INIT_WORK(&local->dynamic_ps_enable_work,
  605. ieee80211_dynamic_ps_enable_work);
  606. INIT_WORK(&local->dynamic_ps_disable_work,
  607. ieee80211_dynamic_ps_disable_work);
  608. setup_timer(&local->dynamic_ps_timer,
  609. ieee80211_dynamic_ps_timer, (unsigned long) local);
  610. sta_info_init(local);
  611. for (i = 0; i < IEEE80211_MAX_QUEUES; i++)
  612. skb_queue_head_init(&local->pending[i]);
  613. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  614. (unsigned long)local);
  615. tasklet_disable(&local->tx_pending_tasklet);
  616. tasklet_init(&local->tasklet,
  617. ieee80211_tasklet_handler,
  618. (unsigned long) local);
  619. tasklet_disable(&local->tasklet);
  620. skb_queue_head_init(&local->skb_queue);
  621. skb_queue_head_init(&local->skb_queue_unreliable);
  622. spin_lock_init(&local->ampdu_lock);
  623. return local_to_hw(local);
  624. }
  625. EXPORT_SYMBOL(ieee80211_alloc_hw);
  626. int ieee80211_register_hw(struct ieee80211_hw *hw)
  627. {
  628. struct ieee80211_local *local = hw_to_local(hw);
  629. int result;
  630. enum ieee80211_band band;
  631. int channels, i, j, max_bitrates;
  632. bool supp_ht;
  633. static const u32 cipher_suites[] = {
  634. WLAN_CIPHER_SUITE_WEP40,
  635. WLAN_CIPHER_SUITE_WEP104,
  636. WLAN_CIPHER_SUITE_TKIP,
  637. WLAN_CIPHER_SUITE_CCMP,
  638. /* keep last -- depends on hw flags! */
  639. WLAN_CIPHER_SUITE_AES_CMAC
  640. };
  641. /*
  642. * generic code guarantees at least one band,
  643. * set this very early because much code assumes
  644. * that hw.conf.channel is assigned
  645. */
  646. channels = 0;
  647. max_bitrates = 0;
  648. supp_ht = false;
  649. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  650. struct ieee80211_supported_band *sband;
  651. sband = local->hw.wiphy->bands[band];
  652. if (!sband)
  653. continue;
  654. if (!local->oper_channel) {
  655. /* init channel we're on */
  656. local->hw.conf.channel =
  657. local->oper_channel = &sband->channels[0];
  658. local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
  659. }
  660. channels += sband->n_channels;
  661. if (max_bitrates < sband->n_bitrates)
  662. max_bitrates = sband->n_bitrates;
  663. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  664. }
  665. local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
  666. sizeof(void *) * channels, GFP_KERNEL);
  667. if (!local->int_scan_req)
  668. return -ENOMEM;
  669. /* if low-level driver supports AP, we also support VLAN */
  670. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  671. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  672. /* mac80211 always supports monitor */
  673. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  674. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  675. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  676. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  677. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  678. /*
  679. * Calculate scan IE length -- we need this to alloc
  680. * memory and to subtract from the driver limit. It
  681. * includes the (extended) supported rates and HT
  682. * information -- SSID is the driver's responsibility.
  683. */
  684. local->scan_ies_len = 4 + max_bitrates; /* (ext) supp rates */
  685. if (supp_ht)
  686. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  687. if (!local->ops->hw_scan) {
  688. /* For hw_scan, driver needs to set these up. */
  689. local->hw.wiphy->max_scan_ssids = 4;
  690. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  691. }
  692. /*
  693. * If the driver supports any scan IEs, then assume the
  694. * limit includes the IEs mac80211 will add, otherwise
  695. * leave it at zero and let the driver sort it out; we
  696. * still pass our IEs to the driver but userspace will
  697. * not be allowed to in that case.
  698. */
  699. if (local->hw.wiphy->max_scan_ie_len)
  700. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  701. local->hw.wiphy->cipher_suites = cipher_suites;
  702. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  703. if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
  704. local->hw.wiphy->n_cipher_suites--;
  705. result = wiphy_register(local->hw.wiphy);
  706. if (result < 0)
  707. goto fail_wiphy_register;
  708. /*
  709. * We use the number of queues for feature tests (QoS, HT) internally
  710. * so restrict them appropriately.
  711. */
  712. if (hw->queues > IEEE80211_MAX_QUEUES)
  713. hw->queues = IEEE80211_MAX_QUEUES;
  714. local->workqueue =
  715. create_singlethread_workqueue(wiphy_name(local->hw.wiphy));
  716. if (!local->workqueue) {
  717. result = -ENOMEM;
  718. goto fail_workqueue;
  719. }
  720. /*
  721. * The hardware needs headroom for sending the frame,
  722. * and we need some headroom for passing the frame to monitor
  723. * interfaces, but never both at the same time.
  724. */
  725. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  726. sizeof(struct ieee80211_tx_status_rtap_hdr));
  727. debugfs_hw_add(local);
  728. if (local->hw.max_listen_interval == 0)
  729. local->hw.max_listen_interval = 1;
  730. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  731. result = sta_info_start(local);
  732. if (result < 0)
  733. goto fail_sta_info;
  734. result = ieee80211_wep_init(local);
  735. if (result < 0) {
  736. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  737. wiphy_name(local->hw.wiphy), result);
  738. goto fail_wep;
  739. }
  740. rtnl_lock();
  741. result = ieee80211_init_rate_ctrl_alg(local,
  742. hw->rate_control_algorithm);
  743. if (result < 0) {
  744. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  745. "algorithm\n", wiphy_name(local->hw.wiphy));
  746. goto fail_rate;
  747. }
  748. /* add one default STA interface if supported */
  749. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  750. result = ieee80211_if_add(local, "wlan%d", NULL,
  751. NL80211_IFTYPE_STATION, NULL);
  752. if (result)
  753. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  754. wiphy_name(local->hw.wiphy));
  755. }
  756. rtnl_unlock();
  757. ieee80211_led_init(local);
  758. /* alloc internal scan request */
  759. i = 0;
  760. local->int_scan_req->ssids = &local->scan_ssid;
  761. local->int_scan_req->n_ssids = 1;
  762. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  763. if (!hw->wiphy->bands[band])
  764. continue;
  765. for (j = 0; j < hw->wiphy->bands[band]->n_channels; j++) {
  766. local->int_scan_req->channels[i] =
  767. &hw->wiphy->bands[band]->channels[j];
  768. i++;
  769. }
  770. }
  771. local->network_latency_notifier.notifier_call =
  772. ieee80211_max_network_latency;
  773. result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
  774. &local->network_latency_notifier);
  775. if (result) {
  776. rtnl_lock();
  777. goto fail_pm_qos;
  778. }
  779. return 0;
  780. fail_pm_qos:
  781. ieee80211_led_exit(local);
  782. ieee80211_remove_interfaces(local);
  783. fail_rate:
  784. rtnl_unlock();
  785. ieee80211_wep_free(local);
  786. fail_wep:
  787. sta_info_stop(local);
  788. fail_sta_info:
  789. debugfs_hw_del(local);
  790. destroy_workqueue(local->workqueue);
  791. fail_workqueue:
  792. wiphy_unregister(local->hw.wiphy);
  793. fail_wiphy_register:
  794. kfree(local->int_scan_req->channels);
  795. return result;
  796. }
  797. EXPORT_SYMBOL(ieee80211_register_hw);
  798. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  799. {
  800. struct ieee80211_local *local = hw_to_local(hw);
  801. tasklet_kill(&local->tx_pending_tasklet);
  802. tasklet_kill(&local->tasklet);
  803. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  804. &local->network_latency_notifier);
  805. rtnl_lock();
  806. /*
  807. * At this point, interface list manipulations are fine
  808. * because the driver cannot be handing us frames any
  809. * more and the tasklet is killed.
  810. */
  811. ieee80211_remove_interfaces(local);
  812. rtnl_unlock();
  813. cancel_work_sync(&local->reconfig_filter);
  814. ieee80211_clear_tx_pending(local);
  815. sta_info_stop(local);
  816. rate_control_deinitialize(local);
  817. debugfs_hw_del(local);
  818. if (skb_queue_len(&local->skb_queue)
  819. || skb_queue_len(&local->skb_queue_unreliable))
  820. printk(KERN_WARNING "%s: skb_queue not empty\n",
  821. wiphy_name(local->hw.wiphy));
  822. skb_queue_purge(&local->skb_queue);
  823. skb_queue_purge(&local->skb_queue_unreliable);
  824. destroy_workqueue(local->workqueue);
  825. wiphy_unregister(local->hw.wiphy);
  826. ieee80211_wep_free(local);
  827. ieee80211_led_exit(local);
  828. kfree(local->int_scan_req);
  829. }
  830. EXPORT_SYMBOL(ieee80211_unregister_hw);
  831. void ieee80211_free_hw(struct ieee80211_hw *hw)
  832. {
  833. struct ieee80211_local *local = hw_to_local(hw);
  834. mutex_destroy(&local->iflist_mtx);
  835. mutex_destroy(&local->scan_mtx);
  836. wiphy_free(local->hw.wiphy);
  837. }
  838. EXPORT_SYMBOL(ieee80211_free_hw);
  839. static int __init ieee80211_init(void)
  840. {
  841. struct sk_buff *skb;
  842. int ret;
  843. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  844. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  845. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  846. ret = rc80211_minstrel_init();
  847. if (ret)
  848. return ret;
  849. ret = rc80211_pid_init();
  850. if (ret)
  851. return ret;
  852. ieee80211_debugfs_netdev_init();
  853. return 0;
  854. }
  855. static void __exit ieee80211_exit(void)
  856. {
  857. rc80211_pid_exit();
  858. rc80211_minstrel_exit();
  859. /*
  860. * For key todo, it'll be empty by now but the work
  861. * might still be scheduled.
  862. */
  863. flush_scheduled_work();
  864. if (mesh_allocated)
  865. ieee80211s_stop();
  866. ieee80211_debugfs_netdev_exit();
  867. }
  868. subsys_initcall(ieee80211_init);
  869. module_exit(ieee80211_exit);
  870. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  871. MODULE_LICENSE("GPL");