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