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