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