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