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