• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2008-2010	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  */
9 
10 #include <linux/export.h>
11 #include <linux/etherdevice.h>
12 #include <net/mac80211.h>
13 #include <asm/unaligned.h>
14 #include "ieee80211_i.h"
15 #include "rate.h"
16 #include "mesh.h"
17 #include "led.h"
18 #include "wme.h"
19 
20 #ifdef CONFIG_DRIVERS_HDF_XR829
21 extern void wal_netif_rx(struct sk_buff *skb);
22 #endif
23 
mac80211_tx_status_irqsafe(struct ieee80211_hw * hw,struct sk_buff * skb)24 void mac80211_tx_status_irqsafe(struct ieee80211_hw *hw,
25 				 struct sk_buff *skb)
26 {
27 	struct ieee80211_local *local = hw_to_local(hw);
28 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
29 	int tmp;
30 
31 	skb->pkt_type = IEEE80211_TX_STATUS_MSG;
32 	skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
33 		       &local->skb_queue : &local->skb_queue_unreliable, skb);
34 	tmp = skb_queue_len(&local->skb_queue) +
35 		skb_queue_len(&local->skb_queue_unreliable);
36 	while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
37 	       (skb = skb_dequeue(&local->skb_queue_unreliable))) {
38 		mac80211_free_txskb(hw, skb);
39 		tmp--;
40 		I802_DEBUG_INC(local->tx_status_drop);
41 	}
42 	tasklet_schedule(&local->tasklet);
43 }
44 
ieee80211_handle_filtered_frame(struct ieee80211_local * local,struct sta_info * sta,struct sk_buff * skb)45 static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
46 					    struct sta_info *sta,
47 					    struct sk_buff *skb)
48 {
49 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
50 	struct ieee80211_hdr *hdr = (void *)skb->data;
51 	int ac;
52 
53 	if (info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
54 			   IEEE80211_TX_CTL_AMPDU)) {
55 		mac80211_free_txskb(&local->hw, skb);
56 		return;
57 	}
58 
59 	/*
60 	 * This skb 'survived' a round-trip through the driver, and
61 	 * hopefully the driver didn't mangle it too badly. However,
62 	 * we can definitely not rely on the control information
63 	 * being correct. Clear it so we don't get junk there, and
64 	 * indicate that it needs new processing, but must not be
65 	 * modified/encrypted again.
66 	 */
67 	memset(&info->control, 0, sizeof(info->control));
68 
69 	info->control.jiffies = jiffies;
70 	info->control.vif = &sta->sdata->vif;
71 	info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING |
72 		       IEEE80211_TX_INTFL_RETRANSMISSION;
73 	info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
74 
75 	sta->status_stats.filtered++;
76 
77 	/*
78 	 * Clear more-data bit on filtered frames, it might be set
79 	 * but later frames might time out so it might have to be
80 	 * clear again ... It's all rather unlikely (this frame
81 	 * should time out first, right?) but let's not confuse
82 	 * peers unnecessarily.
83 	 */
84 	if (hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_MOREDATA))
85 		hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_MOREDATA);
86 
87 	if (ieee80211_is_data_qos(hdr->frame_control)) {
88 		u8 *p = ieee80211_get_qos_ctl(hdr);
89 		int tid = *p & IEEE80211_QOS_CTL_TID_MASK;
90 
91 		/*
92 		 * Clear EOSP if set, this could happen e.g.
93 		 * if an absence period (us being a P2P GO)
94 		 * shortens the SP.
95 		 */
96 		if (*p & IEEE80211_QOS_CTL_EOSP)
97 			*p &= ~IEEE80211_QOS_CTL_EOSP;
98 		ac = ieee80211_ac_from_tid(tid);
99 	} else {
100 		ac = IEEE80211_AC_BE;
101 	}
102 
103 	/*
104 	 * Clear the TX filter mask for this STA when sending the next
105 	 * packet. If the STA went to power save mode, this will happen
106 	 * when it wakes up for the next time.
107 	 */
108 	set_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT);
109 	ieee80211_clear_fast_xmit(sta);
110 
111 	/*
112 	 * This code races in the following way:
113 	 *
114 	 *  (1) STA sends frame indicating it will go to sleep and does so
115 	 *  (2) hardware/firmware adds STA to filter list, passes frame up
116 	 *  (3) hardware/firmware processes TX fifo and suppresses a frame
117 	 *  (4) we get TX status before having processed the frame and
118 	 *	knowing that the STA has gone to sleep.
119 	 *
120 	 * This is actually quite unlikely even when both those events are
121 	 * processed from interrupts coming in quickly after one another or
122 	 * even at the same time because we queue both TX status events and
123 	 * RX frames to be processed by a tasklet and process them in the
124 	 * same order that they were received or TX status last. Hence, there
125 	 * is no race as long as the frame RX is processed before the next TX
126 	 * status, which drivers can ensure, see below.
127 	 *
128 	 * Note that this can only happen if the hardware or firmware can
129 	 * actually add STAs to the filter list, if this is done by the
130 	 * driver in response to set_tim() (which will only reduce the race
131 	 * this whole filtering tries to solve, not completely solve it)
132 	 * this situation cannot happen.
133 	 *
134 	 * To completely solve this race drivers need to make sure that they
135 	 *  (a) don't mix the irq-safe/not irq-safe TX status/RX processing
136 	 *	functions and
137 	 *  (b) always process RX events before TX status events if ordering
138 	 *      can be unknown, for example with different interrupt status
139 	 *	bits.
140 	 *  (c) if PS mode transitions are manual (i.e. the flag
141 	 *      %IEEE80211_HW_AP_LINK_PS is set), always process PS state
142 	 *      changes before calling TX status events if ordering can be
143 	 *	unknown.
144 	 */
145 	if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
146 	    skb_queue_len(&sta->tx_filtered[ac]) < STA_MAX_TX_BUFFER) {
147 		skb_queue_tail(&sta->tx_filtered[ac], skb);
148 		sta_info_recalc_tim(sta);
149 
150 		if (!timer_pending(&local->sta_cleanup))
151 			mod_timer(&local->sta_cleanup,
152 				  round_jiffies(jiffies +
153 						STA_INFO_CLEANUP_INTERVAL));
154 		return;
155 	}
156 
157 	if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
158 	    !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
159 		/* Software retry the packet once */
160 		info->flags |= IEEE80211_TX_INTFL_RETRIED;
161 		ieee80211_add_pending_skb(local, skb);
162 		return;
163 	}
164 
165 	ps_dbg_ratelimited(sta->sdata,
166 			   "dropped TX filtered frame, queue_len=%d PS=%d @%lu\n",
167 			   skb_queue_len(&sta->tx_filtered[ac]),
168 			   !!test_sta_flag(sta, WLAN_STA_PS_STA), jiffies);
169 	mac80211_free_txskb(&local->hw, skb);
170 }
171 
ieee80211_check_pending_bar(struct sta_info * sta,u8 * addr,u8 tid)172 static void ieee80211_check_pending_bar(struct sta_info *sta, u8 *addr, u8 tid)
173 {
174 	struct tid_ampdu_tx *tid_tx;
175 
176 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
177 	if (!tid_tx || !tid_tx->bar_pending)
178 		return;
179 
180 	tid_tx->bar_pending = false;
181 	mac80211_send_bar(&sta->sdata->vif, addr, tid, tid_tx->failed_bar_ssn);
182 }
183 
ieee80211_frame_acked(struct sta_info * sta,struct sk_buff * skb)184 static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
185 {
186 	struct ieee80211_mgmt *mgmt = (void *) skb->data;
187 	struct ieee80211_local *local = sta->local;
188 	struct ieee80211_sub_if_data *sdata = sta->sdata;
189 	struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
190 
191 	if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
192 		sta->status_stats.last_ack = jiffies;
193 		if (txinfo->status.is_valid_ack_signal) {
194 			sta->status_stats.last_ack_signal =
195 					 (s8)txinfo->status.ack_signal;
196 			sta->status_stats.ack_signal_filled = true;
197 			ewma_avg_signal_add(&sta->status_stats.avg_ack_signal,
198 					    -txinfo->status.ack_signal);
199 		}
200 	}
201 
202 	if (ieee80211_is_data_qos(mgmt->frame_control)) {
203 		struct ieee80211_hdr *hdr = (void *) skb->data;
204 		u8 *qc = ieee80211_get_qos_ctl(hdr);
205 		u16 tid = qc[0] & 0xf;
206 
207 		ieee80211_check_pending_bar(sta, hdr->addr1, tid);
208 	}
209 
210 	if (ieee80211_is_action(mgmt->frame_control) &&
211 	    !ieee80211_has_protected(mgmt->frame_control) &&
212 	    mgmt->u.action.category == WLAN_CATEGORY_HT &&
213 	    mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS &&
214 	    ieee80211_sdata_running(sdata)) {
215 		enum ieee80211_smps_mode smps_mode;
216 
217 		switch (mgmt->u.action.u.ht_smps.smps_control) {
218 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
219 			smps_mode = IEEE80211_SMPS_DYNAMIC;
220 			break;
221 		case WLAN_HT_SMPS_CONTROL_STATIC:
222 			smps_mode = IEEE80211_SMPS_STATIC;
223 			break;
224 		case WLAN_HT_SMPS_CONTROL_DISABLED:
225 		default: /* shouldn't happen since we don't send that */
226 			smps_mode = IEEE80211_SMPS_OFF;
227 			break;
228 		}
229 
230 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
231 			/*
232 			 * This update looks racy, but isn't -- if we come
233 			 * here we've definitely got a station that we're
234 			 * talking to, and on a managed interface that can
235 			 * only be the AP. And the only other place updating
236 			 * this variable in managed mode is before association.
237 			 */
238 			sdata->smps_mode = smps_mode;
239 			mac80211_queue_work(&local->hw, &sdata->recalc_smps);
240 		} else if (sdata->vif.type == NL80211_IFTYPE_AP ||
241 			   sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
242 			sta->known_smps_mode = smps_mode;
243 		}
244 	}
245 }
246 
ieee80211_set_bar_pending(struct sta_info * sta,u8 tid,u16 ssn)247 static void ieee80211_set_bar_pending(struct sta_info *sta, u8 tid, u16 ssn)
248 {
249 	struct tid_ampdu_tx *tid_tx;
250 
251 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
252 	if (!tid_tx)
253 		return;
254 
255 	tid_tx->failed_bar_ssn = ssn;
256 	tid_tx->bar_pending = true;
257 }
258 
ieee80211_tx_radiotap_len(struct ieee80211_tx_info * info,struct ieee80211_tx_status * status)259 static int ieee80211_tx_radiotap_len(struct ieee80211_tx_info *info,
260 				     struct ieee80211_tx_status *status)
261 {
262 	int len = sizeof(struct ieee80211_radiotap_header);
263 
264 	/* IEEE80211_RADIOTAP_RATE rate */
265 	if (status && status->rate && !(status->rate->flags &
266 					(RATE_INFO_FLAGS_MCS |
267 					 RATE_INFO_FLAGS_DMG |
268 					 RATE_INFO_FLAGS_EDMG |
269 					 RATE_INFO_FLAGS_VHT_MCS |
270 					 RATE_INFO_FLAGS_HE_MCS)))
271 		len += 2;
272 	else if (info->status.rates[0].idx >= 0 &&
273 		 !(info->status.rates[0].flags &
274 		   (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS)))
275 		len += 2;
276 
277 	/* IEEE80211_RADIOTAP_TX_FLAGS */
278 	len += 2;
279 
280 	/* IEEE80211_RADIOTAP_DATA_RETRIES */
281 	len += 1;
282 
283 	/* IEEE80211_RADIOTAP_MCS
284 	 * IEEE80211_RADIOTAP_VHT */
285 	if (status && status->rate) {
286 		if (status->rate->flags & RATE_INFO_FLAGS_MCS)
287 			len += 3;
288 		else if (status->rate->flags & RATE_INFO_FLAGS_VHT_MCS)
289 			len = ALIGN(len, 2) + 12;
290 		else if (status->rate->flags & RATE_INFO_FLAGS_HE_MCS)
291 			len = ALIGN(len, 2) + 12;
292 	} else if (info->status.rates[0].idx >= 0) {
293 		if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS)
294 			len += 3;
295 		else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS)
296 			len = ALIGN(len, 2) + 12;
297 	}
298 
299 	return len;
300 }
301 
302 static void
ieee80211_add_tx_radiotap_header(struct ieee80211_local * local,struct ieee80211_supported_band * sband,struct sk_buff * skb,int retry_count,int rtap_len,int shift,struct ieee80211_tx_status * status)303 ieee80211_add_tx_radiotap_header(struct ieee80211_local *local,
304 				 struct ieee80211_supported_band *sband,
305 				 struct sk_buff *skb, int retry_count,
306 				 int rtap_len, int shift,
307 				 struct ieee80211_tx_status *status)
308 {
309 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
310 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
311 	struct ieee80211_radiotap_header *rthdr;
312 	unsigned char *pos;
313 	u16 legacy_rate = 0;
314 	u16 txflags;
315 
316 	rthdr = skb_push(skb, rtap_len);
317 
318 	memset(rthdr, 0, rtap_len);
319 	rthdr->it_len = cpu_to_le16(rtap_len);
320 	rthdr->it_present =
321 		cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
322 			    (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
323 	pos = (unsigned char *)(rthdr + 1);
324 
325 	/*
326 	 * XXX: Once radiotap gets the bitmap reset thing the vendor
327 	 *	extensions proposal contains, we can actually report
328 	 *	the whole set of tries we did.
329 	 */
330 
331 	/* IEEE80211_RADIOTAP_RATE */
332 
333 	if (status && status->rate) {
334 		if (!(status->rate->flags & (RATE_INFO_FLAGS_MCS |
335 					     RATE_INFO_FLAGS_DMG |
336 					     RATE_INFO_FLAGS_EDMG |
337 					     RATE_INFO_FLAGS_VHT_MCS |
338 					     RATE_INFO_FLAGS_HE_MCS)))
339 			legacy_rate = status->rate->legacy;
340 	} else if (info->status.rates[0].idx >= 0 &&
341 		 !(info->status.rates[0].flags & (IEEE80211_TX_RC_MCS |
342 						  IEEE80211_TX_RC_VHT_MCS)))
343 		legacy_rate =
344 			sband->bitrates[info->status.rates[0].idx].bitrate;
345 
346 	if (legacy_rate) {
347 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
348 		*pos = DIV_ROUND_UP(legacy_rate, 5 * (1 << shift));
349 		/* padding for tx flags */
350 		pos += 2;
351 	}
352 
353 	/* IEEE80211_RADIOTAP_TX_FLAGS */
354 	txflags = 0;
355 	if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
356 	    !is_multicast_ether_addr(hdr->addr1))
357 		txflags |= IEEE80211_RADIOTAP_F_TX_FAIL;
358 
359 	if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
360 		txflags |= IEEE80211_RADIOTAP_F_TX_CTS;
361 	if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
362 		txflags |= IEEE80211_RADIOTAP_F_TX_RTS;
363 
364 	put_unaligned_le16(txflags, pos);
365 	pos += 2;
366 
367 	/* IEEE80211_RADIOTAP_DATA_RETRIES */
368 	/* for now report the total retry_count */
369 	*pos = retry_count;
370 	pos++;
371 
372 	if (status && status->rate &&
373 	    (status->rate->flags & RATE_INFO_FLAGS_MCS)) {
374 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
375 		pos[0] = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
376 			 IEEE80211_RADIOTAP_MCS_HAVE_GI |
377 			 IEEE80211_RADIOTAP_MCS_HAVE_BW;
378 		if (status->rate->flags & RATE_INFO_FLAGS_SHORT_GI)
379 			pos[1] |= IEEE80211_RADIOTAP_MCS_SGI;
380 		if (status->rate->bw == RATE_INFO_BW_40)
381 			pos[1] |= IEEE80211_RADIOTAP_MCS_BW_40;
382 		pos[2] = status->rate->mcs;
383 		pos += 3;
384 	} else if (status && status->rate &&
385 		   (status->rate->flags & RATE_INFO_FLAGS_VHT_MCS)) {
386 		u16 known = local->hw.radiotap_vht_details &
387 			(IEEE80211_RADIOTAP_VHT_KNOWN_GI |
388 			 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH);
389 
390 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
391 
392 		/* required alignment from rthdr */
393 		pos = (u8 *)rthdr + ALIGN(pos - (u8 *)rthdr, 2);
394 
395 		/* u16 known - IEEE80211_RADIOTAP_VHT_KNOWN_* */
396 		put_unaligned_le16(known, pos);
397 		pos += 2;
398 
399 		/* u8 flags - IEEE80211_RADIOTAP_VHT_FLAG_* */
400 		if (status->rate->flags & RATE_INFO_FLAGS_SHORT_GI)
401 			*pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
402 		pos++;
403 
404 		/* u8 bandwidth */
405 		switch (status->rate->bw) {
406 		case RATE_INFO_BW_160:
407 			*pos = 11;
408 			break;
409 		case RATE_INFO_BW_80:
410 			*pos = 4;
411 			break;
412 		case RATE_INFO_BW_40:
413 			*pos = 1;
414 			break;
415 		default:
416 			*pos = 0;
417 			break;
418 		}
419 		pos++;
420 
421 		/* u8 mcs_nss[4] */
422 		*pos = (status->rate->mcs << 4) | status->rate->nss;
423 		pos += 4;
424 
425 		/* u8 coding */
426 		pos++;
427 		/* u8 group_id */
428 		pos++;
429 		/* u16 partial_aid */
430 		pos += 2;
431 	} else if (status && status->rate &&
432 		   (status->rate->flags & RATE_INFO_FLAGS_HE_MCS)) {
433 		struct ieee80211_radiotap_he *he;
434 
435 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
436 
437 		/* required alignment from rthdr */
438 		pos = (u8 *)rthdr + ALIGN(pos - (u8 *)rthdr, 2);
439 		he = (struct ieee80211_radiotap_he *)pos;
440 
441 		he->data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_SU |
442 					IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
443 					IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
444 					IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
445 
446 		he->data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN);
447 
448 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
449 
450 		he->data6 |= HE_PREP(DATA6_NSTS, status->rate->nss);
451 
452 #define CHECK_GI(s) \
453 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
454 	(int)NL80211_RATE_INFO_HE_GI_##s)
455 
456 		CHECK_GI(0_8);
457 		CHECK_GI(1_6);
458 		CHECK_GI(3_2);
459 
460 		he->data3 |= HE_PREP(DATA3_DATA_MCS, status->rate->mcs);
461 		he->data3 |= HE_PREP(DATA3_DATA_DCM, status->rate->he_dcm);
462 
463 		he->data5 |= HE_PREP(DATA5_GI, status->rate->he_gi);
464 
465 		switch (status->rate->bw) {
466 		case RATE_INFO_BW_20:
467 			he->data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
468 					     IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
469 			break;
470 		case RATE_INFO_BW_40:
471 			he->data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
472 					     IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
473 			break;
474 		case RATE_INFO_BW_80:
475 			he->data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
476 					     IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
477 			break;
478 		case RATE_INFO_BW_160:
479 			he->data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
480 					     IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
481 			break;
482 		case RATE_INFO_BW_HE_RU:
483 #define CHECK_RU_ALLOC(s) \
484 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
485 	NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
486 
487 			CHECK_RU_ALLOC(26);
488 			CHECK_RU_ALLOC(52);
489 			CHECK_RU_ALLOC(106);
490 			CHECK_RU_ALLOC(242);
491 			CHECK_RU_ALLOC(484);
492 			CHECK_RU_ALLOC(996);
493 			CHECK_RU_ALLOC(2x996);
494 
495 			he->data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
496 					     status->rate->he_ru_alloc + 4);
497 			break;
498 		default:
499 			WARN_ONCE(1, "Invalid SU BW %d\n", status->rate->bw);
500 		}
501 
502 		pos += sizeof(struct ieee80211_radiotap_he);
503 	}
504 
505 	if ((status && status->rate) || info->status.rates[0].idx < 0)
506 		return;
507 
508 	/* IEEE80211_RADIOTAP_MCS
509 	 * IEEE80211_RADIOTAP_VHT */
510 	if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
511 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
512 		pos[0] = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
513 			 IEEE80211_RADIOTAP_MCS_HAVE_GI |
514 			 IEEE80211_RADIOTAP_MCS_HAVE_BW;
515 		if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
516 			pos[1] |= IEEE80211_RADIOTAP_MCS_SGI;
517 		if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
518 			pos[1] |= IEEE80211_RADIOTAP_MCS_BW_40;
519 		if (info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)
520 			pos[1] |= IEEE80211_RADIOTAP_MCS_FMT_GF;
521 		pos[2] = info->status.rates[0].idx;
522 		pos += 3;
523 	} else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
524 		u16 known = local->hw.radiotap_vht_details &
525 			(IEEE80211_RADIOTAP_VHT_KNOWN_GI |
526 			 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH);
527 
528 		rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
529 
530 		/* required alignment from rthdr */
531 		pos = (u8 *)rthdr + ALIGN(pos - (u8 *)rthdr, 2);
532 
533 		/* u16 known - IEEE80211_RADIOTAP_VHT_KNOWN_* */
534 		put_unaligned_le16(known, pos);
535 		pos += 2;
536 
537 		/* u8 flags - IEEE80211_RADIOTAP_VHT_FLAG_* */
538 		if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
539 			*pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
540 		pos++;
541 
542 		/* u8 bandwidth */
543 		if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
544 			*pos = 1;
545 		else if (info->status.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
546 			*pos = 4;
547 		else if (info->status.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
548 			*pos = 11;
549 		else /* IEEE80211_TX_RC_{20_MHZ_WIDTH,FIXME:DUP_DATA} */
550 			*pos = 0;
551 		pos++;
552 
553 		/* u8 mcs_nss[4] */
554 		*pos = (ieee80211_rate_get_vht_mcs(&info->status.rates[0]) << 4) |
555 			ieee80211_rate_get_vht_nss(&info->status.rates[0]);
556 		pos += 4;
557 
558 		/* u8 coding */
559 		pos++;
560 		/* u8 group_id */
561 		pos++;
562 		/* u16 partial_aid */
563 		pos += 2;
564 	}
565 }
566 
567 /*
568  * Handles the tx for TDLS teardown frames.
569  * If the frame wasn't ACKed by the peer - it will be re-sent through the AP
570  */
ieee80211_tdls_td_tx_handle(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,u32 flags)571 static void ieee80211_tdls_td_tx_handle(struct ieee80211_local *local,
572 					struct ieee80211_sub_if_data *sdata,
573 					struct sk_buff *skb, u32 flags)
574 {
575 	struct sk_buff *teardown_skb;
576 	struct sk_buff *orig_teardown_skb;
577 	bool is_teardown = false;
578 
579 	/* Get the teardown data we need and free the lock */
580 	spin_lock(&sdata->u.mgd.teardown_lock);
581 	teardown_skb = sdata->u.mgd.teardown_skb;
582 	orig_teardown_skb = sdata->u.mgd.orig_teardown_skb;
583 	if ((skb == orig_teardown_skb) && teardown_skb) {
584 		sdata->u.mgd.teardown_skb = NULL;
585 		sdata->u.mgd.orig_teardown_skb = NULL;
586 		is_teardown = true;
587 	}
588 	spin_unlock(&sdata->u.mgd.teardown_lock);
589 
590 	if (is_teardown) {
591 		/* This mechanism relies on being able to get ACKs */
592 		WARN_ON(!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS));
593 
594 		/* Check if peer has ACKed */
595 		if (flags & IEEE80211_TX_STAT_ACK) {
596 			dev_kfree_skb_any(teardown_skb);
597 		} else {
598 			tdls_dbg(sdata,
599 				 "TDLS Resending teardown through AP\n");
600 
601 			ieee80211_subif_start_xmit(teardown_skb, skb->dev);
602 		}
603 	}
604 }
605 
606 static struct ieee80211_sub_if_data *
ieee80211_sdata_from_skb(struct ieee80211_local * local,struct sk_buff * skb)607 ieee80211_sdata_from_skb(struct ieee80211_local *local, struct sk_buff *skb)
608 {
609 	struct ieee80211_sub_if_data *sdata;
610 
611 	if (skb->dev) {
612 		list_for_each_entry_rcu(sdata, &local->interfaces, list) {
613 			if (!sdata->dev)
614 				continue;
615 
616 			if (skb->dev == sdata->dev)
617 				return sdata;
618 		}
619 
620 		return NULL;
621 	}
622 
623 	return rcu_dereference(local->p2p_sdata);
624 }
625 
ieee80211_report_ack_skb(struct ieee80211_local * local,struct ieee80211_tx_info * info,bool acked,bool dropped)626 static void ieee80211_report_ack_skb(struct ieee80211_local *local,
627 				     struct ieee80211_tx_info *info,
628 				     bool acked, bool dropped)
629 {
630 	struct sk_buff *skb;
631 	unsigned long flags;
632 
633 	spin_lock_irqsave(&local->ack_status_lock, flags);
634 	skb = idr_remove(&local->ack_status_frames, info->ack_frame_id);
635 	spin_unlock_irqrestore(&local->ack_status_lock, flags);
636 
637 	if (!skb)
638 		return;
639 
640 	if (info->flags & IEEE80211_TX_INTFL_NL80211_FRAME_TX) {
641 		u64 cookie = IEEE80211_SKB_CB(skb)->ack.cookie;
642 		struct ieee80211_sub_if_data *sdata;
643 		struct ieee80211_hdr *hdr = (void *)skb->data;
644 
645 		rcu_read_lock();
646 		sdata = ieee80211_sdata_from_skb(local, skb);
647 		if (sdata) {
648 			if (ieee80211_is_any_nullfunc(hdr->frame_control))
649 				cfg80211_probe_status(sdata->dev, hdr->addr1,
650 						      cookie, acked,
651 						      info->status.ack_signal,
652 						      info->status.is_valid_ack_signal,
653 						      GFP_ATOMIC);
654 			else
655 				cfg80211_mgmt_tx_status(&sdata->wdev, cookie,
656 							skb->data, skb->len,
657 							acked, GFP_ATOMIC);
658 		}
659 		rcu_read_unlock();
660 
661 		dev_kfree_skb_any(skb);
662 	} else if (dropped) {
663 		dev_kfree_skb_any(skb);
664 	} else {
665 		/* consumes skb */
666 		skb_complete_wifi_ack(skb, acked);
667 	}
668 }
669 
ieee80211_report_used_skb(struct ieee80211_local * local,struct sk_buff * skb,bool dropped)670 static void ieee80211_report_used_skb(struct ieee80211_local *local,
671 				      struct sk_buff *skb, bool dropped)
672 {
673 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
674 	struct ieee80211_hdr *hdr = (void *)skb->data;
675 	bool acked = info->flags & IEEE80211_TX_STAT_ACK;
676 
677 	if (dropped)
678 		acked = false;
679 
680 	if (info->flags & IEEE80211_TX_INTFL_MLME_CONN_TX) {
681 		struct ieee80211_sub_if_data *sdata;
682 
683 		rcu_read_lock();
684 
685 		sdata = ieee80211_sdata_from_skb(local, skb);
686 
687 		if (!sdata) {
688 			skb->dev = NULL;
689 		} else {
690 			unsigned int hdr_size =
691 				ieee80211_hdrlen(hdr->frame_control);
692 
693 			/* Check to see if packet is a TDLS teardown packet */
694 			if (ieee80211_is_data(hdr->frame_control) &&
695 			    (ieee80211_get_tdls_action(skb, hdr_size) ==
696 			     WLAN_TDLS_TEARDOWN))
697 				ieee80211_tdls_td_tx_handle(local, sdata, skb,
698 							    info->flags);
699 			else
700 				ieee80211_mgd_conn_tx_status(sdata,
701 							     hdr->frame_control,
702 							     acked);
703 		}
704 
705 		rcu_read_unlock();
706 	} else if (info->ack_frame_id) {
707 		ieee80211_report_ack_skb(local, info, acked, dropped);
708 	}
709 
710 	if (!dropped && skb->destructor) {
711 		skb->wifi_acked_valid = 1;
712 		skb->wifi_acked = acked;
713 	}
714 
715 	ieee80211_led_tx(local);
716 
717 	if (skb_has_frag_list(skb)) {
718 		kfree_skb_list(skb_shinfo(skb)->frag_list);
719 		skb_shinfo(skb)->frag_list = NULL;
720 	}
721 }
722 
723 /*
724  * Use a static threshold for now, best value to be determined
725  * by testing ...
726  * Should it depend on:
727  *  - on # of retransmissions
728  *  - current throughput (higher value for higher tpt)?
729  */
730 #define STA_LOST_PKT_THRESHOLD	50
731 #define STA_LOST_TDLS_PKT_THRESHOLD	10
732 #define STA_LOST_TDLS_PKT_TIME		(10*HZ) /* 10secs since last ACK */
733 
ieee80211_lost_packet(struct sta_info * sta,struct ieee80211_tx_info * info)734 static void ieee80211_lost_packet(struct sta_info *sta,
735 				  struct ieee80211_tx_info *info)
736 {
737 	/* If driver relies on its own algorithm for station kickout, skip
738 	 * mac80211 packet loss mechanism.
739 	 */
740 	if (ieee80211_hw_check(&sta->local->hw, REPORTS_LOW_ACK))
741 		return;
742 
743 	/* This packet was aggregated but doesn't carry status info */
744 	if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
745 	    !(info->flags & IEEE80211_TX_STAT_AMPDU))
746 		return;
747 
748 	sta->status_stats.lost_packets++;
749 	if (!sta->sta.tdls &&
750 	    sta->status_stats.lost_packets < STA_LOST_PKT_THRESHOLD)
751 		return;
752 
753 	/*
754 	 * If we're in TDLS mode, make sure that all STA_LOST_TDLS_PKT_THRESHOLD
755 	 * of the last packets were lost, and that no ACK was received in the
756 	 * last STA_LOST_TDLS_PKT_TIME ms, before triggering the CQM packet-loss
757 	 * mechanism.
758 	 */
759 	if (sta->sta.tdls &&
760 	    (sta->status_stats.lost_packets < STA_LOST_TDLS_PKT_THRESHOLD ||
761 	     time_before(jiffies,
762 			 sta->status_stats.last_tdls_pkt_time +
763 			 STA_LOST_TDLS_PKT_TIME)))
764 		return;
765 
766 	cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
767 				    sta->status_stats.lost_packets, GFP_ATOMIC);
768 	sta->status_stats.lost_packets = 0;
769 }
770 
ieee80211_tx_get_rates(struct ieee80211_hw * hw,struct ieee80211_tx_info * info,int * retry_count)771 static int ieee80211_tx_get_rates(struct ieee80211_hw *hw,
772 				  struct ieee80211_tx_info *info,
773 				  int *retry_count)
774 {
775 	int rates_idx = -1;
776 	int count = -1;
777 	int i;
778 
779 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
780 		if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
781 		    !(info->flags & IEEE80211_TX_STAT_AMPDU)) {
782 			/* just the first aggr frame carry status info */
783 			info->status.rates[i].idx = -1;
784 			info->status.rates[i].count = 0;
785 			break;
786 		} else if (info->status.rates[i].idx < 0) {
787 			break;
788 		} else if (i >= hw->max_report_rates) {
789 			/* the HW cannot have attempted that rate */
790 			info->status.rates[i].idx = -1;
791 			info->status.rates[i].count = 0;
792 			break;
793 		}
794 
795 		count += info->status.rates[i].count;
796 	}
797 	rates_idx = i - 1;
798 
799 	if (count < 0)
800 		count = 0;
801 
802 	*retry_count = count;
803 	return rates_idx;
804 }
805 
ieee80211_tx_monitor(struct ieee80211_local * local,struct sk_buff * skb,struct ieee80211_supported_band * sband,int retry_count,int shift,bool send_to_cooked,struct ieee80211_tx_status * status)806 void ieee80211_tx_monitor(struct ieee80211_local *local, struct sk_buff *skb,
807 			  struct ieee80211_supported_band *sband,
808 			  int retry_count, int shift, bool send_to_cooked,
809 			  struct ieee80211_tx_status *status)
810 {
811 	struct sk_buff *skb2;
812 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
813 	struct ieee80211_sub_if_data *sdata;
814 	struct net_device *prev_dev = NULL;
815 	int rtap_len;
816 
817 	/* send frame to monitor interfaces now */
818 	rtap_len = ieee80211_tx_radiotap_len(info, status);
819 	if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
820 		pr_err("ieee80211_tx_status: headroom too small\n");
821 		dev_kfree_skb(skb);
822 		return;
823 	}
824 	ieee80211_add_tx_radiotap_header(local, sband, skb, retry_count,
825 					 rtap_len, shift, status);
826 
827 	/* XXX: is this sufficient for BPF? */
828 	skb_reset_mac_header(skb);
829 	skb->ip_summed = CHECKSUM_UNNECESSARY;
830 	skb->pkt_type = PACKET_OTHERHOST;
831 	skb->protocol = htons(ETH_P_802_2);
832 	memset(skb->cb, 0, sizeof(skb->cb));
833 
834 	rcu_read_lock();
835 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
836 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
837 			if (!ieee80211_sdata_running(sdata))
838 				continue;
839 
840 			if ((sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) &&
841 			    !send_to_cooked)
842 				continue;
843 
844 			if (prev_dev) {
845 				skb2 = skb_clone(skb, GFP_ATOMIC);
846 				if (skb2) {
847 					skb2->dev = prev_dev;
848 #ifndef CONFIG_DRIVERS_HDF_XR829
849 					netif_rx(skb2);
850 #else
851 					wal_netif_rx(skb2);
852 #endif
853 				}
854 			}
855 
856 			prev_dev = sdata->dev;
857 		}
858 	}
859 	if (prev_dev) {
860 		skb->dev = prev_dev;
861 #ifndef CONFIG_DRIVERS_HDF_XR829
862 		netif_rx(skb);
863 #else
864 		wal_netif_rx(skb);
865 #endif
866 		skb = NULL;
867 	}
868 	rcu_read_unlock();
869 	dev_kfree_skb(skb);
870 }
871 
__ieee80211_tx_status(struct ieee80211_hw * hw,struct ieee80211_tx_status * status)872 static void __ieee80211_tx_status(struct ieee80211_hw *hw,
873 				  struct ieee80211_tx_status *status)
874 {
875 	struct sk_buff *skb = status->skb;
876 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
877 	struct ieee80211_local *local = hw_to_local(hw);
878 	struct ieee80211_tx_info *info = status->info;
879 	struct sta_info *sta;
880 	__le16 fc;
881 	struct ieee80211_supported_band *sband;
882 	int retry_count;
883 	int rates_idx;
884 	bool send_to_cooked;
885 	bool acked;
886 	struct ieee80211_bar *bar;
887 	int shift = 0;
888 	int tid = IEEE80211_NUM_TIDS;
889 
890 	rates_idx = ieee80211_tx_get_rates(hw, info, &retry_count);
891 
892 	sband = local->hw.wiphy->bands[info->band];
893 	fc = hdr->frame_control;
894 
895 	if (status->sta) {
896 		sta = container_of(status->sta, struct sta_info, sta);
897 		shift = ieee80211_vif_get_shift(&sta->sdata->vif);
898 
899 		if (info->flags & IEEE80211_TX_STATUS_EOSP)
900 			clear_sta_flag(sta, WLAN_STA_SP);
901 
902 		acked = !!(info->flags & IEEE80211_TX_STAT_ACK);
903 
904 		/* mesh Peer Service Period support */
905 		if (ieee80211_vif_is_mesh(&sta->sdata->vif) &&
906 		    ieee80211_is_data_qos(fc))
907 			ieee80211_mpsp_trigger_process(
908 				ieee80211_get_qos_ctl(hdr), sta, true, acked);
909 
910 		if (!acked && test_sta_flag(sta, WLAN_STA_PS_STA)) {
911 			/*
912 			 * The STA is in power save mode, so assume
913 			 * that this TX packet failed because of that.
914 			 */
915 			ieee80211_handle_filtered_frame(local, sta, skb);
916 			return;
917 		}
918 
919 		if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL) &&
920 		    (ieee80211_is_data(hdr->frame_control)) &&
921 		    (rates_idx != -1))
922 			sta->tx_stats.last_rate =
923 				info->status.rates[rates_idx];
924 
925 		if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
926 		    (ieee80211_is_data_qos(fc))) {
927 			u16 ssn;
928 			u8 *qc;
929 
930 			qc = ieee80211_get_qos_ctl(hdr);
931 			tid = qc[0] & 0xf;
932 			ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
933 						& IEEE80211_SCTL_SEQ);
934 			mac80211_send_bar(&sta->sdata->vif, hdr->addr1,
935 					   tid, ssn);
936 		} else if (ieee80211_is_data_qos(fc)) {
937 			u8 *qc = ieee80211_get_qos_ctl(hdr);
938 
939 			tid = qc[0] & 0xf;
940 		}
941 
942 		if (!acked && ieee80211_is_back_req(fc)) {
943 			u16 control;
944 
945 			/*
946 			 * BAR failed, store the last SSN and retry sending
947 			 * the BAR when the next unicast transmission on the
948 			 * same TID succeeds.
949 			 */
950 			bar = (struct ieee80211_bar *) skb->data;
951 			control = le16_to_cpu(bar->control);
952 			if (!(control & IEEE80211_BAR_CTRL_MULTI_TID)) {
953 				u16 ssn = le16_to_cpu(bar->start_seq_num);
954 
955 				tid = (control &
956 				       IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
957 				      IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
958 
959 				ieee80211_set_bar_pending(sta, tid, ssn);
960 			}
961 		}
962 
963 		if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
964 			ieee80211_handle_filtered_frame(local, sta, skb);
965 			return;
966 		} else {
967 			if (!acked)
968 				sta->status_stats.retry_failed++;
969 			sta->status_stats.retry_count += retry_count;
970 
971 			if (ieee80211_is_data_present(fc)) {
972 				if (!acked)
973 					sta->status_stats.msdu_failed[tid]++;
974 
975 				sta->status_stats.msdu_retries[tid] +=
976 					retry_count;
977 			}
978 		}
979 
980 		rate_control_tx_status(local, sband, status);
981 		if (ieee80211_vif_is_mesh(&sta->sdata->vif))
982 			ieee80211s_update_metric(local, sta, status);
983 
984 		if (!(info->flags & IEEE80211_TX_CTL_INJECTED) && acked)
985 			ieee80211_frame_acked(sta, skb);
986 
987 		if ((sta->sdata->vif.type == NL80211_IFTYPE_STATION) &&
988 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
989 			ieee80211_sta_tx_notify(sta->sdata, (void *) skb->data,
990 						acked, info->status.tx_time);
991 
992 		if (info->status.tx_time &&
993 		    wiphy_ext_feature_isset(local->hw.wiphy,
994 					    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
995 			mac80211_sta_register_airtime(&sta->sta, tid,
996 						       info->status.tx_time, 0);
997 
998 		if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
999 			if (info->flags & IEEE80211_TX_STAT_ACK) {
1000 				if (sta->status_stats.lost_packets)
1001 					sta->status_stats.lost_packets = 0;
1002 
1003 				/* Track when last TDLS packet was ACKed */
1004 				if (test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
1005 					sta->status_stats.last_tdls_pkt_time =
1006 						jiffies;
1007 			} else {
1008 				ieee80211_lost_packet(sta, info);
1009 			}
1010 		}
1011 	}
1012 
1013 	/* SNMP counters
1014 	 * Fragments are passed to low-level drivers as separate skbs, so these
1015 	 * are actually fragments, not frames. Update frame counters only for
1016 	 * the first fragment of the frame. */
1017 	if ((info->flags & IEEE80211_TX_STAT_ACK) ||
1018 	    (info->flags & IEEE80211_TX_STAT_NOACK_TRANSMITTED)) {
1019 		if (ieee80211_is_first_frag(hdr->seq_ctrl)) {
1020 			I802_DEBUG_INC(local->dot11TransmittedFrameCount);
1021 			if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
1022 				I802_DEBUG_INC(local->dot11MulticastTransmittedFrameCount);
1023 			if (retry_count > 0)
1024 				I802_DEBUG_INC(local->dot11RetryCount);
1025 			if (retry_count > 1)
1026 				I802_DEBUG_INC(local->dot11MultipleRetryCount);
1027 		}
1028 
1029 		/* This counter shall be incremented for an acknowledged MPDU
1030 		 * with an individual address in the address 1 field or an MPDU
1031 		 * with a multicast address in the address 1 field of type Data
1032 		 * or Management. */
1033 		if (!is_multicast_ether_addr(hdr->addr1) ||
1034 		    ieee80211_is_data(fc) ||
1035 		    ieee80211_is_mgmt(fc))
1036 			I802_DEBUG_INC(local->dot11TransmittedFragmentCount);
1037 	} else {
1038 		if (ieee80211_is_first_frag(hdr->seq_ctrl))
1039 			I802_DEBUG_INC(local->dot11FailedCount);
1040 	}
1041 
1042 	if (ieee80211_is_any_nullfunc(fc) &&
1043 	    ieee80211_has_pm(fc) &&
1044 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1045 	    !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
1046 	    local->ps_sdata && !(local->scanning)) {
1047 		if (info->flags & IEEE80211_TX_STAT_ACK) {
1048 			local->ps_sdata->u.mgd.flags |=
1049 					IEEE80211_STA_NULLFUNC_ACKED;
1050 		} else
1051 			mod_timer(&local->dynamic_ps_timer, jiffies +
1052 					msecs_to_jiffies(10));
1053 	}
1054 
1055 	ieee80211_report_used_skb(local, skb, false);
1056 
1057 	/* this was a transmitted frame, but now we want to reuse it */
1058 	skb_orphan(skb);
1059 
1060 	/* Need to make a copy before skb->cb gets cleared */
1061 	send_to_cooked = !!(info->flags & IEEE80211_TX_CTL_INJECTED) ||
1062 			 !(ieee80211_is_data(fc));
1063 
1064 	/*
1065 	 * This is a bit racy but we can avoid a lot of work
1066 	 * with this test...
1067 	 */
1068 	if (!local->monitors && (!send_to_cooked || !local->cooked_mntrs)) {
1069 		dev_kfree_skb(skb);
1070 		return;
1071 	}
1072 
1073 	/* send to monitor interfaces */
1074 	ieee80211_tx_monitor(local, skb, sband, retry_count, shift,
1075 			     send_to_cooked, status);
1076 }
1077 
mac80211_tx_status(struct ieee80211_hw * hw,struct sk_buff * skb)1078 void mac80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
1079 {
1080 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1081 	struct ieee80211_local *local = hw_to_local(hw);
1082 	struct ieee80211_tx_status status = {
1083 		.skb = skb,
1084 		.info = IEEE80211_SKB_CB(skb),
1085 	};
1086 	struct rhlist_head *tmp;
1087 	struct sta_info *sta;
1088 
1089 	rcu_read_lock();
1090 
1091 	for_each_sta_info(local, hdr->addr1, sta, tmp) {
1092 		/* skip wrong virtual interface */
1093 		if (!ether_addr_equal(hdr->addr2, sta->sdata->vif.addr))
1094 			continue;
1095 
1096 		status.sta = &sta->sta;
1097 		break;
1098 	}
1099 
1100 	__ieee80211_tx_status(hw, &status);
1101 	rcu_read_unlock();
1102 }
1103 
mac80211_tx_status_ext(struct ieee80211_hw * hw,struct ieee80211_tx_status * status)1104 void mac80211_tx_status_ext(struct ieee80211_hw *hw,
1105 			     struct ieee80211_tx_status *status)
1106 {
1107 	struct ieee80211_local *local = hw_to_local(hw);
1108 	struct ieee80211_tx_info *info = status->info;
1109 	struct ieee80211_sta *pubsta = status->sta;
1110 	struct ieee80211_supported_band *sband;
1111 	int retry_count;
1112 	bool acked, noack_success;
1113 
1114 	if (status->skb)
1115 		return __ieee80211_tx_status(hw, status);
1116 
1117 	if (!status->sta)
1118 		return;
1119 
1120 	ieee80211_tx_get_rates(hw, info, &retry_count);
1121 
1122 	sband = hw->wiphy->bands[info->band];
1123 
1124 	acked = !!(info->flags & IEEE80211_TX_STAT_ACK);
1125 	noack_success = !!(info->flags & IEEE80211_TX_STAT_NOACK_TRANSMITTED);
1126 
1127 	if (pubsta) {
1128 		struct sta_info *sta;
1129 
1130 		sta = container_of(pubsta, struct sta_info, sta);
1131 
1132 		if (!acked)
1133 			sta->status_stats.retry_failed++;
1134 		sta->status_stats.retry_count += retry_count;
1135 
1136 		if (acked) {
1137 			sta->status_stats.last_ack = jiffies;
1138 
1139 			if (sta->status_stats.lost_packets)
1140 				sta->status_stats.lost_packets = 0;
1141 
1142 			/* Track when last TDLS packet was ACKed */
1143 			if (test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH))
1144 				sta->status_stats.last_tdls_pkt_time = jiffies;
1145 		} else if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1146 			return;
1147 		} else {
1148 			ieee80211_lost_packet(sta, info);
1149 		}
1150 
1151 		rate_control_tx_status(local, sband, status);
1152 		if (ieee80211_vif_is_mesh(&sta->sdata->vif))
1153 			ieee80211s_update_metric(local, sta, status);
1154 	}
1155 
1156 	if (acked || noack_success) {
1157 		I802_DEBUG_INC(local->dot11TransmittedFrameCount);
1158 		if (!pubsta)
1159 			I802_DEBUG_INC(local->dot11MulticastTransmittedFrameCount);
1160 		if (retry_count > 0)
1161 			I802_DEBUG_INC(local->dot11RetryCount);
1162 		if (retry_count > 1)
1163 			I802_DEBUG_INC(local->dot11MultipleRetryCount);
1164 	} else {
1165 		I802_DEBUG_INC(local->dot11FailedCount);
1166 	}
1167 }
1168 
mac80211_tx_rate_update(struct ieee80211_hw * hw,struct ieee80211_sta * pubsta,struct ieee80211_tx_info * info)1169 void mac80211_tx_rate_update(struct ieee80211_hw *hw,
1170 			      struct ieee80211_sta *pubsta,
1171 			      struct ieee80211_tx_info *info)
1172 {
1173 	struct ieee80211_local *local = hw_to_local(hw);
1174 	struct ieee80211_supported_band *sband = hw->wiphy->bands[info->band];
1175 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1176 	struct ieee80211_tx_status status = {
1177 		.info = info,
1178 		.sta = pubsta,
1179 	};
1180 
1181 	rate_control_tx_status(local, sband, &status);
1182 
1183 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
1184 		sta->tx_stats.last_rate = info->status.rates[0];
1185 }
1186 
mac80211_report_low_ack(struct ieee80211_sta * pubsta,u32 num_packets)1187 void mac80211_report_low_ack(struct ieee80211_sta *pubsta, u32 num_packets)
1188 {
1189 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1190 	cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
1191 				    num_packets, GFP_ATOMIC);
1192 }
1193 
mac80211_free_txskb(struct ieee80211_hw * hw,struct sk_buff * skb)1194 void mac80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb)
1195 {
1196 	struct ieee80211_local *local = hw_to_local(hw);
1197 
1198 	ieee80211_report_used_skb(local, skb, true);
1199 	dev_kfree_skb_any(skb);
1200 }
1201 
ieee80211_purge_tx_queue(struct ieee80211_hw * hw,struct sk_buff_head * skbs)1202 void ieee80211_purge_tx_queue(struct ieee80211_hw *hw,
1203 			      struct sk_buff_head *skbs)
1204 {
1205 	struct sk_buff *skb;
1206 
1207 	while ((skb = __skb_dequeue(skbs)))
1208 		mac80211_free_txskb(hw, skb);
1209 }
1210