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