xref: /linux/net/mac80211/tx.c (revision dd9a41bc61cc62d38306465ed62373b98df0049e)
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 2007	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright (C) 2018-2020 Intel Corporation
9  *
10  * Transmit and frame generation functions.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/if_vlan.h>
17 #include <linux/etherdevice.h>
18 #include <linux/bitmap.h>
19 #include <linux/rcupdate.h>
20 #include <linux/export.h>
21 #include <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <net/codel.h>
26 #include <net/codel_impl.h>
27 #include <asm/unaligned.h>
28 #include <net/fq_impl.h>
29 
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "led.h"
33 #include "mesh.h"
34 #include "wep.h"
35 #include "wpa.h"
36 #include "wme.h"
37 #include "rate.h"
38 
39 /* misc utils */
40 
41 static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
42 {
43 	struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
44 
45 	u64_stats_update_begin(&tstats->syncp);
46 	tstats->tx_packets++;
47 	tstats->tx_bytes += len;
48 	u64_stats_update_end(&tstats->syncp);
49 }
50 
51 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
52 				 struct sk_buff *skb, int group_addr,
53 				 int next_frag_len)
54 {
55 	int rate, mrate, erp, dur, i, shift = 0;
56 	struct ieee80211_rate *txrate;
57 	struct ieee80211_local *local = tx->local;
58 	struct ieee80211_supported_band *sband;
59 	struct ieee80211_hdr *hdr;
60 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
61 	struct ieee80211_chanctx_conf *chanctx_conf;
62 	u32 rate_flags = 0;
63 
64 	/* assume HW handles this */
65 	if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
66 		return 0;
67 
68 	rcu_read_lock();
69 	chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
70 	if (chanctx_conf) {
71 		shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
72 		rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
73 	}
74 	rcu_read_unlock();
75 
76 	/* uh huh? */
77 	if (WARN_ON_ONCE(tx->rate.idx < 0))
78 		return 0;
79 
80 	sband = local->hw.wiphy->bands[info->band];
81 	txrate = &sband->bitrates[tx->rate.idx];
82 
83 	erp = txrate->flags & IEEE80211_RATE_ERP_G;
84 
85 	/*
86 	 * data and mgmt (except PS Poll):
87 	 * - during CFP: 32768
88 	 * - during contention period:
89 	 *   if addr1 is group address: 0
90 	 *   if more fragments = 0 and addr1 is individual address: time to
91 	 *      transmit one ACK plus SIFS
92 	 *   if more fragments = 1 and addr1 is individual address: time to
93 	 *      transmit next fragment plus 2 x ACK plus 3 x SIFS
94 	 *
95 	 * IEEE 802.11, 9.6:
96 	 * - control response frame (CTS or ACK) shall be transmitted using the
97 	 *   same rate as the immediately previous frame in the frame exchange
98 	 *   sequence, if this rate belongs to the PHY mandatory rates, or else
99 	 *   at the highest possible rate belonging to the PHY rates in the
100 	 *   BSSBasicRateSet
101 	 */
102 	hdr = (struct ieee80211_hdr *)skb->data;
103 	if (ieee80211_is_ctl(hdr->frame_control)) {
104 		/* TODO: These control frames are not currently sent by
105 		 * mac80211, but should they be implemented, this function
106 		 * needs to be updated to support duration field calculation.
107 		 *
108 		 * RTS: time needed to transmit pending data/mgmt frame plus
109 		 *    one CTS frame plus one ACK frame plus 3 x SIFS
110 		 * CTS: duration of immediately previous RTS minus time
111 		 *    required to transmit CTS and its SIFS
112 		 * ACK: 0 if immediately previous directed data/mgmt had
113 		 *    more=0, with more=1 duration in ACK frame is duration
114 		 *    from previous frame minus time needed to transmit ACK
115 		 *    and its SIFS
116 		 * PS Poll: BIT(15) | BIT(14) | aid
117 		 */
118 		return 0;
119 	}
120 
121 	/* data/mgmt */
122 	if (0 /* FIX: data/mgmt during CFP */)
123 		return cpu_to_le16(32768);
124 
125 	if (group_addr) /* Group address as the destination - no ACK */
126 		return 0;
127 
128 	/* Individual destination address:
129 	 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
130 	 * CTS and ACK frames shall be transmitted using the highest rate in
131 	 * basic rate set that is less than or equal to the rate of the
132 	 * immediately previous frame and that is using the same modulation
133 	 * (CCK or OFDM). If no basic rate set matches with these requirements,
134 	 * the highest mandatory rate of the PHY that is less than or equal to
135 	 * the rate of the previous frame is used.
136 	 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
137 	 */
138 	rate = -1;
139 	/* use lowest available if everything fails */
140 	mrate = sband->bitrates[0].bitrate;
141 	for (i = 0; i < sband->n_bitrates; i++) {
142 		struct ieee80211_rate *r = &sband->bitrates[i];
143 
144 		if (r->bitrate > txrate->bitrate)
145 			break;
146 
147 		if ((rate_flags & r->flags) != rate_flags)
148 			continue;
149 
150 		if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
151 			rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
152 
153 		switch (sband->band) {
154 		case NL80211_BAND_2GHZ: {
155 			u32 flag;
156 			if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
157 				flag = IEEE80211_RATE_MANDATORY_G;
158 			else
159 				flag = IEEE80211_RATE_MANDATORY_B;
160 			if (r->flags & flag)
161 				mrate = r->bitrate;
162 			break;
163 		}
164 		case NL80211_BAND_5GHZ:
165 		case NL80211_BAND_6GHZ:
166 			if (r->flags & IEEE80211_RATE_MANDATORY_A)
167 				mrate = r->bitrate;
168 			break;
169 		case NL80211_BAND_60GHZ:
170 			/* TODO, for now fall through */
171 		case NUM_NL80211_BANDS:
172 			WARN_ON(1);
173 			break;
174 		}
175 	}
176 	if (rate == -1) {
177 		/* No matching basic rate found; use highest suitable mandatory
178 		 * PHY rate */
179 		rate = DIV_ROUND_UP(mrate, 1 << shift);
180 	}
181 
182 	/* Don't calculate ACKs for QoS Frames with NoAck Policy set */
183 	if (ieee80211_is_data_qos(hdr->frame_control) &&
184 	    *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
185 		dur = 0;
186 	else
187 		/* Time needed to transmit ACK
188 		 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
189 		 * to closest integer */
190 		dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
191 				tx->sdata->vif.bss_conf.use_short_preamble,
192 				shift);
193 
194 	if (next_frag_len) {
195 		/* Frame is fragmented: duration increases with time needed to
196 		 * transmit next fragment plus ACK and 2 x SIFS. */
197 		dur *= 2; /* ACK + SIFS */
198 		/* next fragment */
199 		dur += ieee80211_frame_duration(sband->band, next_frag_len,
200 				txrate->bitrate, erp,
201 				tx->sdata->vif.bss_conf.use_short_preamble,
202 				shift);
203 	}
204 
205 	return cpu_to_le16(dur);
206 }
207 
208 /* tx handlers */
209 static ieee80211_tx_result debug_noinline
210 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
211 {
212 	struct ieee80211_local *local = tx->local;
213 	struct ieee80211_if_managed *ifmgd;
214 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
215 
216 	/* driver doesn't support power save */
217 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
218 		return TX_CONTINUE;
219 
220 	/* hardware does dynamic power save */
221 	if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
222 		return TX_CONTINUE;
223 
224 	/* dynamic power save disabled */
225 	if (local->hw.conf.dynamic_ps_timeout <= 0)
226 		return TX_CONTINUE;
227 
228 	/* we are scanning, don't enable power save */
229 	if (local->scanning)
230 		return TX_CONTINUE;
231 
232 	if (!local->ps_sdata)
233 		return TX_CONTINUE;
234 
235 	/* No point if we're going to suspend */
236 	if (local->quiescing)
237 		return TX_CONTINUE;
238 
239 	/* dynamic ps is supported only in managed mode */
240 	if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
241 		return TX_CONTINUE;
242 
243 	if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
244 		return TX_CONTINUE;
245 
246 	ifmgd = &tx->sdata->u.mgd;
247 
248 	/*
249 	 * Don't wakeup from power save if u-apsd is enabled, voip ac has
250 	 * u-apsd enabled and the frame is in voip class. This effectively
251 	 * means that even if all access categories have u-apsd enabled, in
252 	 * practise u-apsd is only used with the voip ac. This is a
253 	 * workaround for the case when received voip class packets do not
254 	 * have correct qos tag for some reason, due the network or the
255 	 * peer application.
256 	 *
257 	 * Note: ifmgd->uapsd_queues access is racy here. If the value is
258 	 * changed via debugfs, user needs to reassociate manually to have
259 	 * everything in sync.
260 	 */
261 	if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
262 	    (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
263 	    skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
264 		return TX_CONTINUE;
265 
266 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
267 		ieee80211_stop_queues_by_reason(&local->hw,
268 						IEEE80211_MAX_QUEUE_MAP,
269 						IEEE80211_QUEUE_STOP_REASON_PS,
270 						false);
271 		ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
272 		ieee80211_queue_work(&local->hw,
273 				     &local->dynamic_ps_disable_work);
274 	}
275 
276 	/* Don't restart the timer if we're not disassociated */
277 	if (!ifmgd->associated)
278 		return TX_CONTINUE;
279 
280 	mod_timer(&local->dynamic_ps_timer, jiffies +
281 		  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
282 
283 	return TX_CONTINUE;
284 }
285 
286 static ieee80211_tx_result debug_noinline
287 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
288 {
289 
290 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
291 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
292 	bool assoc = false;
293 
294 	if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
295 		return TX_CONTINUE;
296 
297 	if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
298 	    test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
299 	    !ieee80211_is_probe_req(hdr->frame_control) &&
300 	    !ieee80211_is_any_nullfunc(hdr->frame_control))
301 		/*
302 		 * When software scanning only nullfunc frames (to notify
303 		 * the sleep state to the AP) and probe requests (for the
304 		 * active scan) are allowed, all other frames should not be
305 		 * sent and we should not get here, but if we do
306 		 * nonetheless, drop them to avoid sending them
307 		 * off-channel. See the link below and
308 		 * ieee80211_start_scan() for more.
309 		 *
310 		 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
311 		 */
312 		return TX_DROP;
313 
314 	if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
315 		return TX_CONTINUE;
316 
317 	if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
318 		return TX_CONTINUE;
319 
320 	if (tx->flags & IEEE80211_TX_PS_BUFFERED)
321 		return TX_CONTINUE;
322 
323 	if (tx->sta)
324 		assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
325 
326 	if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
327 		if (unlikely(!assoc &&
328 			     ieee80211_is_data(hdr->frame_control))) {
329 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
330 			sdata_info(tx->sdata,
331 				   "dropped data frame to not associated station %pM\n",
332 				   hdr->addr1);
333 #endif
334 			I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
335 			return TX_DROP;
336 		}
337 	} else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
338 			    ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
339 		/*
340 		 * No associated STAs - no need to send multicast
341 		 * frames.
342 		 */
343 		return TX_DROP;
344 	}
345 
346 	return TX_CONTINUE;
347 }
348 
349 /* This function is called whenever the AP is about to exceed the maximum limit
350  * of buffered frames for power saving STAs. This situation should not really
351  * happen often during normal operation, so dropping the oldest buffered packet
352  * from each queue should be OK to make some room for new frames. */
353 static void purge_old_ps_buffers(struct ieee80211_local *local)
354 {
355 	int total = 0, purged = 0;
356 	struct sk_buff *skb;
357 	struct ieee80211_sub_if_data *sdata;
358 	struct sta_info *sta;
359 
360 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
361 		struct ps_data *ps;
362 
363 		if (sdata->vif.type == NL80211_IFTYPE_AP)
364 			ps = &sdata->u.ap.ps;
365 		else if (ieee80211_vif_is_mesh(&sdata->vif))
366 			ps = &sdata->u.mesh.ps;
367 		else
368 			continue;
369 
370 		skb = skb_dequeue(&ps->bc_buf);
371 		if (skb) {
372 			purged++;
373 			ieee80211_free_txskb(&local->hw, skb);
374 		}
375 		total += skb_queue_len(&ps->bc_buf);
376 	}
377 
378 	/*
379 	 * Drop one frame from each station from the lowest-priority
380 	 * AC that has frames at all.
381 	 */
382 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
383 		int ac;
384 
385 		for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
386 			skb = skb_dequeue(&sta->ps_tx_buf[ac]);
387 			total += skb_queue_len(&sta->ps_tx_buf[ac]);
388 			if (skb) {
389 				purged++;
390 				ieee80211_free_txskb(&local->hw, skb);
391 				break;
392 			}
393 		}
394 	}
395 
396 	local->total_ps_buffered = total;
397 	ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
398 }
399 
400 static ieee80211_tx_result
401 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
402 {
403 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
404 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
405 	struct ps_data *ps;
406 
407 	/*
408 	 * broadcast/multicast frame
409 	 *
410 	 * If any of the associated/peer stations is in power save mode,
411 	 * the frame is buffered to be sent after DTIM beacon frame.
412 	 * This is done either by the hardware or us.
413 	 */
414 
415 	/* powersaving STAs currently only in AP/VLAN/mesh mode */
416 	if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
417 	    tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
418 		if (!tx->sdata->bss)
419 			return TX_CONTINUE;
420 
421 		ps = &tx->sdata->bss->ps;
422 	} else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
423 		ps = &tx->sdata->u.mesh.ps;
424 	} else {
425 		return TX_CONTINUE;
426 	}
427 
428 
429 	/* no buffering for ordered frames */
430 	if (ieee80211_has_order(hdr->frame_control))
431 		return TX_CONTINUE;
432 
433 	if (ieee80211_is_probe_req(hdr->frame_control))
434 		return TX_CONTINUE;
435 
436 	if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
437 		info->hw_queue = tx->sdata->vif.cab_queue;
438 
439 	/* no stations in PS mode and no buffered packets */
440 	if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
441 		return TX_CONTINUE;
442 
443 	info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
444 
445 	/* device releases frame after DTIM beacon */
446 	if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
447 		return TX_CONTINUE;
448 
449 	/* buffered in mac80211 */
450 	if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
451 		purge_old_ps_buffers(tx->local);
452 
453 	if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
454 		ps_dbg(tx->sdata,
455 		       "BC TX buffer full - dropping the oldest frame\n");
456 		ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
457 	} else
458 		tx->local->total_ps_buffered++;
459 
460 	skb_queue_tail(&ps->bc_buf, tx->skb);
461 
462 	return TX_QUEUED;
463 }
464 
465 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
466 			     struct sk_buff *skb)
467 {
468 	if (!ieee80211_is_mgmt(fc))
469 		return 0;
470 
471 	if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
472 		return 0;
473 
474 	if (!ieee80211_is_robust_mgmt_frame(skb))
475 		return 0;
476 
477 	return 1;
478 }
479 
480 static ieee80211_tx_result
481 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
482 {
483 	struct sta_info *sta = tx->sta;
484 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
485 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
486 	struct ieee80211_local *local = tx->local;
487 
488 	if (unlikely(!sta))
489 		return TX_CONTINUE;
490 
491 	if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
492 		      test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
493 		      test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
494 		     !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
495 		int ac = skb_get_queue_mapping(tx->skb);
496 
497 		if (ieee80211_is_mgmt(hdr->frame_control) &&
498 		    !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
499 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
500 			return TX_CONTINUE;
501 		}
502 
503 		ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
504 		       sta->sta.addr, sta->sta.aid, ac);
505 		if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
506 			purge_old_ps_buffers(tx->local);
507 
508 		/* sync with ieee80211_sta_ps_deliver_wakeup */
509 		spin_lock(&sta->ps_lock);
510 		/*
511 		 * STA woke up the meantime and all the frames on ps_tx_buf have
512 		 * been queued to pending queue. No reordering can happen, go
513 		 * ahead and Tx the packet.
514 		 */
515 		if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
516 		    !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
517 		    !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
518 			spin_unlock(&sta->ps_lock);
519 			return TX_CONTINUE;
520 		}
521 
522 		if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
523 			struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
524 			ps_dbg(tx->sdata,
525 			       "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
526 			       sta->sta.addr, ac);
527 			ieee80211_free_txskb(&local->hw, old);
528 		} else
529 			tx->local->total_ps_buffered++;
530 
531 		info->control.jiffies = jiffies;
532 		info->control.vif = &tx->sdata->vif;
533 		info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
534 		info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
535 		skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
536 		spin_unlock(&sta->ps_lock);
537 
538 		if (!timer_pending(&local->sta_cleanup))
539 			mod_timer(&local->sta_cleanup,
540 				  round_jiffies(jiffies +
541 						STA_INFO_CLEANUP_INTERVAL));
542 
543 		/*
544 		 * We queued up some frames, so the TIM bit might
545 		 * need to be set, recalculate it.
546 		 */
547 		sta_info_recalc_tim(sta);
548 
549 		return TX_QUEUED;
550 	} else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
551 		ps_dbg(tx->sdata,
552 		       "STA %pM in PS mode, but polling/in SP -> send frame\n",
553 		       sta->sta.addr);
554 	}
555 
556 	return TX_CONTINUE;
557 }
558 
559 static ieee80211_tx_result debug_noinline
560 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
561 {
562 	if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
563 		return TX_CONTINUE;
564 
565 	if (tx->flags & IEEE80211_TX_UNICAST)
566 		return ieee80211_tx_h_unicast_ps_buf(tx);
567 	else
568 		return ieee80211_tx_h_multicast_ps_buf(tx);
569 }
570 
571 static ieee80211_tx_result debug_noinline
572 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
573 {
574 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
575 
576 	if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
577 		if (tx->sdata->control_port_no_encrypt)
578 			info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
579 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
580 		info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
581 	}
582 
583 	return TX_CONTINUE;
584 }
585 
586 static ieee80211_tx_result debug_noinline
587 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
588 {
589 	struct ieee80211_key *key;
590 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
591 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
592 
593 	if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
594 		tx->key = NULL;
595 		return TX_CONTINUE;
596 	}
597 
598 	if (tx->sta &&
599 	    (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
600 		tx->key = key;
601 	else if (ieee80211_is_group_privacy_action(tx->skb) &&
602 		(key = rcu_dereference(tx->sdata->default_multicast_key)))
603 		tx->key = key;
604 	else if (ieee80211_is_mgmt(hdr->frame_control) &&
605 		 is_multicast_ether_addr(hdr->addr1) &&
606 		 ieee80211_is_robust_mgmt_frame(tx->skb) &&
607 		 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
608 		tx->key = key;
609 	else if (is_multicast_ether_addr(hdr->addr1) &&
610 		 (key = rcu_dereference(tx->sdata->default_multicast_key)))
611 		tx->key = key;
612 	else if (!is_multicast_ether_addr(hdr->addr1) &&
613 		 (key = rcu_dereference(tx->sdata->default_unicast_key)))
614 		tx->key = key;
615 	else
616 		tx->key = NULL;
617 
618 	if (tx->key) {
619 		bool skip_hw = false;
620 
621 		/* TODO: add threshold stuff again */
622 
623 		switch (tx->key->conf.cipher) {
624 		case WLAN_CIPHER_SUITE_WEP40:
625 		case WLAN_CIPHER_SUITE_WEP104:
626 		case WLAN_CIPHER_SUITE_TKIP:
627 			if (!ieee80211_is_data_present(hdr->frame_control))
628 				tx->key = NULL;
629 			break;
630 		case WLAN_CIPHER_SUITE_CCMP:
631 		case WLAN_CIPHER_SUITE_CCMP_256:
632 		case WLAN_CIPHER_SUITE_GCMP:
633 		case WLAN_CIPHER_SUITE_GCMP_256:
634 			if (!ieee80211_is_data_present(hdr->frame_control) &&
635 			    !ieee80211_use_mfp(hdr->frame_control, tx->sta,
636 					       tx->skb) &&
637 			    !ieee80211_is_group_privacy_action(tx->skb))
638 				tx->key = NULL;
639 			else
640 				skip_hw = (tx->key->conf.flags &
641 					   IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
642 					ieee80211_is_mgmt(hdr->frame_control);
643 			break;
644 		case WLAN_CIPHER_SUITE_AES_CMAC:
645 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
646 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
647 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
648 			if (!ieee80211_is_mgmt(hdr->frame_control))
649 				tx->key = NULL;
650 			break;
651 		}
652 
653 		if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
654 			     !ieee80211_is_deauth(hdr->frame_control)))
655 			return TX_DROP;
656 
657 		if (!skip_hw && tx->key &&
658 		    tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
659 			info->control.hw_key = &tx->key->conf;
660 	} else if (!ieee80211_is_mgmt(hdr->frame_control) && tx->sta &&
661 		   test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
662 		return TX_DROP;
663 	}
664 
665 	return TX_CONTINUE;
666 }
667 
668 static ieee80211_tx_result debug_noinline
669 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
670 {
671 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
672 	struct ieee80211_hdr *hdr = (void *)tx->skb->data;
673 	struct ieee80211_supported_band *sband;
674 	u32 len;
675 	struct ieee80211_tx_rate_control txrc;
676 	struct ieee80211_sta_rates *ratetbl = NULL;
677 	bool assoc = false;
678 
679 	memset(&txrc, 0, sizeof(txrc));
680 
681 	sband = tx->local->hw.wiphy->bands[info->band];
682 
683 	len = min_t(u32, tx->skb->len + FCS_LEN,
684 			 tx->local->hw.wiphy->frag_threshold);
685 
686 	/* set up the tx rate control struct we give the RC algo */
687 	txrc.hw = &tx->local->hw;
688 	txrc.sband = sband;
689 	txrc.bss_conf = &tx->sdata->vif.bss_conf;
690 	txrc.skb = tx->skb;
691 	txrc.reported_rate.idx = -1;
692 	txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
693 
694 	if (tx->sdata->rc_has_mcs_mask[info->band])
695 		txrc.rate_idx_mcs_mask =
696 			tx->sdata->rc_rateidx_mcs_mask[info->band];
697 
698 	txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
699 		    tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
700 		    tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
701 		    tx->sdata->vif.type == NL80211_IFTYPE_OCB);
702 
703 	/* set up RTS protection if desired */
704 	if (len > tx->local->hw.wiphy->rts_threshold) {
705 		txrc.rts = true;
706 	}
707 
708 	info->control.use_rts = txrc.rts;
709 	info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
710 
711 	/*
712 	 * Use short preamble if the BSS can handle it, but not for
713 	 * management frames unless we know the receiver can handle
714 	 * that -- the management frame might be to a station that
715 	 * just wants a probe response.
716 	 */
717 	if (tx->sdata->vif.bss_conf.use_short_preamble &&
718 	    (ieee80211_is_data(hdr->frame_control) ||
719 	     (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
720 		txrc.short_preamble = true;
721 
722 	info->control.short_preamble = txrc.short_preamble;
723 
724 	/* don't ask rate control when rate already injected via radiotap */
725 	if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
726 		return TX_CONTINUE;
727 
728 	if (tx->sta)
729 		assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
730 
731 	/*
732 	 * Lets not bother rate control if we're associated and cannot
733 	 * talk to the sta. This should not happen.
734 	 */
735 	if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
736 		 !rate_usable_index_exists(sband, &tx->sta->sta),
737 		 "%s: Dropped data frame as no usable bitrate found while "
738 		 "scanning and associated. Target station: "
739 		 "%pM on %d GHz band\n",
740 		 tx->sdata->name, hdr->addr1,
741 		 info->band ? 5 : 2))
742 		return TX_DROP;
743 
744 	/*
745 	 * If we're associated with the sta at this point we know we can at
746 	 * least send the frame at the lowest bit rate.
747 	 */
748 	rate_control_get_rate(tx->sdata, tx->sta, &txrc);
749 
750 	if (tx->sta && !info->control.skip_table)
751 		ratetbl = rcu_dereference(tx->sta->sta.rates);
752 
753 	if (unlikely(info->control.rates[0].idx < 0)) {
754 		if (ratetbl) {
755 			struct ieee80211_tx_rate rate = {
756 				.idx = ratetbl->rate[0].idx,
757 				.flags = ratetbl->rate[0].flags,
758 				.count = ratetbl->rate[0].count
759 			};
760 
761 			if (ratetbl->rate[0].idx < 0)
762 				return TX_DROP;
763 
764 			tx->rate = rate;
765 		} else {
766 			return TX_DROP;
767 		}
768 	} else {
769 		tx->rate = info->control.rates[0];
770 	}
771 
772 	if (txrc.reported_rate.idx < 0) {
773 		txrc.reported_rate = tx->rate;
774 		if (tx->sta && ieee80211_is_data(hdr->frame_control))
775 			tx->sta->tx_stats.last_rate = txrc.reported_rate;
776 	} else if (tx->sta)
777 		tx->sta->tx_stats.last_rate = txrc.reported_rate;
778 
779 	if (ratetbl)
780 		return TX_CONTINUE;
781 
782 	if (unlikely(!info->control.rates[0].count))
783 		info->control.rates[0].count = 1;
784 
785 	if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
786 			 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
787 		info->control.rates[0].count = 1;
788 
789 	return TX_CONTINUE;
790 }
791 
792 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
793 {
794 	u16 *seq = &sta->tid_seq[tid];
795 	__le16 ret = cpu_to_le16(*seq);
796 
797 	/* Increase the sequence number. */
798 	*seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
799 
800 	return ret;
801 }
802 
803 static ieee80211_tx_result debug_noinline
804 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
805 {
806 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
807 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
808 	int tid;
809 
810 	/*
811 	 * Packet injection may want to control the sequence
812 	 * number, if we have no matching interface then we
813 	 * neither assign one ourselves nor ask the driver to.
814 	 */
815 	if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
816 		return TX_CONTINUE;
817 
818 	if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
819 		return TX_CONTINUE;
820 
821 	if (ieee80211_hdrlen(hdr->frame_control) < 24)
822 		return TX_CONTINUE;
823 
824 	if (ieee80211_is_qos_nullfunc(hdr->frame_control))
825 		return TX_CONTINUE;
826 
827 	/*
828 	 * Anything but QoS data that has a sequence number field
829 	 * (is long enough) gets a sequence number from the global
830 	 * counter.  QoS data frames with a multicast destination
831 	 * also use the global counter (802.11-2012 9.3.2.10).
832 	 */
833 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
834 	    is_multicast_ether_addr(hdr->addr1)) {
835 		if (tx->flags & IEEE80211_TX_NO_SEQNO)
836 			return TX_CONTINUE;
837 		/* driver should assign sequence number */
838 		info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
839 		/* for pure STA mode without beacons, we can do it */
840 		hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
841 		tx->sdata->sequence_number += 0x10;
842 		if (tx->sta)
843 			tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
844 		return TX_CONTINUE;
845 	}
846 
847 	/*
848 	 * This should be true for injected/management frames only, for
849 	 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
850 	 * above since they are not QoS-data frames.
851 	 */
852 	if (!tx->sta)
853 		return TX_CONTINUE;
854 
855 	/* include per-STA, per-TID sequence counter */
856 	tid = ieee80211_get_tid(hdr);
857 	tx->sta->tx_stats.msdu[tid]++;
858 
859 	hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
860 
861 	return TX_CONTINUE;
862 }
863 
864 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
865 			      struct sk_buff *skb, int hdrlen,
866 			      int frag_threshold)
867 {
868 	struct ieee80211_local *local = tx->local;
869 	struct ieee80211_tx_info *info;
870 	struct sk_buff *tmp;
871 	int per_fragm = frag_threshold - hdrlen - FCS_LEN;
872 	int pos = hdrlen + per_fragm;
873 	int rem = skb->len - hdrlen - per_fragm;
874 
875 	if (WARN_ON(rem < 0))
876 		return -EINVAL;
877 
878 	/* first fragment was already added to queue by caller */
879 
880 	while (rem) {
881 		int fraglen = per_fragm;
882 
883 		if (fraglen > rem)
884 			fraglen = rem;
885 		rem -= fraglen;
886 		tmp = dev_alloc_skb(local->tx_headroom +
887 				    frag_threshold +
888 				    tx->sdata->encrypt_headroom +
889 				    IEEE80211_ENCRYPT_TAILROOM);
890 		if (!tmp)
891 			return -ENOMEM;
892 
893 		__skb_queue_tail(&tx->skbs, tmp);
894 
895 		skb_reserve(tmp,
896 			    local->tx_headroom + tx->sdata->encrypt_headroom);
897 
898 		/* copy control information */
899 		memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
900 
901 		info = IEEE80211_SKB_CB(tmp);
902 		info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
903 				 IEEE80211_TX_CTL_FIRST_FRAGMENT);
904 
905 		if (rem)
906 			info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
907 
908 		skb_copy_queue_mapping(tmp, skb);
909 		tmp->priority = skb->priority;
910 		tmp->dev = skb->dev;
911 
912 		/* copy header and data */
913 		skb_put_data(tmp, skb->data, hdrlen);
914 		skb_put_data(tmp, skb->data + pos, fraglen);
915 
916 		pos += fraglen;
917 	}
918 
919 	/* adjust first fragment's length */
920 	skb_trim(skb, hdrlen + per_fragm);
921 	return 0;
922 }
923 
924 static ieee80211_tx_result debug_noinline
925 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
926 {
927 	struct sk_buff *skb = tx->skb;
928 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
929 	struct ieee80211_hdr *hdr = (void *)skb->data;
930 	int frag_threshold = tx->local->hw.wiphy->frag_threshold;
931 	int hdrlen;
932 	int fragnum;
933 
934 	/* no matter what happens, tx->skb moves to tx->skbs */
935 	__skb_queue_tail(&tx->skbs, skb);
936 	tx->skb = NULL;
937 
938 	if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
939 		return TX_CONTINUE;
940 
941 	if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
942 		return TX_CONTINUE;
943 
944 	/*
945 	 * Warn when submitting a fragmented A-MPDU frame and drop it.
946 	 * This scenario is handled in ieee80211_tx_prepare but extra
947 	 * caution taken here as fragmented ampdu may cause Tx stop.
948 	 */
949 	if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
950 		return TX_DROP;
951 
952 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
953 
954 	/* internal error, why isn't DONTFRAG set? */
955 	if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
956 		return TX_DROP;
957 
958 	/*
959 	 * Now fragment the frame. This will allocate all the fragments and
960 	 * chain them (using skb as the first fragment) to skb->next.
961 	 * During transmission, we will remove the successfully transmitted
962 	 * fragments from this list. When the low-level driver rejects one
963 	 * of the fragments then we will simply pretend to accept the skb
964 	 * but store it away as pending.
965 	 */
966 	if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
967 		return TX_DROP;
968 
969 	/* update duration/seq/flags of fragments */
970 	fragnum = 0;
971 
972 	skb_queue_walk(&tx->skbs, skb) {
973 		const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
974 
975 		hdr = (void *)skb->data;
976 		info = IEEE80211_SKB_CB(skb);
977 
978 		if (!skb_queue_is_last(&tx->skbs, skb)) {
979 			hdr->frame_control |= morefrags;
980 			/*
981 			 * No multi-rate retries for fragmented frames, that
982 			 * would completely throw off the NAV at other STAs.
983 			 */
984 			info->control.rates[1].idx = -1;
985 			info->control.rates[2].idx = -1;
986 			info->control.rates[3].idx = -1;
987 			BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
988 			info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
989 		} else {
990 			hdr->frame_control &= ~morefrags;
991 		}
992 		hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
993 		fragnum++;
994 	}
995 
996 	return TX_CONTINUE;
997 }
998 
999 static ieee80211_tx_result debug_noinline
1000 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1001 {
1002 	struct sk_buff *skb;
1003 	int ac = -1;
1004 
1005 	if (!tx->sta)
1006 		return TX_CONTINUE;
1007 
1008 	skb_queue_walk(&tx->skbs, skb) {
1009 		ac = skb_get_queue_mapping(skb);
1010 		tx->sta->tx_stats.bytes[ac] += skb->len;
1011 	}
1012 	if (ac >= 0)
1013 		tx->sta->tx_stats.packets[ac]++;
1014 
1015 	return TX_CONTINUE;
1016 }
1017 
1018 static ieee80211_tx_result debug_noinline
1019 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1020 {
1021 	if (!tx->key)
1022 		return TX_CONTINUE;
1023 
1024 	switch (tx->key->conf.cipher) {
1025 	case WLAN_CIPHER_SUITE_WEP40:
1026 	case WLAN_CIPHER_SUITE_WEP104:
1027 		return ieee80211_crypto_wep_encrypt(tx);
1028 	case WLAN_CIPHER_SUITE_TKIP:
1029 		return ieee80211_crypto_tkip_encrypt(tx);
1030 	case WLAN_CIPHER_SUITE_CCMP:
1031 		return ieee80211_crypto_ccmp_encrypt(
1032 			tx, IEEE80211_CCMP_MIC_LEN);
1033 	case WLAN_CIPHER_SUITE_CCMP_256:
1034 		return ieee80211_crypto_ccmp_encrypt(
1035 			tx, IEEE80211_CCMP_256_MIC_LEN);
1036 	case WLAN_CIPHER_SUITE_AES_CMAC:
1037 		return ieee80211_crypto_aes_cmac_encrypt(tx);
1038 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1039 		return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1040 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1041 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1042 		return ieee80211_crypto_aes_gmac_encrypt(tx);
1043 	case WLAN_CIPHER_SUITE_GCMP:
1044 	case WLAN_CIPHER_SUITE_GCMP_256:
1045 		return ieee80211_crypto_gcmp_encrypt(tx);
1046 	default:
1047 		return ieee80211_crypto_hw_encrypt(tx);
1048 	}
1049 
1050 	return TX_DROP;
1051 }
1052 
1053 static ieee80211_tx_result debug_noinline
1054 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1055 {
1056 	struct sk_buff *skb;
1057 	struct ieee80211_hdr *hdr;
1058 	int next_len;
1059 	bool group_addr;
1060 
1061 	skb_queue_walk(&tx->skbs, skb) {
1062 		hdr = (void *) skb->data;
1063 		if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1064 			break; /* must not overwrite AID */
1065 		if (!skb_queue_is_last(&tx->skbs, skb)) {
1066 			struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1067 			next_len = next->len;
1068 		} else
1069 			next_len = 0;
1070 		group_addr = is_multicast_ether_addr(hdr->addr1);
1071 
1072 		hdr->duration_id =
1073 			ieee80211_duration(tx, skb, group_addr, next_len);
1074 	}
1075 
1076 	return TX_CONTINUE;
1077 }
1078 
1079 /* actual transmit path */
1080 
1081 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1082 				  struct sk_buff *skb,
1083 				  struct ieee80211_tx_info *info,
1084 				  struct tid_ampdu_tx *tid_tx,
1085 				  int tid)
1086 {
1087 	bool queued = false;
1088 	bool reset_agg_timer = false;
1089 	struct sk_buff *purge_skb = NULL;
1090 
1091 	if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1092 		info->flags |= IEEE80211_TX_CTL_AMPDU;
1093 		reset_agg_timer = true;
1094 	} else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1095 		/*
1096 		 * nothing -- this aggregation session is being started
1097 		 * but that might still fail with the driver
1098 		 */
1099 	} else if (!tx->sta->sta.txq[tid]) {
1100 		spin_lock(&tx->sta->lock);
1101 		/*
1102 		 * Need to re-check now, because we may get here
1103 		 *
1104 		 *  1) in the window during which the setup is actually
1105 		 *     already done, but not marked yet because not all
1106 		 *     packets are spliced over to the driver pending
1107 		 *     queue yet -- if this happened we acquire the lock
1108 		 *     either before or after the splice happens, but
1109 		 *     need to recheck which of these cases happened.
1110 		 *
1111 		 *  2) during session teardown, if the OPERATIONAL bit
1112 		 *     was cleared due to the teardown but the pointer
1113 		 *     hasn't been assigned NULL yet (or we loaded it
1114 		 *     before it was assigned) -- in this case it may
1115 		 *     now be NULL which means we should just let the
1116 		 *     packet pass through because splicing the frames
1117 		 *     back is already done.
1118 		 */
1119 		tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1120 
1121 		if (!tid_tx) {
1122 			/* do nothing, let packet pass through */
1123 		} else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1124 			info->flags |= IEEE80211_TX_CTL_AMPDU;
1125 			reset_agg_timer = true;
1126 		} else {
1127 			queued = true;
1128 			if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1129 				clear_sta_flag(tx->sta, WLAN_STA_SP);
1130 				ps_dbg(tx->sta->sdata,
1131 				       "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1132 				       tx->sta->sta.addr, tx->sta->sta.aid);
1133 			}
1134 			info->control.vif = &tx->sdata->vif;
1135 			info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1136 			info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1137 			__skb_queue_tail(&tid_tx->pending, skb);
1138 			if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1139 				purge_skb = __skb_dequeue(&tid_tx->pending);
1140 		}
1141 		spin_unlock(&tx->sta->lock);
1142 
1143 		if (purge_skb)
1144 			ieee80211_free_txskb(&tx->local->hw, purge_skb);
1145 	}
1146 
1147 	/* reset session timer */
1148 	if (reset_agg_timer)
1149 		tid_tx->last_tx = jiffies;
1150 
1151 	return queued;
1152 }
1153 
1154 /*
1155  * initialises @tx
1156  * pass %NULL for the station if unknown, a valid pointer if known
1157  * or an ERR_PTR() if the station is known not to exist
1158  */
1159 static ieee80211_tx_result
1160 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1161 		     struct ieee80211_tx_data *tx,
1162 		     struct sta_info *sta, struct sk_buff *skb)
1163 {
1164 	struct ieee80211_local *local = sdata->local;
1165 	struct ieee80211_hdr *hdr;
1166 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1167 	int tid;
1168 
1169 	memset(tx, 0, sizeof(*tx));
1170 	tx->skb = skb;
1171 	tx->local = local;
1172 	tx->sdata = sdata;
1173 	__skb_queue_head_init(&tx->skbs);
1174 
1175 	/*
1176 	 * If this flag is set to true anywhere, and we get here,
1177 	 * we are doing the needed processing, so remove the flag
1178 	 * now.
1179 	 */
1180 	info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1181 
1182 	hdr = (struct ieee80211_hdr *) skb->data;
1183 
1184 	if (likely(sta)) {
1185 		if (!IS_ERR(sta))
1186 			tx->sta = sta;
1187 	} else {
1188 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1189 			tx->sta = rcu_dereference(sdata->u.vlan.sta);
1190 			if (!tx->sta && sdata->wdev.use_4addr)
1191 				return TX_DROP;
1192 		} else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1193 					  IEEE80211_TX_CTL_INJECTED) ||
1194 			   tx->sdata->control_port_protocol == tx->skb->protocol) {
1195 			tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1196 		}
1197 		if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1198 			tx->sta = sta_info_get(sdata, hdr->addr1);
1199 	}
1200 
1201 	if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1202 	    !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1203 	    ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1204 	    !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1205 		struct tid_ampdu_tx *tid_tx;
1206 
1207 		tid = ieee80211_get_tid(hdr);
1208 
1209 		tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1210 		if (tid_tx) {
1211 			bool queued;
1212 
1213 			queued = ieee80211_tx_prep_agg(tx, skb, info,
1214 						       tid_tx, tid);
1215 
1216 			if (unlikely(queued))
1217 				return TX_QUEUED;
1218 		}
1219 	}
1220 
1221 	if (is_multicast_ether_addr(hdr->addr1)) {
1222 		tx->flags &= ~IEEE80211_TX_UNICAST;
1223 		info->flags |= IEEE80211_TX_CTL_NO_ACK;
1224 	} else
1225 		tx->flags |= IEEE80211_TX_UNICAST;
1226 
1227 	if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1228 		if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1229 		    skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1230 		    info->flags & IEEE80211_TX_CTL_AMPDU)
1231 			info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1232 	}
1233 
1234 	if (!tx->sta)
1235 		info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1236 	else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1237 		info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1238 		ieee80211_check_fast_xmit(tx->sta);
1239 	}
1240 
1241 	info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1242 
1243 	return TX_CONTINUE;
1244 }
1245 
1246 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1247 					  struct ieee80211_vif *vif,
1248 					  struct sta_info *sta,
1249 					  struct sk_buff *skb)
1250 {
1251 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1252 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1253 	struct ieee80211_txq *txq = NULL;
1254 
1255 	if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1256 	    (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1257 		return NULL;
1258 
1259 	if (!(info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP) &&
1260 	    unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1261 		if ((!ieee80211_is_mgmt(hdr->frame_control) ||
1262 		     ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1263 		     vif->type == NL80211_IFTYPE_STATION) &&
1264 		    sta && sta->uploaded) {
1265 			/*
1266 			 * This will be NULL if the driver didn't set the
1267 			 * opt-in hardware flag.
1268 			 */
1269 			txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1270 		}
1271 	} else if (sta) {
1272 		u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1273 
1274 		if (!sta->uploaded)
1275 			return NULL;
1276 
1277 		txq = sta->sta.txq[tid];
1278 	} else if (vif) {
1279 		txq = vif->txq;
1280 	}
1281 
1282 	if (!txq)
1283 		return NULL;
1284 
1285 	return to_txq_info(txq);
1286 }
1287 
1288 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1289 {
1290 	IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1291 }
1292 
1293 static u32 codel_skb_len_func(const struct sk_buff *skb)
1294 {
1295 	return skb->len;
1296 }
1297 
1298 static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1299 {
1300 	const struct ieee80211_tx_info *info;
1301 
1302 	info = (const struct ieee80211_tx_info *)skb->cb;
1303 	return info->control.enqueue_time;
1304 }
1305 
1306 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1307 					  void *ctx)
1308 {
1309 	struct ieee80211_local *local;
1310 	struct txq_info *txqi;
1311 	struct fq *fq;
1312 	struct fq_flow *flow;
1313 
1314 	txqi = ctx;
1315 	local = vif_to_sdata(txqi->txq.vif)->local;
1316 	fq = &local->fq;
1317 
1318 	if (cvars == &txqi->def_cvars)
1319 		flow = &txqi->def_flow;
1320 	else
1321 		flow = &fq->flows[cvars - local->cvars];
1322 
1323 	return fq_flow_dequeue(fq, flow);
1324 }
1325 
1326 static void codel_drop_func(struct sk_buff *skb,
1327 			    void *ctx)
1328 {
1329 	struct ieee80211_local *local;
1330 	struct ieee80211_hw *hw;
1331 	struct txq_info *txqi;
1332 
1333 	txqi = ctx;
1334 	local = vif_to_sdata(txqi->txq.vif)->local;
1335 	hw = &local->hw;
1336 
1337 	ieee80211_free_txskb(hw, skb);
1338 }
1339 
1340 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1341 					   struct fq_tin *tin,
1342 					   struct fq_flow *flow)
1343 {
1344 	struct ieee80211_local *local;
1345 	struct txq_info *txqi;
1346 	struct codel_vars *cvars;
1347 	struct codel_params *cparams;
1348 	struct codel_stats *cstats;
1349 
1350 	local = container_of(fq, struct ieee80211_local, fq);
1351 	txqi = container_of(tin, struct txq_info, tin);
1352 	cstats = &txqi->cstats;
1353 
1354 	if (txqi->txq.sta) {
1355 		struct sta_info *sta = container_of(txqi->txq.sta,
1356 						    struct sta_info, sta);
1357 		cparams = &sta->cparams;
1358 	} else {
1359 		cparams = &local->cparams;
1360 	}
1361 
1362 	if (flow == &txqi->def_flow)
1363 		cvars = &txqi->def_cvars;
1364 	else
1365 		cvars = &local->cvars[flow - fq->flows];
1366 
1367 	return codel_dequeue(txqi,
1368 			     &flow->backlog,
1369 			     cparams,
1370 			     cvars,
1371 			     cstats,
1372 			     codel_skb_len_func,
1373 			     codel_skb_time_func,
1374 			     codel_drop_func,
1375 			     codel_dequeue_func);
1376 }
1377 
1378 static void fq_skb_free_func(struct fq *fq,
1379 			     struct fq_tin *tin,
1380 			     struct fq_flow *flow,
1381 			     struct sk_buff *skb)
1382 {
1383 	struct ieee80211_local *local;
1384 
1385 	local = container_of(fq, struct ieee80211_local, fq);
1386 	ieee80211_free_txskb(&local->hw, skb);
1387 }
1388 
1389 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1390 						struct fq_tin *tin,
1391 						int idx,
1392 						struct sk_buff *skb)
1393 {
1394 	struct txq_info *txqi;
1395 
1396 	txqi = container_of(tin, struct txq_info, tin);
1397 	return &txqi->def_flow;
1398 }
1399 
1400 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1401 				  struct txq_info *txqi,
1402 				  struct sk_buff *skb)
1403 {
1404 	struct fq *fq = &local->fq;
1405 	struct fq_tin *tin = &txqi->tin;
1406 	u32 flow_idx = fq_flow_idx(fq, skb);
1407 
1408 	ieee80211_set_skb_enqueue_time(skb);
1409 
1410 	spin_lock_bh(&fq->lock);
1411 	fq_tin_enqueue(fq, tin, flow_idx, skb,
1412 		       fq_skb_free_func,
1413 		       fq_flow_get_default_func);
1414 	spin_unlock_bh(&fq->lock);
1415 }
1416 
1417 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1418 				struct fq_flow *flow, struct sk_buff *skb,
1419 				void *data)
1420 {
1421 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1422 
1423 	return info->control.vif == data;
1424 }
1425 
1426 void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1427 			       struct ieee80211_sub_if_data *sdata)
1428 {
1429 	struct fq *fq = &local->fq;
1430 	struct txq_info *txqi;
1431 	struct fq_tin *tin;
1432 	struct ieee80211_sub_if_data *ap;
1433 
1434 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1435 		return;
1436 
1437 	ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1438 
1439 	if (!ap->vif.txq)
1440 		return;
1441 
1442 	txqi = to_txq_info(ap->vif.txq);
1443 	tin = &txqi->tin;
1444 
1445 	spin_lock_bh(&fq->lock);
1446 	fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1447 		      fq_skb_free_func);
1448 	spin_unlock_bh(&fq->lock);
1449 }
1450 
1451 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1452 			struct sta_info *sta,
1453 			struct txq_info *txqi, int tid)
1454 {
1455 	fq_tin_init(&txqi->tin);
1456 	fq_flow_init(&txqi->def_flow);
1457 	codel_vars_init(&txqi->def_cvars);
1458 	codel_stats_init(&txqi->cstats);
1459 	__skb_queue_head_init(&txqi->frags);
1460 	INIT_LIST_HEAD(&txqi->schedule_order);
1461 
1462 	txqi->txq.vif = &sdata->vif;
1463 
1464 	if (!sta) {
1465 		sdata->vif.txq = &txqi->txq;
1466 		txqi->txq.tid = 0;
1467 		txqi->txq.ac = IEEE80211_AC_BE;
1468 
1469 		return;
1470 	}
1471 
1472 	if (tid == IEEE80211_NUM_TIDS) {
1473 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1474 			/* Drivers need to opt in to the management MPDU TXQ */
1475 			if (!ieee80211_hw_check(&sdata->local->hw,
1476 						STA_MMPDU_TXQ))
1477 				return;
1478 		} else if (!ieee80211_hw_check(&sdata->local->hw,
1479 					       BUFF_MMPDU_TXQ)) {
1480 			/* Drivers need to opt in to the bufferable MMPDU TXQ */
1481 			return;
1482 		}
1483 		txqi->txq.ac = IEEE80211_AC_VO;
1484 	} else {
1485 		txqi->txq.ac = ieee80211_ac_from_tid(tid);
1486 	}
1487 
1488 	txqi->txq.sta = &sta->sta;
1489 	txqi->txq.tid = tid;
1490 	sta->sta.txq[tid] = &txqi->txq;
1491 }
1492 
1493 void ieee80211_txq_purge(struct ieee80211_local *local,
1494 			 struct txq_info *txqi)
1495 {
1496 	struct fq *fq = &local->fq;
1497 	struct fq_tin *tin = &txqi->tin;
1498 
1499 	spin_lock_bh(&fq->lock);
1500 	fq_tin_reset(fq, tin, fq_skb_free_func);
1501 	ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
1502 	spin_unlock_bh(&fq->lock);
1503 
1504 	spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1505 	list_del_init(&txqi->schedule_order);
1506 	spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
1507 }
1508 
1509 void ieee80211_txq_set_params(struct ieee80211_local *local)
1510 {
1511 	if (local->hw.wiphy->txq_limit)
1512 		local->fq.limit = local->hw.wiphy->txq_limit;
1513 	else
1514 		local->hw.wiphy->txq_limit = local->fq.limit;
1515 
1516 	if (local->hw.wiphy->txq_memory_limit)
1517 		local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1518 	else
1519 		local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1520 
1521 	if (local->hw.wiphy->txq_quantum)
1522 		local->fq.quantum = local->hw.wiphy->txq_quantum;
1523 	else
1524 		local->hw.wiphy->txq_quantum = local->fq.quantum;
1525 }
1526 
1527 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1528 {
1529 	struct fq *fq = &local->fq;
1530 	int ret;
1531 	int i;
1532 	bool supp_vht = false;
1533 	enum nl80211_band band;
1534 
1535 	if (!local->ops->wake_tx_queue)
1536 		return 0;
1537 
1538 	ret = fq_init(fq, 4096);
1539 	if (ret)
1540 		return ret;
1541 
1542 	/*
1543 	 * If the hardware doesn't support VHT, it is safe to limit the maximum
1544 	 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1545 	 */
1546 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1547 		struct ieee80211_supported_band *sband;
1548 
1549 		sband = local->hw.wiphy->bands[band];
1550 		if (!sband)
1551 			continue;
1552 
1553 		supp_vht = supp_vht || sband->vht_cap.vht_supported;
1554 	}
1555 
1556 	if (!supp_vht)
1557 		fq->memory_limit = 4 << 20; /* 4 Mbytes */
1558 
1559 	codel_params_init(&local->cparams);
1560 	local->cparams.interval = MS2TIME(100);
1561 	local->cparams.target = MS2TIME(20);
1562 	local->cparams.ecn = true;
1563 
1564 	local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1565 			       GFP_KERNEL);
1566 	if (!local->cvars) {
1567 		spin_lock_bh(&fq->lock);
1568 		fq_reset(fq, fq_skb_free_func);
1569 		spin_unlock_bh(&fq->lock);
1570 		return -ENOMEM;
1571 	}
1572 
1573 	for (i = 0; i < fq->flows_cnt; i++)
1574 		codel_vars_init(&local->cvars[i]);
1575 
1576 	ieee80211_txq_set_params(local);
1577 
1578 	return 0;
1579 }
1580 
1581 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1582 {
1583 	struct fq *fq = &local->fq;
1584 
1585 	if (!local->ops->wake_tx_queue)
1586 		return;
1587 
1588 	kfree(local->cvars);
1589 	local->cvars = NULL;
1590 
1591 	spin_lock_bh(&fq->lock);
1592 	fq_reset(fq, fq_skb_free_func);
1593 	spin_unlock_bh(&fq->lock);
1594 }
1595 
1596 static bool ieee80211_queue_skb(struct ieee80211_local *local,
1597 				struct ieee80211_sub_if_data *sdata,
1598 				struct sta_info *sta,
1599 				struct sk_buff *skb)
1600 {
1601 	struct ieee80211_vif *vif;
1602 	struct txq_info *txqi;
1603 
1604 	if (!local->ops->wake_tx_queue ||
1605 	    sdata->vif.type == NL80211_IFTYPE_MONITOR)
1606 		return false;
1607 
1608 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1609 		sdata = container_of(sdata->bss,
1610 				     struct ieee80211_sub_if_data, u.ap);
1611 
1612 	vif = &sdata->vif;
1613 	txqi = ieee80211_get_txq(local, vif, sta, skb);
1614 
1615 	if (!txqi)
1616 		return false;
1617 
1618 	ieee80211_txq_enqueue(local, txqi, skb);
1619 
1620 	schedule_and_wake_txq(local, txqi);
1621 
1622 	return true;
1623 }
1624 
1625 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1626 			       struct ieee80211_vif *vif,
1627 			       struct sta_info *sta,
1628 			       struct sk_buff_head *skbs,
1629 			       bool txpending)
1630 {
1631 	struct ieee80211_tx_control control = {};
1632 	struct sk_buff *skb, *tmp;
1633 	unsigned long flags;
1634 
1635 	skb_queue_walk_safe(skbs, skb, tmp) {
1636 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1637 		int q = info->hw_queue;
1638 
1639 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1640 		if (WARN_ON_ONCE(q >= local->hw.queues)) {
1641 			__skb_unlink(skb, skbs);
1642 			ieee80211_free_txskb(&local->hw, skb);
1643 			continue;
1644 		}
1645 #endif
1646 
1647 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1648 		if (local->queue_stop_reasons[q] ||
1649 		    (!txpending && !skb_queue_empty(&local->pending[q]))) {
1650 			if (unlikely(info->flags &
1651 				     IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1652 				if (local->queue_stop_reasons[q] &
1653 				    ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1654 					/*
1655 					 * Drop off-channel frames if queues
1656 					 * are stopped for any reason other
1657 					 * than off-channel operation. Never
1658 					 * queue them.
1659 					 */
1660 					spin_unlock_irqrestore(
1661 						&local->queue_stop_reason_lock,
1662 						flags);
1663 					ieee80211_purge_tx_queue(&local->hw,
1664 								 skbs);
1665 					return true;
1666 				}
1667 			} else {
1668 
1669 				/*
1670 				 * Since queue is stopped, queue up frames for
1671 				 * later transmission from the tx-pending
1672 				 * tasklet when the queue is woken again.
1673 				 */
1674 				if (txpending)
1675 					skb_queue_splice_init(skbs,
1676 							      &local->pending[q]);
1677 				else
1678 					skb_queue_splice_tail_init(skbs,
1679 								   &local->pending[q]);
1680 
1681 				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1682 						       flags);
1683 				return false;
1684 			}
1685 		}
1686 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1687 
1688 		info->control.vif = vif;
1689 		control.sta = sta ? &sta->sta : NULL;
1690 
1691 		__skb_unlink(skb, skbs);
1692 		drv_tx(local, &control, skb);
1693 	}
1694 
1695 	return true;
1696 }
1697 
1698 /*
1699  * Returns false if the frame couldn't be transmitted but was queued instead.
1700  */
1701 static bool __ieee80211_tx(struct ieee80211_local *local,
1702 			   struct sk_buff_head *skbs, int led_len,
1703 			   struct sta_info *sta, bool txpending)
1704 {
1705 	struct ieee80211_tx_info *info;
1706 	struct ieee80211_sub_if_data *sdata;
1707 	struct ieee80211_vif *vif;
1708 	struct sk_buff *skb;
1709 	bool result = true;
1710 	__le16 fc;
1711 
1712 	if (WARN_ON(skb_queue_empty(skbs)))
1713 		return true;
1714 
1715 	skb = skb_peek(skbs);
1716 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1717 	info = IEEE80211_SKB_CB(skb);
1718 	sdata = vif_to_sdata(info->control.vif);
1719 	if (sta && !sta->uploaded)
1720 		sta = NULL;
1721 
1722 	switch (sdata->vif.type) {
1723 	case NL80211_IFTYPE_MONITOR:
1724 		if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1725 			vif = &sdata->vif;
1726 			break;
1727 		}
1728 		sdata = rcu_dereference(local->monitor_sdata);
1729 		if (sdata) {
1730 			vif = &sdata->vif;
1731 			info->hw_queue =
1732 				vif->hw_queue[skb_get_queue_mapping(skb)];
1733 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1734 			ieee80211_purge_tx_queue(&local->hw, skbs);
1735 			return true;
1736 		} else
1737 			vif = NULL;
1738 		break;
1739 	case NL80211_IFTYPE_AP_VLAN:
1740 		sdata = container_of(sdata->bss,
1741 				     struct ieee80211_sub_if_data, u.ap);
1742 		/* fall through */
1743 	default:
1744 		vif = &sdata->vif;
1745 		break;
1746 	}
1747 
1748 	result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
1749 
1750 	ieee80211_tpt_led_trig_tx(local, fc, led_len);
1751 
1752 	WARN_ON_ONCE(!skb_queue_empty(skbs));
1753 
1754 	return result;
1755 }
1756 
1757 /*
1758  * Invoke TX handlers, return 0 on success and non-zero if the
1759  * frame was dropped or queued.
1760  *
1761  * The handlers are split into an early and late part. The latter is everything
1762  * that can be sensitive to reordering, and will be deferred to after packets
1763  * are dequeued from the intermediate queues (when they are enabled).
1764  */
1765 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
1766 {
1767 	ieee80211_tx_result res = TX_DROP;
1768 
1769 #define CALL_TXH(txh) \
1770 	do {				\
1771 		res = txh(tx);		\
1772 		if (res != TX_CONTINUE)	\
1773 			goto txh_done;	\
1774 	} while (0)
1775 
1776 	CALL_TXH(ieee80211_tx_h_dynamic_ps);
1777 	CALL_TXH(ieee80211_tx_h_check_assoc);
1778 	CALL_TXH(ieee80211_tx_h_ps_buf);
1779 	CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1780 	CALL_TXH(ieee80211_tx_h_select_key);
1781 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1782 		CALL_TXH(ieee80211_tx_h_rate_ctrl);
1783 
1784  txh_done:
1785 	if (unlikely(res == TX_DROP)) {
1786 		I802_DEBUG_INC(tx->local->tx_handlers_drop);
1787 		if (tx->skb)
1788 			ieee80211_free_txskb(&tx->local->hw, tx->skb);
1789 		else
1790 			ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1791 		return -1;
1792 	} else if (unlikely(res == TX_QUEUED)) {
1793 		I802_DEBUG_INC(tx->local->tx_handlers_queued);
1794 		return -1;
1795 	}
1796 
1797 	return 0;
1798 }
1799 
1800 /*
1801  * Late handlers can be called while the sta lock is held. Handlers that can
1802  * cause packets to be generated will cause deadlock!
1803  */
1804 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1805 {
1806 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1807 	ieee80211_tx_result res = TX_CONTINUE;
1808 
1809 	if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1810 		__skb_queue_tail(&tx->skbs, tx->skb);
1811 		tx->skb = NULL;
1812 		goto txh_done;
1813 	}
1814 
1815 	CALL_TXH(ieee80211_tx_h_michael_mic_add);
1816 	CALL_TXH(ieee80211_tx_h_sequence);
1817 	CALL_TXH(ieee80211_tx_h_fragment);
1818 	/* handlers after fragment must be aware of tx info fragmentation! */
1819 	CALL_TXH(ieee80211_tx_h_stats);
1820 	CALL_TXH(ieee80211_tx_h_encrypt);
1821 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1822 		CALL_TXH(ieee80211_tx_h_calculate_duration);
1823 #undef CALL_TXH
1824 
1825  txh_done:
1826 	if (unlikely(res == TX_DROP)) {
1827 		I802_DEBUG_INC(tx->local->tx_handlers_drop);
1828 		if (tx->skb)
1829 			ieee80211_free_txskb(&tx->local->hw, tx->skb);
1830 		else
1831 			ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1832 		return -1;
1833 	} else if (unlikely(res == TX_QUEUED)) {
1834 		I802_DEBUG_INC(tx->local->tx_handlers_queued);
1835 		return -1;
1836 	}
1837 
1838 	return 0;
1839 }
1840 
1841 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1842 {
1843 	int r = invoke_tx_handlers_early(tx);
1844 
1845 	if (r)
1846 		return r;
1847 	return invoke_tx_handlers_late(tx);
1848 }
1849 
1850 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1851 			      struct ieee80211_vif *vif, struct sk_buff *skb,
1852 			      int band, struct ieee80211_sta **sta)
1853 {
1854 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1855 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1856 	struct ieee80211_tx_data tx;
1857 	struct sk_buff *skb2;
1858 
1859 	if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1860 		return false;
1861 
1862 	info->band = band;
1863 	info->control.vif = vif;
1864 	info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1865 
1866 	if (invoke_tx_handlers(&tx))
1867 		return false;
1868 
1869 	if (sta) {
1870 		if (tx.sta)
1871 			*sta = &tx.sta->sta;
1872 		else
1873 			*sta = NULL;
1874 	}
1875 
1876 	/* this function isn't suitable for fragmented data frames */
1877 	skb2 = __skb_dequeue(&tx.skbs);
1878 	if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1879 		ieee80211_free_txskb(hw, skb2);
1880 		ieee80211_purge_tx_queue(hw, &tx.skbs);
1881 		return false;
1882 	}
1883 
1884 	return true;
1885 }
1886 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1887 
1888 /*
1889  * Returns false if the frame couldn't be transmitted but was queued instead.
1890  */
1891 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1892 			 struct sta_info *sta, struct sk_buff *skb,
1893 			 bool txpending, u32 txdata_flags)
1894 {
1895 	struct ieee80211_local *local = sdata->local;
1896 	struct ieee80211_tx_data tx;
1897 	ieee80211_tx_result res_prepare;
1898 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1899 	bool result = true;
1900 	int led_len;
1901 
1902 	if (unlikely(skb->len < 10)) {
1903 		dev_kfree_skb(skb);
1904 		return true;
1905 	}
1906 
1907 	/* initialises tx */
1908 	led_len = skb->len;
1909 	res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1910 
1911 	tx.flags |= txdata_flags;
1912 
1913 	if (unlikely(res_prepare == TX_DROP)) {
1914 		ieee80211_free_txskb(&local->hw, skb);
1915 		return true;
1916 	} else if (unlikely(res_prepare == TX_QUEUED)) {
1917 		return true;
1918 	}
1919 
1920 	/* set up hw_queue value early */
1921 	if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1922 	    !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1923 		info->hw_queue =
1924 			sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1925 
1926 	if (invoke_tx_handlers_early(&tx))
1927 		return true;
1928 
1929 	if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1930 		return true;
1931 
1932 	if (!invoke_tx_handlers_late(&tx))
1933 		result = __ieee80211_tx(local, &tx.skbs, led_len,
1934 					tx.sta, txpending);
1935 
1936 	return result;
1937 }
1938 
1939 /* device xmit handlers */
1940 
1941 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1942 				struct sk_buff *skb,
1943 				int head_need, bool may_encrypt)
1944 {
1945 	struct ieee80211_local *local = sdata->local;
1946 	struct ieee80211_hdr *hdr;
1947 	bool enc_tailroom;
1948 	int tail_need = 0;
1949 
1950 	hdr = (struct ieee80211_hdr *) skb->data;
1951 	enc_tailroom = may_encrypt &&
1952 		       (sdata->crypto_tx_tailroom_needed_cnt ||
1953 			ieee80211_is_mgmt(hdr->frame_control));
1954 
1955 	if (enc_tailroom) {
1956 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
1957 		tail_need -= skb_tailroom(skb);
1958 		tail_need = max_t(int, tail_need, 0);
1959 	}
1960 
1961 	if (skb_cloned(skb) &&
1962 	    (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1963 	     !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
1964 		I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1965 	else if (head_need || tail_need)
1966 		I802_DEBUG_INC(local->tx_expand_skb_head);
1967 	else
1968 		return 0;
1969 
1970 	if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1971 		wiphy_debug(local->hw.wiphy,
1972 			    "failed to reallocate TX buffer\n");
1973 		return -ENOMEM;
1974 	}
1975 
1976 	return 0;
1977 }
1978 
1979 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1980 		    struct sta_info *sta, struct sk_buff *skb,
1981 		    u32 txdata_flags)
1982 {
1983 	struct ieee80211_local *local = sdata->local;
1984 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1985 	struct ieee80211_hdr *hdr;
1986 	int headroom;
1987 	bool may_encrypt;
1988 
1989 	may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1990 
1991 	headroom = local->tx_headroom;
1992 	if (may_encrypt)
1993 		headroom += sdata->encrypt_headroom;
1994 	headroom -= skb_headroom(skb);
1995 	headroom = max_t(int, 0, headroom);
1996 
1997 	if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1998 		ieee80211_free_txskb(&local->hw, skb);
1999 		return;
2000 	}
2001 
2002 	hdr = (struct ieee80211_hdr *) skb->data;
2003 	info->control.vif = &sdata->vif;
2004 
2005 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2006 		if (ieee80211_is_data(hdr->frame_control) &&
2007 		    is_unicast_ether_addr(hdr->addr1)) {
2008 			if (mesh_nexthop_resolve(sdata, skb))
2009 				return; /* skb queued: don't free */
2010 		} else {
2011 			ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2012 		}
2013 	}
2014 
2015 	ieee80211_set_qos_hdr(sdata, skb);
2016 	ieee80211_tx(sdata, sta, skb, false, txdata_flags);
2017 }
2018 
2019 static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
2020 					struct sk_buff *skb)
2021 {
2022 	struct ieee80211_radiotap_iterator iterator;
2023 	struct ieee80211_radiotap_header *rthdr =
2024 		(struct ieee80211_radiotap_header *) skb->data;
2025 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2026 	struct ieee80211_supported_band *sband =
2027 		local->hw.wiphy->bands[info->band];
2028 	int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2029 						   NULL);
2030 	u16 txflags;
2031 	u16 rate = 0;
2032 	bool rate_found = false;
2033 	u8 rate_retries = 0;
2034 	u16 rate_flags = 0;
2035 	u8 mcs_known, mcs_flags, mcs_bw;
2036 	u16 vht_known;
2037 	u8 vht_mcs = 0, vht_nss = 0;
2038 	int i;
2039 
2040 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2041 		       IEEE80211_TX_CTL_DONTFRAG;
2042 
2043 	/*
2044 	 * for every radiotap entry that is present
2045 	 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2046 	 * entries present, or -EINVAL on error)
2047 	 */
2048 
2049 	while (!ret) {
2050 		ret = ieee80211_radiotap_iterator_next(&iterator);
2051 
2052 		if (ret)
2053 			continue;
2054 
2055 		/* see if this argument is something we can use */
2056 		switch (iterator.this_arg_index) {
2057 		/*
2058 		 * You must take care when dereferencing iterator.this_arg
2059 		 * for multibyte types... the pointer is not aligned.  Use
2060 		 * get_unaligned((type *)iterator.this_arg) to dereference
2061 		 * iterator.this_arg for type "type" safely on all arches.
2062 		*/
2063 		case IEEE80211_RADIOTAP_FLAGS:
2064 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2065 				/*
2066 				 * this indicates that the skb we have been
2067 				 * handed has the 32-bit FCS CRC at the end...
2068 				 * we should react to that by snipping it off
2069 				 * because it will be recomputed and added
2070 				 * on transmission
2071 				 */
2072 				if (skb->len < (iterator._max_length + FCS_LEN))
2073 					return false;
2074 
2075 				skb_trim(skb, skb->len - FCS_LEN);
2076 			}
2077 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2078 				info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2079 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2080 				info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2081 			break;
2082 
2083 		case IEEE80211_RADIOTAP_TX_FLAGS:
2084 			txflags = get_unaligned_le16(iterator.this_arg);
2085 			if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2086 				info->flags |= IEEE80211_TX_CTL_NO_ACK;
2087 			break;
2088 
2089 		case IEEE80211_RADIOTAP_RATE:
2090 			rate = *iterator.this_arg;
2091 			rate_flags = 0;
2092 			rate_found = true;
2093 			break;
2094 
2095 		case IEEE80211_RADIOTAP_DATA_RETRIES:
2096 			rate_retries = *iterator.this_arg;
2097 			break;
2098 
2099 		case IEEE80211_RADIOTAP_MCS:
2100 			mcs_known = iterator.this_arg[0];
2101 			mcs_flags = iterator.this_arg[1];
2102 			if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2103 				break;
2104 
2105 			rate_found = true;
2106 			rate = iterator.this_arg[2];
2107 			rate_flags = IEEE80211_TX_RC_MCS;
2108 
2109 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2110 			    mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2111 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2112 
2113 			mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2114 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2115 			    mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2116 				rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2117 			break;
2118 
2119 		case IEEE80211_RADIOTAP_VHT:
2120 			vht_known = get_unaligned_le16(iterator.this_arg);
2121 			rate_found = true;
2122 
2123 			rate_flags = IEEE80211_TX_RC_VHT_MCS;
2124 			if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2125 			    (iterator.this_arg[2] &
2126 			     IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2127 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2128 			if (vht_known &
2129 			    IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2130 				if (iterator.this_arg[3] == 1)
2131 					rate_flags |=
2132 						IEEE80211_TX_RC_40_MHZ_WIDTH;
2133 				else if (iterator.this_arg[3] == 4)
2134 					rate_flags |=
2135 						IEEE80211_TX_RC_80_MHZ_WIDTH;
2136 				else if (iterator.this_arg[3] == 11)
2137 					rate_flags |=
2138 						IEEE80211_TX_RC_160_MHZ_WIDTH;
2139 			}
2140 
2141 			vht_mcs = iterator.this_arg[4] >> 4;
2142 			vht_nss = iterator.this_arg[4] & 0xF;
2143 			break;
2144 
2145 		/*
2146 		 * Please update the file
2147 		 * Documentation/networking/mac80211-injection.txt
2148 		 * when parsing new fields here.
2149 		 */
2150 
2151 		default:
2152 			break;
2153 		}
2154 	}
2155 
2156 	if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2157 		return false;
2158 
2159 	if (rate_found) {
2160 		info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2161 
2162 		for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2163 			info->control.rates[i].idx = -1;
2164 			info->control.rates[i].flags = 0;
2165 			info->control.rates[i].count = 0;
2166 		}
2167 
2168 		if (rate_flags & IEEE80211_TX_RC_MCS) {
2169 			info->control.rates[0].idx = rate;
2170 		} else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2171 			ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2172 					       vht_nss);
2173 		} else {
2174 			for (i = 0; i < sband->n_bitrates; i++) {
2175 				if (rate * 5 != sband->bitrates[i].bitrate)
2176 					continue;
2177 
2178 				info->control.rates[0].idx = i;
2179 				break;
2180 			}
2181 		}
2182 
2183 		if (info->control.rates[0].idx < 0)
2184 			info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2185 
2186 		info->control.rates[0].flags = rate_flags;
2187 		info->control.rates[0].count = min_t(u8, rate_retries + 1,
2188 						     local->hw.max_rate_tries);
2189 	}
2190 
2191 	/*
2192 	 * remove the radiotap header
2193 	 * iterator->_max_length was sanity-checked against
2194 	 * skb->len by iterator init
2195 	 */
2196 	skb_pull(skb, iterator._max_length);
2197 
2198 	return true;
2199 }
2200 
2201 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2202 					 struct net_device *dev)
2203 {
2204 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2205 	struct ieee80211_chanctx_conf *chanctx_conf;
2206 	struct ieee80211_radiotap_header *prthdr =
2207 		(struct ieee80211_radiotap_header *)skb->data;
2208 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2209 	struct ieee80211_hdr *hdr;
2210 	struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2211 	struct cfg80211_chan_def *chandef;
2212 	u16 len_rthdr;
2213 	int hdrlen;
2214 
2215 	/* check for not even having the fixed radiotap header part */
2216 	if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2217 		goto fail; /* too short to be possibly valid */
2218 
2219 	/* is it a header version we can trust to find length from? */
2220 	if (unlikely(prthdr->it_version))
2221 		goto fail; /* only version 0 is supported */
2222 
2223 	/* then there must be a radiotap header with a length we can use */
2224 	len_rthdr = ieee80211_get_radiotap_len(skb->data);
2225 
2226 	/* does the skb contain enough to deliver on the alleged length? */
2227 	if (unlikely(skb->len < len_rthdr))
2228 		goto fail; /* skb too short for claimed rt header extent */
2229 
2230 	/*
2231 	 * fix up the pointers accounting for the radiotap
2232 	 * header still being in there.  We are being given
2233 	 * a precooked IEEE80211 header so no need for
2234 	 * normal processing
2235 	 */
2236 	skb_set_mac_header(skb, len_rthdr);
2237 	/*
2238 	 * these are just fixed to the end of the rt area since we
2239 	 * don't have any better information and at this point, nobody cares
2240 	 */
2241 	skb_set_network_header(skb, len_rthdr);
2242 	skb_set_transport_header(skb, len_rthdr);
2243 
2244 	if (skb->len < len_rthdr + 2)
2245 		goto fail;
2246 
2247 	hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2248 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
2249 
2250 	if (skb->len < len_rthdr + hdrlen)
2251 		goto fail;
2252 
2253 	/*
2254 	 * Initialize skb->protocol if the injected frame is a data frame
2255 	 * carrying a rfc1042 header
2256 	 */
2257 	if (ieee80211_is_data(hdr->frame_control) &&
2258 	    skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2259 		u8 *payload = (u8 *)hdr + hdrlen;
2260 
2261 		if (ether_addr_equal(payload, rfc1042_header))
2262 			skb->protocol = cpu_to_be16((payload[6] << 8) |
2263 						    payload[7]);
2264 	}
2265 
2266 	/*
2267 	 * Initialize skb->priority for QoS frames. This is put in the TID field
2268 	 * of the frame before passing it to the driver.
2269 	 */
2270 	if (ieee80211_is_data_qos(hdr->frame_control)) {
2271 		u8 *p = ieee80211_get_qos_ctl(hdr);
2272 		skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2273 	}
2274 
2275 	memset(info, 0, sizeof(*info));
2276 
2277 	info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2278 		      IEEE80211_TX_CTL_INJECTED;
2279 
2280 	rcu_read_lock();
2281 
2282 	/*
2283 	 * We process outgoing injected frames that have a local address
2284 	 * we handle as though they are non-injected frames.
2285 	 * This code here isn't entirely correct, the local MAC address
2286 	 * isn't always enough to find the interface to use; for proper
2287 	 * VLAN/WDS support we will need a different mechanism (which
2288 	 * likely isn't going to be monitor interfaces).
2289 	 *
2290 	 * This is necessary, for example, for old hostapd versions that
2291 	 * don't use nl80211-based management TX/RX.
2292 	 */
2293 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2294 
2295 	list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2296 		if (!ieee80211_sdata_running(tmp_sdata))
2297 			continue;
2298 		if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2299 		    tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2300 		    tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2301 			continue;
2302 		if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2303 			sdata = tmp_sdata;
2304 			break;
2305 		}
2306 	}
2307 
2308 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2309 	if (!chanctx_conf) {
2310 		tmp_sdata = rcu_dereference(local->monitor_sdata);
2311 		if (tmp_sdata)
2312 			chanctx_conf =
2313 				rcu_dereference(tmp_sdata->vif.chanctx_conf);
2314 	}
2315 
2316 	if (chanctx_conf)
2317 		chandef = &chanctx_conf->def;
2318 	else if (!local->use_chanctx)
2319 		chandef = &local->_oper_chandef;
2320 	else
2321 		goto fail_rcu;
2322 
2323 	/*
2324 	 * Frame injection is not allowed if beaconing is not allowed
2325 	 * or if we need radar detection. Beaconing is usually not allowed when
2326 	 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2327 	 * Passive scan is also used in world regulatory domains where
2328 	 * your country is not known and as such it should be treated as
2329 	 * NO TX unless the channel is explicitly allowed in which case
2330 	 * your current regulatory domain would not have the passive scan
2331 	 * flag.
2332 	 *
2333 	 * Since AP mode uses monitor interfaces to inject/TX management
2334 	 * frames we can make AP mode the exception to this rule once it
2335 	 * supports radar detection as its implementation can deal with
2336 	 * radar detection by itself. We can do that later by adding a
2337 	 * monitor flag interfaces used for AP support.
2338 	 */
2339 	if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2340 				     sdata->vif.type))
2341 		goto fail_rcu;
2342 
2343 	info->band = chandef->chan->band;
2344 
2345 	/* process and remove the injection radiotap header */
2346 	if (!ieee80211_parse_tx_radiotap(local, skb))
2347 		goto fail_rcu;
2348 
2349 	ieee80211_xmit(sdata, NULL, skb, 0);
2350 	rcu_read_unlock();
2351 
2352 	return NETDEV_TX_OK;
2353 
2354 fail_rcu:
2355 	rcu_read_unlock();
2356 fail:
2357 	dev_kfree_skb(skb);
2358 	return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2359 }
2360 
2361 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2362 {
2363 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2364 
2365 	return ethertype == ETH_P_TDLS &&
2366 	       skb->len > 14 &&
2367 	       skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2368 }
2369 
2370 int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2371 			    struct sk_buff *skb,
2372 			    struct sta_info **sta_out)
2373 {
2374 	struct sta_info *sta;
2375 
2376 	switch (sdata->vif.type) {
2377 	case NL80211_IFTYPE_AP_VLAN:
2378 		sta = rcu_dereference(sdata->u.vlan.sta);
2379 		if (sta) {
2380 			*sta_out = sta;
2381 			return 0;
2382 		} else if (sdata->wdev.use_4addr) {
2383 			return -ENOLINK;
2384 		}
2385 		/* fall through */
2386 	case NL80211_IFTYPE_AP:
2387 	case NL80211_IFTYPE_OCB:
2388 	case NL80211_IFTYPE_ADHOC:
2389 		if (is_multicast_ether_addr(skb->data)) {
2390 			*sta_out = ERR_PTR(-ENOENT);
2391 			return 0;
2392 		}
2393 		sta = sta_info_get_bss(sdata, skb->data);
2394 		break;
2395 	case NL80211_IFTYPE_WDS:
2396 		sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2397 		break;
2398 #ifdef CONFIG_MAC80211_MESH
2399 	case NL80211_IFTYPE_MESH_POINT:
2400 		/* determined much later */
2401 		*sta_out = NULL;
2402 		return 0;
2403 #endif
2404 	case NL80211_IFTYPE_STATION:
2405 		if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2406 			sta = sta_info_get(sdata, skb->data);
2407 			if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2408 				if (test_sta_flag(sta,
2409 						  WLAN_STA_TDLS_PEER_AUTH)) {
2410 					*sta_out = sta;
2411 					return 0;
2412 				}
2413 
2414 				/*
2415 				 * TDLS link during setup - throw out frames to
2416 				 * peer. Allow TDLS-setup frames to unauthorized
2417 				 * peers for the special case of a link teardown
2418 				 * after a TDLS sta is removed due to being
2419 				 * unreachable.
2420 				 */
2421 				if (!ieee80211_is_tdls_setup(skb))
2422 					return -EINVAL;
2423 			}
2424 
2425 		}
2426 
2427 		sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2428 		if (!sta)
2429 			return -ENOLINK;
2430 		break;
2431 	default:
2432 		return -EINVAL;
2433 	}
2434 
2435 	*sta_out = sta ?: ERR_PTR(-ENOENT);
2436 	return 0;
2437 }
2438 
2439 static int ieee80211_store_ack_skb(struct ieee80211_local *local,
2440 				   struct sk_buff *skb,
2441 				   u32 *info_flags)
2442 {
2443 	struct sk_buff *ack_skb = skb_clone_sk(skb);
2444 	u16 info_id = 0;
2445 
2446 	if (ack_skb) {
2447 		unsigned long flags;
2448 		int id;
2449 
2450 		spin_lock_irqsave(&local->ack_status_lock, flags);
2451 		id = idr_alloc(&local->ack_status_frames, ack_skb,
2452 			       1, 0x2000, GFP_ATOMIC);
2453 		spin_unlock_irqrestore(&local->ack_status_lock, flags);
2454 
2455 		if (id >= 0) {
2456 			info_id = id;
2457 			*info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2458 		} else {
2459 			kfree_skb(ack_skb);
2460 		}
2461 	}
2462 
2463 	return info_id;
2464 }
2465 
2466 /**
2467  * ieee80211_build_hdr - build 802.11 header in the given frame
2468  * @sdata: virtual interface to build the header for
2469  * @skb: the skb to build the header in
2470  * @info_flags: skb flags to set
2471  * @ctrl_flags: info control flags to set
2472  *
2473  * This function takes the skb with 802.3 header and reformats the header to
2474  * the appropriate IEEE 802.11 header based on which interface the packet is
2475  * being transmitted on.
2476  *
2477  * Note that this function also takes care of the TX status request and
2478  * potential unsharing of the SKB - this needs to be interleaved with the
2479  * header building.
2480  *
2481  * The function requires the read-side RCU lock held
2482  *
2483  * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2484  */
2485 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2486 					   struct sk_buff *skb, u32 info_flags,
2487 					   struct sta_info *sta, u32 ctrl_flags)
2488 {
2489 	struct ieee80211_local *local = sdata->local;
2490 	struct ieee80211_tx_info *info;
2491 	int head_need;
2492 	u16 ethertype, hdrlen,  meshhdrlen = 0;
2493 	__le16 fc;
2494 	struct ieee80211_hdr hdr;
2495 	struct ieee80211s_hdr mesh_hdr __maybe_unused;
2496 	struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2497 	const u8 *encaps_data;
2498 	int encaps_len, skip_header_bytes;
2499 	bool wme_sta = false, authorized = false;
2500 	bool tdls_peer;
2501 	bool multicast;
2502 	u16 info_id = 0;
2503 	struct ieee80211_chanctx_conf *chanctx_conf;
2504 	struct ieee80211_sub_if_data *ap_sdata;
2505 	enum nl80211_band band;
2506 	int ret;
2507 
2508 	if (IS_ERR(sta))
2509 		sta = NULL;
2510 
2511 #ifdef CONFIG_MAC80211_DEBUGFS
2512 	if (local->force_tx_status)
2513 		info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2514 #endif
2515 
2516 	/* convert Ethernet header to proper 802.11 header (based on
2517 	 * operation mode) */
2518 	ethertype = (skb->data[12] << 8) | skb->data[13];
2519 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2520 
2521 	switch (sdata->vif.type) {
2522 	case NL80211_IFTYPE_AP_VLAN:
2523 		if (sdata->wdev.use_4addr) {
2524 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2525 			/* RA TA DA SA */
2526 			memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2527 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2528 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2529 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2530 			hdrlen = 30;
2531 			authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2532 			wme_sta = sta->sta.wme;
2533 		}
2534 		ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2535 					u.ap);
2536 		chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2537 		if (!chanctx_conf) {
2538 			ret = -ENOTCONN;
2539 			goto free;
2540 		}
2541 		band = chanctx_conf->def.chan->band;
2542 		if (sdata->wdev.use_4addr)
2543 			break;
2544 		/* fall through */
2545 	case NL80211_IFTYPE_AP:
2546 		if (sdata->vif.type == NL80211_IFTYPE_AP)
2547 			chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2548 		if (!chanctx_conf) {
2549 			ret = -ENOTCONN;
2550 			goto free;
2551 		}
2552 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2553 		/* DA BSSID SA */
2554 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2555 		memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2556 		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2557 		hdrlen = 24;
2558 		band = chanctx_conf->def.chan->band;
2559 		break;
2560 	case NL80211_IFTYPE_WDS:
2561 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2562 		/* RA TA DA SA */
2563 		memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2564 		memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2565 		memcpy(hdr.addr3, skb->data, ETH_ALEN);
2566 		memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2567 		hdrlen = 30;
2568 		/*
2569 		 * This is the exception! WDS style interfaces are prohibited
2570 		 * when channel contexts are in used so this must be valid
2571 		 */
2572 		band = local->hw.conf.chandef.chan->band;
2573 		break;
2574 #ifdef CONFIG_MAC80211_MESH
2575 	case NL80211_IFTYPE_MESH_POINT:
2576 		if (!is_multicast_ether_addr(skb->data)) {
2577 			struct sta_info *next_hop;
2578 			bool mpp_lookup = true;
2579 
2580 			mpath = mesh_path_lookup(sdata, skb->data);
2581 			if (mpath) {
2582 				mpp_lookup = false;
2583 				next_hop = rcu_dereference(mpath->next_hop);
2584 				if (!next_hop ||
2585 				    !(mpath->flags & (MESH_PATH_ACTIVE |
2586 						      MESH_PATH_RESOLVING)))
2587 					mpp_lookup = true;
2588 			}
2589 
2590 			if (mpp_lookup) {
2591 				mppath = mpp_path_lookup(sdata, skb->data);
2592 				if (mppath)
2593 					mppath->exp_time = jiffies;
2594 			}
2595 
2596 			if (mppath && mpath)
2597 				mesh_path_del(sdata, mpath->dst);
2598 		}
2599 
2600 		/*
2601 		 * Use address extension if it is a packet from
2602 		 * another interface or if we know the destination
2603 		 * is being proxied by a portal (i.e. portal address
2604 		 * differs from proxied address)
2605 		 */
2606 		if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2607 		    !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2608 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2609 					skb->data, skb->data + ETH_ALEN);
2610 			meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2611 							       NULL, NULL);
2612 		} else {
2613 			/* DS -> MBSS (802.11-2012 13.11.3.3).
2614 			 * For unicast with unknown forwarding information,
2615 			 * destination might be in the MBSS or if that fails
2616 			 * forwarded to another mesh gate. In either case
2617 			 * resolution will be handled in ieee80211_xmit(), so
2618 			 * leave the original DA. This also works for mcast */
2619 			const u8 *mesh_da = skb->data;
2620 
2621 			if (mppath)
2622 				mesh_da = mppath->mpp;
2623 			else if (mpath)
2624 				mesh_da = mpath->dst;
2625 
2626 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2627 					mesh_da, sdata->vif.addr);
2628 			if (is_multicast_ether_addr(mesh_da))
2629 				/* DA TA mSA AE:SA */
2630 				meshhdrlen = ieee80211_new_mesh_header(
2631 						sdata, &mesh_hdr,
2632 						skb->data + ETH_ALEN, NULL);
2633 			else
2634 				/* RA TA mDA mSA AE:DA SA */
2635 				meshhdrlen = ieee80211_new_mesh_header(
2636 						sdata, &mesh_hdr, skb->data,
2637 						skb->data + ETH_ALEN);
2638 
2639 		}
2640 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2641 		if (!chanctx_conf) {
2642 			ret = -ENOTCONN;
2643 			goto free;
2644 		}
2645 		band = chanctx_conf->def.chan->band;
2646 
2647 		/* For injected frames, fill RA right away as nexthop lookup
2648 		 * will be skipped.
2649 		 */
2650 		if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2651 		    is_zero_ether_addr(hdr.addr1))
2652 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2653 		break;
2654 #endif
2655 	case NL80211_IFTYPE_STATION:
2656 		/* we already did checks when looking up the RA STA */
2657 		tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2658 
2659 		if (tdls_peer) {
2660 			/* DA SA BSSID */
2661 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2662 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2663 			memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2664 			hdrlen = 24;
2665 		}  else if (sdata->u.mgd.use_4addr &&
2666 			    cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2667 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2668 					  IEEE80211_FCTL_TODS);
2669 			/* RA TA DA SA */
2670 			memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2671 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2672 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2673 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2674 			hdrlen = 30;
2675 		} else {
2676 			fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2677 			/* BSSID SA DA */
2678 			memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2679 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2680 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2681 			hdrlen = 24;
2682 		}
2683 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2684 		if (!chanctx_conf) {
2685 			ret = -ENOTCONN;
2686 			goto free;
2687 		}
2688 		band = chanctx_conf->def.chan->band;
2689 		break;
2690 	case NL80211_IFTYPE_OCB:
2691 		/* DA SA BSSID */
2692 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2693 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2694 		eth_broadcast_addr(hdr.addr3);
2695 		hdrlen = 24;
2696 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2697 		if (!chanctx_conf) {
2698 			ret = -ENOTCONN;
2699 			goto free;
2700 		}
2701 		band = chanctx_conf->def.chan->band;
2702 		break;
2703 	case NL80211_IFTYPE_ADHOC:
2704 		/* DA SA BSSID */
2705 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2706 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2707 		memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2708 		hdrlen = 24;
2709 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2710 		if (!chanctx_conf) {
2711 			ret = -ENOTCONN;
2712 			goto free;
2713 		}
2714 		band = chanctx_conf->def.chan->band;
2715 		break;
2716 	default:
2717 		ret = -EINVAL;
2718 		goto free;
2719 	}
2720 
2721 	multicast = is_multicast_ether_addr(hdr.addr1);
2722 
2723 	/* sta is always NULL for mesh */
2724 	if (sta) {
2725 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2726 		wme_sta = sta->sta.wme;
2727 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2728 		/* For mesh, the use of the QoS header is mandatory */
2729 		wme_sta = true;
2730 	}
2731 
2732 	/* receiver does QoS (which also means we do) use it */
2733 	if (wme_sta) {
2734 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2735 		hdrlen += 2;
2736 	}
2737 
2738 	/*
2739 	 * Drop unicast frames to unauthorised stations unless they are
2740 	 * EAPOL frames from the local station.
2741 	 */
2742 	if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2743 		     (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2744 		     !multicast && !authorized &&
2745 		     (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2746 		      !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2747 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2748 		net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2749 				    sdata->name, hdr.addr1);
2750 #endif
2751 
2752 		I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2753 
2754 		ret = -EPERM;
2755 		goto free;
2756 	}
2757 
2758 	if (unlikely(!multicast && skb->sk &&
2759 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
2760 		info_id = ieee80211_store_ack_skb(local, skb, &info_flags);
2761 
2762 	/*
2763 	 * If the skb is shared we need to obtain our own copy.
2764 	 */
2765 	if (skb_shared(skb)) {
2766 		struct sk_buff *tmp_skb = skb;
2767 
2768 		/* can't happen -- skb is a clone if info_id != 0 */
2769 		WARN_ON(info_id);
2770 
2771 		skb = skb_clone(skb, GFP_ATOMIC);
2772 		kfree_skb(tmp_skb);
2773 
2774 		if (!skb) {
2775 			ret = -ENOMEM;
2776 			goto free;
2777 		}
2778 	}
2779 
2780 	hdr.frame_control = fc;
2781 	hdr.duration_id = 0;
2782 	hdr.seq_ctrl = 0;
2783 
2784 	skip_header_bytes = ETH_HLEN;
2785 	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2786 		encaps_data = bridge_tunnel_header;
2787 		encaps_len = sizeof(bridge_tunnel_header);
2788 		skip_header_bytes -= 2;
2789 	} else if (ethertype >= ETH_P_802_3_MIN) {
2790 		encaps_data = rfc1042_header;
2791 		encaps_len = sizeof(rfc1042_header);
2792 		skip_header_bytes -= 2;
2793 	} else {
2794 		encaps_data = NULL;
2795 		encaps_len = 0;
2796 	}
2797 
2798 	skb_pull(skb, skip_header_bytes);
2799 	head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2800 
2801 	/*
2802 	 * So we need to modify the skb header and hence need a copy of
2803 	 * that. The head_need variable above doesn't, so far, include
2804 	 * the needed header space that we don't need right away. If we
2805 	 * can, then we don't reallocate right now but only after the
2806 	 * frame arrives at the master device (if it does...)
2807 	 *
2808 	 * If we cannot, however, then we will reallocate to include all
2809 	 * the ever needed space. Also, if we need to reallocate it anyway,
2810 	 * make it big enough for everything we may ever need.
2811 	 */
2812 
2813 	if (head_need > 0 || skb_cloned(skb)) {
2814 		head_need += sdata->encrypt_headroom;
2815 		head_need += local->tx_headroom;
2816 		head_need = max_t(int, 0, head_need);
2817 		if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2818 			ieee80211_free_txskb(&local->hw, skb);
2819 			skb = NULL;
2820 			return ERR_PTR(-ENOMEM);
2821 		}
2822 	}
2823 
2824 	if (encaps_data)
2825 		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2826 
2827 #ifdef CONFIG_MAC80211_MESH
2828 	if (meshhdrlen > 0)
2829 		memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2830 #endif
2831 
2832 	if (ieee80211_is_data_qos(fc)) {
2833 		__le16 *qos_control;
2834 
2835 		qos_control = skb_push(skb, 2);
2836 		memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2837 		/*
2838 		 * Maybe we could actually set some fields here, for now just
2839 		 * initialise to zero to indicate no special operation.
2840 		 */
2841 		*qos_control = 0;
2842 	} else
2843 		memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2844 
2845 	skb_reset_mac_header(skb);
2846 
2847 	info = IEEE80211_SKB_CB(skb);
2848 	memset(info, 0, sizeof(*info));
2849 
2850 	info->flags = info_flags;
2851 	info->ack_frame_id = info_id;
2852 	info->band = band;
2853 	info->control.flags = ctrl_flags;
2854 
2855 	return skb;
2856  free:
2857 	kfree_skb(skb);
2858 	return ERR_PTR(ret);
2859 }
2860 
2861 /*
2862  * fast-xmit overview
2863  *
2864  * The core idea of this fast-xmit is to remove per-packet checks by checking
2865  * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2866  * checks that are needed to get the sta->fast_tx pointer assigned, after which
2867  * much less work can be done per packet. For example, fragmentation must be
2868  * disabled or the fast_tx pointer will not be set. All the conditions are seen
2869  * in the code here.
2870  *
2871  * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2872  * header and other data to aid packet processing in ieee80211_xmit_fast().
2873  *
2874  * The most difficult part of this is that when any of these assumptions
2875  * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2876  * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2877  * since the per-packet code no longer checks the conditions. This is reflected
2878  * by the calls to these functions throughout the rest of the code, and must be
2879  * maintained if any of the TX path checks change.
2880  */
2881 
2882 void ieee80211_check_fast_xmit(struct sta_info *sta)
2883 {
2884 	struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2885 	struct ieee80211_local *local = sta->local;
2886 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2887 	struct ieee80211_hdr *hdr = (void *)build.hdr;
2888 	struct ieee80211_chanctx_conf *chanctx_conf;
2889 	__le16 fc;
2890 
2891 	if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2892 		return;
2893 
2894 	/* Locking here protects both the pointer itself, and against concurrent
2895 	 * invocations winning data access races to, e.g., the key pointer that
2896 	 * is used.
2897 	 * Without it, the invocation of this function right after the key
2898 	 * pointer changes wouldn't be sufficient, as another CPU could access
2899 	 * the pointer, then stall, and then do the cache update after the CPU
2900 	 * that invalidated the key.
2901 	 * With the locking, such scenarios cannot happen as the check for the
2902 	 * key and the fast-tx assignment are done atomically, so the CPU that
2903 	 * modifies the key will either wait or other one will see the key
2904 	 * cleared/changed already.
2905 	 */
2906 	spin_lock_bh(&sta->lock);
2907 	if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2908 	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2909 	    sdata->vif.type == NL80211_IFTYPE_STATION)
2910 		goto out;
2911 
2912 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2913 		goto out;
2914 
2915 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2916 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2917 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2918 	    test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2919 		goto out;
2920 
2921 	if (sdata->noack_map)
2922 		goto out;
2923 
2924 	/* fast-xmit doesn't handle fragmentation at all */
2925 	if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2926 	    !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
2927 		goto out;
2928 
2929 	rcu_read_lock();
2930 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2931 	if (!chanctx_conf) {
2932 		rcu_read_unlock();
2933 		goto out;
2934 	}
2935 	build.band = chanctx_conf->def.chan->band;
2936 	rcu_read_unlock();
2937 
2938 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2939 
2940 	switch (sdata->vif.type) {
2941 	case NL80211_IFTYPE_ADHOC:
2942 		/* DA SA BSSID */
2943 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2944 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2945 		memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2946 		build.hdr_len = 24;
2947 		break;
2948 	case NL80211_IFTYPE_STATION:
2949 		if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2950 			/* DA SA BSSID */
2951 			build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2952 			build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2953 			memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2954 			build.hdr_len = 24;
2955 			break;
2956 		}
2957 
2958 		if (sdata->u.mgd.use_4addr) {
2959 			/* non-regular ethertype cannot use the fastpath */
2960 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2961 					  IEEE80211_FCTL_TODS);
2962 			/* RA TA DA SA */
2963 			memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2964 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2965 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2966 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2967 			build.hdr_len = 30;
2968 			break;
2969 		}
2970 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2971 		/* BSSID SA DA */
2972 		memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2973 		build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2974 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2975 		build.hdr_len = 24;
2976 		break;
2977 	case NL80211_IFTYPE_AP_VLAN:
2978 		if (sdata->wdev.use_4addr) {
2979 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2980 					  IEEE80211_FCTL_TODS);
2981 			/* RA TA DA SA */
2982 			memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2983 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2984 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2985 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2986 			build.hdr_len = 30;
2987 			break;
2988 		}
2989 		/* fall through */
2990 	case NL80211_IFTYPE_AP:
2991 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2992 		/* DA BSSID SA */
2993 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2994 		memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2995 		build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2996 		build.hdr_len = 24;
2997 		break;
2998 	default:
2999 		/* not handled on fast-xmit */
3000 		goto out;
3001 	}
3002 
3003 	if (sta->sta.wme) {
3004 		build.hdr_len += 2;
3005 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3006 	}
3007 
3008 	/* We store the key here so there's no point in using rcu_dereference()
3009 	 * but that's fine because the code that changes the pointers will call
3010 	 * this function after doing so. For a single CPU that would be enough,
3011 	 * for multiple see the comment above.
3012 	 */
3013 	build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3014 	if (!build.key)
3015 		build.key = rcu_access_pointer(sdata->default_unicast_key);
3016 	if (build.key) {
3017 		bool gen_iv, iv_spc, mmic;
3018 
3019 		gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3020 		iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3021 		mmic = build.key->conf.flags &
3022 			(IEEE80211_KEY_FLAG_GENERATE_MMIC |
3023 			 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3024 
3025 		/* don't handle software crypto */
3026 		if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3027 			goto out;
3028 
3029 		/* Key is being removed */
3030 		if (build.key->flags & KEY_FLAG_TAINTED)
3031 			goto out;
3032 
3033 		switch (build.key->conf.cipher) {
3034 		case WLAN_CIPHER_SUITE_CCMP:
3035 		case WLAN_CIPHER_SUITE_CCMP_256:
3036 			if (gen_iv)
3037 				build.pn_offs = build.hdr_len;
3038 			if (gen_iv || iv_spc)
3039 				build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3040 			break;
3041 		case WLAN_CIPHER_SUITE_GCMP:
3042 		case WLAN_CIPHER_SUITE_GCMP_256:
3043 			if (gen_iv)
3044 				build.pn_offs = build.hdr_len;
3045 			if (gen_iv || iv_spc)
3046 				build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3047 			break;
3048 		case WLAN_CIPHER_SUITE_TKIP:
3049 			/* cannot handle MMIC or IV generation in xmit-fast */
3050 			if (mmic || gen_iv)
3051 				goto out;
3052 			if (iv_spc)
3053 				build.hdr_len += IEEE80211_TKIP_IV_LEN;
3054 			break;
3055 		case WLAN_CIPHER_SUITE_WEP40:
3056 		case WLAN_CIPHER_SUITE_WEP104:
3057 			/* cannot handle IV generation in fast-xmit */
3058 			if (gen_iv)
3059 				goto out;
3060 			if (iv_spc)
3061 				build.hdr_len += IEEE80211_WEP_IV_LEN;
3062 			break;
3063 		case WLAN_CIPHER_SUITE_AES_CMAC:
3064 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3065 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3066 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3067 			WARN(1,
3068 			     "management cipher suite 0x%x enabled for data\n",
3069 			     build.key->conf.cipher);
3070 			goto out;
3071 		default:
3072 			/* we don't know how to generate IVs for this at all */
3073 			if (WARN_ON(gen_iv))
3074 				goto out;
3075 			/* pure hardware keys are OK, of course */
3076 			if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3077 				break;
3078 			/* cipher scheme might require space allocation */
3079 			if (iv_spc &&
3080 			    build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3081 				goto out;
3082 			if (iv_spc)
3083 				build.hdr_len += build.key->conf.iv_len;
3084 		}
3085 
3086 		fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3087 	}
3088 
3089 	hdr->frame_control = fc;
3090 
3091 	memcpy(build.hdr + build.hdr_len,
3092 	       rfc1042_header,  sizeof(rfc1042_header));
3093 	build.hdr_len += sizeof(rfc1042_header);
3094 
3095 	fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3096 	/* if the kmemdup fails, continue w/o fast_tx */
3097 	if (!fast_tx)
3098 		goto out;
3099 
3100  out:
3101 	/* we might have raced against another call to this function */
3102 	old = rcu_dereference_protected(sta->fast_tx,
3103 					lockdep_is_held(&sta->lock));
3104 	rcu_assign_pointer(sta->fast_tx, fast_tx);
3105 	if (old)
3106 		kfree_rcu(old, rcu_head);
3107 	spin_unlock_bh(&sta->lock);
3108 }
3109 
3110 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3111 {
3112 	struct sta_info *sta;
3113 
3114 	rcu_read_lock();
3115 	list_for_each_entry_rcu(sta, &local->sta_list, list)
3116 		ieee80211_check_fast_xmit(sta);
3117 	rcu_read_unlock();
3118 }
3119 
3120 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3121 {
3122 	struct ieee80211_local *local = sdata->local;
3123 	struct sta_info *sta;
3124 
3125 	rcu_read_lock();
3126 
3127 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
3128 		if (sdata != sta->sdata &&
3129 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3130 			continue;
3131 		ieee80211_check_fast_xmit(sta);
3132 	}
3133 
3134 	rcu_read_unlock();
3135 }
3136 
3137 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3138 {
3139 	struct ieee80211_fast_tx *fast_tx;
3140 
3141 	spin_lock_bh(&sta->lock);
3142 	fast_tx = rcu_dereference_protected(sta->fast_tx,
3143 					    lockdep_is_held(&sta->lock));
3144 	RCU_INIT_POINTER(sta->fast_tx, NULL);
3145 	spin_unlock_bh(&sta->lock);
3146 
3147 	if (fast_tx)
3148 		kfree_rcu(fast_tx, rcu_head);
3149 }
3150 
3151 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3152 					struct sk_buff *skb, int headroom)
3153 {
3154 	if (skb_headroom(skb) < headroom) {
3155 		I802_DEBUG_INC(local->tx_expand_skb_head);
3156 
3157 		if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3158 			wiphy_debug(local->hw.wiphy,
3159 				    "failed to reallocate TX buffer\n");
3160 			return false;
3161 		}
3162 	}
3163 
3164 	return true;
3165 }
3166 
3167 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3168 					 struct ieee80211_fast_tx *fast_tx,
3169 					 struct sk_buff *skb)
3170 {
3171 	struct ieee80211_local *local = sdata->local;
3172 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3173 	struct ieee80211_hdr *hdr;
3174 	struct ethhdr *amsdu_hdr;
3175 	int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3176 	int subframe_len = skb->len - hdr_len;
3177 	void *data;
3178 	u8 *qc, *h_80211_src, *h_80211_dst;
3179 	const u8 *bssid;
3180 
3181 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3182 		return false;
3183 
3184 	if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3185 		return true;
3186 
3187 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
3188 		return false;
3189 
3190 	data = skb_push(skb, sizeof(*amsdu_hdr));
3191 	memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3192 	hdr = data;
3193 	amsdu_hdr = data + hdr_len;
3194 	/* h_80211_src/dst is addr* field within hdr */
3195 	h_80211_src = data + fast_tx->sa_offs;
3196 	h_80211_dst = data + fast_tx->da_offs;
3197 
3198 	amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3199 	ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3200 	ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3201 
3202 	/* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3203 	 * fields needs to be changed to BSSID for A-MSDU frames depending
3204 	 * on FromDS/ToDS values.
3205 	 */
3206 	switch (sdata->vif.type) {
3207 	case NL80211_IFTYPE_STATION:
3208 		bssid = sdata->u.mgd.bssid;
3209 		break;
3210 	case NL80211_IFTYPE_AP:
3211 	case NL80211_IFTYPE_AP_VLAN:
3212 		bssid = sdata->vif.addr;
3213 		break;
3214 	default:
3215 		bssid = NULL;
3216 	}
3217 
3218 	if (bssid && ieee80211_has_fromds(hdr->frame_control))
3219 		ether_addr_copy(h_80211_src, bssid);
3220 
3221 	if (bssid && ieee80211_has_tods(hdr->frame_control))
3222 		ether_addr_copy(h_80211_dst, bssid);
3223 
3224 	qc = ieee80211_get_qos_ctl(hdr);
3225 	*qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3226 
3227 	info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3228 
3229 	return true;
3230 }
3231 
3232 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3233 				      struct sta_info *sta,
3234 				      struct ieee80211_fast_tx *fast_tx,
3235 				      struct sk_buff *skb)
3236 {
3237 	struct ieee80211_local *local = sdata->local;
3238 	struct fq *fq = &local->fq;
3239 	struct fq_tin *tin;
3240 	struct fq_flow *flow;
3241 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3242 	struct ieee80211_txq *txq = sta->sta.txq[tid];
3243 	struct txq_info *txqi;
3244 	struct sk_buff **frag_tail, *head;
3245 	int subframe_len = skb->len - ETH_ALEN;
3246 	u8 max_subframes = sta->sta.max_amsdu_subframes;
3247 	int max_frags = local->hw.max_tx_fragments;
3248 	int max_amsdu_len = sta->sta.max_amsdu_len;
3249 	int orig_truesize;
3250 	u32 flow_idx;
3251 	__be16 len;
3252 	void *data;
3253 	bool ret = false;
3254 	unsigned int orig_len;
3255 	int n = 2, nfrags, pad = 0;
3256 	u16 hdrlen;
3257 
3258 	if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3259 		return false;
3260 
3261 	if (skb_is_gso(skb))
3262 		return false;
3263 
3264 	if (!txq)
3265 		return false;
3266 
3267 	txqi = to_txq_info(txq);
3268 	if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3269 		return false;
3270 
3271 	if (sta->sta.max_rc_amsdu_len)
3272 		max_amsdu_len = min_t(int, max_amsdu_len,
3273 				      sta->sta.max_rc_amsdu_len);
3274 
3275 	if (sta->sta.max_tid_amsdu_len[tid])
3276 		max_amsdu_len = min_t(int, max_amsdu_len,
3277 				      sta->sta.max_tid_amsdu_len[tid]);
3278 
3279 	flow_idx = fq_flow_idx(fq, skb);
3280 
3281 	spin_lock_bh(&fq->lock);
3282 
3283 	/* TODO: Ideally aggregation should be done on dequeue to remain
3284 	 * responsive to environment changes.
3285 	 */
3286 
3287 	tin = &txqi->tin;
3288 	flow = fq_flow_classify(fq, tin, flow_idx, skb,
3289 				fq_flow_get_default_func);
3290 	head = skb_peek_tail(&flow->queue);
3291 	if (!head || skb_is_gso(head))
3292 		goto out;
3293 
3294 	orig_truesize = head->truesize;
3295 	orig_len = head->len;
3296 
3297 	if (skb->len + head->len > max_amsdu_len)
3298 		goto out;
3299 
3300 	nfrags = 1 + skb_shinfo(skb)->nr_frags;
3301 	nfrags += 1 + skb_shinfo(head)->nr_frags;
3302 	frag_tail = &skb_shinfo(head)->frag_list;
3303 	while (*frag_tail) {
3304 		nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3305 		frag_tail = &(*frag_tail)->next;
3306 		n++;
3307 	}
3308 
3309 	if (max_subframes && n > max_subframes)
3310 		goto out;
3311 
3312 	if (max_frags && nfrags > max_frags)
3313 		goto out;
3314 
3315 	if (!drv_can_aggregate_in_amsdu(local, head, skb))
3316 		goto out;
3317 
3318 	if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3319 		goto out;
3320 
3321 	/*
3322 	 * Pad out the previous subframe to a multiple of 4 by adding the
3323 	 * padding to the next one, that's being added. Note that head->len
3324 	 * is the length of the full A-MSDU, but that works since each time
3325 	 * we add a new subframe we pad out the previous one to a multiple
3326 	 * of 4 and thus it no longer matters in the next round.
3327 	 */
3328 	hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3329 	if ((head->len - hdrlen) & 3)
3330 		pad = 4 - ((head->len - hdrlen) & 3);
3331 
3332 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3333 						     2 + pad))
3334 		goto out_recalc;
3335 
3336 	ret = true;
3337 	data = skb_push(skb, ETH_ALEN + 2);
3338 	memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3339 
3340 	data += 2 * ETH_ALEN;
3341 	len = cpu_to_be16(subframe_len);
3342 	memcpy(data, &len, 2);
3343 	memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3344 
3345 	memset(skb_push(skb, pad), 0, pad);
3346 
3347 	head->len += skb->len;
3348 	head->data_len += skb->len;
3349 	*frag_tail = skb;
3350 
3351 out_recalc:
3352 	fq->memory_usage += head->truesize - orig_truesize;
3353 	if (head->len != orig_len) {
3354 		flow->backlog += head->len - orig_len;
3355 		tin->backlog_bytes += head->len - orig_len;
3356 
3357 		fq_recalc_backlog(fq, tin, flow);
3358 	}
3359 out:
3360 	spin_unlock_bh(&fq->lock);
3361 
3362 	return ret;
3363 }
3364 
3365 /*
3366  * Can be called while the sta lock is held. Anything that can cause packets to
3367  * be generated will cause deadlock!
3368  */
3369 static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3370 				       struct sta_info *sta, u8 pn_offs,
3371 				       struct ieee80211_key *key,
3372 				       struct sk_buff *skb)
3373 {
3374 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3375 	struct ieee80211_hdr *hdr = (void *)skb->data;
3376 	u8 tid = IEEE80211_NUM_TIDS;
3377 
3378 	if (key)
3379 		info->control.hw_key = &key->conf;
3380 
3381 	ieee80211_tx_stats(skb->dev, skb->len);
3382 
3383 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3384 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3385 		hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3386 	} else {
3387 		info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3388 		hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3389 		sdata->sequence_number += 0x10;
3390 	}
3391 
3392 	if (skb_shinfo(skb)->gso_size)
3393 		sta->tx_stats.msdu[tid] +=
3394 			DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3395 	else
3396 		sta->tx_stats.msdu[tid]++;
3397 
3398 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3399 
3400 	/* statistics normally done by ieee80211_tx_h_stats (but that
3401 	 * has to consider fragmentation, so is more complex)
3402 	 */
3403 	sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3404 	sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3405 
3406 	if (pn_offs) {
3407 		u64 pn;
3408 		u8 *crypto_hdr = skb->data + pn_offs;
3409 
3410 		switch (key->conf.cipher) {
3411 		case WLAN_CIPHER_SUITE_CCMP:
3412 		case WLAN_CIPHER_SUITE_CCMP_256:
3413 		case WLAN_CIPHER_SUITE_GCMP:
3414 		case WLAN_CIPHER_SUITE_GCMP_256:
3415 			pn = atomic64_inc_return(&key->conf.tx_pn);
3416 			crypto_hdr[0] = pn;
3417 			crypto_hdr[1] = pn >> 8;
3418 			crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3419 			crypto_hdr[4] = pn >> 16;
3420 			crypto_hdr[5] = pn >> 24;
3421 			crypto_hdr[6] = pn >> 32;
3422 			crypto_hdr[7] = pn >> 40;
3423 			break;
3424 		}
3425 	}
3426 }
3427 
3428 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3429 				struct sta_info *sta,
3430 				struct ieee80211_fast_tx *fast_tx,
3431 				struct sk_buff *skb)
3432 {
3433 	struct ieee80211_local *local = sdata->local;
3434 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3435 	int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3436 	int hw_headroom = sdata->local->hw.extra_tx_headroom;
3437 	struct ethhdr eth;
3438 	struct ieee80211_tx_info *info;
3439 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3440 	struct ieee80211_tx_data tx;
3441 	ieee80211_tx_result r;
3442 	struct tid_ampdu_tx *tid_tx = NULL;
3443 	u8 tid = IEEE80211_NUM_TIDS;
3444 
3445 	/* control port protocol needs a lot of special handling */
3446 	if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3447 		return false;
3448 
3449 	/* only RFC 1042 SNAP */
3450 	if (ethertype < ETH_P_802_3_MIN)
3451 		return false;
3452 
3453 	/* don't handle TX status request here either */
3454 	if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3455 		return false;
3456 
3457 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3458 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3459 		tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3460 		if (tid_tx) {
3461 			if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3462 				return false;
3463 			if (tid_tx->timeout)
3464 				tid_tx->last_tx = jiffies;
3465 		}
3466 	}
3467 
3468 	/* after this point (skb is modified) we cannot return false */
3469 
3470 	if (skb_shared(skb)) {
3471 		struct sk_buff *tmp_skb = skb;
3472 
3473 		skb = skb_clone(skb, GFP_ATOMIC);
3474 		kfree_skb(tmp_skb);
3475 
3476 		if (!skb)
3477 			return true;
3478 	}
3479 
3480 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3481 	    ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3482 		return true;
3483 
3484 	/* will not be crypto-handled beyond what we do here, so use false
3485 	 * as the may-encrypt argument for the resize to not account for
3486 	 * more room than we already have in 'extra_head'
3487 	 */
3488 	if (unlikely(ieee80211_skb_resize(sdata, skb,
3489 					  max_t(int, extra_head + hw_headroom -
3490 						     skb_headroom(skb), 0),
3491 					  false))) {
3492 		kfree_skb(skb);
3493 		return true;
3494 	}
3495 
3496 	memcpy(&eth, skb->data, ETH_HLEN - 2);
3497 	hdr = skb_push(skb, extra_head);
3498 	memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3499 	memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3500 	memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3501 
3502 	info = IEEE80211_SKB_CB(skb);
3503 	memset(info, 0, sizeof(*info));
3504 	info->band = fast_tx->band;
3505 	info->control.vif = &sdata->vif;
3506 	info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3507 		      IEEE80211_TX_CTL_DONTFRAG |
3508 		      (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3509 	info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
3510 
3511 #ifdef CONFIG_MAC80211_DEBUGFS
3512 	if (local->force_tx_status)
3513 		info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3514 #endif
3515 
3516 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3517 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3518 		*ieee80211_get_qos_ctl(hdr) = tid;
3519 	}
3520 
3521 	__skb_queue_head_init(&tx.skbs);
3522 
3523 	tx.flags = IEEE80211_TX_UNICAST;
3524 	tx.local = local;
3525 	tx.sdata = sdata;
3526 	tx.sta = sta;
3527 	tx.key = fast_tx->key;
3528 
3529 	if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3530 		tx.skb = skb;
3531 		r = ieee80211_tx_h_rate_ctrl(&tx);
3532 		skb = tx.skb;
3533 		tx.skb = NULL;
3534 
3535 		if (r != TX_CONTINUE) {
3536 			if (r != TX_QUEUED)
3537 				kfree_skb(skb);
3538 			return true;
3539 		}
3540 	}
3541 
3542 	if (ieee80211_queue_skb(local, sdata, sta, skb))
3543 		return true;
3544 
3545 	ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3546 				   fast_tx->key, skb);
3547 
3548 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3549 		sdata = container_of(sdata->bss,
3550 				     struct ieee80211_sub_if_data, u.ap);
3551 
3552 	__skb_queue_tail(&tx.skbs, skb);
3553 	ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3554 	return true;
3555 }
3556 
3557 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3558 				     struct ieee80211_txq *txq)
3559 {
3560 	struct ieee80211_local *local = hw_to_local(hw);
3561 	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3562 	struct ieee80211_hdr *hdr;
3563 	struct sk_buff *skb = NULL;
3564 	struct fq *fq = &local->fq;
3565 	struct fq_tin *tin = &txqi->tin;
3566 	struct ieee80211_tx_info *info;
3567 	struct ieee80211_tx_data tx;
3568 	ieee80211_tx_result r;
3569 	struct ieee80211_vif *vif = txq->vif;
3570 
3571 	WARN_ON_ONCE(softirq_count() == 0);
3572 
3573 	if (!ieee80211_txq_airtime_check(hw, txq))
3574 		return NULL;
3575 
3576 begin:
3577 	spin_lock_bh(&fq->lock);
3578 
3579 	if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3580 	    test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
3581 		goto out;
3582 
3583 	if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3584 		set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3585 		goto out;
3586 	}
3587 
3588 	/* Make sure fragments stay together. */
3589 	skb = __skb_dequeue(&txqi->frags);
3590 	if (skb)
3591 		goto out;
3592 
3593 	skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3594 	if (!skb)
3595 		goto out;
3596 
3597 	spin_unlock_bh(&fq->lock);
3598 
3599 	hdr = (struct ieee80211_hdr *)skb->data;
3600 	info = IEEE80211_SKB_CB(skb);
3601 
3602 	memset(&tx, 0, sizeof(tx));
3603 	__skb_queue_head_init(&tx.skbs);
3604 	tx.local = local;
3605 	tx.skb = skb;
3606 	tx.sdata = vif_to_sdata(info->control.vif);
3607 
3608 	if (txq->sta) {
3609 		tx.sta = container_of(txq->sta, struct sta_info, sta);
3610 		/*
3611 		 * Drop unicast frames to unauthorised stations unless they are
3612 		 * EAPOL frames from the local station.
3613 		 */
3614 		if (unlikely(ieee80211_is_data(hdr->frame_control) &&
3615 			     !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
3616 			     tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3617 			     !is_multicast_ether_addr(hdr->addr1) &&
3618 			     !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3619 			     (!(info->control.flags &
3620 				IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3621 			      !ether_addr_equal(tx.sdata->vif.addr,
3622 						hdr->addr2)))) {
3623 			I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3624 			ieee80211_free_txskb(&local->hw, skb);
3625 			goto begin;
3626 		}
3627 	}
3628 
3629 	/*
3630 	 * The key can be removed while the packet was queued, so need to call
3631 	 * this here to get the current key.
3632 	 */
3633 	r = ieee80211_tx_h_select_key(&tx);
3634 	if (r != TX_CONTINUE) {
3635 		ieee80211_free_txskb(&local->hw, skb);
3636 		goto begin;
3637 	}
3638 
3639 	if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3640 		info->flags |= IEEE80211_TX_CTL_AMPDU;
3641 	else
3642 		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3643 
3644 	if (info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP)
3645 		goto encap_out;
3646 
3647 	if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3648 		struct sta_info *sta = container_of(txq->sta, struct sta_info,
3649 						    sta);
3650 		u8 pn_offs = 0;
3651 
3652 		if (tx.key &&
3653 		    (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3654 			pn_offs = ieee80211_hdrlen(hdr->frame_control);
3655 
3656 		ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3657 					   tx.key, skb);
3658 	} else {
3659 		if (invoke_tx_handlers_late(&tx))
3660 			goto begin;
3661 
3662 		skb = __skb_dequeue(&tx.skbs);
3663 
3664 		if (!skb_queue_empty(&tx.skbs)) {
3665 			spin_lock_bh(&fq->lock);
3666 			skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3667 			spin_unlock_bh(&fq->lock);
3668 		}
3669 	}
3670 
3671 	if (skb_has_frag_list(skb) &&
3672 	    !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3673 		if (skb_linearize(skb)) {
3674 			ieee80211_free_txskb(&local->hw, skb);
3675 			goto begin;
3676 		}
3677 	}
3678 
3679 	switch (tx.sdata->vif.type) {
3680 	case NL80211_IFTYPE_MONITOR:
3681 		if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3682 			vif = &tx.sdata->vif;
3683 			break;
3684 		}
3685 		tx.sdata = rcu_dereference(local->monitor_sdata);
3686 		if (tx.sdata) {
3687 			vif = &tx.sdata->vif;
3688 			info->hw_queue =
3689 				vif->hw_queue[skb_get_queue_mapping(skb)];
3690 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3691 			ieee80211_free_txskb(&local->hw, skb);
3692 			goto begin;
3693 		} else {
3694 			vif = NULL;
3695 		}
3696 		break;
3697 	case NL80211_IFTYPE_AP_VLAN:
3698 		tx.sdata = container_of(tx.sdata->bss,
3699 					struct ieee80211_sub_if_data, u.ap);
3700 		/* fall through */
3701 	default:
3702 		vif = &tx.sdata->vif;
3703 		break;
3704 	}
3705 
3706 encap_out:
3707 	IEEE80211_SKB_CB(skb)->control.vif = vif;
3708 
3709 	if (vif &&
3710 	    wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
3711 		u32 airtime;
3712 
3713 		airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3714 							     skb->len);
3715 		if (airtime) {
3716 			airtime = ieee80211_info_set_tx_time_est(info, airtime);
3717 			ieee80211_sta_update_pending_airtime(local, tx.sta,
3718 							     txq->ac,
3719 							     airtime,
3720 							     false);
3721 		}
3722 	}
3723 
3724 	return skb;
3725 
3726 out:
3727 	spin_unlock_bh(&fq->lock);
3728 
3729 	return skb;
3730 }
3731 EXPORT_SYMBOL(ieee80211_tx_dequeue);
3732 
3733 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3734 {
3735 	struct ieee80211_local *local = hw_to_local(hw);
3736 	struct ieee80211_txq *ret = NULL;
3737 	struct txq_info *txqi = NULL, *head = NULL;
3738 	bool found_eligible_txq = false;
3739 
3740 	spin_lock_bh(&local->active_txq_lock[ac]);
3741 
3742  begin:
3743 	txqi = list_first_entry_or_null(&local->active_txqs[ac],
3744 					struct txq_info,
3745 					schedule_order);
3746 	if (!txqi)
3747 		goto out;
3748 
3749 	if (txqi == head) {
3750 		if (!found_eligible_txq)
3751 			goto out;
3752 		else
3753 			found_eligible_txq = false;
3754 	}
3755 
3756 	if (!head)
3757 		head = txqi;
3758 
3759 	if (txqi->txq.sta) {
3760 		struct sta_info *sta = container_of(txqi->txq.sta,
3761 						    struct sta_info, sta);
3762 		bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3763 		s64 deficit = sta->airtime[txqi->txq.ac].deficit;
3764 
3765 		if (aql_check)
3766 			found_eligible_txq = true;
3767 
3768 		if (deficit < 0)
3769 			sta->airtime[txqi->txq.ac].deficit +=
3770 				sta->airtime_weight;
3771 
3772 		if (deficit < 0 || !aql_check) {
3773 			list_move_tail(&txqi->schedule_order,
3774 				       &local->active_txqs[txqi->txq.ac]);
3775 			goto begin;
3776 		}
3777 	}
3778 
3779 
3780 	if (txqi->schedule_round == local->schedule_round[ac])
3781 		goto out;
3782 
3783 	list_del_init(&txqi->schedule_order);
3784 	txqi->schedule_round = local->schedule_round[ac];
3785 	ret = &txqi->txq;
3786 
3787 out:
3788 	spin_unlock_bh(&local->active_txq_lock[ac]);
3789 	return ret;
3790 }
3791 EXPORT_SYMBOL(ieee80211_next_txq);
3792 
3793 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3794 			      struct ieee80211_txq *txq,
3795 			      bool force)
3796 {
3797 	struct ieee80211_local *local = hw_to_local(hw);
3798 	struct txq_info *txqi = to_txq_info(txq);
3799 
3800 	spin_lock_bh(&local->active_txq_lock[txq->ac]);
3801 
3802 	if (list_empty(&txqi->schedule_order) &&
3803 	    (force || !skb_queue_empty(&txqi->frags) ||
3804 	     txqi->tin.backlog_packets)) {
3805 		/* If airtime accounting is active, always enqueue STAs at the
3806 		 * head of the list to ensure that they only get moved to the
3807 		 * back by the airtime DRR scheduler once they have a negative
3808 		 * deficit. A station that already has a negative deficit will
3809 		 * get immediately moved to the back of the list on the next
3810 		 * call to ieee80211_next_txq().
3811 		 */
3812 		if (txqi->txq.sta &&
3813 		    wiphy_ext_feature_isset(local->hw.wiphy,
3814 					    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3815 			list_add(&txqi->schedule_order,
3816 				 &local->active_txqs[txq->ac]);
3817 		else
3818 			list_add_tail(&txqi->schedule_order,
3819 				      &local->active_txqs[txq->ac]);
3820 	}
3821 
3822 	spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3823 }
3824 EXPORT_SYMBOL(__ieee80211_schedule_txq);
3825 
3826 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3827 				 struct ieee80211_txq *txq)
3828 {
3829 	struct sta_info *sta;
3830 	struct ieee80211_local *local = hw_to_local(hw);
3831 
3832 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
3833 		return true;
3834 
3835 	if (!txq->sta)
3836 		return true;
3837 
3838 	sta = container_of(txq->sta, struct sta_info, sta);
3839 	if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3840 	    sta->airtime[txq->ac].aql_limit_low)
3841 		return true;
3842 
3843 	if (atomic_read(&local->aql_total_pending_airtime) <
3844 	    local->aql_threshold &&
3845 	    atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3846 	    sta->airtime[txq->ac].aql_limit_high)
3847 		return true;
3848 
3849 	return false;
3850 }
3851 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3852 
3853 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3854 				struct ieee80211_txq *txq)
3855 {
3856 	struct ieee80211_local *local = hw_to_local(hw);
3857 	struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3858 	struct sta_info *sta;
3859 	u8 ac = txq->ac;
3860 
3861 	spin_lock_bh(&local->active_txq_lock[ac]);
3862 
3863 	if (!txqi->txq.sta)
3864 		goto out;
3865 
3866 	if (list_empty(&txqi->schedule_order))
3867 		goto out;
3868 
3869 	list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3870 				 schedule_order) {
3871 		if (iter == txqi)
3872 			break;
3873 
3874 		if (!iter->txq.sta) {
3875 			list_move_tail(&iter->schedule_order,
3876 				       &local->active_txqs[ac]);
3877 			continue;
3878 		}
3879 		sta = container_of(iter->txq.sta, struct sta_info, sta);
3880 		if (sta->airtime[ac].deficit < 0)
3881 			sta->airtime[ac].deficit += sta->airtime_weight;
3882 		list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3883 	}
3884 
3885 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
3886 	if (sta->airtime[ac].deficit >= 0)
3887 		goto out;
3888 
3889 	sta->airtime[ac].deficit += sta->airtime_weight;
3890 	list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
3891 	spin_unlock_bh(&local->active_txq_lock[ac]);
3892 
3893 	return false;
3894 out:
3895 	if (!list_empty(&txqi->schedule_order))
3896 		list_del_init(&txqi->schedule_order);
3897 	spin_unlock_bh(&local->active_txq_lock[ac]);
3898 
3899 	return true;
3900 }
3901 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3902 
3903 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
3904 {
3905 	struct ieee80211_local *local = hw_to_local(hw);
3906 
3907 	spin_lock_bh(&local->active_txq_lock[ac]);
3908 	local->schedule_round[ac]++;
3909 	spin_unlock_bh(&local->active_txq_lock[ac]);
3910 }
3911 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
3912 
3913 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3914 				  struct net_device *dev,
3915 				  u32 info_flags,
3916 				  u32 ctrl_flags)
3917 {
3918 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3919 	struct ieee80211_local *local = sdata->local;
3920 	struct sta_info *sta;
3921 	struct sk_buff *next;
3922 
3923 	if (unlikely(skb->len < ETH_HLEN)) {
3924 		kfree_skb(skb);
3925 		return;
3926 	}
3927 
3928 	rcu_read_lock();
3929 
3930 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3931 		goto out_free;
3932 
3933 	if (IS_ERR(sta))
3934 		sta = NULL;
3935 
3936 	if (local->ops->wake_tx_queue) {
3937 		u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3938 		skb_set_queue_mapping(skb, queue);
3939 	}
3940 
3941 	if (sta) {
3942 		struct ieee80211_fast_tx *fast_tx;
3943 
3944 		sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
3945 
3946 		fast_tx = rcu_dereference(sta->fast_tx);
3947 
3948 		if (fast_tx &&
3949 		    ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
3950 			goto out;
3951 	}
3952 
3953 	if (skb_is_gso(skb)) {
3954 		struct sk_buff *segs;
3955 
3956 		segs = skb_gso_segment(skb, 0);
3957 		if (IS_ERR(segs)) {
3958 			goto out_free;
3959 		} else if (segs) {
3960 			consume_skb(skb);
3961 			skb = segs;
3962 		}
3963 	} else {
3964 		/* we cannot process non-linear frames on this path */
3965 		if (skb_linearize(skb)) {
3966 			kfree_skb(skb);
3967 			goto out;
3968 		}
3969 
3970 		/* the frame could be fragmented, software-encrypted, and other
3971 		 * things so we cannot really handle checksum offload with it -
3972 		 * fix it up in software before we handle anything else.
3973 		 */
3974 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
3975 			skb_set_transport_header(skb,
3976 						 skb_checksum_start_offset(skb));
3977 			if (skb_checksum_help(skb))
3978 				goto out_free;
3979 		}
3980 	}
3981 
3982 	skb_list_walk_safe(skb, skb, next) {
3983 		skb_mark_not_on_list(skb);
3984 
3985 		skb = ieee80211_build_hdr(sdata, skb, info_flags,
3986 					  sta, ctrl_flags);
3987 		if (IS_ERR(skb)) {
3988 			kfree_skb_list(next);
3989 			goto out;
3990 		}
3991 
3992 		ieee80211_tx_stats(dev, skb->len);
3993 
3994 		ieee80211_xmit(sdata, sta, skb, 0);
3995 	}
3996 	goto out;
3997  out_free:
3998 	kfree_skb(skb);
3999  out:
4000 	rcu_read_unlock();
4001 }
4002 
4003 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4004 {
4005 	struct ethhdr *eth;
4006 	int err;
4007 
4008 	err = skb_ensure_writable(skb, ETH_HLEN);
4009 	if (unlikely(err))
4010 		return err;
4011 
4012 	eth = (void *)skb->data;
4013 	ether_addr_copy(eth->h_dest, sta->sta.addr);
4014 
4015 	return 0;
4016 }
4017 
4018 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4019 					   struct net_device *dev)
4020 {
4021 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4022 	const struct ethhdr *eth = (void *)skb->data;
4023 	const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4024 	__be16 ethertype;
4025 
4026 	if (likely(!is_multicast_ether_addr(eth->h_dest)))
4027 		return false;
4028 
4029 	switch (sdata->vif.type) {
4030 	case NL80211_IFTYPE_AP_VLAN:
4031 		if (sdata->u.vlan.sta)
4032 			return false;
4033 		if (sdata->wdev.use_4addr)
4034 			return false;
4035 		/* fall through */
4036 	case NL80211_IFTYPE_AP:
4037 		/* check runtime toggle for this bss */
4038 		if (!sdata->bss->multicast_to_unicast)
4039 			return false;
4040 		break;
4041 	default:
4042 		return false;
4043 	}
4044 
4045 	/* multicast to unicast conversion only for some payload */
4046 	ethertype = eth->h_proto;
4047 	if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4048 		ethertype = ethvlan->h_vlan_encapsulated_proto;
4049 	switch (ethertype) {
4050 	case htons(ETH_P_ARP):
4051 	case htons(ETH_P_IP):
4052 	case htons(ETH_P_IPV6):
4053 		break;
4054 	default:
4055 		return false;
4056 	}
4057 
4058 	return true;
4059 }
4060 
4061 static void
4062 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4063 			     struct sk_buff_head *queue)
4064 {
4065 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4066 	struct ieee80211_local *local = sdata->local;
4067 	const struct ethhdr *eth = (struct ethhdr *)skb->data;
4068 	struct sta_info *sta, *first = NULL;
4069 	struct sk_buff *cloned_skb;
4070 
4071 	rcu_read_lock();
4072 
4073 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
4074 		if (sdata != sta->sdata)
4075 			/* AP-VLAN mismatch */
4076 			continue;
4077 		if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4078 			/* do not send back to source */
4079 			continue;
4080 		if (!first) {
4081 			first = sta;
4082 			continue;
4083 		}
4084 		cloned_skb = skb_clone(skb, GFP_ATOMIC);
4085 		if (!cloned_skb)
4086 			goto multicast;
4087 		if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4088 			dev_kfree_skb(cloned_skb);
4089 			goto multicast;
4090 		}
4091 		__skb_queue_tail(queue, cloned_skb);
4092 	}
4093 
4094 	if (likely(first)) {
4095 		if (unlikely(ieee80211_change_da(skb, first)))
4096 			goto multicast;
4097 		__skb_queue_tail(queue, skb);
4098 	} else {
4099 		/* no STA connected, drop */
4100 		kfree_skb(skb);
4101 		skb = NULL;
4102 	}
4103 
4104 	goto out;
4105 multicast:
4106 	__skb_queue_purge(queue);
4107 	__skb_queue_tail(queue, skb);
4108 out:
4109 	rcu_read_unlock();
4110 }
4111 
4112 /**
4113  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4114  * @skb: packet to be sent
4115  * @dev: incoming interface
4116  *
4117  * On failure skb will be freed.
4118  */
4119 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4120 				       struct net_device *dev)
4121 {
4122 	if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4123 		struct sk_buff_head queue;
4124 
4125 		__skb_queue_head_init(&queue);
4126 		ieee80211_convert_to_unicast(skb, dev, &queue);
4127 		while ((skb = __skb_dequeue(&queue)))
4128 			__ieee80211_subif_start_xmit(skb, dev, 0, 0);
4129 	} else {
4130 		__ieee80211_subif_start_xmit(skb, dev, 0, 0);
4131 	}
4132 
4133 	return NETDEV_TX_OK;
4134 }
4135 
4136 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4137 			      struct sk_buff *skb, int led_len,
4138 			      struct sta_info *sta,
4139 			      bool txpending)
4140 {
4141 	struct ieee80211_local *local = sdata->local;
4142 	struct ieee80211_tx_control control = {};
4143 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4144 	struct ieee80211_sta *pubsta = NULL;
4145 	unsigned long flags;
4146 	int q = info->hw_queue;
4147 
4148 	if (ieee80211_queue_skb(local, sdata, sta, skb))
4149 		return true;
4150 
4151 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4152 
4153 	if (local->queue_stop_reasons[q] ||
4154 	    (!txpending && !skb_queue_empty(&local->pending[q]))) {
4155 		if (txpending)
4156 			skb_queue_head(&local->pending[q], skb);
4157 		else
4158 			skb_queue_tail(&local->pending[q], skb);
4159 
4160 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4161 
4162 		return false;
4163 	}
4164 
4165 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4166 
4167 	if (sta && sta->uploaded)
4168 		pubsta = &sta->sta;
4169 
4170 	control.sta = pubsta;
4171 
4172 	drv_tx(local, &control, skb);
4173 
4174 	return true;
4175 }
4176 
4177 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4178 				struct net_device *dev, struct sta_info *sta,
4179 				struct sk_buff *skb)
4180 {
4181 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4182 	struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4183 	struct ieee80211_local *local = sdata->local;
4184 	bool authorized = false;
4185 	bool multicast;
4186 	unsigned char *ra = ehdr->h_dest;
4187 
4188 	if (IS_ERR(sta) || (sta && !sta->uploaded))
4189 		sta = NULL;
4190 
4191 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
4192 	    (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER)))
4193 		ra = sdata->u.mgd.bssid;
4194 
4195 	if (!is_valid_ether_addr(ra))
4196 		goto out_free;
4197 
4198 	multicast = is_multicast_ether_addr(ra);
4199 
4200 	if (sta)
4201 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
4202 
4203 	if (!multicast && !authorized &&
4204 	    (ehdr->h_proto != sdata->control_port_protocol ||
4205 	     !ether_addr_equal(sdata->vif.addr, ehdr->h_source)))
4206 		goto out_free;
4207 
4208 	if (multicast && sdata->vif.type == NL80211_IFTYPE_AP &&
4209 	    !atomic_read(&sdata->u.ap.num_mcast_sta))
4210 		goto out_free;
4211 
4212 	if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4213 	    test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4214 		goto out_free;
4215 
4216 	if (unlikely(!multicast && skb->sk &&
4217 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
4218 		ieee80211_store_ack_skb(local, skb, &info->flags);
4219 
4220 	memset(info, 0, sizeof(*info));
4221 
4222 	if (unlikely(sdata->control_port_protocol == ehdr->h_proto)) {
4223 		if (sdata->control_port_no_encrypt)
4224 			info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4225 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
4226 	}
4227 
4228 	if (multicast)
4229 		info->flags |= IEEE80211_TX_CTL_NO_ACK;
4230 
4231 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4232 
4233 	ieee80211_tx_stats(dev, skb->len);
4234 
4235 	if (sta) {
4236 		sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4237 		sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
4238 	}
4239 
4240 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4241 		sdata = container_of(sdata->bss,
4242 				     struct ieee80211_sub_if_data, u.ap);
4243 
4244 	info->control.flags |= IEEE80211_TX_CTRL_HW_80211_ENCAP;
4245 	info->control.vif = &sdata->vif;
4246 
4247 	ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4248 
4249 	return;
4250 
4251 out_free:
4252 	kfree_skb(skb);
4253 }
4254 
4255 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4256 					    struct net_device *dev)
4257 {
4258 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4259 	struct sta_info *sta;
4260 
4261 	if (WARN_ON(!sdata->hw_80211_encap)) {
4262 		kfree_skb(skb);
4263 		return NETDEV_TX_OK;
4264 	}
4265 
4266 	if (unlikely(skb->len < ETH_HLEN)) {
4267 		kfree_skb(skb);
4268 		return NETDEV_TX_OK;
4269 	}
4270 
4271 	rcu_read_lock();
4272 
4273 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4274 		kfree_skb(skb);
4275 	else
4276 		ieee80211_8023_xmit(sdata, dev, sta, skb);
4277 
4278 	rcu_read_unlock();
4279 
4280 	return NETDEV_TX_OK;
4281 }
4282 
4283 struct sk_buff *
4284 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4285 			      struct sk_buff *skb, u32 info_flags)
4286 {
4287 	struct ieee80211_hdr *hdr;
4288 	struct ieee80211_tx_data tx = {
4289 		.local = sdata->local,
4290 		.sdata = sdata,
4291 	};
4292 	struct sta_info *sta;
4293 
4294 	rcu_read_lock();
4295 
4296 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4297 		kfree_skb(skb);
4298 		skb = ERR_PTR(-EINVAL);
4299 		goto out;
4300 	}
4301 
4302 	skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
4303 	if (IS_ERR(skb))
4304 		goto out;
4305 
4306 	hdr = (void *)skb->data;
4307 	tx.sta = sta_info_get(sdata, hdr->addr1);
4308 	tx.skb = skb;
4309 
4310 	if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4311 		rcu_read_unlock();
4312 		kfree_skb(skb);
4313 		return ERR_PTR(-EINVAL);
4314 	}
4315 
4316 out:
4317 	rcu_read_unlock();
4318 	return skb;
4319 }
4320 
4321 /*
4322  * ieee80211_clear_tx_pending may not be called in a context where
4323  * it is possible that it packets could come in again.
4324  */
4325 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4326 {
4327 	struct sk_buff *skb;
4328 	int i;
4329 
4330 	for (i = 0; i < local->hw.queues; i++) {
4331 		while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4332 			ieee80211_free_txskb(&local->hw, skb);
4333 	}
4334 }
4335 
4336 /*
4337  * Returns false if the frame couldn't be transmitted but was queued instead,
4338  * which in this case means re-queued -- take as an indication to stop sending
4339  * more pending frames.
4340  */
4341 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4342 				     struct sk_buff *skb)
4343 {
4344 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4345 	struct ieee80211_sub_if_data *sdata;
4346 	struct sta_info *sta;
4347 	struct ieee80211_hdr *hdr;
4348 	bool result;
4349 	struct ieee80211_chanctx_conf *chanctx_conf;
4350 
4351 	sdata = vif_to_sdata(info->control.vif);
4352 
4353 	if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
4354 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4355 		if (unlikely(!chanctx_conf)) {
4356 			dev_kfree_skb(skb);
4357 			return true;
4358 		}
4359 		info->band = chanctx_conf->def.chan->band;
4360 		result = ieee80211_tx(sdata, NULL, skb, true, 0);
4361 	} else if (info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP) {
4362 		if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4363 			dev_kfree_skb(skb);
4364 			return true;
4365 		}
4366 
4367 		if (IS_ERR(sta) || (sta && !sta->uploaded))
4368 			sta = NULL;
4369 
4370 		result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
4371 	} else {
4372 		struct sk_buff_head skbs;
4373 
4374 		__skb_queue_head_init(&skbs);
4375 		__skb_queue_tail(&skbs, skb);
4376 
4377 		hdr = (struct ieee80211_hdr *)skb->data;
4378 		sta = sta_info_get(sdata, hdr->addr1);
4379 
4380 		result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
4381 	}
4382 
4383 	return result;
4384 }
4385 
4386 /*
4387  * Transmit all pending packets. Called from tasklet.
4388  */
4389 void ieee80211_tx_pending(unsigned long data)
4390 {
4391 	struct ieee80211_local *local = (struct ieee80211_local *)data;
4392 	unsigned long flags;
4393 	int i;
4394 	bool txok;
4395 
4396 	rcu_read_lock();
4397 
4398 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4399 	for (i = 0; i < local->hw.queues; i++) {
4400 		/*
4401 		 * If queue is stopped by something other than due to pending
4402 		 * frames, or we have no pending frames, proceed to next queue.
4403 		 */
4404 		if (local->queue_stop_reasons[i] ||
4405 		    skb_queue_empty(&local->pending[i]))
4406 			continue;
4407 
4408 		while (!skb_queue_empty(&local->pending[i])) {
4409 			struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
4410 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4411 
4412 			if (WARN_ON(!info->control.vif)) {
4413 				ieee80211_free_txskb(&local->hw, skb);
4414 				continue;
4415 			}
4416 
4417 			spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4418 						flags);
4419 
4420 			txok = ieee80211_tx_pending_skb(local, skb);
4421 			spin_lock_irqsave(&local->queue_stop_reason_lock,
4422 					  flags);
4423 			if (!txok)
4424 				break;
4425 		}
4426 
4427 		if (skb_queue_empty(&local->pending[i]))
4428 			ieee80211_propagate_queue_wake(local, i);
4429 	}
4430 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4431 
4432 	rcu_read_unlock();
4433 }
4434 
4435 /* functions for drivers to get certain frames */
4436 
4437 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4438 				       struct ps_data *ps, struct sk_buff *skb,
4439 				       bool is_template)
4440 {
4441 	u8 *pos, *tim;
4442 	int aid0 = 0;
4443 	int i, have_bits = 0, n1, n2;
4444 
4445 	/* Generate bitmap for TIM only if there are any STAs in power save
4446 	 * mode. */
4447 	if (atomic_read(&ps->num_sta_ps) > 0)
4448 		/* in the hope that this is faster than
4449 		 * checking byte-for-byte */
4450 		have_bits = !bitmap_empty((unsigned long *)ps->tim,
4451 					  IEEE80211_MAX_AID+1);
4452 	if (!is_template) {
4453 		if (ps->dtim_count == 0)
4454 			ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4455 		else
4456 			ps->dtim_count--;
4457 	}
4458 
4459 	tim = pos = skb_put(skb, 6);
4460 	*pos++ = WLAN_EID_TIM;
4461 	*pos++ = 4;
4462 	*pos++ = ps->dtim_count;
4463 	*pos++ = sdata->vif.bss_conf.dtim_period;
4464 
4465 	if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4466 		aid0 = 1;
4467 
4468 	ps->dtim_bc_mc = aid0 == 1;
4469 
4470 	if (have_bits) {
4471 		/* Find largest even number N1 so that bits numbered 1 through
4472 		 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4473 		 * (N2 + 1) x 8 through 2007 are 0. */
4474 		n1 = 0;
4475 		for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4476 			if (ps->tim[i]) {
4477 				n1 = i & 0xfe;
4478 				break;
4479 			}
4480 		}
4481 		n2 = n1;
4482 		for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4483 			if (ps->tim[i]) {
4484 				n2 = i;
4485 				break;
4486 			}
4487 		}
4488 
4489 		/* Bitmap control */
4490 		*pos++ = n1 | aid0;
4491 		/* Part Virt Bitmap */
4492 		skb_put(skb, n2 - n1);
4493 		memcpy(pos, ps->tim + n1, n2 - n1 + 1);
4494 
4495 		tim[1] = n2 - n1 + 4;
4496 	} else {
4497 		*pos++ = aid0; /* Bitmap control */
4498 		*pos++ = 0; /* Part Virt Bitmap */
4499 	}
4500 }
4501 
4502 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4503 				    struct ps_data *ps, struct sk_buff *skb,
4504 				    bool is_template)
4505 {
4506 	struct ieee80211_local *local = sdata->local;
4507 
4508 	/*
4509 	 * Not very nice, but we want to allow the driver to call
4510 	 * ieee80211_beacon_get() as a response to the set_tim()
4511 	 * callback. That, however, is already invoked under the
4512 	 * sta_lock to guarantee consistent and race-free update
4513 	 * of the tim bitmap in mac80211 and the driver.
4514 	 */
4515 	if (local->tim_in_locked_section) {
4516 		__ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4517 	} else {
4518 		spin_lock_bh(&local->tim_lock);
4519 		__ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4520 		spin_unlock_bh(&local->tim_lock);
4521 	}
4522 
4523 	return 0;
4524 }
4525 
4526 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4527 			      struct beacon_data *beacon)
4528 {
4529 	struct probe_resp *resp;
4530 	u8 *beacon_data;
4531 	size_t beacon_data_len;
4532 	int i;
4533 	u8 count = beacon->csa_current_counter;
4534 
4535 	switch (sdata->vif.type) {
4536 	case NL80211_IFTYPE_AP:
4537 		beacon_data = beacon->tail;
4538 		beacon_data_len = beacon->tail_len;
4539 		break;
4540 	case NL80211_IFTYPE_ADHOC:
4541 		beacon_data = beacon->head;
4542 		beacon_data_len = beacon->head_len;
4543 		break;
4544 	case NL80211_IFTYPE_MESH_POINT:
4545 		beacon_data = beacon->head;
4546 		beacon_data_len = beacon->head_len;
4547 		break;
4548 	default:
4549 		return;
4550 	}
4551 
4552 	rcu_read_lock();
4553 	for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
4554 		resp = rcu_dereference(sdata->u.ap.probe_resp);
4555 
4556 		if (beacon->csa_counter_offsets[i]) {
4557 			if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4558 					 beacon_data_len)) {
4559 				rcu_read_unlock();
4560 				return;
4561 			}
4562 
4563 			beacon_data[beacon->csa_counter_offsets[i]] = count;
4564 		}
4565 
4566 		if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4567 			resp->data[resp->csa_counter_offsets[i]] = count;
4568 	}
4569 	rcu_read_unlock();
4570 }
4571 
4572 static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4573 {
4574 	beacon->csa_current_counter--;
4575 
4576 	/* the counter should never reach 0 */
4577 	WARN_ON_ONCE(!beacon->csa_current_counter);
4578 
4579 	return beacon->csa_current_counter;
4580 }
4581 
4582 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4583 {
4584 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4585 	struct beacon_data *beacon = NULL;
4586 	u8 count = 0;
4587 
4588 	rcu_read_lock();
4589 
4590 	if (sdata->vif.type == NL80211_IFTYPE_AP)
4591 		beacon = rcu_dereference(sdata->u.ap.beacon);
4592 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4593 		beacon = rcu_dereference(sdata->u.ibss.presp);
4594 	else if (ieee80211_vif_is_mesh(&sdata->vif))
4595 		beacon = rcu_dereference(sdata->u.mesh.beacon);
4596 
4597 	if (!beacon)
4598 		goto unlock;
4599 
4600 	count = __ieee80211_csa_update_counter(beacon);
4601 
4602 unlock:
4603 	rcu_read_unlock();
4604 	return count;
4605 }
4606 EXPORT_SYMBOL(ieee80211_csa_update_counter);
4607 
4608 void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4609 {
4610 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4611 	struct beacon_data *beacon = NULL;
4612 
4613 	rcu_read_lock();
4614 
4615 	if (sdata->vif.type == NL80211_IFTYPE_AP)
4616 		beacon = rcu_dereference(sdata->u.ap.beacon);
4617 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4618 		beacon = rcu_dereference(sdata->u.ibss.presp);
4619 	else if (ieee80211_vif_is_mesh(&sdata->vif))
4620 		beacon = rcu_dereference(sdata->u.mesh.beacon);
4621 
4622 	if (!beacon)
4623 		goto unlock;
4624 
4625 	if (counter < beacon->csa_current_counter)
4626 		beacon->csa_current_counter = counter;
4627 
4628 unlock:
4629 	rcu_read_unlock();
4630 }
4631 EXPORT_SYMBOL(ieee80211_csa_set_counter);
4632 
4633 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4634 {
4635 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4636 	struct beacon_data *beacon = NULL;
4637 	u8 *beacon_data;
4638 	size_t beacon_data_len;
4639 	int ret = false;
4640 
4641 	if (!ieee80211_sdata_running(sdata))
4642 		return false;
4643 
4644 	rcu_read_lock();
4645 	if (vif->type == NL80211_IFTYPE_AP) {
4646 		struct ieee80211_if_ap *ap = &sdata->u.ap;
4647 
4648 		beacon = rcu_dereference(ap->beacon);
4649 		if (WARN_ON(!beacon || !beacon->tail))
4650 			goto out;
4651 		beacon_data = beacon->tail;
4652 		beacon_data_len = beacon->tail_len;
4653 	} else if (vif->type == NL80211_IFTYPE_ADHOC) {
4654 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4655 
4656 		beacon = rcu_dereference(ifibss->presp);
4657 		if (!beacon)
4658 			goto out;
4659 
4660 		beacon_data = beacon->head;
4661 		beacon_data_len = beacon->head_len;
4662 	} else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4663 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4664 
4665 		beacon = rcu_dereference(ifmsh->beacon);
4666 		if (!beacon)
4667 			goto out;
4668 
4669 		beacon_data = beacon->head;
4670 		beacon_data_len = beacon->head_len;
4671 	} else {
4672 		WARN_ON(1);
4673 		goto out;
4674 	}
4675 
4676 	if (!beacon->csa_counter_offsets[0])
4677 		goto out;
4678 
4679 	if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
4680 		goto out;
4681 
4682 	if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
4683 		ret = true;
4684  out:
4685 	rcu_read_unlock();
4686 
4687 	return ret;
4688 }
4689 EXPORT_SYMBOL(ieee80211_csa_is_complete);
4690 
4691 static int ieee80211_beacon_protect(struct sk_buff *skb,
4692 				    struct ieee80211_local *local,
4693 				    struct ieee80211_sub_if_data *sdata)
4694 {
4695 	ieee80211_tx_result res;
4696 	struct ieee80211_tx_data tx;
4697 	struct sk_buff *check_skb;
4698 
4699 	memset(&tx, 0, sizeof(tx));
4700 	tx.key = rcu_dereference(sdata->default_beacon_key);
4701 	if (!tx.key)
4702 		return 0;
4703 	tx.local = local;
4704 	tx.sdata = sdata;
4705 	__skb_queue_head_init(&tx.skbs);
4706 	__skb_queue_tail(&tx.skbs, skb);
4707 	res = ieee80211_tx_h_encrypt(&tx);
4708 	check_skb = __skb_dequeue(&tx.skbs);
4709 	/* we may crash after this, but it'd be a bug in crypto */
4710 	WARN_ON(check_skb != skb);
4711 	if (WARN_ON_ONCE(res != TX_CONTINUE))
4712 		return -EINVAL;
4713 
4714 	return 0;
4715 }
4716 
4717 static struct sk_buff *
4718 __ieee80211_beacon_get(struct ieee80211_hw *hw,
4719 		       struct ieee80211_vif *vif,
4720 		       struct ieee80211_mutable_offsets *offs,
4721 		       bool is_template)
4722 {
4723 	struct ieee80211_local *local = hw_to_local(hw);
4724 	struct beacon_data *beacon = NULL;
4725 	struct sk_buff *skb = NULL;
4726 	struct ieee80211_tx_info *info;
4727 	struct ieee80211_sub_if_data *sdata = NULL;
4728 	enum nl80211_band band;
4729 	struct ieee80211_tx_rate_control txrc;
4730 	struct ieee80211_chanctx_conf *chanctx_conf;
4731 	int csa_off_base = 0;
4732 
4733 	rcu_read_lock();
4734 
4735 	sdata = vif_to_sdata(vif);
4736 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4737 
4738 	if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
4739 		goto out;
4740 
4741 	if (offs)
4742 		memset(offs, 0, sizeof(*offs));
4743 
4744 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
4745 		struct ieee80211_if_ap *ap = &sdata->u.ap;
4746 
4747 		beacon = rcu_dereference(ap->beacon);
4748 		if (beacon) {
4749 			if (beacon->csa_counter_offsets[0]) {
4750 				if (!is_template)
4751 					__ieee80211_csa_update_counter(beacon);
4752 
4753 				ieee80211_set_csa(sdata, beacon);
4754 			}
4755 
4756 			/*
4757 			 * headroom, head length,
4758 			 * tail length and maximum TIM length
4759 			 */
4760 			skb = dev_alloc_skb(local->tx_headroom +
4761 					    beacon->head_len +
4762 					    beacon->tail_len + 256 +
4763 					    local->hw.extra_beacon_tailroom);
4764 			if (!skb)
4765 				goto out;
4766 
4767 			skb_reserve(skb, local->tx_headroom);
4768 			skb_put_data(skb, beacon->head, beacon->head_len);
4769 
4770 			ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4771 						 is_template);
4772 
4773 			if (offs) {
4774 				offs->tim_offset = beacon->head_len;
4775 				offs->tim_length = skb->len - beacon->head_len;
4776 
4777 				/* for AP the csa offsets are from tail */
4778 				csa_off_base = skb->len;
4779 			}
4780 
4781 			if (beacon->tail)
4782 				skb_put_data(skb, beacon->tail,
4783 					     beacon->tail_len);
4784 
4785 			if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4786 				goto out;
4787 		} else
4788 			goto out;
4789 	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
4790 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4791 		struct ieee80211_hdr *hdr;
4792 
4793 		beacon = rcu_dereference(ifibss->presp);
4794 		if (!beacon)
4795 			goto out;
4796 
4797 		if (beacon->csa_counter_offsets[0]) {
4798 			if (!is_template)
4799 				__ieee80211_csa_update_counter(beacon);
4800 
4801 			ieee80211_set_csa(sdata, beacon);
4802 		}
4803 
4804 		skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
4805 				    local->hw.extra_beacon_tailroom);
4806 		if (!skb)
4807 			goto out;
4808 		skb_reserve(skb, local->tx_headroom);
4809 		skb_put_data(skb, beacon->head, beacon->head_len);
4810 
4811 		hdr = (struct ieee80211_hdr *) skb->data;
4812 		hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4813 						 IEEE80211_STYPE_BEACON);
4814 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4815 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4816 
4817 		beacon = rcu_dereference(ifmsh->beacon);
4818 		if (!beacon)
4819 			goto out;
4820 
4821 		if (beacon->csa_counter_offsets[0]) {
4822 			if (!is_template)
4823 				/* TODO: For mesh csa_counter is in TU, so
4824 				 * decrementing it by one isn't correct, but
4825 				 * for now we leave it consistent with overall
4826 				 * mac80211's behavior.
4827 				 */
4828 				__ieee80211_csa_update_counter(beacon);
4829 
4830 			ieee80211_set_csa(sdata, beacon);
4831 		}
4832 
4833 		if (ifmsh->sync_ops)
4834 			ifmsh->sync_ops->adjust_tsf(sdata, beacon);
4835 
4836 		skb = dev_alloc_skb(local->tx_headroom +
4837 				    beacon->head_len +
4838 				    256 + /* TIM IE */
4839 				    beacon->tail_len +
4840 				    local->hw.extra_beacon_tailroom);
4841 		if (!skb)
4842 			goto out;
4843 		skb_reserve(skb, local->tx_headroom);
4844 		skb_put_data(skb, beacon->head, beacon->head_len);
4845 		ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4846 
4847 		if (offs) {
4848 			offs->tim_offset = beacon->head_len;
4849 			offs->tim_length = skb->len - beacon->head_len;
4850 		}
4851 
4852 		skb_put_data(skb, beacon->tail, beacon->tail_len);
4853 	} else {
4854 		WARN_ON(1);
4855 		goto out;
4856 	}
4857 
4858 	/* CSA offsets */
4859 	if (offs && beacon) {
4860 		int i;
4861 
4862 		for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
4863 			u16 csa_off = beacon->csa_counter_offsets[i];
4864 
4865 			if (!csa_off)
4866 				continue;
4867 
4868 			offs->csa_counter_offs[i] = csa_off_base + csa_off;
4869 		}
4870 	}
4871 
4872 	band = chanctx_conf->def.chan->band;
4873 
4874 	info = IEEE80211_SKB_CB(skb);
4875 
4876 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4877 	info->flags |= IEEE80211_TX_CTL_NO_ACK;
4878 	info->band = band;
4879 
4880 	memset(&txrc, 0, sizeof(txrc));
4881 	txrc.hw = hw;
4882 	txrc.sband = local->hw.wiphy->bands[band];
4883 	txrc.bss_conf = &sdata->vif.bss_conf;
4884 	txrc.skb = skb;
4885 	txrc.reported_rate.idx = -1;
4886 	txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
4887 	txrc.bss = true;
4888 	rate_control_get_rate(sdata, NULL, &txrc);
4889 
4890 	info->control.vif = vif;
4891 
4892 	info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4893 			IEEE80211_TX_CTL_ASSIGN_SEQ |
4894 			IEEE80211_TX_CTL_FIRST_FRAGMENT;
4895  out:
4896 	rcu_read_unlock();
4897 	return skb;
4898 
4899 }
4900 
4901 struct sk_buff *
4902 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4903 			      struct ieee80211_vif *vif,
4904 			      struct ieee80211_mutable_offsets *offs)
4905 {
4906 	return __ieee80211_beacon_get(hw, vif, offs, true);
4907 }
4908 EXPORT_SYMBOL(ieee80211_beacon_get_template);
4909 
4910 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4911 					 struct ieee80211_vif *vif,
4912 					 u16 *tim_offset, u16 *tim_length)
4913 {
4914 	struct ieee80211_mutable_offsets offs = {};
4915 	struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
4916 	struct sk_buff *copy;
4917 	struct ieee80211_supported_band *sband;
4918 	int shift;
4919 
4920 	if (!bcn)
4921 		return bcn;
4922 
4923 	if (tim_offset)
4924 		*tim_offset = offs.tim_offset;
4925 
4926 	if (tim_length)
4927 		*tim_length = offs.tim_length;
4928 
4929 	if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4930 	    !hw_to_local(hw)->monitors)
4931 		return bcn;
4932 
4933 	/* send a copy to monitor interfaces */
4934 	copy = skb_copy(bcn, GFP_ATOMIC);
4935 	if (!copy)
4936 		return bcn;
4937 
4938 	shift = ieee80211_vif_get_shift(vif);
4939 	sband = ieee80211_get_sband(vif_to_sdata(vif));
4940 	if (!sband)
4941 		return bcn;
4942 
4943 	ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4944 			     NULL);
4945 
4946 	return bcn;
4947 }
4948 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
4949 
4950 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4951 					struct ieee80211_vif *vif)
4952 {
4953 	struct ieee80211_if_ap *ap = NULL;
4954 	struct sk_buff *skb = NULL;
4955 	struct probe_resp *presp = NULL;
4956 	struct ieee80211_hdr *hdr;
4957 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4958 
4959 	if (sdata->vif.type != NL80211_IFTYPE_AP)
4960 		return NULL;
4961 
4962 	rcu_read_lock();
4963 
4964 	ap = &sdata->u.ap;
4965 	presp = rcu_dereference(ap->probe_resp);
4966 	if (!presp)
4967 		goto out;
4968 
4969 	skb = dev_alloc_skb(presp->len);
4970 	if (!skb)
4971 		goto out;
4972 
4973 	skb_put_data(skb, presp->data, presp->len);
4974 
4975 	hdr = (struct ieee80211_hdr *) skb->data;
4976 	memset(hdr->addr1, 0, sizeof(hdr->addr1));
4977 
4978 out:
4979 	rcu_read_unlock();
4980 	return skb;
4981 }
4982 EXPORT_SYMBOL(ieee80211_proberesp_get);
4983 
4984 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4985 				     struct ieee80211_vif *vif)
4986 {
4987 	struct ieee80211_sub_if_data *sdata;
4988 	struct ieee80211_if_managed *ifmgd;
4989 	struct ieee80211_pspoll *pspoll;
4990 	struct ieee80211_local *local;
4991 	struct sk_buff *skb;
4992 
4993 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4994 		return NULL;
4995 
4996 	sdata = vif_to_sdata(vif);
4997 	ifmgd = &sdata->u.mgd;
4998 	local = sdata->local;
4999 
5000 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
5001 	if (!skb)
5002 		return NULL;
5003 
5004 	skb_reserve(skb, local->hw.extra_tx_headroom);
5005 
5006 	pspoll = skb_put_zero(skb, sizeof(*pspoll));
5007 	pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5008 					    IEEE80211_STYPE_PSPOLL);
5009 	pspoll->aid = cpu_to_le16(ifmgd->aid);
5010 
5011 	/* aid in PS-Poll has its two MSBs each set to 1 */
5012 	pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5013 
5014 	memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5015 	memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5016 
5017 	return skb;
5018 }
5019 EXPORT_SYMBOL(ieee80211_pspoll_get);
5020 
5021 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5022 				       struct ieee80211_vif *vif,
5023 				       bool qos_ok)
5024 {
5025 	struct ieee80211_hdr_3addr *nullfunc;
5026 	struct ieee80211_sub_if_data *sdata;
5027 	struct ieee80211_if_managed *ifmgd;
5028 	struct ieee80211_local *local;
5029 	struct sk_buff *skb;
5030 	bool qos = false;
5031 
5032 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5033 		return NULL;
5034 
5035 	sdata = vif_to_sdata(vif);
5036 	ifmgd = &sdata->u.mgd;
5037 	local = sdata->local;
5038 
5039 	if (qos_ok) {
5040 		struct sta_info *sta;
5041 
5042 		rcu_read_lock();
5043 		sta = sta_info_get(sdata, ifmgd->bssid);
5044 		qos = sta && sta->sta.wme;
5045 		rcu_read_unlock();
5046 	}
5047 
5048 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5049 			    sizeof(*nullfunc) + 2);
5050 	if (!skb)
5051 		return NULL;
5052 
5053 	skb_reserve(skb, local->hw.extra_tx_headroom);
5054 
5055 	nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
5056 	nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5057 					      IEEE80211_STYPE_NULLFUNC |
5058 					      IEEE80211_FCTL_TODS);
5059 	if (qos) {
5060 		__le16 qoshdr = cpu_to_le16(7);
5061 
5062 		BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5063 			      IEEE80211_STYPE_NULLFUNC) !=
5064 			     IEEE80211_STYPE_QOS_NULLFUNC);
5065 		nullfunc->frame_control |=
5066 			cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5067 		skb->priority = 7;
5068 		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
5069 		skb_put_data(skb, &qoshdr, sizeof(qoshdr));
5070 	}
5071 
5072 	memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5073 	memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5074 	memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5075 
5076 	return skb;
5077 }
5078 EXPORT_SYMBOL(ieee80211_nullfunc_get);
5079 
5080 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
5081 				       const u8 *src_addr,
5082 				       const u8 *ssid, size_t ssid_len,
5083 				       size_t tailroom)
5084 {
5085 	struct ieee80211_local *local = hw_to_local(hw);
5086 	struct ieee80211_hdr_3addr *hdr;
5087 	struct sk_buff *skb;
5088 	size_t ie_ssid_len;
5089 	u8 *pos;
5090 
5091 	ie_ssid_len = 2 + ssid_len;
5092 
5093 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
5094 			    ie_ssid_len + tailroom);
5095 	if (!skb)
5096 		return NULL;
5097 
5098 	skb_reserve(skb, local->hw.extra_tx_headroom);
5099 
5100 	hdr = skb_put_zero(skb, sizeof(*hdr));
5101 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5102 					 IEEE80211_STYPE_PROBE_REQ);
5103 	eth_broadcast_addr(hdr->addr1);
5104 	memcpy(hdr->addr2, src_addr, ETH_ALEN);
5105 	eth_broadcast_addr(hdr->addr3);
5106 
5107 	pos = skb_put(skb, ie_ssid_len);
5108 	*pos++ = WLAN_EID_SSID;
5109 	*pos++ = ssid_len;
5110 	if (ssid_len)
5111 		memcpy(pos, ssid, ssid_len);
5112 	pos += ssid_len;
5113 
5114 	return skb;
5115 }
5116 EXPORT_SYMBOL(ieee80211_probereq_get);
5117 
5118 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5119 		       const void *frame, size_t frame_len,
5120 		       const struct ieee80211_tx_info *frame_txctl,
5121 		       struct ieee80211_rts *rts)
5122 {
5123 	const struct ieee80211_hdr *hdr = frame;
5124 
5125 	rts->frame_control =
5126 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
5127 	rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5128 					       frame_txctl);
5129 	memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5130 	memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5131 }
5132 EXPORT_SYMBOL(ieee80211_rts_get);
5133 
5134 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5135 			     const void *frame, size_t frame_len,
5136 			     const struct ieee80211_tx_info *frame_txctl,
5137 			     struct ieee80211_cts *cts)
5138 {
5139 	const struct ieee80211_hdr *hdr = frame;
5140 
5141 	cts->frame_control =
5142 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
5143 	cts->duration = ieee80211_ctstoself_duration(hw, vif,
5144 						     frame_len, frame_txctl);
5145 	memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5146 }
5147 EXPORT_SYMBOL(ieee80211_ctstoself_get);
5148 
5149 struct sk_buff *
5150 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
5151 			  struct ieee80211_vif *vif)
5152 {
5153 	struct ieee80211_local *local = hw_to_local(hw);
5154 	struct sk_buff *skb = NULL;
5155 	struct ieee80211_tx_data tx;
5156 	struct ieee80211_sub_if_data *sdata;
5157 	struct ps_data *ps;
5158 	struct ieee80211_tx_info *info;
5159 	struct ieee80211_chanctx_conf *chanctx_conf;
5160 
5161 	sdata = vif_to_sdata(vif);
5162 
5163 	rcu_read_lock();
5164 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
5165 
5166 	if (!chanctx_conf)
5167 		goto out;
5168 
5169 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
5170 		struct beacon_data *beacon =
5171 				rcu_dereference(sdata->u.ap.beacon);
5172 
5173 		if (!beacon || !beacon->head)
5174 			goto out;
5175 
5176 		ps = &sdata->u.ap.ps;
5177 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5178 		ps = &sdata->u.mesh.ps;
5179 	} else {
5180 		goto out;
5181 	}
5182 
5183 	if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
5184 		goto out; /* send buffered bc/mc only after DTIM beacon */
5185 
5186 	while (1) {
5187 		skb = skb_dequeue(&ps->bc_buf);
5188 		if (!skb)
5189 			goto out;
5190 		local->total_ps_buffered--;
5191 
5192 		if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
5193 			struct ieee80211_hdr *hdr =
5194 				(struct ieee80211_hdr *) skb->data;
5195 			/* more buffered multicast/broadcast frames ==> set
5196 			 * MoreData flag in IEEE 802.11 header to inform PS
5197 			 * STAs */
5198 			hdr->frame_control |=
5199 				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5200 		}
5201 
5202 		if (sdata->vif.type == NL80211_IFTYPE_AP)
5203 			sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
5204 		if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
5205 			break;
5206 		ieee80211_free_txskb(hw, skb);
5207 	}
5208 
5209 	info = IEEE80211_SKB_CB(skb);
5210 
5211 	tx.flags |= IEEE80211_TX_PS_BUFFERED;
5212 	info->band = chanctx_conf->def.chan->band;
5213 
5214 	if (invoke_tx_handlers(&tx))
5215 		skb = NULL;
5216  out:
5217 	rcu_read_unlock();
5218 
5219 	return skb;
5220 }
5221 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
5222 
5223 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5224 {
5225 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5226 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5227 	struct ieee80211_local *local = sdata->local;
5228 	int ret;
5229 	u32 queues;
5230 
5231 	lockdep_assert_held(&local->sta_mtx);
5232 
5233 	/* only some cases are supported right now */
5234 	switch (sdata->vif.type) {
5235 	case NL80211_IFTYPE_STATION:
5236 	case NL80211_IFTYPE_AP:
5237 	case NL80211_IFTYPE_AP_VLAN:
5238 		break;
5239 	default:
5240 		WARN_ON(1);
5241 		return -EINVAL;
5242 	}
5243 
5244 	if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5245 		return -EINVAL;
5246 
5247 	if (sta->reserved_tid == tid) {
5248 		ret = 0;
5249 		goto out;
5250 	}
5251 
5252 	if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5253 		sdata_err(sdata, "TID reservation already active\n");
5254 		ret = -EALREADY;
5255 		goto out;
5256 	}
5257 
5258 	ieee80211_stop_vif_queues(sdata->local, sdata,
5259 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5260 
5261 	synchronize_net();
5262 
5263 	/* Tear down BA sessions so we stop aggregating on this TID */
5264 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
5265 		set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5266 		__ieee80211_stop_tx_ba_session(sta, tid,
5267 					       AGG_STOP_LOCAL_REQUEST);
5268 	}
5269 
5270 	queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
5271 	__ieee80211_flush_queues(local, sdata, queues, false);
5272 
5273 	sta->reserved_tid = tid;
5274 
5275 	ieee80211_wake_vif_queues(local, sdata,
5276 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5277 
5278 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
5279 		clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5280 
5281 	ret = 0;
5282  out:
5283 	return ret;
5284 }
5285 EXPORT_SYMBOL(ieee80211_reserve_tid);
5286 
5287 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5288 {
5289 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5290 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5291 
5292 	lockdep_assert_held(&sdata->local->sta_mtx);
5293 
5294 	/* only some cases are supported right now */
5295 	switch (sdata->vif.type) {
5296 	case NL80211_IFTYPE_STATION:
5297 	case NL80211_IFTYPE_AP:
5298 	case NL80211_IFTYPE_AP_VLAN:
5299 		break;
5300 	default:
5301 		WARN_ON(1);
5302 		return;
5303 	}
5304 
5305 	if (tid != sta->reserved_tid) {
5306 		sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5307 		return;
5308 	}
5309 
5310 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5311 }
5312 EXPORT_SYMBOL(ieee80211_unreserve_tid);
5313 
5314 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5315 				 struct sk_buff *skb, int tid,
5316 				 enum nl80211_band band, u32 txdata_flags)
5317 {
5318 	int ac = ieee80211_ac_from_tid(tid);
5319 
5320 	skb_reset_mac_header(skb);
5321 	skb_set_queue_mapping(skb, ac);
5322 	skb->priority = tid;
5323 
5324 	skb->dev = sdata->dev;
5325 
5326 	/*
5327 	 * The other path calling ieee80211_xmit is from the tasklet,
5328 	 * and while we can handle concurrent transmissions locking
5329 	 * requirements are that we do not come into tx with bhs on.
5330 	 */
5331 	local_bh_disable();
5332 	IEEE80211_SKB_CB(skb)->band = band;
5333 	ieee80211_xmit(sdata, NULL, skb, txdata_flags);
5334 	local_bh_enable();
5335 }
5336 
5337 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5338 			      const u8 *buf, size_t len,
5339 			      const u8 *dest, __be16 proto, bool unencrypted)
5340 {
5341 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5342 	struct ieee80211_local *local = sdata->local;
5343 	struct sk_buff *skb;
5344 	struct ethhdr *ehdr;
5345 	u32 ctrl_flags = 0;
5346 	u32 flags;
5347 
5348 	/* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5349 	 * or Pre-Authentication
5350 	 */
5351 	if (proto != sdata->control_port_protocol &&
5352 	    proto != cpu_to_be16(ETH_P_PREAUTH))
5353 		return -EINVAL;
5354 
5355 	if (proto == sdata->control_port_protocol)
5356 		ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
5357 
5358 	if (unencrypted)
5359 		flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5360 	else
5361 		flags = 0;
5362 
5363 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5364 			    sizeof(struct ethhdr) + len);
5365 	if (!skb)
5366 		return -ENOMEM;
5367 
5368 	skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5369 
5370 	skb_put_data(skb, buf, len);
5371 
5372 	ehdr = skb_push(skb, sizeof(struct ethhdr));
5373 	memcpy(ehdr->h_dest, dest, ETH_ALEN);
5374 	memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5375 	ehdr->h_proto = proto;
5376 
5377 	skb->dev = dev;
5378 	skb->protocol = htons(ETH_P_802_3);
5379 	skb_reset_network_header(skb);
5380 	skb_reset_mac_header(skb);
5381 
5382 	local_bh_disable();
5383 	__ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags);
5384 	local_bh_enable();
5385 
5386 	return 0;
5387 }
5388 
5389 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5390 			      const u8 *buf, size_t len)
5391 {
5392 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5393 	struct ieee80211_local *local = sdata->local;
5394 	struct sk_buff *skb;
5395 
5396 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5397 			    30 + /* header size */
5398 			    18); /* 11s header size */
5399 	if (!skb)
5400 		return -ENOMEM;
5401 
5402 	skb_reserve(skb, local->hw.extra_tx_headroom);
5403 	skb_put_data(skb, buf, len);
5404 
5405 	skb->dev = dev;
5406 	skb->protocol = htons(ETH_P_802_3);
5407 	skb_reset_network_header(skb);
5408 	skb_reset_mac_header(skb);
5409 
5410 	local_bh_disable();
5411 	__ieee80211_subif_start_xmit(skb, skb->dev, 0,
5412 				     IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);
5413 	local_bh_enable();
5414 
5415 	return 0;
5416 }
5417