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