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