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