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