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