status.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
  126. {
  127. struct ieee80211_mgmt *mgmt = (void *) skb->data;
  128. struct ieee80211_local *local = sta->local;
  129. struct ieee80211_sub_if_data *sdata = sta->sdata;
  130. if (ieee80211_is_action(mgmt->frame_control) &&
  131. sdata->vif.type == NL80211_IFTYPE_STATION &&
  132. mgmt->u.action.category == WLAN_CATEGORY_HT &&
  133. mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS) {
  134. /*
  135. * This update looks racy, but isn't -- if we come
  136. * here we've definitely got a station that we're
  137. * talking to, and on a managed interface that can
  138. * only be the AP. And the only other place updating
  139. * this variable is before we're associated.
  140. */
  141. switch (mgmt->u.action.u.ht_smps.smps_control) {
  142. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  143. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_DYNAMIC;
  144. break;
  145. case WLAN_HT_SMPS_CONTROL_STATIC:
  146. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_STATIC;
  147. break;
  148. case WLAN_HT_SMPS_CONTROL_DISABLED:
  149. default: /* shouldn't happen since we don't send that */
  150. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_OFF;
  151. break;
  152. }
  153. ieee80211_queue_work(&local->hw, &local->recalc_smps);
  154. }
  155. }
  156. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  157. {
  158. struct sk_buff *skb2;
  159. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  160. struct ieee80211_local *local = hw_to_local(hw);
  161. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  162. u16 frag, type;
  163. __le16 fc;
  164. struct ieee80211_supported_band *sband;
  165. struct ieee80211_tx_status_rtap_hdr *rthdr;
  166. struct ieee80211_sub_if_data *sdata;
  167. struct net_device *prev_dev = NULL;
  168. struct sta_info *sta, *tmp;
  169. int retry_count = -1, i;
  170. bool injected;
  171. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  172. /* the HW cannot have attempted that rate */
  173. if (i >= hw->max_rates) {
  174. info->status.rates[i].idx = -1;
  175. info->status.rates[i].count = 0;
  176. }
  177. retry_count += info->status.rates[i].count;
  178. }
  179. if (retry_count < 0)
  180. retry_count = 0;
  181. rcu_read_lock();
  182. sband = local->hw.wiphy->bands[info->band];
  183. for_each_sta_info(local, hdr->addr1, sta, tmp) {
  184. /* skip wrong virtual interface */
  185. if (memcmp(hdr->addr2, sta->sdata->vif.addr, ETH_ALEN))
  186. continue;
  187. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  188. test_sta_flags(sta, WLAN_STA_PS_STA)) {
  189. /*
  190. * The STA is in power save mode, so assume
  191. * that this TX packet failed because of that.
  192. */
  193. ieee80211_handle_filtered_frame(local, sta, skb);
  194. rcu_read_unlock();
  195. return;
  196. }
  197. fc = hdr->frame_control;
  198. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  199. (ieee80211_is_data_qos(fc))) {
  200. u16 tid, ssn;
  201. u8 *qc;
  202. qc = ieee80211_get_qos_ctl(hdr);
  203. tid = qc[0] & 0xf;
  204. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  205. & IEEE80211_SCTL_SEQ);
  206. ieee80211_send_bar(sta->sdata, hdr->addr1,
  207. tid, ssn);
  208. }
  209. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  210. ieee80211_handle_filtered_frame(local, sta, skb);
  211. rcu_read_unlock();
  212. return;
  213. } else {
  214. if (!(info->flags & IEEE80211_TX_STAT_ACK))
  215. sta->tx_retry_failed++;
  216. sta->tx_retry_count += retry_count;
  217. }
  218. rate_control_tx_status(local, sband, sta, skb);
  219. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  220. ieee80211s_update_metric(local, sta, skb);
  221. if (!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  222. (info->flags & IEEE80211_TX_STAT_ACK))
  223. ieee80211_frame_acked(sta, skb);
  224. }
  225. rcu_read_unlock();
  226. ieee80211_led_tx(local, 0);
  227. /* SNMP counters
  228. * Fragments are passed to low-level drivers as separate skbs, so these
  229. * are actually fragments, not frames. Update frame counters only for
  230. * the first fragment of the frame. */
  231. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  232. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  233. if (info->flags & IEEE80211_TX_STAT_ACK) {
  234. if (frag == 0) {
  235. local->dot11TransmittedFrameCount++;
  236. if (is_multicast_ether_addr(hdr->addr1))
  237. local->dot11MulticastTransmittedFrameCount++;
  238. if (retry_count > 0)
  239. local->dot11RetryCount++;
  240. if (retry_count > 1)
  241. local->dot11MultipleRetryCount++;
  242. }
  243. /* This counter shall be incremented for an acknowledged MPDU
  244. * with an individual address in the address 1 field or an MPDU
  245. * with a multicast address in the address 1 field of type Data
  246. * or Management. */
  247. if (!is_multicast_ether_addr(hdr->addr1) ||
  248. type == IEEE80211_FTYPE_DATA ||
  249. type == IEEE80211_FTYPE_MGMT)
  250. local->dot11TransmittedFragmentCount++;
  251. } else {
  252. if (frag == 0)
  253. local->dot11FailedCount++;
  254. }
  255. /* this was a transmitted frame, but now we want to reuse it */
  256. skb_orphan(skb);
  257. /*
  258. * This is a bit racy but we can avoid a lot of work
  259. * with this test...
  260. */
  261. if (!local->monitors && !local->cooked_mntrs) {
  262. dev_kfree_skb(skb);
  263. return;
  264. }
  265. /* send frame to monitor interfaces now */
  266. if (skb_headroom(skb) < sizeof(*rthdr)) {
  267. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  268. dev_kfree_skb(skb);
  269. return;
  270. }
  271. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  272. skb_push(skb, sizeof(*rthdr));
  273. memset(rthdr, 0, sizeof(*rthdr));
  274. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  275. rthdr->hdr.it_present =
  276. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  277. (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
  278. (1 << IEEE80211_RADIOTAP_RATE));
  279. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  280. !is_multicast_ether_addr(hdr->addr1))
  281. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  282. /*
  283. * XXX: Once radiotap gets the bitmap reset thing the vendor
  284. * extensions proposal contains, we can actually report
  285. * the whole set of tries we did.
  286. */
  287. if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  288. (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  289. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  290. else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  291. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  292. if (info->status.rates[0].idx >= 0 &&
  293. !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
  294. rthdr->rate = sband->bitrates[
  295. info->status.rates[0].idx].bitrate / 5;
  296. /* for now report the total retry_count */
  297. rthdr->data_retries = retry_count;
  298. /* Need to make a copy before skb->cb gets cleared */
  299. injected = !!(info->flags & IEEE80211_TX_CTL_INJECTED);
  300. /* XXX: is this sufficient for BPF? */
  301. skb_set_mac_header(skb, 0);
  302. skb->ip_summed = CHECKSUM_UNNECESSARY;
  303. skb->pkt_type = PACKET_OTHERHOST;
  304. skb->protocol = htons(ETH_P_802_2);
  305. memset(skb->cb, 0, sizeof(skb->cb));
  306. rcu_read_lock();
  307. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  308. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  309. if (!netif_running(sdata->dev))
  310. continue;
  311. if ((sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) &&
  312. !injected &&
  313. (type == IEEE80211_FTYPE_DATA))
  314. continue;
  315. if (prev_dev) {
  316. skb2 = skb_clone(skb, GFP_ATOMIC);
  317. if (skb2) {
  318. skb2->dev = prev_dev;
  319. netif_rx(skb2);
  320. }
  321. }
  322. prev_dev = sdata->dev;
  323. }
  324. }
  325. if (prev_dev) {
  326. skb->dev = prev_dev;
  327. netif_rx(skb);
  328. skb = NULL;
  329. }
  330. rcu_read_unlock();
  331. dev_kfree_skb(skb);
  332. }
  333. EXPORT_SYMBOL(ieee80211_tx_status);