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