status.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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-2010 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/export.h>
  13. #include <linux/etherdevice.h>
  14. #include <net/mac80211.h>
  15. #include <asm/unaligned.h>
  16. #include "ieee80211_i.h"
  17. #include "rate.h"
  18. #include "mesh.h"
  19. #include "led.h"
  20. #include "wme.h"
  21. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  22. struct sk_buff *skb)
  23. {
  24. struct ieee80211_local *local = hw_to_local(hw);
  25. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  26. int tmp;
  27. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  28. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  29. &local->skb_queue : &local->skb_queue_unreliable, skb);
  30. tmp = skb_queue_len(&local->skb_queue) +
  31. skb_queue_len(&local->skb_queue_unreliable);
  32. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  33. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  34. ieee80211_free_txskb(hw, skb);
  35. tmp--;
  36. I802_DEBUG_INC(local->tx_status_drop);
  37. }
  38. tasklet_schedule(&local->tasklet);
  39. }
  40. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  41. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  42. struct sta_info *sta,
  43. struct sk_buff *skb)
  44. {
  45. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  46. struct ieee80211_hdr *hdr = (void *)skb->data;
  47. int ac;
  48. if (info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
  49. IEEE80211_TX_CTL_AMPDU)) {
  50. ieee80211_free_txskb(&local->hw, skb);
  51. return;
  52. }
  53. /*
  54. * This skb 'survived' a round-trip through the driver, and
  55. * hopefully the driver didn't mangle it too badly. However,
  56. * we can definitely not rely on the control information
  57. * being correct. Clear it so we don't get junk there, and
  58. * indicate that it needs new processing, but must not be
  59. * modified/encrypted again.
  60. */
  61. memset(&info->control, 0, sizeof(info->control));
  62. info->control.jiffies = jiffies;
  63. info->control.vif = &sta->sdata->vif;
  64. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING |
  65. IEEE80211_TX_INTFL_RETRANSMISSION;
  66. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  67. sta->status_stats.filtered++;
  68. /*
  69. * Clear more-data bit on filtered frames, it might be set
  70. * but later frames might time out so it might have to be
  71. * clear again ... It's all rather unlikely (this frame
  72. * should time out first, right?) but let's not confuse
  73. * peers unnecessarily.
  74. */
  75. if (hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_MOREDATA))
  76. hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  77. if (ieee80211_is_data_qos(hdr->frame_control)) {
  78. u8 *p = ieee80211_get_qos_ctl(hdr);
  79. int tid = *p & IEEE80211_QOS_CTL_TID_MASK;
  80. /*
  81. * Clear EOSP if set, this could happen e.g.
  82. * if an absence period (us being a P2P GO)
  83. * shortens the SP.
  84. */
  85. if (*p & IEEE80211_QOS_CTL_EOSP)
  86. *p &= ~IEEE80211_QOS_CTL_EOSP;
  87. ac = ieee80211_ac_from_tid(tid);
  88. } else {
  89. ac = IEEE80211_AC_BE;
  90. }
  91. /*
  92. * Clear the TX filter mask for this STA when sending the next
  93. * packet. If the STA went to power save mode, this will happen
  94. * when it wakes up for the next time.
  95. */
  96. set_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT);
  97. ieee80211_clear_fast_xmit(sta);
  98. /*
  99. * This code races in the following way:
  100. *
  101. * (1) STA sends frame indicating it will go to sleep and does so
  102. * (2) hardware/firmware adds STA to filter list, passes frame up
  103. * (3) hardware/firmware processes TX fifo and suppresses a frame
  104. * (4) we get TX status before having processed the frame and
  105. * knowing that the STA has gone to sleep.
  106. *
  107. * This is actually quite unlikely even when both those events are
  108. * processed from interrupts coming in quickly after one another or
  109. * even at the same time because we queue both TX status events and
  110. * RX frames to be processed by a tasklet and process them in the
  111. * same order that they were received or TX status last. Hence, there
  112. * is no race as long as the frame RX is processed before the next TX
  113. * status, which drivers can ensure, see below.
  114. *
  115. * Note that this can only happen if the hardware or firmware can
  116. * actually add STAs to the filter list, if this is done by the
  117. * driver in response to set_tim() (which will only reduce the race
  118. * this whole filtering tries to solve, not completely solve it)
  119. * this situation cannot happen.
  120. *
  121. * To completely solve this race drivers need to make sure that they
  122. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  123. * functions and
  124. * (b) always process RX events before TX status events if ordering
  125. * can be unknown, for example with different interrupt status
  126. * bits.
  127. * (c) if PS mode transitions are manual (i.e. the flag
  128. * %IEEE80211_HW_AP_LINK_PS is set), always process PS state
  129. * changes before calling TX status events if ordering can be
  130. * unknown.
  131. */
  132. if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
  133. skb_queue_len(&sta->tx_filtered[ac]) < STA_MAX_TX_BUFFER) {
  134. skb_queue_tail(&sta->tx_filtered[ac], skb);
  135. sta_info_recalc_tim(sta);
  136. if (!timer_pending(&local->sta_cleanup))
  137. mod_timer(&local->sta_cleanup,
  138. round_jiffies(jiffies +
  139. STA_INFO_CLEANUP_INTERVAL));
  140. return;
  141. }
  142. if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
  143. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  144. /* Software retry the packet once */
  145. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  146. ieee80211_add_pending_skb(local, skb);
  147. return;
  148. }
  149. ps_dbg_ratelimited(sta->sdata,
  150. "dropped TX filtered frame, queue_len=%d PS=%d @%lu\n",
  151. skb_queue_len(&sta->tx_filtered[ac]),
  152. !!test_sta_flag(sta, WLAN_STA_PS_STA), jiffies);
  153. ieee80211_free_txskb(&local->hw, skb);
  154. }
  155. static void ieee80211_check_pending_bar(struct sta_info *sta, u8 *addr, u8 tid)
  156. {
  157. struct tid_ampdu_tx *tid_tx;
  158. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  159. if (!tid_tx || !tid_tx->bar_pending)
  160. return;
  161. tid_tx->bar_pending = false;
  162. ieee80211_send_bar(&sta->sdata->vif, addr, tid, tid_tx->failed_bar_ssn);
  163. }
  164. static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
  165. {
  166. struct ieee80211_mgmt *mgmt = (void *) skb->data;
  167. struct ieee80211_local *local = sta->local;
  168. struct ieee80211_sub_if_data *sdata = sta->sdata;
  169. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  170. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  171. sta->status_stats.last_ack = jiffies;
  172. if (txinfo->status.is_valid_ack_signal) {
  173. sta->status_stats.last_ack_signal =
  174. (s8)txinfo->status.ack_signal;
  175. sta->status_stats.ack_signal_filled = true;
  176. ewma_avg_signal_add(&sta->status_stats.avg_ack_signal,
  177. -txinfo->status.ack_signal);
  178. }
  179. }
  180. if (ieee80211_is_data_qos(mgmt->frame_control)) {
  181. struct ieee80211_hdr *hdr = (void *) skb->data;
  182. u8 *qc = ieee80211_get_qos_ctl(hdr);
  183. u16 tid = qc[0] & 0xf;
  184. ieee80211_check_pending_bar(sta, hdr->addr1, tid);
  185. }
  186. if (ieee80211_is_action(mgmt->frame_control) &&
  187. !ieee80211_has_protected(mgmt->frame_control) &&
  188. mgmt->u.action.category == WLAN_CATEGORY_HT &&
  189. mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS &&
  190. ieee80211_sdata_running(sdata)) {
  191. enum ieee80211_smps_mode smps_mode;
  192. switch (mgmt->u.action.u.ht_smps.smps_control) {
  193. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  194. smps_mode = IEEE80211_SMPS_DYNAMIC;
  195. break;
  196. case WLAN_HT_SMPS_CONTROL_STATIC:
  197. smps_mode = IEEE80211_SMPS_STATIC;
  198. break;
  199. case WLAN_HT_SMPS_CONTROL_DISABLED:
  200. default: /* shouldn't happen since we don't send that */
  201. smps_mode = IEEE80211_SMPS_OFF;
  202. break;
  203. }
  204. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  205. /*
  206. * This update looks racy, but isn't -- if we come
  207. * here we've definitely got a station that we're
  208. * talking to, and on a managed interface that can
  209. * only be the AP. And the only other place updating
  210. * this variable in managed mode is before association.
  211. */
  212. sdata->smps_mode = smps_mode;
  213. ieee80211_queue_work(&local->hw, &sdata->recalc_smps);
  214. } else if (sdata->vif.type == NL80211_IFTYPE_AP ||
  215. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  216. sta->known_smps_mode = smps_mode;
  217. }
  218. }
  219. }
  220. static void ieee80211_set_bar_pending(struct sta_info *sta, u8 tid, u16 ssn)
  221. {
  222. struct tid_ampdu_tx *tid_tx;
  223. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  224. if (!tid_tx)
  225. return;
  226. tid_tx->failed_bar_ssn = ssn;
  227. tid_tx->bar_pending = true;
  228. }
  229. static int ieee80211_tx_radiotap_len(struct ieee80211_tx_info *info)
  230. {
  231. int len = sizeof(struct ieee80211_radiotap_header);
  232. /* IEEE80211_RADIOTAP_RATE rate */
  233. if (info->status.rates[0].idx >= 0 &&
  234. !(info->status.rates[0].flags & (IEEE80211_TX_RC_MCS |
  235. IEEE80211_TX_RC_VHT_MCS)))
  236. len += 2;
  237. /* IEEE80211_RADIOTAP_TX_FLAGS */
  238. len += 2;
  239. /* IEEE80211_RADIOTAP_DATA_RETRIES */
  240. len += 1;
  241. /* IEEE80211_RADIOTAP_MCS
  242. * IEEE80211_RADIOTAP_VHT */
  243. if (info->status.rates[0].idx >= 0) {
  244. if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS)
  245. len += 3;
  246. else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS)
  247. len = ALIGN(len, 2) + 12;
  248. }
  249. return len;
  250. }
  251. static void
  252. ieee80211_add_tx_radiotap_header(struct ieee80211_local *local,
  253. struct ieee80211_supported_band *sband,
  254. struct sk_buff *skb, int retry_count,
  255. int rtap_len, int shift)
  256. {
  257. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  258. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  259. struct ieee80211_radiotap_header *rthdr;
  260. unsigned char *pos;
  261. u16 txflags;
  262. rthdr = skb_push(skb, rtap_len);
  263. memset(rthdr, 0, rtap_len);
  264. rthdr->it_len = cpu_to_le16(rtap_len);
  265. rthdr->it_present =
  266. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  267. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  268. pos = (unsigned char *)(rthdr + 1);
  269. /*
  270. * XXX: Once radiotap gets the bitmap reset thing the vendor
  271. * extensions proposal contains, we can actually report
  272. * the whole set of tries we did.
  273. */
  274. /* IEEE80211_RADIOTAP_RATE */
  275. if (info->status.rates[0].idx >= 0 &&
  276. !(info->status.rates[0].flags & (IEEE80211_TX_RC_MCS |
  277. IEEE80211_TX_RC_VHT_MCS))) {
  278. u16 rate;
  279. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  280. rate = sband->bitrates[info->status.rates[0].idx].bitrate;
  281. *pos = DIV_ROUND_UP(rate, 5 * (1 << shift));
  282. /* padding for tx flags */
  283. pos += 2;
  284. }
  285. /* IEEE80211_RADIOTAP_TX_FLAGS */
  286. txflags = 0;
  287. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  288. !is_multicast_ether_addr(hdr->addr1))
  289. txflags |= IEEE80211_RADIOTAP_F_TX_FAIL;
  290. if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  291. txflags |= IEEE80211_RADIOTAP_F_TX_CTS;
  292. if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  293. txflags |= IEEE80211_RADIOTAP_F_TX_RTS;
  294. put_unaligned_le16(txflags, pos);
  295. pos += 2;
  296. /* IEEE80211_RADIOTAP_DATA_RETRIES */
  297. /* for now report the total retry_count */
  298. *pos = retry_count;
  299. pos++;
  300. if (info->status.rates[0].idx < 0)
  301. return;
  302. /* IEEE80211_RADIOTAP_MCS
  303. * IEEE80211_RADIOTAP_VHT */
  304. if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
  305. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  306. pos[0] = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  307. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  308. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  309. if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  310. pos[1] |= IEEE80211_RADIOTAP_MCS_SGI;
  311. if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  312. pos[1] |= IEEE80211_RADIOTAP_MCS_BW_40;
  313. if (info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)
  314. pos[1] |= IEEE80211_RADIOTAP_MCS_FMT_GF;
  315. pos[2] = info->status.rates[0].idx;
  316. pos += 3;
  317. } else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
  318. u16 known = local->hw.radiotap_vht_details &
  319. (IEEE80211_RADIOTAP_VHT_KNOWN_GI |
  320. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH);
  321. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
  322. /* required alignment from rthdr */
  323. pos = (u8 *)rthdr + ALIGN(pos - (u8 *)rthdr, 2);
  324. /* u16 known - IEEE80211_RADIOTAP_VHT_KNOWN_* */
  325. put_unaligned_le16(known, pos);
  326. pos += 2;
  327. /* u8 flags - IEEE80211_RADIOTAP_VHT_FLAG_* */
  328. if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  329. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
  330. pos++;
  331. /* u8 bandwidth */
  332. if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  333. *pos = 1;
  334. else if (info->status.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  335. *pos = 4;
  336. else if (info->status.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  337. *pos = 11;
  338. else /* IEEE80211_TX_RC_{20_MHZ_WIDTH,FIXME:DUP_DATA} */
  339. *pos = 0;
  340. pos++;
  341. /* u8 mcs_nss[4] */
  342. *pos = (ieee80211_rate_get_vht_mcs(&info->status.rates[0]) << 4) |
  343. ieee80211_rate_get_vht_nss(&info->status.rates[0]);
  344. pos += 4;
  345. /* u8 coding */
  346. pos++;
  347. /* u8 group_id */
  348. pos++;
  349. /* u16 partial_aid */
  350. pos += 2;
  351. }
  352. }
  353. /*
  354. * Handles the tx for TDLS teardown frames.
  355. * If the frame wasn't ACKed by the peer - it will be re-sent through the AP
  356. */
  357. static void ieee80211_tdls_td_tx_handle(struct ieee80211_local *local,
  358. struct ieee80211_sub_if_data *sdata,
  359. struct sk_buff *skb, u32 flags)
  360. {
  361. struct sk_buff *teardown_skb;
  362. struct sk_buff *orig_teardown_skb;
  363. bool is_teardown = false;
  364. /* Get the teardown data we need and free the lock */
  365. spin_lock(&sdata->u.mgd.teardown_lock);
  366. teardown_skb = sdata->u.mgd.teardown_skb;
  367. orig_teardown_skb = sdata->u.mgd.orig_teardown_skb;
  368. if ((skb == orig_teardown_skb) && teardown_skb) {
  369. sdata->u.mgd.teardown_skb = NULL;
  370. sdata->u.mgd.orig_teardown_skb = NULL;
  371. is_teardown = true;
  372. }
  373. spin_unlock(&sdata->u.mgd.teardown_lock);
  374. if (is_teardown) {
  375. /* This mechanism relies on being able to get ACKs */
  376. WARN_ON(!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS));
  377. /* Check if peer has ACKed */
  378. if (flags & IEEE80211_TX_STAT_ACK) {
  379. dev_kfree_skb_any(teardown_skb);
  380. } else {
  381. tdls_dbg(sdata,
  382. "TDLS Resending teardown through AP\n");
  383. ieee80211_subif_start_xmit(teardown_skb, skb->dev);
  384. }
  385. }
  386. }
  387. static struct ieee80211_sub_if_data *
  388. ieee80211_sdata_from_skb(struct ieee80211_local *local, struct sk_buff *skb)
  389. {
  390. struct ieee80211_sub_if_data *sdata;
  391. if (skb->dev) {
  392. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  393. if (!sdata->dev)
  394. continue;
  395. if (skb->dev == sdata->dev)
  396. return sdata;
  397. }
  398. return NULL;
  399. }
  400. return rcu_dereference(local->p2p_sdata);
  401. }
  402. static void ieee80211_report_ack_skb(struct ieee80211_local *local,
  403. struct ieee80211_tx_info *info,
  404. bool acked, bool dropped)
  405. {
  406. struct sk_buff *skb;
  407. unsigned long flags;
  408. spin_lock_irqsave(&local->ack_status_lock, flags);
  409. skb = idr_remove(&local->ack_status_frames, info->ack_frame_id);
  410. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  411. if (!skb)
  412. return;
  413. if (info->flags & IEEE80211_TX_INTFL_NL80211_FRAME_TX) {
  414. u64 cookie = IEEE80211_SKB_CB(skb)->ack.cookie;
  415. struct ieee80211_sub_if_data *sdata;
  416. struct ieee80211_hdr *hdr = (void *)skb->data;
  417. rcu_read_lock();
  418. sdata = ieee80211_sdata_from_skb(local, skb);
  419. if (sdata) {
  420. if (ieee80211_is_any_nullfunc(hdr->frame_control))
  421. cfg80211_probe_status(sdata->dev, hdr->addr1,
  422. cookie, acked,
  423. info->status.ack_signal,
  424. info->status.is_valid_ack_signal,
  425. GFP_ATOMIC);
  426. else
  427. cfg80211_mgmt_tx_status(&sdata->wdev, cookie,
  428. skb->data, skb->len,
  429. acked, GFP_ATOMIC);
  430. }
  431. rcu_read_unlock();
  432. dev_kfree_skb_any(skb);
  433. } else if (dropped) {
  434. dev_kfree_skb_any(skb);
  435. } else {
  436. /* consumes skb */
  437. skb_complete_wifi_ack(skb, acked);
  438. }
  439. }
  440. static void ieee80211_report_used_skb(struct ieee80211_local *local,
  441. struct sk_buff *skb, bool dropped)
  442. {
  443. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  444. struct ieee80211_hdr *hdr = (void *)skb->data;
  445. bool acked = info->flags & IEEE80211_TX_STAT_ACK;
  446. if (dropped)
  447. acked = false;
  448. if (info->flags & IEEE80211_TX_INTFL_MLME_CONN_TX) {
  449. struct ieee80211_sub_if_data *sdata;
  450. rcu_read_lock();
  451. sdata = ieee80211_sdata_from_skb(local, skb);
  452. if (!sdata) {
  453. skb->dev = NULL;
  454. } else {
  455. unsigned int hdr_size =
  456. ieee80211_hdrlen(hdr->frame_control);
  457. /* Check to see if packet is a TDLS teardown packet */
  458. if (ieee80211_is_data(hdr->frame_control) &&
  459. (ieee80211_get_tdls_action(skb, hdr_size) ==
  460. WLAN_TDLS_TEARDOWN))
  461. ieee80211_tdls_td_tx_handle(local, sdata, skb,
  462. info->flags);
  463. else
  464. ieee80211_mgd_conn_tx_status(sdata,
  465. hdr->frame_control,
  466. acked);
  467. }
  468. rcu_read_unlock();
  469. } else if (info->ack_frame_id) {
  470. ieee80211_report_ack_skb(local, info, acked, dropped);
  471. }
  472. if (!dropped && skb->destructor) {
  473. skb->wifi_acked_valid = 1;
  474. skb->wifi_acked = acked;
  475. }
  476. ieee80211_led_tx(local);
  477. if (skb_has_frag_list(skb)) {
  478. kfree_skb_list(skb_shinfo(skb)->frag_list);
  479. skb_shinfo(skb)->frag_list = NULL;
  480. }
  481. }
  482. /*
  483. * Use a static threshold for now, best value to be determined
  484. * by testing ...
  485. * Should it depend on:
  486. * - on # of retransmissions
  487. * - current throughput (higher value for higher tpt)?
  488. */
  489. #define STA_LOST_PKT_THRESHOLD 50
  490. #define STA_LOST_TDLS_PKT_THRESHOLD 10
  491. #define STA_LOST_TDLS_PKT_TIME (10*HZ) /* 10secs since last ACK */
  492. static void ieee80211_lost_packet(struct sta_info *sta,
  493. struct ieee80211_tx_info *info)
  494. {
  495. /* If driver relies on its own algorithm for station kickout, skip
  496. * mac80211 packet loss mechanism.
  497. */
  498. if (ieee80211_hw_check(&sta->local->hw, REPORTS_LOW_ACK))
  499. return;
  500. /* This packet was aggregated but doesn't carry status info */
  501. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  502. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  503. return;
  504. sta->status_stats.lost_packets++;
  505. if (!sta->sta.tdls &&
  506. sta->status_stats.lost_packets < STA_LOST_PKT_THRESHOLD)
  507. return;
  508. /*
  509. * If we're in TDLS mode, make sure that all STA_LOST_TDLS_PKT_THRESHOLD
  510. * of the last packets were lost, and that no ACK was received in the
  511. * last STA_LOST_TDLS_PKT_TIME ms, before triggering the CQM packet-loss
  512. * mechanism.
  513. */
  514. if (sta->sta.tdls &&
  515. (sta->status_stats.lost_packets < STA_LOST_TDLS_PKT_THRESHOLD ||
  516. time_before(jiffies,
  517. sta->status_stats.last_tdls_pkt_time +
  518. STA_LOST_TDLS_PKT_TIME)))
  519. return;
  520. cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
  521. sta->status_stats.lost_packets, GFP_ATOMIC);
  522. sta->status_stats.lost_packets = 0;
  523. }
  524. static int ieee80211_tx_get_rates(struct ieee80211_hw *hw,
  525. struct ieee80211_tx_info *info,
  526. int *retry_count)
  527. {
  528. int rates_idx = -1;
  529. int count = -1;
  530. int i;
  531. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  532. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  533. !(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  534. /* just the first aggr frame carry status info */
  535. info->status.rates[i].idx = -1;
  536. info->status.rates[i].count = 0;
  537. break;
  538. } else if (info->status.rates[i].idx < 0) {
  539. break;
  540. } else if (i >= hw->max_report_rates) {
  541. /* the HW cannot have attempted that rate */
  542. info->status.rates[i].idx = -1;
  543. info->status.rates[i].count = 0;
  544. break;
  545. }
  546. count += info->status.rates[i].count;
  547. }
  548. rates_idx = i - 1;
  549. if (count < 0)
  550. count = 0;
  551. *retry_count = count;
  552. return rates_idx;
  553. }
  554. void ieee80211_tx_monitor(struct ieee80211_local *local, struct sk_buff *skb,
  555. struct ieee80211_supported_band *sband,
  556. int retry_count, int shift, bool send_to_cooked)
  557. {
  558. struct sk_buff *skb2;
  559. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  560. struct ieee80211_sub_if_data *sdata;
  561. struct net_device *prev_dev = NULL;
  562. int rtap_len;
  563. /* send frame to monitor interfaces now */
  564. rtap_len = ieee80211_tx_radiotap_len(info);
  565. if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
  566. pr_err("ieee80211_tx_status: headroom too small\n");
  567. dev_kfree_skb(skb);
  568. return;
  569. }
  570. ieee80211_add_tx_radiotap_header(local, sband, skb, retry_count,
  571. rtap_len, shift);
  572. /* XXX: is this sufficient for BPF? */
  573. skb_reset_mac_header(skb);
  574. skb->ip_summed = CHECKSUM_UNNECESSARY;
  575. skb->pkt_type = PACKET_OTHERHOST;
  576. skb->protocol = htons(ETH_P_802_2);
  577. memset(skb->cb, 0, sizeof(skb->cb));
  578. rcu_read_lock();
  579. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  580. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  581. if (!ieee80211_sdata_running(sdata))
  582. continue;
  583. if ((sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) &&
  584. !send_to_cooked)
  585. continue;
  586. if (prev_dev) {
  587. skb2 = skb_clone(skb, GFP_ATOMIC);
  588. if (skb2) {
  589. skb2->dev = prev_dev;
  590. netif_rx(skb2);
  591. }
  592. }
  593. prev_dev = sdata->dev;
  594. }
  595. }
  596. if (prev_dev) {
  597. skb->dev = prev_dev;
  598. netif_rx(skb);
  599. skb = NULL;
  600. }
  601. rcu_read_unlock();
  602. dev_kfree_skb(skb);
  603. }
  604. static void __ieee80211_tx_status(struct ieee80211_hw *hw,
  605. struct ieee80211_tx_status *status)
  606. {
  607. struct sk_buff *skb = status->skb;
  608. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  609. struct ieee80211_local *local = hw_to_local(hw);
  610. struct ieee80211_tx_info *info = status->info;
  611. struct sta_info *sta;
  612. __le16 fc;
  613. struct ieee80211_supported_band *sband;
  614. int retry_count;
  615. int rates_idx;
  616. bool send_to_cooked;
  617. bool acked;
  618. struct ieee80211_bar *bar;
  619. int shift = 0;
  620. int tid = IEEE80211_NUM_TIDS;
  621. rates_idx = ieee80211_tx_get_rates(hw, info, &retry_count);
  622. sband = local->hw.wiphy->bands[info->band];
  623. fc = hdr->frame_control;
  624. if (status->sta) {
  625. sta = container_of(status->sta, struct sta_info, sta);
  626. shift = ieee80211_vif_get_shift(&sta->sdata->vif);
  627. if (info->flags & IEEE80211_TX_STATUS_EOSP)
  628. clear_sta_flag(sta, WLAN_STA_SP);
  629. acked = !!(info->flags & IEEE80211_TX_STAT_ACK);
  630. /* mesh Peer Service Period support */
  631. if (ieee80211_vif_is_mesh(&sta->sdata->vif) &&
  632. ieee80211_is_data_qos(fc))
  633. ieee80211_mpsp_trigger_process(
  634. ieee80211_get_qos_ctl(hdr), sta, true, acked);
  635. if (!acked && test_sta_flag(sta, WLAN_STA_PS_STA)) {
  636. /*
  637. * The STA is in power save mode, so assume
  638. * that this TX packet failed because of that.
  639. */
  640. ieee80211_handle_filtered_frame(local, sta, skb);
  641. return;
  642. }
  643. if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL) &&
  644. (ieee80211_is_data(hdr->frame_control)) &&
  645. (rates_idx != -1))
  646. sta->tx_stats.last_rate =
  647. info->status.rates[rates_idx];
  648. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  649. (ieee80211_is_data_qos(fc))) {
  650. u16 ssn;
  651. u8 *qc;
  652. qc = ieee80211_get_qos_ctl(hdr);
  653. tid = qc[0] & 0xf;
  654. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  655. & IEEE80211_SCTL_SEQ);
  656. ieee80211_send_bar(&sta->sdata->vif, hdr->addr1,
  657. tid, ssn);
  658. } else if (ieee80211_is_data_qos(fc)) {
  659. u8 *qc = ieee80211_get_qos_ctl(hdr);
  660. tid = qc[0] & 0xf;
  661. }
  662. if (!acked && ieee80211_is_back_req(fc)) {
  663. u16 control;
  664. /*
  665. * BAR failed, store the last SSN and retry sending
  666. * the BAR when the next unicast transmission on the
  667. * same TID succeeds.
  668. */
  669. bar = (struct ieee80211_bar *) skb->data;
  670. control = le16_to_cpu(bar->control);
  671. if (!(control & IEEE80211_BAR_CTRL_MULTI_TID)) {
  672. u16 ssn = le16_to_cpu(bar->start_seq_num);
  673. tid = (control &
  674. IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
  675. IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
  676. ieee80211_set_bar_pending(sta, tid, ssn);
  677. }
  678. }
  679. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  680. ieee80211_handle_filtered_frame(local, sta, skb);
  681. return;
  682. } else {
  683. if (!acked)
  684. sta->status_stats.retry_failed++;
  685. sta->status_stats.retry_count += retry_count;
  686. if (ieee80211_is_data_present(fc)) {
  687. if (!acked)
  688. sta->status_stats.msdu_failed[tid]++;
  689. sta->status_stats.msdu_retries[tid] +=
  690. retry_count;
  691. }
  692. }
  693. rate_control_tx_status(local, sband, status);
  694. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  695. ieee80211s_update_metric(local, sta, status);
  696. if (!(info->flags & IEEE80211_TX_CTL_INJECTED) && acked)
  697. ieee80211_frame_acked(sta, skb);
  698. if ((sta->sdata->vif.type == NL80211_IFTYPE_STATION) &&
  699. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  700. ieee80211_sta_tx_notify(sta->sdata, (void *) skb->data,
  701. acked, info->status.tx_time);
  702. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  703. if (info->flags & IEEE80211_TX_STAT_ACK) {
  704. if (sta->status_stats.lost_packets)
  705. sta->status_stats.lost_packets = 0;
  706. /* Track when last TDLS packet was ACKed */
  707. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
  708. sta->status_stats.last_tdls_pkt_time =
  709. jiffies;
  710. } else {
  711. ieee80211_lost_packet(sta, info);
  712. }
  713. }
  714. }
  715. /* SNMP counters
  716. * Fragments are passed to low-level drivers as separate skbs, so these
  717. * are actually fragments, not frames. Update frame counters only for
  718. * the first fragment of the frame. */
  719. if ((info->flags & IEEE80211_TX_STAT_ACK) ||
  720. (info->flags & IEEE80211_TX_STAT_NOACK_TRANSMITTED)) {
  721. if (ieee80211_is_first_frag(hdr->seq_ctrl)) {
  722. I802_DEBUG_INC(local->dot11TransmittedFrameCount);
  723. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  724. I802_DEBUG_INC(local->dot11MulticastTransmittedFrameCount);
  725. if (retry_count > 0)
  726. I802_DEBUG_INC(local->dot11RetryCount);
  727. if (retry_count > 1)
  728. I802_DEBUG_INC(local->dot11MultipleRetryCount);
  729. }
  730. /* This counter shall be incremented for an acknowledged MPDU
  731. * with an individual address in the address 1 field or an MPDU
  732. * with a multicast address in the address 1 field of type Data
  733. * or Management. */
  734. if (!is_multicast_ether_addr(hdr->addr1) ||
  735. ieee80211_is_data(fc) ||
  736. ieee80211_is_mgmt(fc))
  737. I802_DEBUG_INC(local->dot11TransmittedFragmentCount);
  738. } else {
  739. if (ieee80211_is_first_frag(hdr->seq_ctrl))
  740. I802_DEBUG_INC(local->dot11FailedCount);
  741. }
  742. if (ieee80211_is_any_nullfunc(fc) &&
  743. ieee80211_has_pm(fc) &&
  744. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
  745. !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  746. local->ps_sdata && !(local->scanning)) {
  747. if (info->flags & IEEE80211_TX_STAT_ACK) {
  748. local->ps_sdata->u.mgd.flags |=
  749. IEEE80211_STA_NULLFUNC_ACKED;
  750. } else
  751. mod_timer(&local->dynamic_ps_timer, jiffies +
  752. msecs_to_jiffies(10));
  753. }
  754. ieee80211_report_used_skb(local, skb, false);
  755. /* this was a transmitted frame, but now we want to reuse it */
  756. skb_orphan(skb);
  757. /* Need to make a copy before skb->cb gets cleared */
  758. send_to_cooked = !!(info->flags & IEEE80211_TX_CTL_INJECTED) ||
  759. !(ieee80211_is_data(fc));
  760. /*
  761. * This is a bit racy but we can avoid a lot of work
  762. * with this test...
  763. */
  764. if (!local->monitors && (!send_to_cooked || !local->cooked_mntrs)) {
  765. dev_kfree_skb(skb);
  766. return;
  767. }
  768. /* send to monitor interfaces */
  769. ieee80211_tx_monitor(local, skb, sband, retry_count, shift, send_to_cooked);
  770. }
  771. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  772. {
  773. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  774. struct ieee80211_local *local = hw_to_local(hw);
  775. struct ieee80211_tx_status status = {
  776. .skb = skb,
  777. .info = IEEE80211_SKB_CB(skb),
  778. };
  779. struct rhlist_head *tmp;
  780. struct sta_info *sta;
  781. rcu_read_lock();
  782. for_each_sta_info(local, hdr->addr1, sta, tmp) {
  783. /* skip wrong virtual interface */
  784. if (!ether_addr_equal(hdr->addr2, sta->sdata->vif.addr))
  785. continue;
  786. status.sta = &sta->sta;
  787. break;
  788. }
  789. __ieee80211_tx_status(hw, &status);
  790. rcu_read_unlock();
  791. }
  792. EXPORT_SYMBOL(ieee80211_tx_status);
  793. void ieee80211_tx_status_ext(struct ieee80211_hw *hw,
  794. struct ieee80211_tx_status *status)
  795. {
  796. struct ieee80211_local *local = hw_to_local(hw);
  797. struct ieee80211_tx_info *info = status->info;
  798. struct ieee80211_sta *pubsta = status->sta;
  799. struct ieee80211_supported_band *sband;
  800. int retry_count;
  801. bool acked, noack_success;
  802. if (status->skb)
  803. return __ieee80211_tx_status(hw, status);
  804. if (!status->sta)
  805. return;
  806. ieee80211_tx_get_rates(hw, info, &retry_count);
  807. sband = hw->wiphy->bands[info->band];
  808. acked = !!(info->flags & IEEE80211_TX_STAT_ACK);
  809. noack_success = !!(info->flags & IEEE80211_TX_STAT_NOACK_TRANSMITTED);
  810. if (pubsta) {
  811. struct sta_info *sta;
  812. sta = container_of(pubsta, struct sta_info, sta);
  813. if (!acked)
  814. sta->status_stats.retry_failed++;
  815. sta->status_stats.retry_count += retry_count;
  816. if (acked) {
  817. sta->status_stats.last_ack = jiffies;
  818. if (sta->status_stats.lost_packets)
  819. sta->status_stats.lost_packets = 0;
  820. /* Track when last TDLS packet was ACKed */
  821. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
  822. sta->status_stats.last_tdls_pkt_time = jiffies;
  823. } else if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  824. return;
  825. } else {
  826. ieee80211_lost_packet(sta, info);
  827. }
  828. rate_control_tx_status(local, sband, status);
  829. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  830. ieee80211s_update_metric(local, sta, status);
  831. }
  832. if (acked || noack_success) {
  833. I802_DEBUG_INC(local->dot11TransmittedFrameCount);
  834. if (!pubsta)
  835. I802_DEBUG_INC(local->dot11MulticastTransmittedFrameCount);
  836. if (retry_count > 0)
  837. I802_DEBUG_INC(local->dot11RetryCount);
  838. if (retry_count > 1)
  839. I802_DEBUG_INC(local->dot11MultipleRetryCount);
  840. } else {
  841. I802_DEBUG_INC(local->dot11FailedCount);
  842. }
  843. }
  844. EXPORT_SYMBOL(ieee80211_tx_status_ext);
  845. void ieee80211_report_low_ack(struct ieee80211_sta *pubsta, u32 num_packets)
  846. {
  847. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  848. cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
  849. num_packets, GFP_ATOMIC);
  850. }
  851. EXPORT_SYMBOL(ieee80211_report_low_ack);
  852. void ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb)
  853. {
  854. struct ieee80211_local *local = hw_to_local(hw);
  855. ieee80211_report_used_skb(local, skb, true);
  856. dev_kfree_skb_any(skb);
  857. }
  858. EXPORT_SYMBOL(ieee80211_free_txskb);
  859. void ieee80211_purge_tx_queue(struct ieee80211_hw *hw,
  860. struct sk_buff_head *skbs)
  861. {
  862. struct sk_buff *skb;
  863. while ((skb = __skb_dequeue(skbs)))
  864. ieee80211_free_txskb(hw, skb);
  865. }