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