xref: /linux/net/mac80211/tx.c (revision 737eb75a815f9c08dcbb6631db57f4f4b0540a5b)
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) || !sta->uploaded)
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 			rcu_read_lock();
3104 			link = rcu_dereference(sdata->link[tdls_link_id]);
3105 			if (!WARN_ON_ONCE(!link))
3106 				memcpy(hdr->addr3, link->u.mgd.bssid, ETH_ALEN);
3107 			rcu_read_unlock();
3108 			build.hdr_len = 24;
3109 			break;
3110 		}
3111 
3112 		if (sdata->u.mgd.use_4addr) {
3113 			/* non-regular ethertype cannot use the fastpath */
3114 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3115 					  IEEE80211_FCTL_TODS);
3116 			/* RA TA DA SA */
3117 			memcpy(hdr->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
3118 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3119 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3120 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3121 			build.hdr_len = 30;
3122 			break;
3123 		}
3124 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
3125 		/* BSSID SA DA */
3126 		memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
3127 		build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3128 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3129 		build.hdr_len = 24;
3130 		break;
3131 	case NL80211_IFTYPE_AP_VLAN:
3132 		if (sdata->wdev.use_4addr) {
3133 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3134 					  IEEE80211_FCTL_TODS);
3135 			/* RA TA DA SA */
3136 			memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3137 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3138 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3139 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3140 			build.hdr_len = 30;
3141 			break;
3142 		}
3143 		fallthrough;
3144 	case NL80211_IFTYPE_AP:
3145 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3146 		/* DA BSSID SA */
3147 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3148 		if (sta->sta.mlo || !ieee80211_vif_is_mld(&sdata->vif)) {
3149 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3150 		} else {
3151 			unsigned int link_id = sta->deflink.link_id;
3152 			struct ieee80211_link_data *link;
3153 
3154 			rcu_read_lock();
3155 			link = rcu_dereference(sdata->link[link_id]);
3156 			if (WARN_ON(!link)) {
3157 				rcu_read_unlock();
3158 				goto out;
3159 			}
3160 			memcpy(hdr->addr2, link->conf->addr, ETH_ALEN);
3161 			rcu_read_unlock();
3162 		}
3163 		build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3164 		build.hdr_len = 24;
3165 		break;
3166 	default:
3167 		/* not handled on fast-xmit */
3168 		goto out;
3169 	}
3170 
3171 	if (sta->sta.wme) {
3172 		build.hdr_len += 2;
3173 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3174 	}
3175 
3176 	/* We store the key here so there's no point in using rcu_dereference()
3177 	 * but that's fine because the code that changes the pointers will call
3178 	 * this function after doing so. For a single CPU that would be enough,
3179 	 * for multiple see the comment above.
3180 	 */
3181 	build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3182 	if (!build.key)
3183 		build.key = rcu_access_pointer(sdata->default_unicast_key);
3184 	if (build.key) {
3185 		bool gen_iv, iv_spc, mmic;
3186 
3187 		gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3188 		iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3189 		mmic = build.key->conf.flags &
3190 			(IEEE80211_KEY_FLAG_GENERATE_MMIC |
3191 			 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3192 
3193 		/* don't handle software crypto */
3194 		if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3195 			goto out;
3196 
3197 		/* Key is being removed */
3198 		if (build.key->flags & KEY_FLAG_TAINTED)
3199 			goto out;
3200 
3201 		switch (build.key->conf.cipher) {
3202 		case WLAN_CIPHER_SUITE_CCMP:
3203 		case WLAN_CIPHER_SUITE_CCMP_256:
3204 			if (gen_iv)
3205 				build.pn_offs = build.hdr_len;
3206 			if (gen_iv || iv_spc)
3207 				build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3208 			break;
3209 		case WLAN_CIPHER_SUITE_GCMP:
3210 		case WLAN_CIPHER_SUITE_GCMP_256:
3211 			if (gen_iv)
3212 				build.pn_offs = build.hdr_len;
3213 			if (gen_iv || iv_spc)
3214 				build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3215 			break;
3216 		case WLAN_CIPHER_SUITE_TKIP:
3217 			/* cannot handle MMIC or IV generation in xmit-fast */
3218 			if (mmic || gen_iv)
3219 				goto out;
3220 			if (iv_spc)
3221 				build.hdr_len += IEEE80211_TKIP_IV_LEN;
3222 			break;
3223 		case WLAN_CIPHER_SUITE_WEP40:
3224 		case WLAN_CIPHER_SUITE_WEP104:
3225 			/* cannot handle IV generation in fast-xmit */
3226 			if (gen_iv)
3227 				goto out;
3228 			if (iv_spc)
3229 				build.hdr_len += IEEE80211_WEP_IV_LEN;
3230 			break;
3231 		case WLAN_CIPHER_SUITE_AES_CMAC:
3232 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3233 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3234 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3235 			WARN(1,
3236 			     "management cipher suite 0x%x enabled for data\n",
3237 			     build.key->conf.cipher);
3238 			goto out;
3239 		default:
3240 			/* we don't know how to generate IVs for this at all */
3241 			if (WARN_ON(gen_iv))
3242 				goto out;
3243 		}
3244 
3245 		fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3246 	}
3247 
3248 	hdr->frame_control = fc;
3249 
3250 	memcpy(build.hdr + build.hdr_len,
3251 	       rfc1042_header,  sizeof(rfc1042_header));
3252 	build.hdr_len += sizeof(rfc1042_header);
3253 
3254 	fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3255 	/* if the kmemdup fails, continue w/o fast_tx */
3256 
3257  out:
3258 	/* we might have raced against another call to this function */
3259 	old = rcu_dereference_protected(sta->fast_tx,
3260 					lockdep_is_held(&sta->lock));
3261 	rcu_assign_pointer(sta->fast_tx, fast_tx);
3262 	if (old)
3263 		kfree_rcu(old, rcu_head);
3264 	spin_unlock_bh(&sta->lock);
3265 }
3266 
3267 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3268 {
3269 	struct sta_info *sta;
3270 
3271 	rcu_read_lock();
3272 	list_for_each_entry_rcu(sta, &local->sta_list, list)
3273 		ieee80211_check_fast_xmit(sta);
3274 	rcu_read_unlock();
3275 }
3276 
3277 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3278 {
3279 	struct ieee80211_local *local = sdata->local;
3280 	struct sta_info *sta;
3281 
3282 	rcu_read_lock();
3283 
3284 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
3285 		if (sdata != sta->sdata &&
3286 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3287 			continue;
3288 		ieee80211_check_fast_xmit(sta);
3289 	}
3290 
3291 	rcu_read_unlock();
3292 }
3293 
3294 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3295 {
3296 	struct ieee80211_fast_tx *fast_tx;
3297 
3298 	spin_lock_bh(&sta->lock);
3299 	fast_tx = rcu_dereference_protected(sta->fast_tx,
3300 					    lockdep_is_held(&sta->lock));
3301 	RCU_INIT_POINTER(sta->fast_tx, NULL);
3302 	spin_unlock_bh(&sta->lock);
3303 
3304 	if (fast_tx)
3305 		kfree_rcu(fast_tx, rcu_head);
3306 }
3307 
3308 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3309 					struct sk_buff *skb, int headroom)
3310 {
3311 	if (skb_headroom(skb) < headroom) {
3312 		I802_DEBUG_INC(local->tx_expand_skb_head);
3313 
3314 		if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3315 			wiphy_debug(local->hw.wiphy,
3316 				    "failed to reallocate TX buffer\n");
3317 			return false;
3318 		}
3319 	}
3320 
3321 	return true;
3322 }
3323 
3324 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3325 					 struct ieee80211_fast_tx *fast_tx,
3326 					 struct sk_buff *skb)
3327 {
3328 	struct ieee80211_local *local = sdata->local;
3329 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3330 	struct ieee80211_hdr *hdr;
3331 	struct ethhdr *amsdu_hdr;
3332 	int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3333 	int subframe_len = skb->len - hdr_len;
3334 	void *data;
3335 	u8 *qc, *h_80211_src, *h_80211_dst;
3336 	const u8 *bssid;
3337 
3338 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3339 		return false;
3340 
3341 	if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3342 		return true;
3343 
3344 	if (!ieee80211_amsdu_realloc_pad(local, skb,
3345 					 sizeof(*amsdu_hdr) +
3346 					 local->hw.extra_tx_headroom))
3347 		return false;
3348 
3349 	data = skb_push(skb, sizeof(*amsdu_hdr));
3350 	memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3351 	hdr = data;
3352 	amsdu_hdr = data + hdr_len;
3353 	/* h_80211_src/dst is addr* field within hdr */
3354 	h_80211_src = data + fast_tx->sa_offs;
3355 	h_80211_dst = data + fast_tx->da_offs;
3356 
3357 	amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3358 	ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3359 	ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3360 
3361 	/* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3362 	 * fields needs to be changed to BSSID for A-MSDU frames depending
3363 	 * on FromDS/ToDS values.
3364 	 */
3365 	switch (sdata->vif.type) {
3366 	case NL80211_IFTYPE_STATION:
3367 		bssid = sdata->vif.cfg.ap_addr;
3368 		break;
3369 	case NL80211_IFTYPE_AP:
3370 	case NL80211_IFTYPE_AP_VLAN:
3371 		bssid = sdata->vif.addr;
3372 		break;
3373 	default:
3374 		bssid = NULL;
3375 	}
3376 
3377 	if (bssid && ieee80211_has_fromds(hdr->frame_control))
3378 		ether_addr_copy(h_80211_src, bssid);
3379 
3380 	if (bssid && ieee80211_has_tods(hdr->frame_control))
3381 		ether_addr_copy(h_80211_dst, bssid);
3382 
3383 	qc = ieee80211_get_qos_ctl(hdr);
3384 	*qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3385 
3386 	info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3387 
3388 	return true;
3389 }
3390 
3391 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3392 				      struct sta_info *sta,
3393 				      struct ieee80211_fast_tx *fast_tx,
3394 				      struct sk_buff *skb,
3395 				      const u8 *da, const u8 *sa)
3396 {
3397 	struct ieee80211_local *local = sdata->local;
3398 	struct fq *fq = &local->fq;
3399 	struct fq_tin *tin;
3400 	struct fq_flow *flow;
3401 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3402 	struct ieee80211_txq *txq = sta->sta.txq[tid];
3403 	struct txq_info *txqi;
3404 	struct sk_buff **frag_tail, *head;
3405 	int subframe_len = skb->len - ETH_ALEN;
3406 	u8 max_subframes = sta->sta.max_amsdu_subframes;
3407 	int max_frags = local->hw.max_tx_fragments;
3408 	int max_amsdu_len = sta->sta.cur->max_amsdu_len;
3409 	int orig_truesize;
3410 	u32 flow_idx;
3411 	__be16 len;
3412 	void *data;
3413 	bool ret = false;
3414 	unsigned int orig_len;
3415 	int n = 2, nfrags, pad = 0;
3416 	u16 hdrlen;
3417 
3418 	if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3419 		return false;
3420 
3421 	if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
3422 		return false;
3423 
3424 	if (ieee80211_vif_is_mesh(&sdata->vif))
3425 		return false;
3426 
3427 	if (skb_is_gso(skb))
3428 		return false;
3429 
3430 	if (!txq)
3431 		return false;
3432 
3433 	txqi = to_txq_info(txq);
3434 	if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3435 		return false;
3436 
3437 	if (sta->sta.cur->max_rc_amsdu_len)
3438 		max_amsdu_len = min_t(int, max_amsdu_len,
3439 				      sta->sta.cur->max_rc_amsdu_len);
3440 
3441 	if (sta->sta.cur->max_tid_amsdu_len[tid])
3442 		max_amsdu_len = min_t(int, max_amsdu_len,
3443 				      sta->sta.cur->max_tid_amsdu_len[tid]);
3444 
3445 	flow_idx = fq_flow_idx(fq, skb);
3446 
3447 	spin_lock_bh(&fq->lock);
3448 
3449 	/* TODO: Ideally aggregation should be done on dequeue to remain
3450 	 * responsive to environment changes.
3451 	 */
3452 
3453 	tin = &txqi->tin;
3454 	flow = fq_flow_classify(fq, tin, flow_idx, skb);
3455 	head = skb_peek_tail(&flow->queue);
3456 	if (!head || skb_is_gso(head))
3457 		goto out;
3458 
3459 	orig_truesize = head->truesize;
3460 	orig_len = head->len;
3461 
3462 	if (skb->len + head->len > max_amsdu_len)
3463 		goto out;
3464 
3465 	nfrags = 1 + skb_shinfo(skb)->nr_frags;
3466 	nfrags += 1 + skb_shinfo(head)->nr_frags;
3467 	frag_tail = &skb_shinfo(head)->frag_list;
3468 	while (*frag_tail) {
3469 		nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3470 		frag_tail = &(*frag_tail)->next;
3471 		n++;
3472 	}
3473 
3474 	if (max_subframes && n > max_subframes)
3475 		goto out;
3476 
3477 	if (max_frags && nfrags > max_frags)
3478 		goto out;
3479 
3480 	if (!drv_can_aggregate_in_amsdu(local, head, skb))
3481 		goto out;
3482 
3483 	if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3484 		goto out;
3485 
3486 	/* If n == 2, the "while (*frag_tail)" loop above didn't execute
3487 	 * and  frag_tail should be &skb_shinfo(head)->frag_list.
3488 	 * However, ieee80211_amsdu_prepare_head() can reallocate it.
3489 	 * Reload frag_tail to have it pointing to the correct place.
3490 	 */
3491 	if (n == 2)
3492 		frag_tail = &skb_shinfo(head)->frag_list;
3493 
3494 	/*
3495 	 * Pad out the previous subframe to a multiple of 4 by adding the
3496 	 * padding to the next one, that's being added. Note that head->len
3497 	 * is the length of the full A-MSDU, but that works since each time
3498 	 * we add a new subframe we pad out the previous one to a multiple
3499 	 * of 4 and thus it no longer matters in the next round.
3500 	 */
3501 	hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3502 	if ((head->len - hdrlen) & 3)
3503 		pad = 4 - ((head->len - hdrlen) & 3);
3504 
3505 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3506 						     2 + pad))
3507 		goto out_recalc;
3508 
3509 	ret = true;
3510 	data = skb_push(skb, ETH_ALEN + 2);
3511 	ether_addr_copy(data, da);
3512 	ether_addr_copy(data + ETH_ALEN, sa);
3513 
3514 	data += 2 * ETH_ALEN;
3515 	len = cpu_to_be16(subframe_len);
3516 	memcpy(data, &len, 2);
3517 	memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3518 
3519 	memset(skb_push(skb, pad), 0, pad);
3520 
3521 	head->len += skb->len;
3522 	head->data_len += skb->len;
3523 	*frag_tail = skb;
3524 
3525 out_recalc:
3526 	fq->memory_usage += head->truesize - orig_truesize;
3527 	if (head->len != orig_len) {
3528 		flow->backlog += head->len - orig_len;
3529 		tin->backlog_bytes += head->len - orig_len;
3530 	}
3531 out:
3532 	spin_unlock_bh(&fq->lock);
3533 
3534 	return ret;
3535 }
3536 
3537 /*
3538  * Can be called while the sta lock is held. Anything that can cause packets to
3539  * be generated will cause deadlock!
3540  */
3541 static ieee80211_tx_result
3542 ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3543 			   struct sta_info *sta, u8 pn_offs,
3544 			   struct ieee80211_key *key,
3545 			   struct ieee80211_tx_data *tx)
3546 {
3547 	struct sk_buff *skb = tx->skb;
3548 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3549 	struct ieee80211_hdr *hdr = (void *)skb->data;
3550 	u8 tid = IEEE80211_NUM_TIDS;
3551 
3552 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
3553 	    ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
3554 		return TX_DROP;
3555 
3556 	if (key)
3557 		info->control.hw_key = &key->conf;
3558 
3559 	dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
3560 
3561 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3562 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3563 		hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3564 	} else {
3565 		info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3566 		hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3567 		sdata->sequence_number += 0x10;
3568 	}
3569 
3570 	if (skb_shinfo(skb)->gso_size)
3571 		sta->deflink.tx_stats.msdu[tid] +=
3572 			DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3573 	else
3574 		sta->deflink.tx_stats.msdu[tid]++;
3575 
3576 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3577 
3578 	/* statistics normally done by ieee80211_tx_h_stats (but that
3579 	 * has to consider fragmentation, so is more complex)
3580 	 */
3581 	sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3582 	sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++;
3583 
3584 	if (pn_offs) {
3585 		u64 pn;
3586 		u8 *crypto_hdr = skb->data + pn_offs;
3587 
3588 		switch (key->conf.cipher) {
3589 		case WLAN_CIPHER_SUITE_CCMP:
3590 		case WLAN_CIPHER_SUITE_CCMP_256:
3591 		case WLAN_CIPHER_SUITE_GCMP:
3592 		case WLAN_CIPHER_SUITE_GCMP_256:
3593 			pn = atomic64_inc_return(&key->conf.tx_pn);
3594 			crypto_hdr[0] = pn;
3595 			crypto_hdr[1] = pn >> 8;
3596 			crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3597 			crypto_hdr[4] = pn >> 16;
3598 			crypto_hdr[5] = pn >> 24;
3599 			crypto_hdr[6] = pn >> 32;
3600 			crypto_hdr[7] = pn >> 40;
3601 			break;
3602 		}
3603 	}
3604 
3605 	return TX_CONTINUE;
3606 }
3607 
3608 static netdev_features_t
3609 ieee80211_sdata_netdev_features(struct ieee80211_sub_if_data *sdata)
3610 {
3611 	if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3612 		return sdata->vif.netdev_features;
3613 
3614 	if (!sdata->bss)
3615 		return 0;
3616 
3617 	sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
3618 	return sdata->vif.netdev_features;
3619 }
3620 
3621 static struct sk_buff *
3622 ieee80211_tx_skb_fixup(struct sk_buff *skb, netdev_features_t features)
3623 {
3624 	if (skb_is_gso(skb)) {
3625 		struct sk_buff *segs;
3626 
3627 		segs = skb_gso_segment(skb, features);
3628 		if (!segs)
3629 			return skb;
3630 		if (IS_ERR(segs))
3631 			goto free;
3632 
3633 		consume_skb(skb);
3634 		return segs;
3635 	}
3636 
3637 	if (skb_needs_linearize(skb, features) && __skb_linearize(skb))
3638 		goto free;
3639 
3640 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3641 		int ofs = skb_checksum_start_offset(skb);
3642 
3643 		if (skb->encapsulation)
3644 			skb_set_inner_transport_header(skb, ofs);
3645 		else
3646 			skb_set_transport_header(skb, ofs);
3647 
3648 		if (skb_csum_hwoffload_help(skb, features))
3649 			goto free;
3650 	}
3651 
3652 	skb_mark_not_on_list(skb);
3653 	return skb;
3654 
3655 free:
3656 	kfree_skb(skb);
3657 	return NULL;
3658 }
3659 
3660 void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3661 			   struct sta_info *sta,
3662 			   struct ieee80211_fast_tx *fast_tx,
3663 			   struct sk_buff *skb, bool ampdu,
3664 			   const u8 *da, const u8 *sa)
3665 {
3666 	struct ieee80211_local *local = sdata->local;
3667 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3668 	struct ieee80211_tx_info *info;
3669 	struct ieee80211_tx_data tx;
3670 	ieee80211_tx_result r;
3671 	int hw_headroom = sdata->local->hw.extra_tx_headroom;
3672 	int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3673 
3674 	skb = skb_share_check(skb, GFP_ATOMIC);
3675 	if (unlikely(!skb))
3676 		return;
3677 
3678 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3679 	    ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb, da, sa))
3680 		return;
3681 
3682 	/* will not be crypto-handled beyond what we do here, so use false
3683 	 * as the may-encrypt argument for the resize to not account for
3684 	 * more room than we already have in 'extra_head'
3685 	 */
3686 	if (unlikely(ieee80211_skb_resize(sdata, skb,
3687 					  max_t(int, extra_head + hw_headroom -
3688 						     skb_headroom(skb), 0),
3689 					  ENCRYPT_NO)))
3690 		goto free;
3691 
3692 	hdr = skb_push(skb, extra_head);
3693 	memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3694 	memcpy(skb->data + fast_tx->da_offs, da, ETH_ALEN);
3695 	memcpy(skb->data + fast_tx->sa_offs, sa, ETH_ALEN);
3696 
3697 	info = IEEE80211_SKB_CB(skb);
3698 	memset(info, 0, sizeof(*info));
3699 	info->band = fast_tx->band;
3700 	info->control.vif = &sdata->vif;
3701 	info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3702 		      IEEE80211_TX_CTL_DONTFRAG;
3703 	info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT |
3704 			      u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
3705 					      IEEE80211_TX_CTRL_MLO_LINK);
3706 
3707 #ifdef CONFIG_MAC80211_DEBUGFS
3708 	if (local->force_tx_status)
3709 		info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3710 #endif
3711 
3712 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3713 		u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3714 
3715 		*ieee80211_get_qos_ctl(hdr) = tid;
3716 	}
3717 
3718 	__skb_queue_head_init(&tx.skbs);
3719 
3720 	tx.flags = IEEE80211_TX_UNICAST;
3721 	tx.local = local;
3722 	tx.sdata = sdata;
3723 	tx.sta = sta;
3724 	tx.key = fast_tx->key;
3725 
3726 	if (ieee80211_queue_skb(local, sdata, sta, skb))
3727 		return;
3728 
3729 	tx.skb = skb;
3730 	r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3731 				       fast_tx->key, &tx);
3732 	tx.skb = NULL;
3733 	if (r == TX_DROP)
3734 		goto free;
3735 
3736 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3737 		sdata = container_of(sdata->bss,
3738 				     struct ieee80211_sub_if_data, u.ap);
3739 
3740 	__skb_queue_tail(&tx.skbs, skb);
3741 	ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3742 	return;
3743 
3744 free:
3745 	kfree_skb(skb);
3746 }
3747 
3748 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3749 				struct sta_info *sta,
3750 				struct ieee80211_fast_tx *fast_tx,
3751 				struct sk_buff *skb)
3752 {
3753 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3754 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3755 	struct tid_ampdu_tx *tid_tx = NULL;
3756 	struct sk_buff *next;
3757 	struct ethhdr eth;
3758 	u8 tid = IEEE80211_NUM_TIDS;
3759 
3760 	/* control port protocol needs a lot of special handling */
3761 	if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3762 		return false;
3763 
3764 	/* only RFC 1042 SNAP */
3765 	if (ethertype < ETH_P_802_3_MIN)
3766 		return false;
3767 
3768 	/* don't handle TX status request here either */
3769 	if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3770 		return false;
3771 
3772 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3773 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3774 		tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3775 		if (tid_tx) {
3776 			if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3777 				return false;
3778 			if (tid_tx->timeout)
3779 				tid_tx->last_tx = jiffies;
3780 		}
3781 	}
3782 
3783 	memcpy(&eth, skb->data, ETH_HLEN - 2);
3784 
3785 	/* after this point (skb is modified) we cannot return false */
3786 	skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
3787 	if (!skb)
3788 		return true;
3789 
3790 	skb_list_walk_safe(skb, skb, next) {
3791 		skb_mark_not_on_list(skb);
3792 		__ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid_tx,
3793 				      eth.h_dest, eth.h_source);
3794 	}
3795 
3796 	return true;
3797 }
3798 
3799 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3800 				     struct ieee80211_txq *txq)
3801 {
3802 	struct ieee80211_local *local = hw_to_local(hw);
3803 	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3804 	struct ieee80211_hdr *hdr;
3805 	struct sk_buff *skb = NULL;
3806 	struct fq *fq = &local->fq;
3807 	struct fq_tin *tin = &txqi->tin;
3808 	struct ieee80211_tx_info *info;
3809 	struct ieee80211_tx_data tx;
3810 	ieee80211_tx_result r;
3811 	struct ieee80211_vif *vif = txq->vif;
3812 	int q = vif->hw_queue[txq->ac];
3813 	unsigned long flags;
3814 	bool q_stopped;
3815 
3816 	WARN_ON_ONCE(softirq_count() == 0);
3817 
3818 	if (!ieee80211_txq_airtime_check(hw, txq))
3819 		return NULL;
3820 
3821 begin:
3822 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3823 	q_stopped = local->queue_stop_reasons[q];
3824 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3825 
3826 	if (unlikely(q_stopped)) {
3827 		/* mark for waking later */
3828 		set_bit(IEEE80211_TXQ_DIRTY, &txqi->flags);
3829 		return NULL;
3830 	}
3831 
3832 	spin_lock_bh(&fq->lock);
3833 
3834 	/* Make sure fragments stay together. */
3835 	skb = __skb_dequeue(&txqi->frags);
3836 	if (unlikely(skb)) {
3837 		if (!(IEEE80211_SKB_CB(skb)->control.flags &
3838 				IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
3839 			goto out;
3840 		IEEE80211_SKB_CB(skb)->control.flags &=
3841 			~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3842 	} else {
3843 		if (unlikely(test_bit(IEEE80211_TXQ_STOP, &txqi->flags)))
3844 			goto out;
3845 
3846 		skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3847 	}
3848 
3849 	if (!skb)
3850 		goto out;
3851 
3852 	spin_unlock_bh(&fq->lock);
3853 
3854 	hdr = (struct ieee80211_hdr *)skb->data;
3855 	info = IEEE80211_SKB_CB(skb);
3856 
3857 	memset(&tx, 0, sizeof(tx));
3858 	__skb_queue_head_init(&tx.skbs);
3859 	tx.local = local;
3860 	tx.skb = skb;
3861 	tx.sdata = vif_to_sdata(info->control.vif);
3862 
3863 	if (txq->sta) {
3864 		tx.sta = container_of(txq->sta, struct sta_info, sta);
3865 		/*
3866 		 * Drop unicast frames to unauthorised stations unless they are
3867 		 * injected frames or EAPOL frames from the local station.
3868 		 */
3869 		if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3870 			     ieee80211_is_data(hdr->frame_control) &&
3871 			     !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
3872 			     tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3873 			     !is_multicast_ether_addr(hdr->addr1) &&
3874 			     !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3875 			     (!(info->control.flags &
3876 				IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3877 			      !ieee80211_is_our_addr(tx.sdata, hdr->addr2,
3878 						     NULL)))) {
3879 			I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3880 			ieee80211_free_txskb(&local->hw, skb);
3881 			goto begin;
3882 		}
3883 	}
3884 
3885 	/*
3886 	 * The key can be removed while the packet was queued, so need to call
3887 	 * this here to get the current key.
3888 	 */
3889 	r = ieee80211_tx_h_select_key(&tx);
3890 	if (r != TX_CONTINUE) {
3891 		ieee80211_free_txskb(&local->hw, skb);
3892 		goto begin;
3893 	}
3894 
3895 	if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3896 		info->flags |= (IEEE80211_TX_CTL_AMPDU |
3897 				IEEE80211_TX_CTL_DONTFRAG);
3898 
3899 	if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
3900 		if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3901 			r = ieee80211_tx_h_rate_ctrl(&tx);
3902 			if (r != TX_CONTINUE) {
3903 				ieee80211_free_txskb(&local->hw, skb);
3904 				goto begin;
3905 			}
3906 		}
3907 		goto encap_out;
3908 	}
3909 
3910 	if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3911 		struct sta_info *sta = container_of(txq->sta, struct sta_info,
3912 						    sta);
3913 		u8 pn_offs = 0;
3914 
3915 		if (tx.key &&
3916 		    (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3917 			pn_offs = ieee80211_hdrlen(hdr->frame_control);
3918 
3919 		r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3920 					       tx.key, &tx);
3921 		if (r != TX_CONTINUE) {
3922 			ieee80211_free_txskb(&local->hw, skb);
3923 			goto begin;
3924 		}
3925 	} else {
3926 		if (invoke_tx_handlers_late(&tx))
3927 			goto begin;
3928 
3929 		skb = __skb_dequeue(&tx.skbs);
3930 
3931 		if (!skb_queue_empty(&tx.skbs)) {
3932 			spin_lock_bh(&fq->lock);
3933 			skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3934 			spin_unlock_bh(&fq->lock);
3935 		}
3936 	}
3937 
3938 	if (skb_has_frag_list(skb) &&
3939 	    !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3940 		if (skb_linearize(skb)) {
3941 			ieee80211_free_txskb(&local->hw, skb);
3942 			goto begin;
3943 		}
3944 	}
3945 
3946 	switch (tx.sdata->vif.type) {
3947 	case NL80211_IFTYPE_MONITOR:
3948 		if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3949 			vif = &tx.sdata->vif;
3950 			break;
3951 		}
3952 		tx.sdata = rcu_dereference(local->monitor_sdata);
3953 		if (tx.sdata) {
3954 			vif = &tx.sdata->vif;
3955 			info->hw_queue =
3956 				vif->hw_queue[skb_get_queue_mapping(skb)];
3957 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3958 			ieee80211_free_txskb(&local->hw, skb);
3959 			goto begin;
3960 		} else {
3961 			vif = NULL;
3962 		}
3963 		break;
3964 	case NL80211_IFTYPE_AP_VLAN:
3965 		tx.sdata = container_of(tx.sdata->bss,
3966 					struct ieee80211_sub_if_data, u.ap);
3967 		fallthrough;
3968 	default:
3969 		vif = &tx.sdata->vif;
3970 		break;
3971 	}
3972 
3973 encap_out:
3974 	IEEE80211_SKB_CB(skb)->control.vif = vif;
3975 
3976 	if (tx.sta &&
3977 	    wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
3978 		bool ampdu = txq->ac != IEEE80211_AC_VO;
3979 		u32 airtime;
3980 
3981 		airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3982 							     skb->len, ampdu);
3983 		if (airtime) {
3984 			airtime = ieee80211_info_set_tx_time_est(info, airtime);
3985 			ieee80211_sta_update_pending_airtime(local, tx.sta,
3986 							     txq->ac,
3987 							     airtime,
3988 							     false);
3989 		}
3990 	}
3991 
3992 	return skb;
3993 
3994 out:
3995 	spin_unlock_bh(&fq->lock);
3996 
3997 	return skb;
3998 }
3999 EXPORT_SYMBOL(ieee80211_tx_dequeue);
4000 
4001 static inline s32 ieee80211_sta_deficit(struct sta_info *sta, u8 ac)
4002 {
4003 	struct airtime_info *air_info = &sta->airtime[ac];
4004 
4005 	return air_info->deficit - atomic_read(&air_info->aql_tx_pending);
4006 }
4007 
4008 static void
4009 ieee80211_txq_set_active(struct txq_info *txqi)
4010 {
4011 	struct sta_info *sta;
4012 
4013 	if (!txqi->txq.sta)
4014 		return;
4015 
4016 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4017 	sta->airtime[txqi->txq.ac].last_active = jiffies;
4018 }
4019 
4020 static bool
4021 ieee80211_txq_keep_active(struct txq_info *txqi)
4022 {
4023 	struct sta_info *sta;
4024 
4025 	if (!txqi->txq.sta)
4026 		return false;
4027 
4028 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4029 	if (ieee80211_sta_deficit(sta, txqi->txq.ac) >= 0)
4030 		return false;
4031 
4032 	return ieee80211_sta_keep_active(sta, txqi->txq.ac);
4033 }
4034 
4035 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
4036 {
4037 	struct ieee80211_local *local = hw_to_local(hw);
4038 	struct ieee80211_txq *ret = NULL;
4039 	struct txq_info *txqi = NULL, *head = NULL;
4040 	bool found_eligible_txq = false;
4041 
4042 	spin_lock_bh(&local->active_txq_lock[ac]);
4043 
4044 	if (!local->schedule_round[ac])
4045 		goto out;
4046 
4047  begin:
4048 	txqi = list_first_entry_or_null(&local->active_txqs[ac],
4049 					struct txq_info,
4050 					schedule_order);
4051 	if (!txqi)
4052 		goto out;
4053 
4054 	if (txqi == head) {
4055 		if (!found_eligible_txq)
4056 			goto out;
4057 		else
4058 			found_eligible_txq = false;
4059 	}
4060 
4061 	if (!head)
4062 		head = txqi;
4063 
4064 	if (txqi->txq.sta) {
4065 		struct sta_info *sta = container_of(txqi->txq.sta,
4066 						    struct sta_info, sta);
4067 		bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
4068 		s32 deficit = ieee80211_sta_deficit(sta, txqi->txq.ac);
4069 
4070 		if (aql_check)
4071 			found_eligible_txq = true;
4072 
4073 		if (deficit < 0)
4074 			sta->airtime[txqi->txq.ac].deficit +=
4075 				sta->airtime_weight;
4076 
4077 		if (deficit < 0 || !aql_check) {
4078 			list_move_tail(&txqi->schedule_order,
4079 				       &local->active_txqs[txqi->txq.ac]);
4080 			goto begin;
4081 		}
4082 	}
4083 
4084 	if (txqi->schedule_round == local->schedule_round[ac])
4085 		goto out;
4086 
4087 	list_del_init(&txqi->schedule_order);
4088 	txqi->schedule_round = local->schedule_round[ac];
4089 	ret = &txqi->txq;
4090 
4091 out:
4092 	spin_unlock_bh(&local->active_txq_lock[ac]);
4093 	return ret;
4094 }
4095 EXPORT_SYMBOL(ieee80211_next_txq);
4096 
4097 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
4098 			      struct ieee80211_txq *txq,
4099 			      bool force)
4100 {
4101 	struct ieee80211_local *local = hw_to_local(hw);
4102 	struct txq_info *txqi = to_txq_info(txq);
4103 	bool has_queue;
4104 
4105 	spin_lock_bh(&local->active_txq_lock[txq->ac]);
4106 
4107 	has_queue = force || txq_has_queue(txq);
4108 	if (list_empty(&txqi->schedule_order) &&
4109 	    (has_queue || ieee80211_txq_keep_active(txqi))) {
4110 		/* If airtime accounting is active, always enqueue STAs at the
4111 		 * head of the list to ensure that they only get moved to the
4112 		 * back by the airtime DRR scheduler once they have a negative
4113 		 * deficit. A station that already has a negative deficit will
4114 		 * get immediately moved to the back of the list on the next
4115 		 * call to ieee80211_next_txq().
4116 		 */
4117 		if (txqi->txq.sta && local->airtime_flags && has_queue &&
4118 		    wiphy_ext_feature_isset(local->hw.wiphy,
4119 					    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
4120 			list_add(&txqi->schedule_order,
4121 				 &local->active_txqs[txq->ac]);
4122 		else
4123 			list_add_tail(&txqi->schedule_order,
4124 				      &local->active_txqs[txq->ac]);
4125 		if (has_queue)
4126 			ieee80211_txq_set_active(txqi);
4127 	}
4128 
4129 	spin_unlock_bh(&local->active_txq_lock[txq->ac]);
4130 }
4131 EXPORT_SYMBOL(__ieee80211_schedule_txq);
4132 
4133 DEFINE_STATIC_KEY_FALSE(aql_disable);
4134 
4135 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
4136 				 struct ieee80211_txq *txq)
4137 {
4138 	struct sta_info *sta;
4139 	struct ieee80211_local *local = hw_to_local(hw);
4140 
4141 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
4142 		return true;
4143 
4144 	if (static_branch_unlikely(&aql_disable))
4145 		return true;
4146 
4147 	if (!txq->sta)
4148 		return true;
4149 
4150 	if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
4151 		return true;
4152 
4153 	sta = container_of(txq->sta, struct sta_info, sta);
4154 	if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4155 	    sta->airtime[txq->ac].aql_limit_low)
4156 		return true;
4157 
4158 	if (atomic_read(&local->aql_total_pending_airtime) <
4159 	    local->aql_threshold &&
4160 	    atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4161 	    sta->airtime[txq->ac].aql_limit_high)
4162 		return true;
4163 
4164 	return false;
4165 }
4166 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
4167 
4168 static bool
4169 ieee80211_txq_schedule_airtime_check(struct ieee80211_local *local, u8 ac)
4170 {
4171 	unsigned int num_txq = 0;
4172 	struct txq_info *txq;
4173 	u32 aql_limit;
4174 
4175 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
4176 		return true;
4177 
4178 	list_for_each_entry(txq, &local->active_txqs[ac], schedule_order)
4179 		num_txq++;
4180 
4181 	aql_limit = (num_txq - 1) * local->aql_txq_limit_low[ac] / 2 +
4182 		    local->aql_txq_limit_high[ac];
4183 
4184 	return atomic_read(&local->aql_ac_pending_airtime[ac]) < aql_limit;
4185 }
4186 
4187 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
4188 				struct ieee80211_txq *txq)
4189 {
4190 	struct ieee80211_local *local = hw_to_local(hw);
4191 	struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
4192 	struct sta_info *sta;
4193 	u8 ac = txq->ac;
4194 
4195 	spin_lock_bh(&local->active_txq_lock[ac]);
4196 
4197 	if (!txqi->txq.sta)
4198 		goto out;
4199 
4200 	if (list_empty(&txqi->schedule_order))
4201 		goto out;
4202 
4203 	if (!ieee80211_txq_schedule_airtime_check(local, ac))
4204 		goto out;
4205 
4206 	list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
4207 				 schedule_order) {
4208 		if (iter == txqi)
4209 			break;
4210 
4211 		if (!iter->txq.sta) {
4212 			list_move_tail(&iter->schedule_order,
4213 				       &local->active_txqs[ac]);
4214 			continue;
4215 		}
4216 		sta = container_of(iter->txq.sta, struct sta_info, sta);
4217 		if (ieee80211_sta_deficit(sta, ac) < 0)
4218 			sta->airtime[ac].deficit += sta->airtime_weight;
4219 		list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
4220 	}
4221 
4222 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4223 	if (sta->airtime[ac].deficit >= 0)
4224 		goto out;
4225 
4226 	sta->airtime[ac].deficit += sta->airtime_weight;
4227 	list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
4228 	spin_unlock_bh(&local->active_txq_lock[ac]);
4229 
4230 	return false;
4231 out:
4232 	if (!list_empty(&txqi->schedule_order))
4233 		list_del_init(&txqi->schedule_order);
4234 	spin_unlock_bh(&local->active_txq_lock[ac]);
4235 
4236 	return true;
4237 }
4238 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
4239 
4240 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
4241 {
4242 	struct ieee80211_local *local = hw_to_local(hw);
4243 
4244 	spin_lock_bh(&local->active_txq_lock[ac]);
4245 
4246 	if (ieee80211_txq_schedule_airtime_check(local, ac)) {
4247 		local->schedule_round[ac]++;
4248 		if (!local->schedule_round[ac])
4249 			local->schedule_round[ac]++;
4250 	} else {
4251 		local->schedule_round[ac] = 0;
4252 	}
4253 
4254 	spin_unlock_bh(&local->active_txq_lock[ac]);
4255 }
4256 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
4257 
4258 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
4259 				  struct net_device *dev,
4260 				  u32 info_flags,
4261 				  u32 ctrl_flags,
4262 				  u64 *cookie)
4263 {
4264 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4265 	struct ieee80211_local *local = sdata->local;
4266 	struct sta_info *sta;
4267 	struct sk_buff *next;
4268 	int len = skb->len;
4269 
4270 	if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
4271 		kfree_skb(skb);
4272 		return;
4273 	}
4274 
4275 	sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4276 
4277 	rcu_read_lock();
4278 
4279 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
4280 	    ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT) &&
4281 	    ieee80211_mesh_xmit_fast(sdata, skb, ctrl_flags))
4282 		goto out;
4283 
4284 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4285 		goto out_free;
4286 
4287 	if (IS_ERR(sta))
4288 		sta = NULL;
4289 
4290 	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
4291 	ieee80211_aggr_check(sdata, sta, skb);
4292 
4293 	if (sta) {
4294 		struct ieee80211_fast_tx *fast_tx;
4295 
4296 		fast_tx = rcu_dereference(sta->fast_tx);
4297 
4298 		if (fast_tx &&
4299 		    ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
4300 			goto out;
4301 	}
4302 
4303 	/* the frame could be fragmented, software-encrypted, and other
4304 	 * things so we cannot really handle checksum or GSO offload.
4305 	 * fix it up in software before we handle anything else.
4306 	 */
4307 	skb = ieee80211_tx_skb_fixup(skb, 0);
4308 	if (!skb) {
4309 		len = 0;
4310 		goto out;
4311 	}
4312 
4313 	skb_list_walk_safe(skb, skb, next) {
4314 		skb_mark_not_on_list(skb);
4315 
4316 		if (skb->protocol == sdata->control_port_protocol)
4317 			ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4318 
4319 		skb = ieee80211_build_hdr(sdata, skb, info_flags,
4320 					  sta, ctrl_flags, cookie);
4321 		if (IS_ERR(skb)) {
4322 			kfree_skb_list(next);
4323 			goto out;
4324 		}
4325 
4326 		dev_sw_netstats_tx_add(dev, 1, skb->len);
4327 
4328 		ieee80211_xmit(sdata, sta, skb);
4329 	}
4330 	goto out;
4331  out_free:
4332 	kfree_skb(skb);
4333 	len = 0;
4334  out:
4335 	if (len)
4336 		ieee80211_tpt_led_trig_tx(local, len);
4337 	rcu_read_unlock();
4338 }
4339 
4340 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4341 {
4342 	struct ethhdr *eth;
4343 	int err;
4344 
4345 	err = skb_ensure_writable(skb, ETH_HLEN);
4346 	if (unlikely(err))
4347 		return err;
4348 
4349 	eth = (void *)skb->data;
4350 	ether_addr_copy(eth->h_dest, sta->sta.addr);
4351 
4352 	return 0;
4353 }
4354 
4355 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4356 					   struct net_device *dev)
4357 {
4358 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4359 	const struct ethhdr *eth = (void *)skb->data;
4360 	const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4361 	__be16 ethertype;
4362 
4363 	switch (sdata->vif.type) {
4364 	case NL80211_IFTYPE_AP_VLAN:
4365 		if (sdata->u.vlan.sta)
4366 			return false;
4367 		if (sdata->wdev.use_4addr)
4368 			return false;
4369 		fallthrough;
4370 	case NL80211_IFTYPE_AP:
4371 		/* check runtime toggle for this bss */
4372 		if (!sdata->bss->multicast_to_unicast)
4373 			return false;
4374 		break;
4375 	default:
4376 		return false;
4377 	}
4378 
4379 	/* multicast to unicast conversion only for some payload */
4380 	ethertype = eth->h_proto;
4381 	if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4382 		ethertype = ethvlan->h_vlan_encapsulated_proto;
4383 	switch (ethertype) {
4384 	case htons(ETH_P_ARP):
4385 	case htons(ETH_P_IP):
4386 	case htons(ETH_P_IPV6):
4387 		break;
4388 	default:
4389 		return false;
4390 	}
4391 
4392 	return true;
4393 }
4394 
4395 static void
4396 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4397 			     struct sk_buff_head *queue)
4398 {
4399 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4400 	struct ieee80211_local *local = sdata->local;
4401 	const struct ethhdr *eth = (struct ethhdr *)skb->data;
4402 	struct sta_info *sta, *first = NULL;
4403 	struct sk_buff *cloned_skb;
4404 
4405 	rcu_read_lock();
4406 
4407 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
4408 		if (sdata != sta->sdata)
4409 			/* AP-VLAN mismatch */
4410 			continue;
4411 		if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4412 			/* do not send back to source */
4413 			continue;
4414 		if (!first) {
4415 			first = sta;
4416 			continue;
4417 		}
4418 		cloned_skb = skb_clone(skb, GFP_ATOMIC);
4419 		if (!cloned_skb)
4420 			goto multicast;
4421 		if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4422 			dev_kfree_skb(cloned_skb);
4423 			goto multicast;
4424 		}
4425 		__skb_queue_tail(queue, cloned_skb);
4426 	}
4427 
4428 	if (likely(first)) {
4429 		if (unlikely(ieee80211_change_da(skb, first)))
4430 			goto multicast;
4431 		__skb_queue_tail(queue, skb);
4432 	} else {
4433 		/* no STA connected, drop */
4434 		kfree_skb(skb);
4435 		skb = NULL;
4436 	}
4437 
4438 	goto out;
4439 multicast:
4440 	__skb_queue_purge(queue);
4441 	__skb_queue_tail(queue, skb);
4442 out:
4443 	rcu_read_unlock();
4444 }
4445 
4446 static void ieee80211_mlo_multicast_tx_one(struct ieee80211_sub_if_data *sdata,
4447 					   struct sk_buff *skb, u32 ctrl_flags,
4448 					   unsigned int link_id)
4449 {
4450 	struct sk_buff *out;
4451 
4452 	out = skb_copy(skb, GFP_ATOMIC);
4453 	if (!out)
4454 		return;
4455 
4456 	ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
4457 	__ieee80211_subif_start_xmit(out, sdata->dev, 0, ctrl_flags, NULL);
4458 }
4459 
4460 static void ieee80211_mlo_multicast_tx(struct net_device *dev,
4461 				       struct sk_buff *skb)
4462 {
4463 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4464 	unsigned long links = sdata->vif.active_links;
4465 	unsigned int link;
4466 	u32 ctrl_flags = IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX;
4467 
4468 	if (hweight16(links) == 1) {
4469 		ctrl_flags |= u32_encode_bits(__ffs(links),
4470 					      IEEE80211_TX_CTRL_MLO_LINK);
4471 
4472 		__ieee80211_subif_start_xmit(skb, sdata->dev, 0, ctrl_flags,
4473 					     NULL);
4474 		return;
4475 	}
4476 
4477 	for_each_set_bit(link, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4478 		ieee80211_mlo_multicast_tx_one(sdata, skb, ctrl_flags, link);
4479 		ctrl_flags = 0;
4480 	}
4481 	kfree_skb(skb);
4482 }
4483 
4484 /**
4485  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4486  * @skb: packet to be sent
4487  * @dev: incoming interface
4488  *
4489  * On failure skb will be freed.
4490  *
4491  * Returns: the netdev TX status (but really only %NETDEV_TX_OK)
4492  */
4493 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4494 				       struct net_device *dev)
4495 {
4496 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4497 	const struct ethhdr *eth = (void *)skb->data;
4498 
4499 	if (likely(!is_multicast_ether_addr(eth->h_dest)))
4500 		goto normal;
4501 
4502 	if (unlikely(!ieee80211_sdata_running(sdata))) {
4503 		kfree_skb(skb);
4504 		return NETDEV_TX_OK;
4505 	}
4506 
4507 	if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4508 		struct sk_buff_head queue;
4509 
4510 		__skb_queue_head_init(&queue);
4511 		ieee80211_convert_to_unicast(skb, dev, &queue);
4512 		while ((skb = __skb_dequeue(&queue)))
4513 			__ieee80211_subif_start_xmit(skb, dev, 0,
4514 						     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4515 						     NULL);
4516 	} else if (ieee80211_vif_is_mld(&sdata->vif) &&
4517 		   sdata->vif.type == NL80211_IFTYPE_AP &&
4518 		   !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX)) {
4519 		ieee80211_mlo_multicast_tx(dev, skb);
4520 	} else {
4521 normal:
4522 		__ieee80211_subif_start_xmit(skb, dev, 0,
4523 					     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4524 					     NULL);
4525 	}
4526 
4527 	return NETDEV_TX_OK;
4528 }
4529 
4530 
4531 
4532 static bool __ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4533 				struct sk_buff *skb, struct sta_info *sta,
4534 				bool txpending)
4535 {
4536 	struct ieee80211_local *local = sdata->local;
4537 	struct ieee80211_tx_control control = {};
4538 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4539 	struct ieee80211_sta *pubsta = NULL;
4540 	unsigned long flags;
4541 	int q = info->hw_queue;
4542 
4543 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4544 
4545 	if (local->queue_stop_reasons[q] ||
4546 	    (!txpending && !skb_queue_empty(&local->pending[q]))) {
4547 		if (txpending)
4548 			skb_queue_head(&local->pending[q], skb);
4549 		else
4550 			skb_queue_tail(&local->pending[q], skb);
4551 
4552 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4553 
4554 		return false;
4555 	}
4556 
4557 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4558 
4559 	if (sta && sta->uploaded)
4560 		pubsta = &sta->sta;
4561 
4562 	control.sta = pubsta;
4563 
4564 	drv_tx(local, &control, skb);
4565 
4566 	return true;
4567 }
4568 
4569 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4570 			      struct sk_buff *skb, struct sta_info *sta,
4571 			      bool txpending)
4572 {
4573 	struct ieee80211_local *local = sdata->local;
4574 	struct sk_buff *next;
4575 	bool ret = true;
4576 
4577 	if (ieee80211_queue_skb(local, sdata, sta, skb))
4578 		return true;
4579 
4580 	skb_list_walk_safe(skb, skb, next) {
4581 		skb_mark_not_on_list(skb);
4582 		if (!__ieee80211_tx_8023(sdata, skb, sta, txpending))
4583 			ret = false;
4584 	}
4585 
4586 	return ret;
4587 }
4588 
4589 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4590 				struct net_device *dev, struct sta_info *sta,
4591 				struct ieee80211_key *key, struct sk_buff *skb)
4592 {
4593 	struct ieee80211_tx_info *info;
4594 	struct ieee80211_local *local = sdata->local;
4595 	struct tid_ampdu_tx *tid_tx;
4596 	struct sk_buff *seg, *next;
4597 	unsigned int skbs = 0, len = 0;
4598 	u16 queue;
4599 	u8 tid;
4600 
4601 	queue = ieee80211_select_queue(sdata, sta, skb);
4602 	skb_set_queue_mapping(skb, queue);
4603 
4604 	if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4605 	    test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4606 		goto out_free;
4607 
4608 	skb = skb_share_check(skb, GFP_ATOMIC);
4609 	if (unlikely(!skb))
4610 		return;
4611 
4612 	ieee80211_aggr_check(sdata, sta, skb);
4613 
4614 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4615 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4616 	if (tid_tx) {
4617 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4618 			/* fall back to non-offload slow path */
4619 			__ieee80211_subif_start_xmit(skb, dev, 0,
4620 						     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4621 						     NULL);
4622 			return;
4623 		}
4624 
4625 		if (tid_tx->timeout)
4626 			tid_tx->last_tx = jiffies;
4627 	}
4628 
4629 	skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
4630 	if (!skb)
4631 		return;
4632 
4633 	info = IEEE80211_SKB_CB(skb);
4634 	memset(info, 0, sizeof(*info));
4635 
4636 	info->hw_queue = sdata->vif.hw_queue[queue];
4637 
4638 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4639 		sdata = container_of(sdata->bss,
4640 				     struct ieee80211_sub_if_data, u.ap);
4641 
4642 	info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
4643 	info->control.vif = &sdata->vif;
4644 
4645 	if (key)
4646 		info->control.hw_key = &key->conf;
4647 
4648 	skb_list_walk_safe(skb, seg, next) {
4649 		skbs++;
4650 		len += seg->len;
4651 		if (seg != skb)
4652 			memcpy(IEEE80211_SKB_CB(seg), info, sizeof(*info));
4653 	}
4654 
4655 	if (unlikely(skb->sk &&
4656 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
4657 		info->status_data = ieee80211_store_ack_skb(local, skb,
4658 							    &info->flags, NULL);
4659 		if (info->status_data)
4660 			info->status_data_idr = 1;
4661 	}
4662 
4663 	dev_sw_netstats_tx_add(dev, skbs, len);
4664 	sta->deflink.tx_stats.packets[queue] += skbs;
4665 	sta->deflink.tx_stats.bytes[queue] += len;
4666 
4667 	ieee80211_tpt_led_trig_tx(local, len);
4668 
4669 	ieee80211_tx_8023(sdata, skb, sta, false);
4670 
4671 	return;
4672 
4673 out_free:
4674 	kfree_skb(skb);
4675 }
4676 
4677 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4678 					    struct net_device *dev)
4679 {
4680 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4681 	struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4682 	struct ieee80211_key *key;
4683 	struct sta_info *sta;
4684 
4685 	if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
4686 		kfree_skb(skb);
4687 		return NETDEV_TX_OK;
4688 	}
4689 
4690 	rcu_read_lock();
4691 
4692 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4693 		kfree_skb(skb);
4694 		goto out;
4695 	}
4696 
4697 	if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4698 	    !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
4699 	    sdata->control_port_protocol == ehdr->h_proto))
4700 		goto skip_offload;
4701 
4702 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4703 	if (!key)
4704 		key = rcu_dereference(sdata->default_unicast_key);
4705 
4706 	if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4707 		    key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4708 		goto skip_offload;
4709 
4710 	sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4711 	ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4712 	goto out;
4713 
4714 skip_offload:
4715 	ieee80211_subif_start_xmit(skb, dev);
4716 out:
4717 	rcu_read_unlock();
4718 
4719 	return NETDEV_TX_OK;
4720 }
4721 
4722 struct sk_buff *
4723 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4724 			      struct sk_buff *skb, u32 info_flags)
4725 {
4726 	struct ieee80211_hdr *hdr;
4727 	struct ieee80211_tx_data tx = {
4728 		.local = sdata->local,
4729 		.sdata = sdata,
4730 	};
4731 	struct sta_info *sta;
4732 
4733 	rcu_read_lock();
4734 
4735 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4736 		kfree_skb(skb);
4737 		skb = ERR_PTR(-EINVAL);
4738 		goto out;
4739 	}
4740 
4741 	skb = ieee80211_build_hdr(sdata, skb, info_flags, sta,
4742 				  IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL);
4743 	if (IS_ERR(skb))
4744 		goto out;
4745 
4746 	hdr = (void *)skb->data;
4747 	tx.sta = sta_info_get(sdata, hdr->addr1);
4748 	tx.skb = skb;
4749 
4750 	if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4751 		rcu_read_unlock();
4752 		kfree_skb(skb);
4753 		return ERR_PTR(-EINVAL);
4754 	}
4755 
4756 out:
4757 	rcu_read_unlock();
4758 	return skb;
4759 }
4760 
4761 /*
4762  * ieee80211_clear_tx_pending may not be called in a context where
4763  * it is possible that it packets could come in again.
4764  */
4765 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4766 {
4767 	struct sk_buff *skb;
4768 	int i;
4769 
4770 	for (i = 0; i < local->hw.queues; i++) {
4771 		while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4772 			ieee80211_free_txskb(&local->hw, skb);
4773 	}
4774 }
4775 
4776 /*
4777  * Returns false if the frame couldn't be transmitted but was queued instead,
4778  * which in this case means re-queued -- take as an indication to stop sending
4779  * more pending frames.
4780  */
4781 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4782 				     struct sk_buff *skb)
4783 {
4784 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4785 	struct ieee80211_sub_if_data *sdata;
4786 	struct sta_info *sta;
4787 	struct ieee80211_hdr *hdr;
4788 	bool result;
4789 	struct ieee80211_chanctx_conf *chanctx_conf;
4790 
4791 	sdata = vif_to_sdata(info->control.vif);
4792 
4793 	if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
4794 		/* update band only for non-MLD */
4795 		if (!ieee80211_vif_is_mld(&sdata->vif)) {
4796 			chanctx_conf =
4797 				rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
4798 			if (unlikely(!chanctx_conf)) {
4799 				dev_kfree_skb(skb);
4800 				return true;
4801 			}
4802 			info->band = chanctx_conf->def.chan->band;
4803 		}
4804 		result = ieee80211_tx(sdata, NULL, skb, true);
4805 	} else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4806 		if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4807 			dev_kfree_skb(skb);
4808 			return true;
4809 		}
4810 
4811 		if (IS_ERR(sta) || (sta && !sta->uploaded))
4812 			sta = NULL;
4813 
4814 		result = ieee80211_tx_8023(sdata, skb, sta, true);
4815 	} else {
4816 		struct sk_buff_head skbs;
4817 
4818 		__skb_queue_head_init(&skbs);
4819 		__skb_queue_tail(&skbs, skb);
4820 
4821 		hdr = (struct ieee80211_hdr *)skb->data;
4822 		sta = sta_info_get(sdata, hdr->addr1);
4823 
4824 		result = __ieee80211_tx(local, &skbs, sta, true);
4825 	}
4826 
4827 	return result;
4828 }
4829 
4830 /*
4831  * Transmit all pending packets. Called from tasklet.
4832  */
4833 void ieee80211_tx_pending(struct tasklet_struct *t)
4834 {
4835 	struct ieee80211_local *local = from_tasklet(local, t,
4836 						     tx_pending_tasklet);
4837 	unsigned long flags;
4838 	int i;
4839 	bool txok;
4840 
4841 	rcu_read_lock();
4842 
4843 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4844 	for (i = 0; i < local->hw.queues; i++) {
4845 		/*
4846 		 * If queue is stopped by something other than due to pending
4847 		 * frames, or we have no pending frames, proceed to next queue.
4848 		 */
4849 		if (local->queue_stop_reasons[i] ||
4850 		    skb_queue_empty(&local->pending[i]))
4851 			continue;
4852 
4853 		while (!skb_queue_empty(&local->pending[i])) {
4854 			struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
4855 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4856 
4857 			if (WARN_ON(!info->control.vif)) {
4858 				ieee80211_free_txskb(&local->hw, skb);
4859 				continue;
4860 			}
4861 
4862 			spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4863 						flags);
4864 
4865 			txok = ieee80211_tx_pending_skb(local, skb);
4866 			spin_lock_irqsave(&local->queue_stop_reason_lock,
4867 					  flags);
4868 			if (!txok)
4869 				break;
4870 		}
4871 	}
4872 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4873 
4874 	rcu_read_unlock();
4875 }
4876 
4877 /* functions for drivers to get certain frames */
4878 
4879 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4880 				       struct ieee80211_link_data *link,
4881 				       struct ps_data *ps, struct sk_buff *skb,
4882 				       bool is_template)
4883 {
4884 	u8 *pos, *tim;
4885 	int aid0 = 0;
4886 	int i, have_bits = 0, n1, n2;
4887 	struct ieee80211_bss_conf *link_conf = link->conf;
4888 
4889 	/* Generate bitmap for TIM only if there are any STAs in power save
4890 	 * mode. */
4891 	if (atomic_read(&ps->num_sta_ps) > 0)
4892 		/* in the hope that this is faster than
4893 		 * checking byte-for-byte */
4894 		have_bits = !bitmap_empty((unsigned long *)ps->tim,
4895 					  IEEE80211_MAX_AID+1);
4896 	if (!is_template) {
4897 		if (ps->dtim_count == 0)
4898 			ps->dtim_count = link_conf->dtim_period - 1;
4899 		else
4900 			ps->dtim_count--;
4901 	}
4902 
4903 	tim = pos = skb_put(skb, 5);
4904 	*pos++ = WLAN_EID_TIM;
4905 	*pos++ = 3;
4906 	*pos++ = ps->dtim_count;
4907 	*pos++ = link_conf->dtim_period;
4908 
4909 	if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4910 		aid0 = 1;
4911 
4912 	ps->dtim_bc_mc = aid0 == 1;
4913 
4914 	if (have_bits) {
4915 		/* Find largest even number N1 so that bits numbered 1 through
4916 		 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4917 		 * (N2 + 1) x 8 through 2007 are 0. */
4918 		n1 = 0;
4919 		for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4920 			if (ps->tim[i]) {
4921 				n1 = i & 0xfe;
4922 				break;
4923 			}
4924 		}
4925 		n2 = n1;
4926 		for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4927 			if (ps->tim[i]) {
4928 				n2 = i;
4929 				break;
4930 			}
4931 		}
4932 
4933 		/* Bitmap control */
4934 		*pos++ = n1 | aid0;
4935 		/* Part Virt Bitmap */
4936 		skb_put_data(skb, ps->tim + n1, n2 - n1 + 1);
4937 
4938 		tim[1] = n2 - n1 + 4;
4939 	} else {
4940 		*pos++ = aid0; /* Bitmap control */
4941 
4942 		if (ieee80211_get_link_sband(link)->band != NL80211_BAND_S1GHZ) {
4943 			tim[1] = 4;
4944 			/* Part Virt Bitmap */
4945 			skb_put_u8(skb, 0);
4946 		}
4947 	}
4948 }
4949 
4950 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4951 				    struct ieee80211_link_data *link,
4952 				    struct ps_data *ps, struct sk_buff *skb,
4953 				    bool is_template)
4954 {
4955 	struct ieee80211_local *local = sdata->local;
4956 
4957 	/*
4958 	 * Not very nice, but we want to allow the driver to call
4959 	 * ieee80211_beacon_get() as a response to the set_tim()
4960 	 * callback. That, however, is already invoked under the
4961 	 * sta_lock to guarantee consistent and race-free update
4962 	 * of the tim bitmap in mac80211 and the driver.
4963 	 */
4964 	if (local->tim_in_locked_section) {
4965 		__ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
4966 	} else {
4967 		spin_lock_bh(&local->tim_lock);
4968 		__ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
4969 		spin_unlock_bh(&local->tim_lock);
4970 	}
4971 
4972 	return 0;
4973 }
4974 
4975 static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4976 					struct beacon_data *beacon,
4977 					struct ieee80211_link_data *link)
4978 {
4979 	u8 *beacon_data, count, max_count = 1;
4980 	struct probe_resp *resp;
4981 	size_t beacon_data_len;
4982 	u16 *bcn_offsets;
4983 	int i;
4984 
4985 	switch (sdata->vif.type) {
4986 	case NL80211_IFTYPE_AP:
4987 		beacon_data = beacon->tail;
4988 		beacon_data_len = beacon->tail_len;
4989 		break;
4990 	case NL80211_IFTYPE_ADHOC:
4991 		beacon_data = beacon->head;
4992 		beacon_data_len = beacon->head_len;
4993 		break;
4994 	case NL80211_IFTYPE_MESH_POINT:
4995 		beacon_data = beacon->head;
4996 		beacon_data_len = beacon->head_len;
4997 		break;
4998 	default:
4999 		return;
5000 	}
5001 
5002 	resp = rcu_dereference(link->u.ap.probe_resp);
5003 
5004 	bcn_offsets = beacon->cntdwn_counter_offsets;
5005 	count = beacon->cntdwn_current_counter;
5006 	if (link->conf->csa_active)
5007 		max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
5008 
5009 	for (i = 0; i < max_count; ++i) {
5010 		if (bcn_offsets[i]) {
5011 			if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len))
5012 				return;
5013 			beacon_data[bcn_offsets[i]] = count;
5014 		}
5015 
5016 		if (sdata->vif.type == NL80211_IFTYPE_AP && resp) {
5017 			u16 *resp_offsets = resp->cntdwn_counter_offsets;
5018 
5019 			resp->data[resp_offsets[i]] = count;
5020 		}
5021 	}
5022 }
5023 
5024 static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
5025 {
5026 	beacon->cntdwn_current_counter--;
5027 
5028 	/* the counter should never reach 0 */
5029 	WARN_ON_ONCE(!beacon->cntdwn_current_counter);
5030 
5031 	return beacon->cntdwn_current_counter;
5032 }
5033 
5034 u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
5035 {
5036 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5037 	struct beacon_data *beacon = NULL;
5038 	u8 count = 0;
5039 
5040 	rcu_read_lock();
5041 
5042 	if (sdata->vif.type == NL80211_IFTYPE_AP)
5043 		beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
5044 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
5045 		beacon = rcu_dereference(sdata->u.ibss.presp);
5046 	else if (ieee80211_vif_is_mesh(&sdata->vif))
5047 		beacon = rcu_dereference(sdata->u.mesh.beacon);
5048 
5049 	if (!beacon)
5050 		goto unlock;
5051 
5052 	count = __ieee80211_beacon_update_cntdwn(beacon);
5053 
5054 unlock:
5055 	rcu_read_unlock();
5056 	return count;
5057 }
5058 EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
5059 
5060 void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
5061 {
5062 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5063 	struct beacon_data *beacon = NULL;
5064 
5065 	rcu_read_lock();
5066 
5067 	if (sdata->vif.type == NL80211_IFTYPE_AP)
5068 		beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
5069 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
5070 		beacon = rcu_dereference(sdata->u.ibss.presp);
5071 	else if (ieee80211_vif_is_mesh(&sdata->vif))
5072 		beacon = rcu_dereference(sdata->u.mesh.beacon);
5073 
5074 	if (!beacon)
5075 		goto unlock;
5076 
5077 	if (counter < beacon->cntdwn_current_counter)
5078 		beacon->cntdwn_current_counter = counter;
5079 
5080 unlock:
5081 	rcu_read_unlock();
5082 }
5083 EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
5084 
5085 bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
5086 {
5087 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5088 	struct beacon_data *beacon = NULL;
5089 	u8 *beacon_data;
5090 	size_t beacon_data_len;
5091 	int ret = false;
5092 
5093 	if (!ieee80211_sdata_running(sdata))
5094 		return false;
5095 
5096 	rcu_read_lock();
5097 	if (vif->type == NL80211_IFTYPE_AP) {
5098 		beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
5099 		if (WARN_ON(!beacon || !beacon->tail))
5100 			goto out;
5101 		beacon_data = beacon->tail;
5102 		beacon_data_len = beacon->tail_len;
5103 	} else if (vif->type == NL80211_IFTYPE_ADHOC) {
5104 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5105 
5106 		beacon = rcu_dereference(ifibss->presp);
5107 		if (!beacon)
5108 			goto out;
5109 
5110 		beacon_data = beacon->head;
5111 		beacon_data_len = beacon->head_len;
5112 	} else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
5113 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
5114 
5115 		beacon = rcu_dereference(ifmsh->beacon);
5116 		if (!beacon)
5117 			goto out;
5118 
5119 		beacon_data = beacon->head;
5120 		beacon_data_len = beacon->head_len;
5121 	} else {
5122 		WARN_ON(1);
5123 		goto out;
5124 	}
5125 
5126 	if (!beacon->cntdwn_counter_offsets[0])
5127 		goto out;
5128 
5129 	if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
5130 		goto out;
5131 
5132 	if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
5133 		ret = true;
5134 
5135  out:
5136 	rcu_read_unlock();
5137 
5138 	return ret;
5139 }
5140 EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
5141 
5142 static int ieee80211_beacon_protect(struct sk_buff *skb,
5143 				    struct ieee80211_local *local,
5144 				    struct ieee80211_sub_if_data *sdata,
5145 				    struct ieee80211_link_data *link)
5146 {
5147 	ieee80211_tx_result res;
5148 	struct ieee80211_tx_data tx;
5149 	struct sk_buff *check_skb;
5150 
5151 	memset(&tx, 0, sizeof(tx));
5152 	tx.key = rcu_dereference(link->default_beacon_key);
5153 	if (!tx.key)
5154 		return 0;
5155 
5156 	if (unlikely(tx.key->flags & KEY_FLAG_TAINTED)) {
5157 		tx.key = NULL;
5158 		return -EINVAL;
5159 	}
5160 
5161 	if (!(tx.key->conf.flags & IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
5162 	    tx.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
5163 		IEEE80211_SKB_CB(skb)->control.hw_key = &tx.key->conf;
5164 
5165 	tx.local = local;
5166 	tx.sdata = sdata;
5167 	__skb_queue_head_init(&tx.skbs);
5168 	__skb_queue_tail(&tx.skbs, skb);
5169 	res = ieee80211_tx_h_encrypt(&tx);
5170 	check_skb = __skb_dequeue(&tx.skbs);
5171 	/* we may crash after this, but it'd be a bug in crypto */
5172 	WARN_ON(check_skb != skb);
5173 	if (WARN_ON_ONCE(res != TX_CONTINUE))
5174 		return -EINVAL;
5175 
5176 	return 0;
5177 }
5178 
5179 static void
5180 ieee80211_beacon_get_finish(struct ieee80211_hw *hw,
5181 			    struct ieee80211_vif *vif,
5182 			    struct ieee80211_link_data *link,
5183 			    struct ieee80211_mutable_offsets *offs,
5184 			    struct beacon_data *beacon,
5185 			    struct sk_buff *skb,
5186 			    struct ieee80211_chanctx_conf *chanctx_conf,
5187 			    u16 csa_off_base)
5188 {
5189 	struct ieee80211_local *local = hw_to_local(hw);
5190 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5191 	struct ieee80211_tx_info *info;
5192 	enum nl80211_band band;
5193 	struct ieee80211_tx_rate_control txrc;
5194 
5195 	/* CSA offsets */
5196 	if (offs && beacon) {
5197 		u16 i;
5198 
5199 		for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
5200 			u16 csa_off = beacon->cntdwn_counter_offsets[i];
5201 
5202 			if (!csa_off)
5203 				continue;
5204 
5205 			offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
5206 		}
5207 	}
5208 
5209 	band = chanctx_conf->def.chan->band;
5210 	info = IEEE80211_SKB_CB(skb);
5211 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5212 	info->flags |= IEEE80211_TX_CTL_NO_ACK;
5213 	info->band = band;
5214 
5215 	memset(&txrc, 0, sizeof(txrc));
5216 	txrc.hw = hw;
5217 	txrc.sband = local->hw.wiphy->bands[band];
5218 	txrc.bss_conf = link->conf;
5219 	txrc.skb = skb;
5220 	txrc.reported_rate.idx = -1;
5221 	if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
5222 		txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
5223 	else
5224 		txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
5225 	txrc.bss = true;
5226 	rate_control_get_rate(sdata, NULL, &txrc);
5227 
5228 	info->control.vif = vif;
5229 	info->control.flags |= u32_encode_bits(link->link_id,
5230 					       IEEE80211_TX_CTRL_MLO_LINK);
5231 	info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
5232 		       IEEE80211_TX_CTL_ASSIGN_SEQ |
5233 		       IEEE80211_TX_CTL_FIRST_FRAGMENT;
5234 }
5235 
5236 static void
5237 ieee80211_beacon_add_mbssid(struct sk_buff *skb, struct beacon_data *beacon,
5238 			    u8 i)
5239 {
5240 	if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt ||
5241 	    i > beacon->mbssid_ies->cnt)
5242 		return;
5243 
5244 	if (i < beacon->mbssid_ies->cnt) {
5245 		skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
5246 			     beacon->mbssid_ies->elem[i].len);
5247 
5248 		if (beacon->rnr_ies && beacon->rnr_ies->cnt) {
5249 			skb_put_data(skb, beacon->rnr_ies->elem[i].data,
5250 				     beacon->rnr_ies->elem[i].len);
5251 
5252 			for (i = beacon->mbssid_ies->cnt; i < beacon->rnr_ies->cnt; i++)
5253 				skb_put_data(skb, beacon->rnr_ies->elem[i].data,
5254 					     beacon->rnr_ies->elem[i].len);
5255 		}
5256 		return;
5257 	}
5258 
5259 	/* i == beacon->mbssid_ies->cnt, include all MBSSID elements */
5260 	for (i = 0; i < beacon->mbssid_ies->cnt; i++)
5261 		skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
5262 			     beacon->mbssid_ies->elem[i].len);
5263 }
5264 
5265 static struct sk_buff *
5266 ieee80211_beacon_get_ap(struct ieee80211_hw *hw,
5267 			struct ieee80211_vif *vif,
5268 			struct ieee80211_link_data *link,
5269 			struct ieee80211_mutable_offsets *offs,
5270 			bool is_template,
5271 			struct beacon_data *beacon,
5272 			struct ieee80211_chanctx_conf *chanctx_conf,
5273 			u8 ema_index)
5274 {
5275 	struct ieee80211_local *local = hw_to_local(hw);
5276 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5277 	struct ieee80211_if_ap *ap = &sdata->u.ap;
5278 	struct sk_buff *skb = NULL;
5279 	u16 csa_off_base = 0;
5280 	int mbssid_len;
5281 
5282 	if (beacon->cntdwn_counter_offsets[0]) {
5283 		if (!is_template)
5284 			ieee80211_beacon_update_cntdwn(vif);
5285 
5286 		ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5287 	}
5288 
5289 	/* headroom, head length,
5290 	 * tail length, maximum TIM length and multiple BSSID length
5291 	 */
5292 	mbssid_len = ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies,
5293 						     beacon->rnr_ies,
5294 						     ema_index);
5295 
5296 	skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5297 			    beacon->tail_len + 256 +
5298 			    local->hw.extra_beacon_tailroom + mbssid_len);
5299 	if (!skb)
5300 		return NULL;
5301 
5302 	skb_reserve(skb, local->tx_headroom);
5303 	skb_put_data(skb, beacon->head, beacon->head_len);
5304 
5305 	ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template);
5306 
5307 	if (offs) {
5308 		offs->tim_offset = beacon->head_len;
5309 		offs->tim_length = skb->len - beacon->head_len;
5310 		offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0];
5311 
5312 		if (mbssid_len) {
5313 			ieee80211_beacon_add_mbssid(skb, beacon, ema_index);
5314 			offs->mbssid_off = skb->len - mbssid_len;
5315 		}
5316 
5317 		/* for AP the csa offsets are from tail */
5318 		csa_off_base = skb->len;
5319 	}
5320 
5321 	if (beacon->tail)
5322 		skb_put_data(skb, beacon->tail, beacon->tail_len);
5323 
5324 	if (ieee80211_beacon_protect(skb, local, sdata, link) < 0)
5325 		return NULL;
5326 
5327 	ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5328 				    chanctx_conf, csa_off_base);
5329 	return skb;
5330 }
5331 
5332 static struct ieee80211_ema_beacons *
5333 ieee80211_beacon_get_ap_ema_list(struct ieee80211_hw *hw,
5334 				 struct ieee80211_vif *vif,
5335 				 struct ieee80211_link_data *link,
5336 				 struct ieee80211_mutable_offsets *offs,
5337 				 bool is_template, struct beacon_data *beacon,
5338 				 struct ieee80211_chanctx_conf *chanctx_conf)
5339 {
5340 	struct ieee80211_ema_beacons *ema = NULL;
5341 
5342 	if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt)
5343 		return NULL;
5344 
5345 	ema = kzalloc(struct_size(ema, bcn, beacon->mbssid_ies->cnt),
5346 		      GFP_ATOMIC);
5347 	if (!ema)
5348 		return NULL;
5349 
5350 	for (ema->cnt = 0; ema->cnt < beacon->mbssid_ies->cnt; ema->cnt++) {
5351 		ema->bcn[ema->cnt].skb =
5352 			ieee80211_beacon_get_ap(hw, vif, link,
5353 						&ema->bcn[ema->cnt].offs,
5354 						is_template, beacon,
5355 						chanctx_conf, ema->cnt);
5356 		if (!ema->bcn[ema->cnt].skb)
5357 			break;
5358 	}
5359 
5360 	if (ema->cnt == beacon->mbssid_ies->cnt)
5361 		return ema;
5362 
5363 	ieee80211_beacon_free_ema_list(ema);
5364 	return NULL;
5365 }
5366 
5367 #define IEEE80211_INCLUDE_ALL_MBSSID_ELEMS -1
5368 
5369 static struct sk_buff *
5370 __ieee80211_beacon_get(struct ieee80211_hw *hw,
5371 		       struct ieee80211_vif *vif,
5372 		       struct ieee80211_mutable_offsets *offs,
5373 		       bool is_template,
5374 		       unsigned int link_id,
5375 		       int ema_index,
5376 		       struct ieee80211_ema_beacons **ema_beacons)
5377 {
5378 	struct ieee80211_local *local = hw_to_local(hw);
5379 	struct beacon_data *beacon = NULL;
5380 	struct sk_buff *skb = NULL;
5381 	struct ieee80211_sub_if_data *sdata = NULL;
5382 	struct ieee80211_chanctx_conf *chanctx_conf;
5383 	struct ieee80211_link_data *link;
5384 
5385 	rcu_read_lock();
5386 
5387 	sdata = vif_to_sdata(vif);
5388 	link = rcu_dereference(sdata->link[link_id]);
5389 	if (!link)
5390 		goto out;
5391 	chanctx_conf =
5392 		rcu_dereference(link->conf->chanctx_conf);
5393 
5394 	if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
5395 		goto out;
5396 
5397 	if (offs)
5398 		memset(offs, 0, sizeof(*offs));
5399 
5400 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
5401 		beacon = rcu_dereference(link->u.ap.beacon);
5402 		if (!beacon)
5403 			goto out;
5404 
5405 		if (ema_beacons) {
5406 			*ema_beacons =
5407 				ieee80211_beacon_get_ap_ema_list(hw, vif, link,
5408 								 offs,
5409 								 is_template,
5410 								 beacon,
5411 								 chanctx_conf);
5412 		} else {
5413 			if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) {
5414 				if (ema_index >= beacon->mbssid_ies->cnt)
5415 					goto out; /* End of MBSSID elements */
5416 
5417 				if (ema_index <= IEEE80211_INCLUDE_ALL_MBSSID_ELEMS)
5418 					ema_index = beacon->mbssid_ies->cnt;
5419 			} else {
5420 				ema_index = 0;
5421 			}
5422 
5423 			skb = ieee80211_beacon_get_ap(hw, vif, link, offs,
5424 						      is_template, beacon,
5425 						      chanctx_conf,
5426 						      ema_index);
5427 		}
5428 	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
5429 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5430 		struct ieee80211_hdr *hdr;
5431 
5432 		beacon = rcu_dereference(ifibss->presp);
5433 		if (!beacon)
5434 			goto out;
5435 
5436 		if (beacon->cntdwn_counter_offsets[0]) {
5437 			if (!is_template)
5438 				__ieee80211_beacon_update_cntdwn(beacon);
5439 
5440 			ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5441 		}
5442 
5443 		skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5444 				    local->hw.extra_beacon_tailroom);
5445 		if (!skb)
5446 			goto out;
5447 		skb_reserve(skb, local->tx_headroom);
5448 		skb_put_data(skb, beacon->head, beacon->head_len);
5449 
5450 		hdr = (struct ieee80211_hdr *) skb->data;
5451 		hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5452 						 IEEE80211_STYPE_BEACON);
5453 
5454 		ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5455 					    chanctx_conf, 0);
5456 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5457 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
5458 
5459 		beacon = rcu_dereference(ifmsh->beacon);
5460 		if (!beacon)
5461 			goto out;
5462 
5463 		if (beacon->cntdwn_counter_offsets[0]) {
5464 			if (!is_template)
5465 				/* TODO: For mesh csa_counter is in TU, so
5466 				 * decrementing it by one isn't correct, but
5467 				 * for now we leave it consistent with overall
5468 				 * mac80211's behavior.
5469 				 */
5470 				__ieee80211_beacon_update_cntdwn(beacon);
5471 
5472 			ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5473 		}
5474 
5475 		if (ifmsh->sync_ops)
5476 			ifmsh->sync_ops->adjust_tsf(sdata, beacon);
5477 
5478 		skb = dev_alloc_skb(local->tx_headroom +
5479 				    beacon->head_len +
5480 				    256 + /* TIM IE */
5481 				    beacon->tail_len +
5482 				    local->hw.extra_beacon_tailroom);
5483 		if (!skb)
5484 			goto out;
5485 		skb_reserve(skb, local->tx_headroom);
5486 		skb_put_data(skb, beacon->head, beacon->head_len);
5487 		ieee80211_beacon_add_tim(sdata, link, &ifmsh->ps, skb,
5488 					 is_template);
5489 
5490 		if (offs) {
5491 			offs->tim_offset = beacon->head_len;
5492 			offs->tim_length = skb->len - beacon->head_len;
5493 		}
5494 
5495 		skb_put_data(skb, beacon->tail, beacon->tail_len);
5496 		ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5497 					    chanctx_conf, 0);
5498 	} else {
5499 		WARN_ON(1);
5500 		goto out;
5501 	}
5502 
5503  out:
5504 	rcu_read_unlock();
5505 	return skb;
5506 
5507 }
5508 
5509 struct sk_buff *
5510 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
5511 			      struct ieee80211_vif *vif,
5512 			      struct ieee80211_mutable_offsets *offs,
5513 			      unsigned int link_id)
5514 {
5515 	return __ieee80211_beacon_get(hw, vif, offs, true, link_id,
5516 				      IEEE80211_INCLUDE_ALL_MBSSID_ELEMS, NULL);
5517 }
5518 EXPORT_SYMBOL(ieee80211_beacon_get_template);
5519 
5520 struct sk_buff *
5521 ieee80211_beacon_get_template_ema_index(struct ieee80211_hw *hw,
5522 					struct ieee80211_vif *vif,
5523 					struct ieee80211_mutable_offsets *offs,
5524 					unsigned int link_id, u8 ema_index)
5525 {
5526 	return __ieee80211_beacon_get(hw, vif, offs, true, link_id, ema_index,
5527 				      NULL);
5528 }
5529 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_index);
5530 
5531 void ieee80211_beacon_free_ema_list(struct ieee80211_ema_beacons *ema_beacons)
5532 {
5533 	u8 i;
5534 
5535 	if (!ema_beacons)
5536 		return;
5537 
5538 	for (i = 0; i < ema_beacons->cnt; i++)
5539 		kfree_skb(ema_beacons->bcn[i].skb);
5540 
5541 	kfree(ema_beacons);
5542 }
5543 EXPORT_SYMBOL(ieee80211_beacon_free_ema_list);
5544 
5545 struct ieee80211_ema_beacons *
5546 ieee80211_beacon_get_template_ema_list(struct ieee80211_hw *hw,
5547 				       struct ieee80211_vif *vif,
5548 				       unsigned int link_id)
5549 {
5550 	struct ieee80211_ema_beacons *ema_beacons = NULL;
5551 
5552 	WARN_ON(__ieee80211_beacon_get(hw, vif, NULL, true, link_id, 0,
5553 				       &ema_beacons));
5554 
5555 	return ema_beacons;
5556 }
5557 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_list);
5558 
5559 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
5560 					 struct ieee80211_vif *vif,
5561 					 u16 *tim_offset, u16 *tim_length,
5562 					 unsigned int link_id)
5563 {
5564 	struct ieee80211_mutable_offsets offs = {};
5565 	struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false,
5566 						     link_id,
5567 						     IEEE80211_INCLUDE_ALL_MBSSID_ELEMS,
5568 						     NULL);
5569 	struct sk_buff *copy;
5570 
5571 	if (!bcn)
5572 		return bcn;
5573 
5574 	if (tim_offset)
5575 		*tim_offset = offs.tim_offset;
5576 
5577 	if (tim_length)
5578 		*tim_length = offs.tim_length;
5579 
5580 	if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
5581 	    !hw_to_local(hw)->monitors)
5582 		return bcn;
5583 
5584 	/* send a copy to monitor interfaces */
5585 	copy = skb_copy(bcn, GFP_ATOMIC);
5586 	if (!copy)
5587 		return bcn;
5588 
5589 	ieee80211_tx_monitor(hw_to_local(hw), copy, 1, false, NULL);
5590 
5591 	return bcn;
5592 }
5593 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
5594 
5595 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
5596 					struct ieee80211_vif *vif)
5597 {
5598 	struct sk_buff *skb = NULL;
5599 	struct probe_resp *presp = NULL;
5600 	struct ieee80211_hdr *hdr;
5601 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5602 
5603 	if (sdata->vif.type != NL80211_IFTYPE_AP)
5604 		return NULL;
5605 
5606 	rcu_read_lock();
5607 	presp = rcu_dereference(sdata->deflink.u.ap.probe_resp);
5608 	if (!presp)
5609 		goto out;
5610 
5611 	skb = dev_alloc_skb(presp->len);
5612 	if (!skb)
5613 		goto out;
5614 
5615 	skb_put_data(skb, presp->data, presp->len);
5616 
5617 	hdr = (struct ieee80211_hdr *) skb->data;
5618 	memset(hdr->addr1, 0, sizeof(hdr->addr1));
5619 
5620 out:
5621 	rcu_read_unlock();
5622 	return skb;
5623 }
5624 EXPORT_SYMBOL(ieee80211_proberesp_get);
5625 
5626 struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5627 						  struct ieee80211_vif *vif)
5628 {
5629 	struct sk_buff *skb = NULL;
5630 	struct fils_discovery_data *tmpl = NULL;
5631 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5632 
5633 	if (sdata->vif.type != NL80211_IFTYPE_AP)
5634 		return NULL;
5635 
5636 	rcu_read_lock();
5637 	tmpl = rcu_dereference(sdata->deflink.u.ap.fils_discovery);
5638 	if (!tmpl) {
5639 		rcu_read_unlock();
5640 		return NULL;
5641 	}
5642 
5643 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5644 	if (skb) {
5645 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5646 		skb_put_data(skb, tmpl->data, tmpl->len);
5647 	}
5648 
5649 	rcu_read_unlock();
5650 	return skb;
5651 }
5652 EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5653 
5654 struct sk_buff *
5655 ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5656 					  struct ieee80211_vif *vif)
5657 {
5658 	struct sk_buff *skb = NULL;
5659 	struct unsol_bcast_probe_resp_data *tmpl = NULL;
5660 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5661 
5662 	if (sdata->vif.type != NL80211_IFTYPE_AP)
5663 		return NULL;
5664 
5665 	rcu_read_lock();
5666 	tmpl = rcu_dereference(sdata->deflink.u.ap.unsol_bcast_probe_resp);
5667 	if (!tmpl) {
5668 		rcu_read_unlock();
5669 		return NULL;
5670 	}
5671 
5672 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5673 	if (skb) {
5674 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5675 		skb_put_data(skb, tmpl->data, tmpl->len);
5676 	}
5677 
5678 	rcu_read_unlock();
5679 	return skb;
5680 }
5681 EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5682 
5683 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5684 				     struct ieee80211_vif *vif)
5685 {
5686 	struct ieee80211_sub_if_data *sdata;
5687 	struct ieee80211_pspoll *pspoll;
5688 	struct ieee80211_local *local;
5689 	struct sk_buff *skb;
5690 
5691 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5692 		return NULL;
5693 
5694 	sdata = vif_to_sdata(vif);
5695 	local = sdata->local;
5696 
5697 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
5698 	if (!skb)
5699 		return NULL;
5700 
5701 	skb_reserve(skb, local->hw.extra_tx_headroom);
5702 
5703 	pspoll = skb_put_zero(skb, sizeof(*pspoll));
5704 	pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5705 					    IEEE80211_STYPE_PSPOLL);
5706 	pspoll->aid = cpu_to_le16(sdata->vif.cfg.aid);
5707 
5708 	/* aid in PS-Poll has its two MSBs each set to 1 */
5709 	pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5710 
5711 	memcpy(pspoll->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN);
5712 	memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5713 
5714 	return skb;
5715 }
5716 EXPORT_SYMBOL(ieee80211_pspoll_get);
5717 
5718 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5719 				       struct ieee80211_vif *vif,
5720 				       int link_id, bool qos_ok)
5721 {
5722 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5723 	struct ieee80211_local *local = sdata->local;
5724 	struct ieee80211_link_data *link = NULL;
5725 	struct ieee80211_hdr_3addr *nullfunc;
5726 	struct sk_buff *skb;
5727 	bool qos = false;
5728 
5729 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5730 		return NULL;
5731 
5732 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5733 			    sizeof(*nullfunc) + 2);
5734 	if (!skb)
5735 		return NULL;
5736 
5737 	rcu_read_lock();
5738 	if (qos_ok) {
5739 		struct sta_info *sta;
5740 
5741 		sta = sta_info_get(sdata, vif->cfg.ap_addr);
5742 		qos = sta && sta->sta.wme;
5743 	}
5744 
5745 	if (link_id >= 0) {
5746 		link = rcu_dereference(sdata->link[link_id]);
5747 		if (WARN_ON_ONCE(!link)) {
5748 			rcu_read_unlock();
5749 			kfree_skb(skb);
5750 			return NULL;
5751 		}
5752 	}
5753 
5754 	skb_reserve(skb, local->hw.extra_tx_headroom);
5755 
5756 	nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
5757 	nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5758 					      IEEE80211_STYPE_NULLFUNC |
5759 					      IEEE80211_FCTL_TODS);
5760 	if (qos) {
5761 		__le16 qoshdr = cpu_to_le16(7);
5762 
5763 		BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5764 			      IEEE80211_STYPE_NULLFUNC) !=
5765 			     IEEE80211_STYPE_QOS_NULLFUNC);
5766 		nullfunc->frame_control |=
5767 			cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5768 		skb->priority = 7;
5769 		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
5770 		skb_put_data(skb, &qoshdr, sizeof(qoshdr));
5771 	}
5772 
5773 	if (link) {
5774 		memcpy(nullfunc->addr1, link->conf->bssid, ETH_ALEN);
5775 		memcpy(nullfunc->addr2, link->conf->addr, ETH_ALEN);
5776 		memcpy(nullfunc->addr3, link->conf->bssid, ETH_ALEN);
5777 	} else {
5778 		memcpy(nullfunc->addr1, vif->cfg.ap_addr, ETH_ALEN);
5779 		memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5780 		memcpy(nullfunc->addr3, vif->cfg.ap_addr, ETH_ALEN);
5781 	}
5782 	rcu_read_unlock();
5783 
5784 	return skb;
5785 }
5786 EXPORT_SYMBOL(ieee80211_nullfunc_get);
5787 
5788 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
5789 				       const u8 *src_addr,
5790 				       const u8 *ssid, size_t ssid_len,
5791 				       size_t tailroom)
5792 {
5793 	struct ieee80211_local *local = hw_to_local(hw);
5794 	struct ieee80211_hdr_3addr *hdr;
5795 	struct sk_buff *skb;
5796 	size_t ie_ssid_len;
5797 	u8 *pos;
5798 
5799 	ie_ssid_len = 2 + ssid_len;
5800 
5801 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
5802 			    ie_ssid_len + tailroom);
5803 	if (!skb)
5804 		return NULL;
5805 
5806 	skb_reserve(skb, local->hw.extra_tx_headroom);
5807 
5808 	hdr = skb_put_zero(skb, sizeof(*hdr));
5809 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5810 					 IEEE80211_STYPE_PROBE_REQ);
5811 	eth_broadcast_addr(hdr->addr1);
5812 	memcpy(hdr->addr2, src_addr, ETH_ALEN);
5813 	eth_broadcast_addr(hdr->addr3);
5814 
5815 	pos = skb_put(skb, ie_ssid_len);
5816 	*pos++ = WLAN_EID_SSID;
5817 	*pos++ = ssid_len;
5818 	if (ssid_len)
5819 		memcpy(pos, ssid, ssid_len);
5820 	pos += ssid_len;
5821 
5822 	return skb;
5823 }
5824 EXPORT_SYMBOL(ieee80211_probereq_get);
5825 
5826 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5827 		       const void *frame, size_t frame_len,
5828 		       const struct ieee80211_tx_info *frame_txctl,
5829 		       struct ieee80211_rts *rts)
5830 {
5831 	const struct ieee80211_hdr *hdr = frame;
5832 
5833 	rts->frame_control =
5834 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
5835 	rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5836 					       frame_txctl);
5837 	memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5838 	memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5839 }
5840 EXPORT_SYMBOL(ieee80211_rts_get);
5841 
5842 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5843 			     const void *frame, size_t frame_len,
5844 			     const struct ieee80211_tx_info *frame_txctl,
5845 			     struct ieee80211_cts *cts)
5846 {
5847 	const struct ieee80211_hdr *hdr = frame;
5848 
5849 	cts->frame_control =
5850 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
5851 	cts->duration = ieee80211_ctstoself_duration(hw, vif,
5852 						     frame_len, frame_txctl);
5853 	memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5854 }
5855 EXPORT_SYMBOL(ieee80211_ctstoself_get);
5856 
5857 struct sk_buff *
5858 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
5859 			  struct ieee80211_vif *vif)
5860 {
5861 	struct ieee80211_local *local = hw_to_local(hw);
5862 	struct sk_buff *skb = NULL;
5863 	struct ieee80211_tx_data tx;
5864 	struct ieee80211_sub_if_data *sdata;
5865 	struct ps_data *ps;
5866 	struct ieee80211_tx_info *info;
5867 	struct ieee80211_chanctx_conf *chanctx_conf;
5868 
5869 	sdata = vif_to_sdata(vif);
5870 
5871 	rcu_read_lock();
5872 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
5873 
5874 	if (!chanctx_conf)
5875 		goto out;
5876 
5877 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
5878 		struct beacon_data *beacon =
5879 				rcu_dereference(sdata->deflink.u.ap.beacon);
5880 
5881 		if (!beacon || !beacon->head)
5882 			goto out;
5883 
5884 		ps = &sdata->u.ap.ps;
5885 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5886 		ps = &sdata->u.mesh.ps;
5887 	} else {
5888 		goto out;
5889 	}
5890 
5891 	if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
5892 		goto out; /* send buffered bc/mc only after DTIM beacon */
5893 
5894 	while (1) {
5895 		skb = skb_dequeue(&ps->bc_buf);
5896 		if (!skb)
5897 			goto out;
5898 		local->total_ps_buffered--;
5899 
5900 		if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
5901 			struct ieee80211_hdr *hdr =
5902 				(struct ieee80211_hdr *) skb->data;
5903 			/* more buffered multicast/broadcast frames ==> set
5904 			 * MoreData flag in IEEE 802.11 header to inform PS
5905 			 * STAs */
5906 			hdr->frame_control |=
5907 				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5908 		}
5909 
5910 		if (sdata->vif.type == NL80211_IFTYPE_AP)
5911 			sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
5912 		if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
5913 			break;
5914 		ieee80211_free_txskb(hw, skb);
5915 	}
5916 
5917 	info = IEEE80211_SKB_CB(skb);
5918 
5919 	tx.flags |= IEEE80211_TX_PS_BUFFERED;
5920 	info->band = chanctx_conf->def.chan->band;
5921 
5922 	if (invoke_tx_handlers(&tx))
5923 		skb = NULL;
5924  out:
5925 	rcu_read_unlock();
5926 
5927 	return skb;
5928 }
5929 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
5930 
5931 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5932 {
5933 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5934 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5935 	struct ieee80211_local *local = sdata->local;
5936 	int ret;
5937 	u32 queues;
5938 
5939 	lockdep_assert_wiphy(local->hw.wiphy);
5940 
5941 	/* only some cases are supported right now */
5942 	switch (sdata->vif.type) {
5943 	case NL80211_IFTYPE_STATION:
5944 	case NL80211_IFTYPE_AP:
5945 	case NL80211_IFTYPE_AP_VLAN:
5946 		break;
5947 	default:
5948 		WARN_ON(1);
5949 		return -EINVAL;
5950 	}
5951 
5952 	if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5953 		return -EINVAL;
5954 
5955 	if (sta->reserved_tid == tid) {
5956 		ret = 0;
5957 		goto out;
5958 	}
5959 
5960 	if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5961 		sdata_err(sdata, "TID reservation already active\n");
5962 		ret = -EALREADY;
5963 		goto out;
5964 	}
5965 
5966 	ieee80211_stop_vif_queues(sdata->local, sdata,
5967 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5968 
5969 	synchronize_net();
5970 
5971 	/* Tear down BA sessions so we stop aggregating on this TID */
5972 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
5973 		set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5974 		__ieee80211_stop_tx_ba_session(sta, tid,
5975 					       AGG_STOP_LOCAL_REQUEST);
5976 	}
5977 
5978 	queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
5979 	__ieee80211_flush_queues(local, sdata, queues, false);
5980 
5981 	sta->reserved_tid = tid;
5982 
5983 	ieee80211_wake_vif_queues(local, sdata,
5984 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5985 
5986 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
5987 		clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5988 
5989 	ret = 0;
5990  out:
5991 	return ret;
5992 }
5993 EXPORT_SYMBOL(ieee80211_reserve_tid);
5994 
5995 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5996 {
5997 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5998 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5999 
6000 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6001 
6002 	/* only some cases are supported right now */
6003 	switch (sdata->vif.type) {
6004 	case NL80211_IFTYPE_STATION:
6005 	case NL80211_IFTYPE_AP:
6006 	case NL80211_IFTYPE_AP_VLAN:
6007 		break;
6008 	default:
6009 		WARN_ON(1);
6010 		return;
6011 	}
6012 
6013 	if (tid != sta->reserved_tid) {
6014 		sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
6015 		return;
6016 	}
6017 
6018 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
6019 }
6020 EXPORT_SYMBOL(ieee80211_unreserve_tid);
6021 
6022 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
6023 				 struct sk_buff *skb, int tid, int link_id,
6024 				 enum nl80211_band band)
6025 {
6026 	const struct ieee80211_hdr *hdr = (void *)skb->data;
6027 	int ac = ieee80211_ac_from_tid(tid);
6028 	unsigned int link;
6029 
6030 	skb_reset_mac_header(skb);
6031 	skb_set_queue_mapping(skb, ac);
6032 	skb->priority = tid;
6033 
6034 	skb->dev = sdata->dev;
6035 
6036 	BUILD_BUG_ON(IEEE80211_LINK_UNSPECIFIED < IEEE80211_MLD_MAX_NUM_LINKS);
6037 	BUILD_BUG_ON(!FIELD_FIT(IEEE80211_TX_CTRL_MLO_LINK,
6038 				IEEE80211_LINK_UNSPECIFIED));
6039 
6040 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
6041 		link = 0;
6042 	} else if (link_id >= 0) {
6043 		link = link_id;
6044 	} else if (memcmp(sdata->vif.addr, hdr->addr2, ETH_ALEN) == 0) {
6045 		/* address from the MLD */
6046 		link = IEEE80211_LINK_UNSPECIFIED;
6047 	} else {
6048 		/* otherwise must be addressed from a link */
6049 		rcu_read_lock();
6050 		for (link = 0; link < ARRAY_SIZE(sdata->vif.link_conf); link++) {
6051 			struct ieee80211_bss_conf *link_conf;
6052 
6053 			link_conf = rcu_dereference(sdata->vif.link_conf[link]);
6054 			if (!link_conf)
6055 				continue;
6056 			if (memcmp(link_conf->addr, hdr->addr2, ETH_ALEN) == 0)
6057 				break;
6058 		}
6059 		rcu_read_unlock();
6060 
6061 		if (WARN_ON_ONCE(link == ARRAY_SIZE(sdata->vif.link_conf)))
6062 			link = ffs(sdata->vif.active_links) - 1;
6063 	}
6064 
6065 	IEEE80211_SKB_CB(skb)->control.flags |=
6066 		u32_encode_bits(link, IEEE80211_TX_CTRL_MLO_LINK);
6067 
6068 	/*
6069 	 * The other path calling ieee80211_xmit is from the tasklet,
6070 	 * and while we can handle concurrent transmissions locking
6071 	 * requirements are that we do not come into tx with bhs on.
6072 	 */
6073 	local_bh_disable();
6074 	IEEE80211_SKB_CB(skb)->band = band;
6075 	ieee80211_xmit(sdata, NULL, skb);
6076 	local_bh_enable();
6077 }
6078 
6079 void ieee80211_tx_skb_tid(struct ieee80211_sub_if_data *sdata,
6080 			  struct sk_buff *skb, int tid, int link_id)
6081 {
6082 	struct ieee80211_chanctx_conf *chanctx_conf;
6083 	enum nl80211_band band;
6084 
6085 	rcu_read_lock();
6086 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
6087 		WARN_ON(link_id >= 0);
6088 		chanctx_conf =
6089 			rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
6090 		if (WARN_ON(!chanctx_conf)) {
6091 			rcu_read_unlock();
6092 			kfree_skb(skb);
6093 			return;
6094 		}
6095 		band = chanctx_conf->def.chan->band;
6096 	} else {
6097 		WARN_ON(link_id >= 0 &&
6098 			!(sdata->vif.active_links & BIT(link_id)));
6099 		/* MLD transmissions must not rely on the band */
6100 		band = 0;
6101 	}
6102 
6103 	__ieee80211_tx_skb_tid_band(sdata, skb, tid, link_id, band);
6104 	rcu_read_unlock();
6105 }
6106 
6107 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
6108 			      const u8 *buf, size_t len,
6109 			      const u8 *dest, __be16 proto, bool unencrypted,
6110 			      int link_id, u64 *cookie)
6111 {
6112 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
6113 	struct ieee80211_local *local = sdata->local;
6114 	struct sta_info *sta;
6115 	struct sk_buff *skb;
6116 	struct ethhdr *ehdr;
6117 	u32 ctrl_flags = 0;
6118 	u32 flags = 0;
6119 	int err;
6120 
6121 	/* mutex lock is only needed for incrementing the cookie counter */
6122 	lockdep_assert_wiphy(local->hw.wiphy);
6123 
6124 	/* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
6125 	 * or Pre-Authentication
6126 	 */
6127 	if (proto != sdata->control_port_protocol &&
6128 	    proto != cpu_to_be16(ETH_P_PREAUTH))
6129 		return -EINVAL;
6130 
6131 	if (proto == sdata->control_port_protocol)
6132 		ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
6133 			      IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
6134 
6135 	if (unencrypted)
6136 		flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
6137 
6138 	if (cookie)
6139 		ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
6140 
6141 	flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
6142 
6143 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
6144 			    sizeof(struct ethhdr) + len);
6145 	if (!skb)
6146 		return -ENOMEM;
6147 
6148 	skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
6149 
6150 	skb_put_data(skb, buf, len);
6151 
6152 	ehdr = skb_push(skb, sizeof(struct ethhdr));
6153 	memcpy(ehdr->h_dest, dest, ETH_ALEN);
6154 
6155 	/* we may override the SA for MLO STA later */
6156 	if (link_id < 0) {
6157 		ctrl_flags |= u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
6158 					      IEEE80211_TX_CTRL_MLO_LINK);
6159 		memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
6160 	} else {
6161 		struct ieee80211_bss_conf *link_conf;
6162 
6163 		ctrl_flags |= u32_encode_bits(link_id,
6164 					      IEEE80211_TX_CTRL_MLO_LINK);
6165 
6166 		rcu_read_lock();
6167 		link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
6168 		if (!link_conf) {
6169 			dev_kfree_skb(skb);
6170 			rcu_read_unlock();
6171 			return -ENOLINK;
6172 		}
6173 		memcpy(ehdr->h_source, link_conf->addr, ETH_ALEN);
6174 		rcu_read_unlock();
6175 	}
6176 
6177 	ehdr->h_proto = proto;
6178 
6179 	skb->dev = dev;
6180 	skb->protocol = proto;
6181 	skb_reset_network_header(skb);
6182 	skb_reset_mac_header(skb);
6183 
6184 	if (local->hw.queues < IEEE80211_NUM_ACS)
6185 		goto start_xmit;
6186 
6187 	/* update QoS header to prioritize control port frames if possible,
6188 	 * priorization also happens for control port frames send over
6189 	 * AF_PACKET
6190 	 */
6191 	rcu_read_lock();
6192 	err = ieee80211_lookup_ra_sta(sdata, skb, &sta);
6193 	if (err) {
6194 		dev_kfree_skb(skb);
6195 		rcu_read_unlock();
6196 		return err;
6197 	}
6198 
6199 	if (!IS_ERR(sta)) {
6200 		u16 queue = ieee80211_select_queue(sdata, sta, skb);
6201 
6202 		skb_set_queue_mapping(skb, queue);
6203 
6204 		/*
6205 		 * for MLO STA, the SA should be the AP MLD address, but
6206 		 * the link ID has been selected already
6207 		 */
6208 		if (sta && sta->sta.mlo)
6209 			memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
6210 	}
6211 	rcu_read_unlock();
6212 
6213 start_xmit:
6214 	local_bh_disable();
6215 	__ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
6216 	local_bh_enable();
6217 
6218 	return 0;
6219 }
6220 
6221 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
6222 			      const u8 *buf, size_t len)
6223 {
6224 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
6225 	struct ieee80211_local *local = sdata->local;
6226 	struct sk_buff *skb;
6227 
6228 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
6229 			    30 + /* header size */
6230 			    18); /* 11s header size */
6231 	if (!skb)
6232 		return -ENOMEM;
6233 
6234 	skb_reserve(skb, local->hw.extra_tx_headroom);
6235 	skb_put_data(skb, buf, len);
6236 
6237 	skb->dev = dev;
6238 	skb->protocol = htons(ETH_P_802_3);
6239 	skb_reset_network_header(skb);
6240 	skb_reset_mac_header(skb);
6241 
6242 	local_bh_disable();
6243 	__ieee80211_subif_start_xmit(skb, skb->dev, 0,
6244 				     IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
6245 				     NULL);
6246 	local_bh_enable();
6247 
6248 	return 0;
6249 }
6250