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