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