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