status.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * Copyright 2008-2009 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <net/mac80211.h>
  12. #include "ieee80211_i.h"
  13. #include "rate.h"
  14. #include "mesh.h"
  15. #include "led.h"
  16. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  17. struct sk_buff *skb)
  18. {
  19. struct ieee80211_local *local = hw_to_local(hw);
  20. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  21. int tmp;
  22. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  23. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  24. &local->skb_queue : &local->skb_queue_unreliable, skb);
  25. tmp = skb_queue_len(&local->skb_queue) +
  26. skb_queue_len(&local->skb_queue_unreliable);
  27. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  28. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  29. dev_kfree_skb_irq(skb);
  30. tmp--;
  31. I802_DEBUG_INC(local->tx_status_drop);
  32. }
  33. tasklet_schedule(&local->tasklet);
  34. }
  35. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  36. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  37. struct sta_info *sta,
  38. struct sk_buff *skb)
  39. {
  40. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  41. /*
  42. * XXX: This is temporary!
  43. *
  44. * The problem here is that when we get here, the driver will
  45. * quite likely have pretty much overwritten info->control by
  46. * using info->driver_data or info->rate_driver_data. Thus,
  47. * when passing out the frame to the driver again, we would be
  48. * passing completely bogus data since the driver would then
  49. * expect a properly filled info->control. In mac80211 itself
  50. * the same problem occurs, since we need info->control.vif
  51. * internally.
  52. *
  53. * To fix this, we should send the frame through TX processing
  54. * again. However, it's not that simple, since the frame will
  55. * have been software-encrypted (if applicable) already, and
  56. * encrypting it again doesn't do much good. So to properly do
  57. * that, we not only have to skip the actual 'raw' encryption
  58. * (key selection etc. still has to be done!) but also the
  59. * sequence number assignment since that impacts the crypto
  60. * encapsulation, of course.
  61. *
  62. * Hence, for now, fix the bug by just dropping the frame.
  63. */
  64. goto drop;
  65. sta->tx_filtered_count++;
  66. /*
  67. * Clear the TX filter mask for this STA when sending the next
  68. * packet. If the STA went to power save mode, this will happen
  69. * when it wakes up for the next time.
  70. */
  71. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  72. /*
  73. * This code races in the following way:
  74. *
  75. * (1) STA sends frame indicating it will go to sleep and does so
  76. * (2) hardware/firmware adds STA to filter list, passes frame up
  77. * (3) hardware/firmware processes TX fifo and suppresses a frame
  78. * (4) we get TX status before having processed the frame and
  79. * knowing that the STA has gone to sleep.
  80. *
  81. * This is actually quite unlikely even when both those events are
  82. * processed from interrupts coming in quickly after one another or
  83. * even at the same time because we queue both TX status events and
  84. * RX frames to be processed by a tasklet and process them in the
  85. * same order that they were received or TX status last. Hence, there
  86. * is no race as long as the frame RX is processed before the next TX
  87. * status, which drivers can ensure, see below.
  88. *
  89. * Note that this can only happen if the hardware or firmware can
  90. * actually add STAs to the filter list, if this is done by the
  91. * driver in response to set_tim() (which will only reduce the race
  92. * this whole filtering tries to solve, not completely solve it)
  93. * this situation cannot happen.
  94. *
  95. * To completely solve this race drivers need to make sure that they
  96. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  97. * functions and
  98. * (b) always process RX events before TX status events if ordering
  99. * can be unknown, for example with different interrupt status
  100. * bits.
  101. */
  102. if (test_sta_flags(sta, WLAN_STA_PS_STA) &&
  103. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  104. skb_queue_tail(&sta->tx_filtered, skb);
  105. return;
  106. }
  107. if (!test_sta_flags(sta, WLAN_STA_PS_STA) &&
  108. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  109. /* Software retry the packet once */
  110. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  111. ieee80211_add_pending_skb(local, skb);
  112. return;
  113. }
  114. drop:
  115. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  116. if (net_ratelimit())
  117. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  118. "queue_len=%d PS=%d @%lu\n",
  119. wiphy_name(local->hw.wiphy),
  120. skb_queue_len(&sta->tx_filtered),
  121. !!test_sta_flags(sta, WLAN_STA_PS_STA), jiffies);
  122. #endif
  123. dev_kfree_skb(skb);
  124. }
  125. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  126. {
  127. struct sk_buff *skb2;
  128. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  129. struct ieee80211_local *local = hw_to_local(hw);
  130. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  131. u16 frag, type;
  132. __le16 fc;
  133. struct ieee80211_supported_band *sband;
  134. struct ieee80211_tx_status_rtap_hdr *rthdr;
  135. struct ieee80211_sub_if_data *sdata;
  136. struct net_device *prev_dev = NULL;
  137. struct sta_info *sta;
  138. int retry_count = -1, i;
  139. bool injected;
  140. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  141. /* the HW cannot have attempted that rate */
  142. if (i >= hw->max_rates) {
  143. info->status.rates[i].idx = -1;
  144. info->status.rates[i].count = 0;
  145. }
  146. retry_count += info->status.rates[i].count;
  147. }
  148. if (retry_count < 0)
  149. retry_count = 0;
  150. rcu_read_lock();
  151. sband = local->hw.wiphy->bands[info->band];
  152. sta = sta_info_get(local, hdr->addr1);
  153. if (sta) {
  154. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  155. test_sta_flags(sta, WLAN_STA_PS_STA)) {
  156. /*
  157. * The STA is in power save mode, so assume
  158. * that this TX packet failed because of that.
  159. */
  160. ieee80211_handle_filtered_frame(local, sta, skb);
  161. rcu_read_unlock();
  162. return;
  163. }
  164. fc = hdr->frame_control;
  165. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  166. (ieee80211_is_data_qos(fc))) {
  167. u16 tid, ssn;
  168. u8 *qc;
  169. qc = ieee80211_get_qos_ctl(hdr);
  170. tid = qc[0] & 0xf;
  171. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  172. & IEEE80211_SCTL_SEQ);
  173. ieee80211_send_bar(sta->sdata, hdr->addr1,
  174. tid, ssn);
  175. }
  176. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  177. ieee80211_handle_filtered_frame(local, sta, skb);
  178. rcu_read_unlock();
  179. return;
  180. } else {
  181. if (!(info->flags & IEEE80211_TX_STAT_ACK))
  182. sta->tx_retry_failed++;
  183. sta->tx_retry_count += retry_count;
  184. }
  185. rate_control_tx_status(local, sband, sta, skb);
  186. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  187. ieee80211s_update_metric(local, sta, skb);
  188. }
  189. rcu_read_unlock();
  190. ieee80211_led_tx(local, 0);
  191. /* SNMP counters
  192. * Fragments are passed to low-level drivers as separate skbs, so these
  193. * are actually fragments, not frames. Update frame counters only for
  194. * the first fragment of the frame. */
  195. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  196. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  197. if (info->flags & IEEE80211_TX_STAT_ACK) {
  198. if (frag == 0) {
  199. local->dot11TransmittedFrameCount++;
  200. if (is_multicast_ether_addr(hdr->addr1))
  201. local->dot11MulticastTransmittedFrameCount++;
  202. if (retry_count > 0)
  203. local->dot11RetryCount++;
  204. if (retry_count > 1)
  205. local->dot11MultipleRetryCount++;
  206. }
  207. /* This counter shall be incremented for an acknowledged MPDU
  208. * with an individual address in the address 1 field or an MPDU
  209. * with a multicast address in the address 1 field of type Data
  210. * or Management. */
  211. if (!is_multicast_ether_addr(hdr->addr1) ||
  212. type == IEEE80211_FTYPE_DATA ||
  213. type == IEEE80211_FTYPE_MGMT)
  214. local->dot11TransmittedFragmentCount++;
  215. } else {
  216. if (frag == 0)
  217. local->dot11FailedCount++;
  218. }
  219. /* this was a transmitted frame, but now we want to reuse it */
  220. skb_orphan(skb);
  221. /*
  222. * This is a bit racy but we can avoid a lot of work
  223. * with this test...
  224. */
  225. if (!local->monitors && !local->cooked_mntrs) {
  226. dev_kfree_skb(skb);
  227. return;
  228. }
  229. /* send frame to monitor interfaces now */
  230. if (skb_headroom(skb) < sizeof(*rthdr)) {
  231. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  232. dev_kfree_skb(skb);
  233. return;
  234. }
  235. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  236. skb_push(skb, sizeof(*rthdr));
  237. memset(rthdr, 0, sizeof(*rthdr));
  238. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  239. rthdr->hdr.it_present =
  240. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  241. (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
  242. (1 << IEEE80211_RADIOTAP_RATE));
  243. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  244. !is_multicast_ether_addr(hdr->addr1))
  245. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  246. /*
  247. * XXX: Once radiotap gets the bitmap reset thing the vendor
  248. * extensions proposal contains, we can actually report
  249. * the whole set of tries we did.
  250. */
  251. if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  252. (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  253. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  254. else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  255. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  256. if (info->status.rates[0].idx >= 0 &&
  257. !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
  258. rthdr->rate = sband->bitrates[
  259. info->status.rates[0].idx].bitrate / 5;
  260. /* for now report the total retry_count */
  261. rthdr->data_retries = retry_count;
  262. /* Need to make a copy before skb->cb gets cleared */
  263. injected = !!(info->flags & IEEE80211_TX_CTL_INJECTED);
  264. /* XXX: is this sufficient for BPF? */
  265. skb_set_mac_header(skb, 0);
  266. skb->ip_summed = CHECKSUM_UNNECESSARY;
  267. skb->pkt_type = PACKET_OTHERHOST;
  268. skb->protocol = htons(ETH_P_802_2);
  269. memset(skb->cb, 0, sizeof(skb->cb));
  270. rcu_read_lock();
  271. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  272. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  273. if (!netif_running(sdata->dev))
  274. continue;
  275. if ((sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) &&
  276. !injected &&
  277. (type == IEEE80211_FTYPE_DATA))
  278. continue;
  279. if (prev_dev) {
  280. skb2 = skb_clone(skb, GFP_ATOMIC);
  281. if (skb2) {
  282. skb2->dev = prev_dev;
  283. netif_rx(skb2);
  284. }
  285. }
  286. prev_dev = sdata->dev;
  287. }
  288. }
  289. if (prev_dev) {
  290. skb->dev = prev_dev;
  291. netif_rx(skb);
  292. skb = NULL;
  293. }
  294. rcu_read_unlock();
  295. dev_kfree_skb(skb);
  296. }
  297. EXPORT_SYMBOL(ieee80211_tx_status);