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