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