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