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