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