status.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. /*
  66. * This skb 'survived' a round-trip through the driver, and
  67. * hopefully the driver didn't mangle it too badly. However,
  68. * we can definitely not rely on the the control information
  69. * being correct. Clear it so we don't get junk there.
  70. */
  71. memset(&info->control, 0, sizeof(info->control));
  72. sta->tx_filtered_count++;
  73. /*
  74. * Clear the TX filter mask for this STA when sending the next
  75. * packet. If the STA went to power save mode, this will happen
  76. * when it wakes up for the next time.
  77. */
  78. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  79. /*
  80. * This code races in the following way:
  81. *
  82. * (1) STA sends frame indicating it will go to sleep and does so
  83. * (2) hardware/firmware adds STA to filter list, passes frame up
  84. * (3) hardware/firmware processes TX fifo and suppresses a frame
  85. * (4) we get TX status before having processed the frame and
  86. * knowing that the STA has gone to sleep.
  87. *
  88. * This is actually quite unlikely even when both those events are
  89. * processed from interrupts coming in quickly after one another or
  90. * even at the same time because we queue both TX status events and
  91. * RX frames to be processed by a tasklet and process them in the
  92. * same order that they were received or TX status last. Hence, there
  93. * is no race as long as the frame RX is processed before the next TX
  94. * status, which drivers can ensure, see below.
  95. *
  96. * Note that this can only happen if the hardware or firmware can
  97. * actually add STAs to the filter list, if this is done by the
  98. * driver in response to set_tim() (which will only reduce the race
  99. * this whole filtering tries to solve, not completely solve it)
  100. * this situation cannot happen.
  101. *
  102. * To completely solve this race drivers need to make sure that they
  103. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  104. * functions and
  105. * (b) always process RX events before TX status events if ordering
  106. * can be unknown, for example with different interrupt status
  107. * bits.
  108. */
  109. if (test_sta_flags(sta, WLAN_STA_PS_STA) &&
  110. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  111. skb_queue_tail(&sta->tx_filtered, skb);
  112. return;
  113. }
  114. if (!test_sta_flags(sta, WLAN_STA_PS_STA) &&
  115. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  116. /* Software retry the packet once */
  117. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  118. ieee80211_add_pending_skb(local, skb);
  119. return;
  120. }
  121. drop:
  122. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  123. if (net_ratelimit())
  124. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  125. "queue_len=%d PS=%d @%lu\n",
  126. wiphy_name(local->hw.wiphy),
  127. skb_queue_len(&sta->tx_filtered),
  128. !!test_sta_flags(sta, WLAN_STA_PS_STA), jiffies);
  129. #endif
  130. dev_kfree_skb(skb);
  131. }
  132. static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
  133. {
  134. struct ieee80211_mgmt *mgmt = (void *) skb->data;
  135. struct ieee80211_local *local = sta->local;
  136. struct ieee80211_sub_if_data *sdata = sta->sdata;
  137. if (ieee80211_is_action(mgmt->frame_control) &&
  138. sdata->vif.type == NL80211_IFTYPE_STATION &&
  139. mgmt->u.action.category == WLAN_CATEGORY_HT &&
  140. mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS) {
  141. /*
  142. * This update looks racy, but isn't -- if we come
  143. * here we've definitely got a station that we're
  144. * talking to, and on a managed interface that can
  145. * only be the AP. And the only other place updating
  146. * this variable is before we're associated.
  147. */
  148. switch (mgmt->u.action.u.ht_smps.smps_control) {
  149. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  150. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_DYNAMIC;
  151. break;
  152. case WLAN_HT_SMPS_CONTROL_STATIC:
  153. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_STATIC;
  154. break;
  155. case WLAN_HT_SMPS_CONTROL_DISABLED:
  156. default: /* shouldn't happen since we don't send that */
  157. sta->sdata->u.mgd.ap_smps = IEEE80211_SMPS_OFF;
  158. break;
  159. }
  160. ieee80211_queue_work(&local->hw, &local->recalc_smps);
  161. }
  162. }
  163. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  164. {
  165. struct sk_buff *skb2;
  166. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  167. struct ieee80211_local *local = hw_to_local(hw);
  168. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  169. u16 frag, type;
  170. __le16 fc;
  171. struct ieee80211_supported_band *sband;
  172. struct ieee80211_tx_status_rtap_hdr *rthdr;
  173. struct ieee80211_sub_if_data *sdata;
  174. struct net_device *prev_dev = NULL;
  175. struct sta_info *sta, *tmp;
  176. int retry_count = -1, i;
  177. bool injected;
  178. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  179. /* the HW cannot have attempted that rate */
  180. if (i >= hw->max_rates) {
  181. info->status.rates[i].idx = -1;
  182. info->status.rates[i].count = 0;
  183. }
  184. retry_count += info->status.rates[i].count;
  185. }
  186. if (retry_count < 0)
  187. retry_count = 0;
  188. rcu_read_lock();
  189. sband = local->hw.wiphy->bands[info->band];
  190. for_each_sta_info(local, hdr->addr1, sta, tmp) {
  191. /* skip wrong virtual interface */
  192. if (memcmp(hdr->addr2, sta->sdata->vif.addr, ETH_ALEN))
  193. continue;
  194. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  195. test_sta_flags(sta, WLAN_STA_PS_STA)) {
  196. /*
  197. * The STA is in power save mode, so assume
  198. * that this TX packet failed because of that.
  199. */
  200. ieee80211_handle_filtered_frame(local, sta, skb);
  201. rcu_read_unlock();
  202. return;
  203. }
  204. fc = hdr->frame_control;
  205. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  206. (ieee80211_is_data_qos(fc))) {
  207. u16 tid, ssn;
  208. u8 *qc;
  209. qc = ieee80211_get_qos_ctl(hdr);
  210. tid = qc[0] & 0xf;
  211. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  212. & IEEE80211_SCTL_SEQ);
  213. ieee80211_send_bar(sta->sdata, hdr->addr1,
  214. tid, ssn);
  215. }
  216. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  217. ieee80211_handle_filtered_frame(local, sta, skb);
  218. rcu_read_unlock();
  219. return;
  220. } else {
  221. if (!(info->flags & IEEE80211_TX_STAT_ACK))
  222. sta->tx_retry_failed++;
  223. sta->tx_retry_count += retry_count;
  224. }
  225. rate_control_tx_status(local, sband, sta, skb);
  226. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  227. ieee80211s_update_metric(local, sta, skb);
  228. if (!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  229. (info->flags & IEEE80211_TX_STAT_ACK))
  230. ieee80211_frame_acked(sta, skb);
  231. }
  232. rcu_read_unlock();
  233. ieee80211_led_tx(local, 0);
  234. /* SNMP counters
  235. * Fragments are passed to low-level drivers as separate skbs, so these
  236. * are actually fragments, not frames. Update frame counters only for
  237. * the first fragment of the frame. */
  238. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  239. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  240. if (info->flags & IEEE80211_TX_STAT_ACK) {
  241. if (frag == 0) {
  242. local->dot11TransmittedFrameCount++;
  243. if (is_multicast_ether_addr(hdr->addr1))
  244. local->dot11MulticastTransmittedFrameCount++;
  245. if (retry_count > 0)
  246. local->dot11RetryCount++;
  247. if (retry_count > 1)
  248. local->dot11MultipleRetryCount++;
  249. }
  250. /* This counter shall be incremented for an acknowledged MPDU
  251. * with an individual address in the address 1 field or an MPDU
  252. * with a multicast address in the address 1 field of type Data
  253. * or Management. */
  254. if (!is_multicast_ether_addr(hdr->addr1) ||
  255. type == IEEE80211_FTYPE_DATA ||
  256. type == IEEE80211_FTYPE_MGMT)
  257. local->dot11TransmittedFragmentCount++;
  258. } else {
  259. if (frag == 0)
  260. local->dot11FailedCount++;
  261. }
  262. /* this was a transmitted frame, but now we want to reuse it */
  263. skb_orphan(skb);
  264. /*
  265. * This is a bit racy but we can avoid a lot of work
  266. * with this test...
  267. */
  268. if (!local->monitors && !local->cooked_mntrs) {
  269. dev_kfree_skb(skb);
  270. return;
  271. }
  272. /* send frame to monitor interfaces now */
  273. if (skb_headroom(skb) < sizeof(*rthdr)) {
  274. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  275. dev_kfree_skb(skb);
  276. return;
  277. }
  278. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  279. skb_push(skb, sizeof(*rthdr));
  280. memset(rthdr, 0, sizeof(*rthdr));
  281. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  282. rthdr->hdr.it_present =
  283. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  284. (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
  285. (1 << IEEE80211_RADIOTAP_RATE));
  286. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  287. !is_multicast_ether_addr(hdr->addr1))
  288. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  289. /*
  290. * XXX: Once radiotap gets the bitmap reset thing the vendor
  291. * extensions proposal contains, we can actually report
  292. * the whole set of tries we did.
  293. */
  294. if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  295. (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  296. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  297. else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  298. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  299. if (info->status.rates[0].idx >= 0 &&
  300. !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
  301. rthdr->rate = sband->bitrates[
  302. info->status.rates[0].idx].bitrate / 5;
  303. /* for now report the total retry_count */
  304. rthdr->data_retries = retry_count;
  305. /* Need to make a copy before skb->cb gets cleared */
  306. injected = !!(info->flags & IEEE80211_TX_CTL_INJECTED);
  307. /* XXX: is this sufficient for BPF? */
  308. skb_set_mac_header(skb, 0);
  309. skb->ip_summed = CHECKSUM_UNNECESSARY;
  310. skb->pkt_type = PACKET_OTHERHOST;
  311. skb->protocol = htons(ETH_P_802_2);
  312. memset(skb->cb, 0, sizeof(skb->cb));
  313. rcu_read_lock();
  314. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  315. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  316. if (!ieee80211_sdata_running(sdata))
  317. continue;
  318. if ((sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) &&
  319. !injected &&
  320. (type == IEEE80211_FTYPE_DATA))
  321. continue;
  322. if (prev_dev) {
  323. skb2 = skb_clone(skb, GFP_ATOMIC);
  324. if (skb2) {
  325. skb2->dev = prev_dev;
  326. netif_rx(skb2);
  327. }
  328. }
  329. prev_dev = sdata->dev;
  330. }
  331. }
  332. if (prev_dev) {
  333. skb->dev = prev_dev;
  334. netif_rx(skb);
  335. skb = NULL;
  336. }
  337. rcu_read_unlock();
  338. dev_kfree_skb(skb);
  339. }
  340. EXPORT_SYMBOL(ieee80211_tx_status);