xref: /linux/net/mac80211/tx.c (revision 48002331da85c7c962cfbc09c8bd84d219a6fe1c)
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_headlen(skb) < 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_headlen(skb) < 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 static bool
2176 ieee80211_vht_tx_rate_supported(const struct ieee80211_sta_vht_cap *vht_cap,
2177 				u8 mcs, u8 nss)
2178 {
2179 	u16 map = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2180 
2181 	if (!vht_cap->vht_supported)
2182 		return false;
2183 
2184 	switch ((map >> (2 * (nss - 1))) & 3) {
2185 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
2186 		return mcs <= 7;
2187 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
2188 		return mcs <= 8;
2189 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
2190 		return mcs <= 9;
2191 	default:
2192 		return false;
2193 	}
2194 }
2195 
2196 bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2197 				 struct net_device *dev,
2198 				 const struct cfg80211_chan_def *chandef,
2199 				 bool trim_fcs)
2200 {
2201 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2202 	struct ieee80211_radiotap_iterator iterator;
2203 	struct ieee80211_radiotap_header *rthdr =
2204 		(struct ieee80211_radiotap_header *) skb->data;
2205 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2206 	u16 txflags;
2207 	u16 rate = 0;
2208 	bool rate_found = false;
2209 	u8 rate_retries = 0;
2210 	u16 rate_flags = 0;
2211 	u8 mcs_known, mcs_flags, mcs_bw;
2212 	u16 vht_known;
2213 	u8 vht_mcs = 0, vht_nss = 0;
2214 	int ret, i;
2215 
2216 	if (!ieee80211_validate_radiotap_len(skb))
2217 		return false;
2218 
2219 	ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2220 					       NULL);
2221 
2222 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2223 		       IEEE80211_TX_CTL_DONTFRAG;
2224 
2225 	/*
2226 	 * for every radiotap entry that is present
2227 	 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2228 	 * entries present, or -EINVAL on error)
2229 	 */
2230 
2231 	while (!ret) {
2232 		ret = ieee80211_radiotap_iterator_next(&iterator);
2233 
2234 		if (ret)
2235 			continue;
2236 
2237 		/* see if this argument is something we can use */
2238 		switch (iterator.this_arg_index) {
2239 		/*
2240 		 * You must take care when dereferencing iterator.this_arg
2241 		 * for multibyte types... the pointer is not aligned.  Use
2242 		 * get_unaligned((type *)iterator.this_arg) to dereference
2243 		 * iterator.this_arg for type "type" safely on all arches.
2244 		*/
2245 		case IEEE80211_RADIOTAP_FLAGS:
2246 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2247 				/*
2248 				 * this indicates that the skb we have been
2249 				 * handed has the 32-bit FCS CRC at the end...
2250 				 * we should react to that by snipping it off
2251 				 * because it will be recomputed and added
2252 				 * on transmission
2253 				 */
2254 				if (skb->len < (iterator._max_length + FCS_LEN))
2255 					return false;
2256 
2257 				if (trim_fcs)
2258 					skb_trim(skb, skb->len - FCS_LEN);
2259 			}
2260 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2261 				info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2262 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2263 				info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2264 			break;
2265 
2266 		case IEEE80211_RADIOTAP_TX_FLAGS:
2267 			txflags = get_unaligned_le16(iterator.this_arg);
2268 			if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2269 				info->flags |= IEEE80211_TX_CTL_NO_ACK;
2270 			if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2271 				info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
2272 			if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2273 				info->control.flags |=
2274 					IEEE80211_TX_CTRL_DONT_REORDER;
2275 			break;
2276 
2277 		case IEEE80211_RADIOTAP_RATE:
2278 			rate = *iterator.this_arg;
2279 			rate_flags = 0;
2280 			rate_found = true;
2281 			break;
2282 
2283 		case IEEE80211_RADIOTAP_ANTENNA:
2284 			/* this can appear multiple times, keep a bitmap */
2285 			/* control.antennas is only a 2-bit bitmap */
2286 			if (*iterator.this_arg < 2)
2287 				info->control.antennas |= BIT(*iterator.this_arg);
2288 			break;
2289 
2290 		case IEEE80211_RADIOTAP_DATA_RETRIES:
2291 			rate_retries = *iterator.this_arg;
2292 			break;
2293 
2294 		case IEEE80211_RADIOTAP_MCS:
2295 			mcs_known = iterator.this_arg[0];
2296 			mcs_flags = iterator.this_arg[1];
2297 			if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2298 				break;
2299 
2300 			rate_found = true;
2301 			rate = iterator.this_arg[2];
2302 			rate_flags = IEEE80211_TX_RC_MCS;
2303 
2304 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2305 			    mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2306 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2307 
2308 			mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2309 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2310 			    mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2311 				rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2312 
2313 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2314 			    mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2315 				info->flags |= IEEE80211_TX_CTL_LDPC;
2316 
2317 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2318 				u8 stbc = u8_get_bits(mcs_flags,
2319 						      IEEE80211_RADIOTAP_MCS_STBC_MASK);
2320 
2321 				info->flags |=
2322 					u32_encode_bits(stbc,
2323 							IEEE80211_TX_CTL_STBC);
2324 			}
2325 			break;
2326 
2327 		case IEEE80211_RADIOTAP_VHT:
2328 			vht_known = get_unaligned_le16(iterator.this_arg);
2329 			rate_found = true;
2330 
2331 			rate_flags = IEEE80211_TX_RC_VHT_MCS;
2332 			if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2333 			    (iterator.this_arg[2] &
2334 			     IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2335 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2336 			if (vht_known &
2337 			    IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2338 				if (iterator.this_arg[3] == 1)
2339 					rate_flags |=
2340 						IEEE80211_TX_RC_40_MHZ_WIDTH;
2341 				else if (iterator.this_arg[3] == 4)
2342 					rate_flags |=
2343 						IEEE80211_TX_RC_80_MHZ_WIDTH;
2344 				else if (iterator.this_arg[3] == 11)
2345 					rate_flags |=
2346 						IEEE80211_TX_RC_160_MHZ_WIDTH;
2347 			}
2348 
2349 			vht_mcs = iterator.this_arg[4] >> 4;
2350 			if (vht_mcs > 11)
2351 				vht_mcs = 0;
2352 			vht_nss = iterator.this_arg[4] & 0xF;
2353 			if (!vht_nss || vht_nss > 8)
2354 				vht_nss = 1;
2355 			break;
2356 
2357 		/*
2358 		 * Please update the file
2359 		 * Documentation/networking/mac80211-injection.rst
2360 		 * when parsing new fields here.
2361 		 */
2362 
2363 		default:
2364 			break;
2365 		}
2366 	}
2367 
2368 	if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2369 		return false;
2370 
2371 	if (rate_found) {
2372 		struct ieee80211_supported_band *sband =
2373 			local->hw.wiphy->bands[info->band];
2374 
2375 		if (!ieee80211_rate_bw_usable(rate_flags, chandef))
2376 			return false;
2377 
2378 		info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2379 
2380 		for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2381 			info->control.rates[i].idx = -1;
2382 			info->control.rates[i].flags = 0;
2383 			info->control.rates[i].count = 0;
2384 		}
2385 
2386 		if (rate_flags & IEEE80211_TX_RC_MCS) {
2387 			/* reset antennas if not enough */
2388 			if (IEEE80211_HT_MCS_CHAINS(rate) >
2389 					hweight8(info->control.antennas))
2390 				info->control.antennas = 0;
2391 
2392 			/* MCS 32 and up aren't handled, e.g. by TX status */
2393 			if (sband && sband->ht_cap.ht_supported && rate < 32 &&
2394 			    sband->ht_cap.mcs.rx_mask[rate / 8] & BIT(rate % 8))
2395 				info->control.rates[0].idx = rate;
2396 		} else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2397 			/* reset antennas if not enough */
2398 			if (vht_nss > hweight8(info->control.antennas))
2399 				info->control.antennas = 0;
2400 
2401 			if (sband &&
2402 			    ieee80211_vht_tx_rate_supported(&sband->vht_cap,
2403 							    vht_mcs, vht_nss))
2404 				ieee80211_rate_set_vht(info->control.rates,
2405 						       vht_mcs, vht_nss);
2406 		} else if (sband) {
2407 			for (i = 0; i < sband->n_bitrates; i++) {
2408 				if (rate * 5 != sband->bitrates[i].bitrate)
2409 					continue;
2410 
2411 				info->control.rates[0].idx = i;
2412 				break;
2413 			}
2414 		}
2415 
2416 		if (info->control.rates[0].idx < 0)
2417 			info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2418 
2419 		info->control.rates[0].flags = rate_flags;
2420 		info->control.rates[0].count = min_t(u8, rate_retries + 1,
2421 						     local->hw.max_rate_tries);
2422 	}
2423 
2424 	return true;
2425 }
2426 
2427 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2428 					 struct net_device *dev)
2429 {
2430 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2431 	struct ieee80211_chanctx_conf *chanctx_conf;
2432 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2433 	struct ieee80211_hdr *hdr;
2434 	struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2435 	struct cfg80211_chan_def *chandef;
2436 	u16 len_rthdr;
2437 	int hdrlen;
2438 
2439 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2440 	if (unlikely(!ieee80211_sdata_running(sdata)))
2441 		goto fail;
2442 
2443 	memset(info, 0, sizeof(*info));
2444 	info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2445 		      IEEE80211_TX_CTL_INJECTED;
2446 
2447 	/* Sanity-check the length of the radiotap header */
2448 	if (!ieee80211_validate_radiotap_len(skb))
2449 		goto fail;
2450 
2451 	/* we now know there is a radiotap header with a length we can use */
2452 	len_rthdr = ieee80211_get_radiotap_len(skb->data);
2453 
2454 	/*
2455 	 * fix up the pointers accounting for the radiotap
2456 	 * header still being in there.  We are being given
2457 	 * a precooked IEEE80211 header so no need for
2458 	 * normal processing
2459 	 */
2460 	skb_set_mac_header(skb, len_rthdr);
2461 	/*
2462 	 * these are just fixed to the end of the rt area since we
2463 	 * don't have any better information and at this point, nobody cares
2464 	 */
2465 	skb_set_network_header(skb, len_rthdr);
2466 	skb_set_transport_header(skb, len_rthdr);
2467 
2468 	if (skb_headlen(skb) < len_rthdr + 2)
2469 		goto fail;
2470 
2471 	hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2472 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
2473 
2474 	if (skb_headlen(skb) < len_rthdr +
2475 			       max_t(int, hdrlen,
2476 				     offsetofend(struct ieee80211_hdr, addr1)))
2477 		goto fail;
2478 
2479 	/*
2480 	 * Initialize skb->protocol if the injected frame is a data frame
2481 	 * carrying a rfc1042 header
2482 	 */
2483 	if (ieee80211_is_data(hdr->frame_control) &&
2484 	    skb_headlen(skb) >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2485 		u8 *payload = (u8 *)hdr + hdrlen;
2486 
2487 		if (ether_addr_equal(payload, rfc1042_header))
2488 			skb->protocol = cpu_to_be16((payload[6] << 8) |
2489 						    payload[7]);
2490 	}
2491 
2492 	rcu_read_lock();
2493 
2494 	/*
2495 	 * We process outgoing injected frames that have a local address
2496 	 * we handle as though they are non-injected frames.
2497 	 * This code here isn't entirely correct, the local MAC address
2498 	 * isn't always enough to find the interface to use; for proper
2499 	 * VLAN support we have an nl80211-based mechanism.
2500 	 *
2501 	 * This is necessary, for example, for old hostapd versions that
2502 	 * don't use nl80211-based management TX/RX.
2503 	 */
2504 	list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2505 		if (!ieee80211_sdata_running(tmp_sdata))
2506 			continue;
2507 		if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2508 		    tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2509 			continue;
2510 		if (hdrlen >= offsetofend(struct ieee80211_hdr, addr2) &&
2511 		    ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2512 			sdata = tmp_sdata;
2513 			break;
2514 		}
2515 	}
2516 
2517 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
2518 	if (!chanctx_conf) {
2519 		tmp_sdata = rcu_dereference(local->monitor_sdata);
2520 		if (tmp_sdata)
2521 			chanctx_conf =
2522 				rcu_dereference(tmp_sdata->vif.bss_conf.chanctx_conf);
2523 	}
2524 
2525 	if (!chanctx_conf) {
2526 		struct ieee80211_chanctx *ctx;
2527 		bool first = true;
2528 
2529 		list_for_each_entry_rcu(ctx, &local->chanctx_list, list) {
2530 			if (!first)
2531 				goto fail_rcu;
2532 			chanctx_conf = &ctx->conf;
2533 			first = false;
2534 		}
2535 	}
2536 
2537 	if (chanctx_conf)
2538 		chandef = &chanctx_conf->def;
2539 	else
2540 		goto fail_rcu;
2541 
2542 	/*
2543 	 * If driver/HW supports IEEE80211_CHAN_CAN_MONITOR we still
2544 	 * shouldn't transmit on disabled channels.
2545 	 */
2546 	if (!cfg80211_chandef_usable(local->hw.wiphy, chandef,
2547 				     IEEE80211_CHAN_DISABLED))
2548 		goto fail_rcu;
2549 
2550 	/*
2551 	 * Frame injection is not allowed if beaconing is not allowed
2552 	 * or if we need radar detection. Beaconing is usually not allowed when
2553 	 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2554 	 * Passive scan is also used in world regulatory domains where
2555 	 * your country is not known and as such it should be treated as
2556 	 * NO TX unless the channel is explicitly allowed in which case
2557 	 * your current regulatory domain would not have the passive scan
2558 	 * flag.
2559 	 *
2560 	 * Since AP mode uses monitor interfaces to inject/TX management
2561 	 * frames we can make AP mode the exception to this rule once it
2562 	 * supports radar detection as its implementation can deal with
2563 	 * radar detection by itself. We can do that later by adding a
2564 	 * monitor flag interfaces used for AP support.
2565 	 */
2566 	if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2567 				     sdata->vif.type))
2568 		goto fail_rcu;
2569 
2570 	info->band = chandef->chan->band;
2571 
2572 	/* Initialize skb->priority according to frame type and TID class,
2573 	 * with respect to the sub interface that the frame will actually
2574 	 * be transmitted on. If the DONT_REORDER flag is set, the original
2575 	 * skb-priority is preserved to assure frames injected with this
2576 	 * flag are not reordered relative to each other.
2577 	 */
2578 	ieee80211_select_queue_80211(sdata, skb, hdr);
2579 	skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority));
2580 
2581 	/*
2582 	 * Process the radiotap header. This will now take into account the
2583 	 * selected chandef above to accurately set injection rates and
2584 	 * retransmissions.
2585 	 */
2586 	if (!ieee80211_parse_tx_radiotap(skb, dev, chandef, true))
2587 		goto fail_rcu;
2588 
2589 	/* remove the injection radiotap header */
2590 	skb_pull(skb, len_rthdr);
2591 
2592 	ieee80211_xmit(sdata, NULL, skb);
2593 	rcu_read_unlock();
2594 
2595 	return NETDEV_TX_OK;
2596 
2597 fail_rcu:
2598 	rcu_read_unlock();
2599 fail:
2600 	dev_kfree_skb(skb);
2601 	return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2602 }
2603 
2604 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2605 {
2606 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2607 
2608 	return ethertype == ETH_P_TDLS &&
2609 	       skb_headlen(skb) > 14 &&
2610 	       skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2611 }
2612 
2613 /*
2614  * Returns an error if the frame should be dropped, otherwise
2615  * the *sta_out pointer is filled:
2616  *  - valid sta pointer: frame goes to that station
2617  *  - NULL: frame will be unicast to a yet unknown station
2618  *  - ERR_PTR(-ENOENT): frame will be group addressed
2619  */
2620 static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2621 				   struct sk_buff *skb,
2622 				   struct sta_info **sta_out, bool bss)
2623 {
2624 	struct sta_info *sta;
2625 
2626 	switch (sdata->vif.type) {
2627 	case NL80211_IFTYPE_AP_VLAN:
2628 		sta = rcu_dereference(sdata->u.vlan.sta);
2629 		if (sta) {
2630 			*sta_out = sta;
2631 			return 0;
2632 		} else if (sdata->wdev.use_4addr) {
2633 			return -ENOLINK;
2634 		}
2635 		fallthrough;
2636 	case NL80211_IFTYPE_AP:
2637 	case NL80211_IFTYPE_OCB:
2638 	case NL80211_IFTYPE_ADHOC:
2639 		if (is_multicast_ether_addr(skb->data)) {
2640 			*sta_out = ERR_PTR(-ENOENT);
2641 			return 0;
2642 		}
2643 		if (bss)
2644 			sta = sta_info_get_bss(sdata, skb->data);
2645 		else
2646 			sta = sta_info_get(sdata, skb->data);
2647 		break;
2648 #ifdef CONFIG_MAC80211_MESH
2649 	case NL80211_IFTYPE_MESH_POINT:
2650 		if (is_multicast_ether_addr(skb->data)) {
2651 			*sta_out = ERR_PTR(-ENOENT);
2652 			return 0;
2653 		}
2654 		/* determined much later */
2655 		*sta_out = NULL;
2656 		return 0;
2657 #endif
2658 	case NL80211_IFTYPE_STATION:
2659 		if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2660 			sta = sta_info_get(sdata, skb->data);
2661 			if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2662 				if (test_sta_flag(sta,
2663 						  WLAN_STA_TDLS_PEER_AUTH)) {
2664 					*sta_out = sta;
2665 					return 0;
2666 				}
2667 
2668 				/*
2669 				 * TDLS link during setup - throw out frames to
2670 				 * peer. Allow TDLS-setup frames to unauthorized
2671 				 * peers for the special case of a link teardown
2672 				 * after a TDLS sta is removed due to being
2673 				 * unreachable.
2674 				 */
2675 				if (!ieee80211_is_tdls_setup(skb))
2676 					return -EINVAL;
2677 			}
2678 
2679 		}
2680 
2681 		sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
2682 		if (!sta)
2683 			return -ENOLINK;
2684 		break;
2685 	case NL80211_IFTYPE_NAN_DATA:
2686 		if (is_multicast_ether_addr(skb->data)) {
2687 			*sta_out = ERR_PTR(-ENOENT);
2688 			return 0;
2689 		}
2690 		sta = sta_info_get(sdata, skb->data);
2691 		break;
2692 	default:
2693 		return -EINVAL;
2694 	}
2695 
2696 	*sta_out = sta;
2697 	return 0;
2698 }
2699 
2700 static void ieee80211_store_ack_skb(struct ieee80211_local *local,
2701 				    struct sk_buff *skb, u64 cookie)
2702 {
2703 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2704 	struct sk_buff *ack_skb;
2705 
2706 	if (skb->sk)
2707 		ack_skb = skb_clone_sk(skb);
2708 	else
2709 		ack_skb = skb_clone(skb, GFP_ATOMIC);
2710 
2711 	if (ack_skb) {
2712 		unsigned long flags;
2713 		int id;
2714 
2715 		spin_lock_irqsave(&local->ack_status_lock, flags);
2716 		id = idr_alloc(&local->ack_status_frames, ack_skb,
2717 			       1, 0x2000, GFP_ATOMIC);
2718 		spin_unlock_irqrestore(&local->ack_status_lock, flags);
2719 
2720 		if (id >= 0) {
2721 			info->status_data = id;
2722 			info->status_data_idr = 1;
2723 			info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2724 			if (cookie)
2725 				IEEE80211_SKB_CB(ack_skb)->ack.cookie = cookie;
2726 		} else {
2727 			kfree_skb(ack_skb);
2728 		}
2729 	}
2730 }
2731 
2732 static int ieee80211_ap_tx_addr_get(struct ieee80211_sub_if_data *sdata,
2733 				    struct sta_info *sta, u8 *addr, u8 *link_id)
2734 {
2735 	struct ieee80211_link_data *link;
2736 
2737 	if (!ieee80211_vif_is_mld(&sdata->vif) || sta->sta.mlo) {
2738 		memcpy(addr, sdata->vif.addr, ETH_ALEN);
2739 		return 0;
2740 	}
2741 
2742 	guard(rcu)();
2743 
2744 	link = rcu_dereference(sdata->link[sta->deflink.link_id]);
2745 	if (WARN_ON(!link))
2746 		return -ENOLINK;
2747 
2748 	memcpy(addr, link->conf->addr, ETH_ALEN);
2749 	if (link_id)
2750 		*link_id = sta->deflink.link_id;
2751 
2752 	return 0;
2753 }
2754 
2755 /**
2756  * ieee80211_build_hdr - build 802.11 header in the given frame
2757  * @sdata: virtual interface to build the header for
2758  * @skb: the skb to build the header in
2759  * @sta: the station pointer
2760  *
2761  * This function takes the skb with 802.3 header and reformats the header to
2762  * the appropriate IEEE 802.11 header based on which interface the packet is
2763  * being transmitted on.
2764  *
2765  * Note that this function also takes care of the TX status request. The skb
2766  * must not be shared, and its TX info must already be initialised - this
2767  * function only fills in what it determines itself.
2768  *
2769  * The function requires the read-side RCU lock held
2770  *
2771  * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2772  */
2773 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2774 					   struct sk_buff *skb,
2775 					   struct sta_info *sta)
2776 {
2777 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2778 	struct ieee80211_local *local = sdata->local;
2779 	int head_need;
2780 	u16 ethertype, hdrlen,  meshhdrlen = 0;
2781 	__le16 fc;
2782 	struct ieee80211_hdr hdr;
2783 	struct ieee80211s_hdr mesh_hdr __maybe_unused;
2784 	struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2785 	const u8 *encaps_data;
2786 	int encaps_len, skip_header_bytes;
2787 	bool wme_sta = false, authorized = false;
2788 	bool tdls_peer;
2789 	bool multicast;
2790 	struct ieee80211_chanctx_conf *chanctx_conf = NULL;
2791 	enum nl80211_band band;
2792 	int ret;
2793 	u8 link_id = u32_get_bits(info->control.flags,
2794 				  IEEE80211_TX_CTRL_MLO_LINK);
2795 
2796 	if (IS_ERR(sta))
2797 		sta = NULL;
2798 
2799 #ifdef CONFIG_MAC80211_DEBUGFS
2800 	if (local->force_tx_status)
2801 		info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2802 #endif
2803 
2804 	/* convert Ethernet header to proper 802.11 header (based on
2805 	 * operation mode) */
2806 	ethertype = (skb->data[12] << 8) | skb->data[13];
2807 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2808 
2809 	if (!ieee80211_vif_is_mld(&sdata->vif))
2810 		chanctx_conf =
2811 			rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
2812 
2813 	switch (sdata->vif.type) {
2814 	case NL80211_IFTYPE_AP_VLAN:
2815 		if (sdata->wdev.use_4addr) {
2816 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2817 			/* RA TA DA SA */
2818 			memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2819 			ret = ieee80211_ap_tx_addr_get(sdata, sta, hdr.addr2,
2820 						       &link_id);
2821 			if (ret)
2822 				goto free;
2823 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2824 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2825 			hdrlen = 30;
2826 			authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2827 			wme_sta = sta->sta.wme;
2828 		}
2829 		if (!ieee80211_vif_is_mld(&sdata->vif)) {
2830 			struct ieee80211_sub_if_data *ap_sdata;
2831 
2832 			/* override chanctx_conf from AP (we don't have one) */
2833 			ap_sdata = container_of(sdata->bss,
2834 						struct ieee80211_sub_if_data,
2835 						u.ap);
2836 			chanctx_conf =
2837 				rcu_dereference(ap_sdata->vif.bss_conf.chanctx_conf);
2838 		}
2839 		if (sdata->wdev.use_4addr)
2840 			break;
2841 		fallthrough;
2842 	case NL80211_IFTYPE_AP:
2843 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2844 		/* DA BSSID SA */
2845 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2846 
2847 		if (sta) {
2848 			ret = ieee80211_ap_tx_addr_get(sdata, sta, hdr.addr2,
2849 						       &link_id);
2850 			if (ret)
2851 				goto free;
2852 		} else if (link_id == IEEE80211_LINK_UNSPECIFIED) {
2853 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2854 		} else {
2855 			struct ieee80211_bss_conf *conf;
2856 
2857 			conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2858 			if (unlikely(!conf)) {
2859 				ret = -ENOLINK;
2860 				goto free;
2861 			}
2862 
2863 			memcpy(hdr.addr2, conf->addr, ETH_ALEN);
2864 		}
2865 
2866 		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2867 		hdrlen = 24;
2868 		break;
2869 #ifdef CONFIG_MAC80211_MESH
2870 	case NL80211_IFTYPE_MESH_POINT:
2871 		if (!is_multicast_ether_addr(skb->data)) {
2872 			struct sta_info *next_hop;
2873 			bool mpp_lookup = true;
2874 
2875 			mpath = mesh_path_lookup(sdata, skb->data);
2876 			if (mpath) {
2877 				mpp_lookup = false;
2878 				next_hop = rcu_dereference(mpath->next_hop);
2879 				if (!next_hop ||
2880 				    !(mpath->flags & (MESH_PATH_ACTIVE |
2881 						      MESH_PATH_RESOLVING)))
2882 					mpp_lookup = true;
2883 			}
2884 
2885 			if (mpp_lookup) {
2886 				mppath = mpp_path_lookup(sdata, skb->data);
2887 				if (mppath)
2888 					mppath->exp_time = jiffies;
2889 			}
2890 
2891 			if (mppath && mpath)
2892 				mesh_path_del(sdata, mpath->dst);
2893 		}
2894 
2895 		/*
2896 		 * Use address extension if it is a packet from
2897 		 * another interface or if we know the destination
2898 		 * is being proxied by a portal (i.e. portal address
2899 		 * differs from proxied address)
2900 		 */
2901 		if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2902 		    !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2903 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2904 					skb->data, skb->data + ETH_ALEN);
2905 			meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2906 							       NULL, NULL);
2907 		} else {
2908 			/* DS -> MBSS (802.11-2012 13.11.3.3).
2909 			 * For unicast with unknown forwarding information,
2910 			 * destination might be in the MBSS or if that fails
2911 			 * forwarded to another mesh gate. In either case
2912 			 * resolution will be handled in ieee80211_xmit(), so
2913 			 * leave the original DA. This also works for mcast */
2914 			const u8 *mesh_da = skb->data;
2915 
2916 			if (mppath)
2917 				mesh_da = mppath->mpp;
2918 			else if (mpath)
2919 				mesh_da = mpath->dst;
2920 
2921 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2922 					mesh_da, sdata->vif.addr);
2923 			if (is_multicast_ether_addr(mesh_da))
2924 				/* DA TA mSA AE:SA */
2925 				meshhdrlen = ieee80211_new_mesh_header(
2926 						sdata, &mesh_hdr,
2927 						skb->data + ETH_ALEN, NULL);
2928 			else
2929 				/* RA TA mDA mSA AE:DA SA */
2930 				meshhdrlen = ieee80211_new_mesh_header(
2931 						sdata, &mesh_hdr, skb->data,
2932 						skb->data + ETH_ALEN);
2933 
2934 		}
2935 
2936 		/* For injected frames, fill RA right away as nexthop lookup
2937 		 * will be skipped.
2938 		 */
2939 		if ((info->control.flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2940 		    is_zero_ether_addr(hdr.addr1))
2941 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2942 		break;
2943 #endif
2944 	case NL80211_IFTYPE_STATION:
2945 		/* we already did checks when looking up the RA STA */
2946 		tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2947 
2948 		if (tdls_peer) {
2949 			/* For TDLS only one link can be valid with peer STA */
2950 			int tdls_link_id = ieee80211_tdls_sta_link_id(sta);
2951 			struct ieee80211_link_data *link;
2952 
2953 			/* DA SA BSSID */
2954 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2955 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2956 			link = rcu_dereference(sdata->link[tdls_link_id]);
2957 			if (WARN_ON_ONCE(!link)) {
2958 				ret = -EINVAL;
2959 				goto free;
2960 			}
2961 			memcpy(hdr.addr3, link->u.mgd.bssid, ETH_ALEN);
2962 			hdrlen = 24;
2963 		}  else if (sdata->u.mgd.use_4addr &&
2964 			    cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2965 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2966 					  IEEE80211_FCTL_TODS);
2967 			/* RA TA DA SA */
2968 			memcpy(hdr.addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
2969 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2970 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2971 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2972 			hdrlen = 30;
2973 		} else {
2974 			fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2975 			/* BSSID SA DA */
2976 			memcpy(hdr.addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
2977 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2978 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2979 			hdrlen = 24;
2980 		}
2981 		break;
2982 	case NL80211_IFTYPE_OCB:
2983 		/* DA SA BSSID */
2984 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2985 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2986 		eth_broadcast_addr(hdr.addr3);
2987 		hdrlen = 24;
2988 		break;
2989 	case NL80211_IFTYPE_ADHOC:
2990 		/* DA SA BSSID */
2991 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2992 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2993 		memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2994 		hdrlen = 24;
2995 		break;
2996 	case NL80211_IFTYPE_NAN_DATA: {
2997 		struct ieee80211_sub_if_data *nmi;
2998 
2999 		/* DA SA Cluster ID */
3000 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
3001 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
3002 		nmi = rcu_dereference(sdata->u.nan_data.nmi);
3003 		if (!nmi) {
3004 			ret = -ENOTCONN;
3005 			goto free;
3006 		}
3007 		memcpy(hdr.addr3, nmi->u.nan.conf.cluster_id, ETH_ALEN);
3008 		hdrlen = 24;
3009 		break;
3010 	}
3011 	default:
3012 		ret = -EINVAL;
3013 		goto free;
3014 	}
3015 
3016 	if (!chanctx_conf) {
3017 		if (sdata->vif.type == NL80211_IFTYPE_NAN_DATA) {
3018 			 /* NAN operates on multiple bands */
3019 			band = NUM_NL80211_BANDS;
3020 		} else if (!ieee80211_vif_is_mld(&sdata->vif)) {
3021 			ret = -ENOTCONN;
3022 			goto free;
3023 		} else {
3024 			/* MLD transmissions must not rely on the band */
3025 			band = 0;
3026 		}
3027 	} else {
3028 		band = chanctx_conf->def.chan->band;
3029 	}
3030 
3031 	multicast = is_multicast_ether_addr(hdr.addr1);
3032 
3033 	/* sta is always NULL for mesh */
3034 	if (sta) {
3035 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
3036 		wme_sta = sta->sta.wme;
3037 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3038 		/* For mesh, the use of the QoS header is mandatory */
3039 		wme_sta = true;
3040 	}
3041 
3042 	/* receiver does QoS (which also means we do) use it */
3043 	if (wme_sta) {
3044 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3045 		hdrlen += 2;
3046 	}
3047 
3048 	/*
3049 	 * Drop unicast frames to unauthorised stations unless they are
3050 	 * EAPOL frames from the local station.
3051 	 */
3052 	if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
3053 		     (sdata->vif.type != NL80211_IFTYPE_OCB) &&
3054 		     !multicast && !authorized &&
3055 		     (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
3056 		      !ieee80211_is_our_addr(sdata, skb->data + ETH_ALEN, NULL)))) {
3057 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3058 		net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
3059 				    sdata->name, hdr.addr1);
3060 #endif
3061 
3062 		I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3063 
3064 		ret = -EPERM;
3065 		goto free;
3066 	}
3067 
3068 	hdr.frame_control = fc;
3069 	hdr.duration_id = 0;
3070 	hdr.seq_ctrl = 0;
3071 
3072 	skip_header_bytes = ETH_HLEN;
3073 	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
3074 		encaps_data = bridge_tunnel_header;
3075 		encaps_len = sizeof(bridge_tunnel_header);
3076 		skip_header_bytes -= 2;
3077 	} else if (ethertype >= ETH_P_802_3_MIN) {
3078 		encaps_data = rfc1042_header;
3079 		encaps_len = sizeof(rfc1042_header);
3080 		skip_header_bytes -= 2;
3081 	} else {
3082 		encaps_data = NULL;
3083 		encaps_len = 0;
3084 	}
3085 
3086 	skb_pull(skb, skip_header_bytes);
3087 	head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
3088 
3089 	/*
3090 	 * So we need to modify the skb header and hence need a copy of
3091 	 * that. The head_need variable above doesn't, so far, include
3092 	 * the needed header space that we don't need right away. If we
3093 	 * can, then we don't reallocate right now but only after the
3094 	 * frame arrives at the master device (if it does...)
3095 	 *
3096 	 * If we cannot, however, then we will reallocate to include all
3097 	 * the ever needed space. Also, if we need to reallocate it anyway,
3098 	 * make it big enough for everything we may ever need.
3099 	 */
3100 
3101 	if (head_need > 0 || skb_cloned(skb)) {
3102 		head_need += IEEE80211_ENCRYPT_HEADROOM;
3103 		head_need += local->tx_headroom;
3104 		head_need = max_t(int, 0, head_need);
3105 		if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
3106 			ret = -ENOMEM;
3107 			goto free;
3108 		}
3109 	}
3110 
3111 	if (encaps_data)
3112 		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
3113 
3114 #ifdef CONFIG_MAC80211_MESH
3115 	if (meshhdrlen > 0)
3116 		memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
3117 #endif
3118 
3119 	if (ieee80211_is_data_qos(fc)) {
3120 		__le16 *qos_control;
3121 
3122 		qos_control = skb_push(skb, 2);
3123 		memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
3124 		/*
3125 		 * Maybe we could actually set some fields here, for now just
3126 		 * initialise to zero to indicate no special operation.
3127 		 */
3128 		*qos_control = 0;
3129 	} else
3130 		memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
3131 
3132 	skb_reset_mac_header(skb);
3133 
3134 	info->band = band;
3135 
3136 	info->control.flags =
3137 		u32_replace_bits(info->control.flags, link_id,
3138 				 IEEE80211_TX_CTRL_MLO_LINK);
3139 	return skb;
3140  free:
3141 	ieee80211_free_txskb(&local->hw, skb);
3142 	return ERR_PTR(ret);
3143 }
3144 
3145 /*
3146  * fast-xmit overview
3147  *
3148  * The core idea of this fast-xmit is to remove per-packet checks by checking
3149  * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
3150  * checks that are needed to get the sta->fast_tx pointer assigned, after which
3151  * much less work can be done per packet. For example, fragmentation must be
3152  * disabled or the fast_tx pointer will not be set. All the conditions are seen
3153  * in the code here.
3154  *
3155  * Once assigned, the fast_tx data structure also caches the per-packet 802.11
3156  * header and other data to aid packet processing in ieee80211_xmit_fast().
3157  *
3158  * The most difficult part of this is that when any of these assumptions
3159  * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
3160  * ieee80211_check_fast_xmit() or friends) is required to reset the data,
3161  * since the per-packet code no longer checks the conditions. This is reflected
3162  * by the calls to these functions throughout the rest of the code, and must be
3163  * maintained if any of the TX path checks change.
3164  */
3165 
3166 void ieee80211_check_fast_xmit(struct sta_info *sta)
3167 {
3168 	struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
3169 	struct ieee80211_local *local = sta->local;
3170 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3171 	struct ieee80211_hdr *hdr = (void *)build.hdr;
3172 	struct ieee80211_chanctx_conf *chanctx_conf;
3173 	__le16 fc;
3174 
3175 	if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
3176 		return;
3177 
3178 	if (ieee80211_vif_is_mesh(&sdata->vif))
3179 		mesh_fast_tx_flush_sta(sdata, sta);
3180 
3181 	/* Locking here protects both the pointer itself, and against concurrent
3182 	 * invocations winning data access races to, e.g., the key pointer that
3183 	 * is used.
3184 	 * Without it, the invocation of this function right after the key
3185 	 * pointer changes wouldn't be sufficient, as another CPU could access
3186 	 * the pointer, then stall, and then do the cache update after the CPU
3187 	 * that invalidated the key.
3188 	 * With the locking, such scenarios cannot happen as the check for the
3189 	 * key and the fast-tx assignment are done atomically, so the CPU that
3190 	 * modifies the key will either wait or other one will see the key
3191 	 * cleared/changed already.
3192 	 */
3193 	spin_lock_bh(&sta->lock);
3194 	if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3195 	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3196 	    sdata->vif.type == NL80211_IFTYPE_STATION)
3197 		goto out;
3198 
3199 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED) || !sta->uploaded)
3200 		goto out;
3201 
3202 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
3203 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
3204 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
3205 	    test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
3206 		goto out;
3207 
3208 	if (sdata->noack_map)
3209 		goto out;
3210 
3211 	/* fast-xmit doesn't handle fragmentation at all */
3212 	if (local->hw.wiphy->frag_threshold != (u32)-1 &&
3213 	    !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
3214 		goto out;
3215 
3216 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
3217 		rcu_read_lock();
3218 		chanctx_conf =
3219 			rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
3220 		if (!chanctx_conf) {
3221 			rcu_read_unlock();
3222 			goto out;
3223 		}
3224 		build.band = chanctx_conf->def.chan->band;
3225 		rcu_read_unlock();
3226 	} else {
3227 		/* MLD transmissions must not rely on the band */
3228 		build.band = 0;
3229 	}
3230 
3231 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
3232 
3233 	switch (sdata->vif.type) {
3234 	case NL80211_IFTYPE_ADHOC:
3235 		/* DA SA BSSID */
3236 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3237 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3238 		memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
3239 		build.hdr_len = 24;
3240 		break;
3241 	case NL80211_IFTYPE_STATION:
3242 		if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
3243 			/* For TDLS only one link can be valid with peer STA */
3244 			int tdls_link_id = ieee80211_tdls_sta_link_id(sta);
3245 			struct ieee80211_link_data *link;
3246 
3247 			/* DA SA BSSID */
3248 			build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3249 			build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3250 			rcu_read_lock();
3251 			link = rcu_dereference(sdata->link[tdls_link_id]);
3252 			if (!WARN_ON_ONCE(!link))
3253 				memcpy(hdr->addr3, link->u.mgd.bssid, ETH_ALEN);
3254 			rcu_read_unlock();
3255 			build.hdr_len = 24;
3256 			break;
3257 		}
3258 
3259 		if (sdata->u.mgd.use_4addr) {
3260 			/* non-regular ethertype cannot use the fastpath */
3261 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3262 					  IEEE80211_FCTL_TODS);
3263 			/* RA TA DA SA */
3264 			memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
3265 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3266 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3267 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3268 			build.hdr_len = 30;
3269 			break;
3270 		}
3271 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
3272 		/* BSSID SA DA */
3273 		memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
3274 		build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3275 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3276 		build.hdr_len = 24;
3277 		break;
3278 	case NL80211_IFTYPE_AP_VLAN:
3279 		if (sdata->wdev.use_4addr) {
3280 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3281 					  IEEE80211_FCTL_TODS);
3282 			/* RA TA DA SA */
3283 			memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3284 			if (ieee80211_ap_tx_addr_get(sdata, sta, hdr->addr2,
3285 						     NULL))
3286 				goto out;
3287 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3288 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3289 			build.hdr_len = 30;
3290 			break;
3291 		}
3292 		fallthrough;
3293 	case NL80211_IFTYPE_AP:
3294 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3295 		/* DA BSSID SA */
3296 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3297 		if (ieee80211_ap_tx_addr_get(sdata, sta, hdr->addr2, NULL))
3298 			goto out;
3299 		build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3300 		build.hdr_len = 24;
3301 		break;
3302 	default:
3303 		/* not handled on fast-xmit */
3304 		goto out;
3305 	}
3306 
3307 	if (sta->sta.wme) {
3308 		build.hdr_len += 2;
3309 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3310 	}
3311 
3312 	/* We store the key here so there's no point in using rcu_dereference()
3313 	 * but that's fine because the code that changes the pointers will call
3314 	 * this function after doing so. For a single CPU that would be enough,
3315 	 * for multiple see the comment above.
3316 	 */
3317 	build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3318 	if (!build.key)
3319 		build.key = rcu_access_pointer(sdata->default_unicast_key);
3320 	if (build.key) {
3321 		bool gen_iv, iv_spc, mmic;
3322 
3323 		gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3324 		iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3325 		mmic = build.key->conf.flags &
3326 			(IEEE80211_KEY_FLAG_GENERATE_MMIC |
3327 			 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3328 
3329 		/* don't handle software crypto */
3330 		if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3331 			goto out;
3332 
3333 		/* Key is being removed */
3334 		if (build.key->flags & KEY_FLAG_TAINTED)
3335 			goto out;
3336 
3337 		switch (build.key->conf.cipher) {
3338 		case WLAN_CIPHER_SUITE_CCMP:
3339 		case WLAN_CIPHER_SUITE_CCMP_256:
3340 			if (gen_iv)
3341 				build.pn_offs = build.hdr_len;
3342 			if (gen_iv || iv_spc)
3343 				build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3344 			break;
3345 		case WLAN_CIPHER_SUITE_GCMP:
3346 		case WLAN_CIPHER_SUITE_GCMP_256:
3347 			if (gen_iv)
3348 				build.pn_offs = build.hdr_len;
3349 			if (gen_iv || iv_spc)
3350 				build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3351 			break;
3352 		case WLAN_CIPHER_SUITE_TKIP:
3353 			/* cannot handle MMIC or IV generation in xmit-fast */
3354 			if (mmic || gen_iv)
3355 				goto out;
3356 			if (iv_spc)
3357 				build.hdr_len += IEEE80211_TKIP_IV_LEN;
3358 			break;
3359 		case WLAN_CIPHER_SUITE_WEP40:
3360 		case WLAN_CIPHER_SUITE_WEP104:
3361 			/* cannot handle IV generation in fast-xmit */
3362 			if (gen_iv)
3363 				goto out;
3364 			if (iv_spc)
3365 				build.hdr_len += IEEE80211_WEP_IV_LEN;
3366 			break;
3367 		case WLAN_CIPHER_SUITE_AES_CMAC:
3368 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3369 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3370 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3371 			WARN(1,
3372 			     "management cipher suite 0x%x enabled for data\n",
3373 			     build.key->conf.cipher);
3374 			goto out;
3375 		default:
3376 			/* we don't know how to generate IVs for this at all */
3377 			if (WARN_ON(gen_iv))
3378 				goto out;
3379 		}
3380 
3381 		fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3382 	}
3383 
3384 	hdr->frame_control = fc;
3385 
3386 	memcpy(build.hdr + build.hdr_len,
3387 	       rfc1042_header,  sizeof(rfc1042_header));
3388 	build.hdr_len += sizeof(rfc1042_header);
3389 
3390 	fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3391 	/* if the kmemdup fails, continue w/o fast_tx */
3392 
3393  out:
3394 	/* we might have raced against another call to this function */
3395 	old = rcu_dereference_protected(sta->fast_tx,
3396 					lockdep_is_held(&sta->lock));
3397 	rcu_assign_pointer(sta->fast_tx, fast_tx);
3398 	if (old)
3399 		kfree_rcu(old, rcu_head);
3400 	spin_unlock_bh(&sta->lock);
3401 }
3402 
3403 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3404 {
3405 	struct sta_info *sta;
3406 
3407 	rcu_read_lock();
3408 	list_for_each_entry_rcu(sta, &local->sta_list, list)
3409 		ieee80211_check_fast_xmit(sta);
3410 	rcu_read_unlock();
3411 }
3412 
3413 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3414 {
3415 	struct ieee80211_local *local = sdata->local;
3416 	struct sta_info *sta;
3417 
3418 	rcu_read_lock();
3419 
3420 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
3421 		if (sdata != sta->sdata &&
3422 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3423 			continue;
3424 		ieee80211_check_fast_xmit(sta);
3425 	}
3426 
3427 	rcu_read_unlock();
3428 }
3429 
3430 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3431 {
3432 	struct ieee80211_fast_tx *fast_tx;
3433 
3434 	spin_lock_bh(&sta->lock);
3435 	fast_tx = rcu_dereference_protected(sta->fast_tx,
3436 					    lockdep_is_held(&sta->lock));
3437 	RCU_INIT_POINTER(sta->fast_tx, NULL);
3438 	spin_unlock_bh(&sta->lock);
3439 
3440 	if (fast_tx)
3441 		kfree_rcu(fast_tx, rcu_head);
3442 }
3443 
3444 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3445 					struct sk_buff *skb, int headroom)
3446 {
3447 	if (skb_headroom(skb) < headroom) {
3448 		I802_DEBUG_INC(local->tx_expand_skb_head);
3449 
3450 		if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3451 			wiphy_debug(local->hw.wiphy,
3452 				    "failed to reallocate TX buffer\n");
3453 			return false;
3454 		}
3455 	}
3456 
3457 	return true;
3458 }
3459 
3460 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3461 					 struct ieee80211_fast_tx *fast_tx,
3462 					 struct sk_buff *skb)
3463 {
3464 	struct ieee80211_local *local = sdata->local;
3465 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3466 	struct ieee80211_hdr *hdr;
3467 	struct ethhdr *amsdu_hdr;
3468 	int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3469 	int subframe_len = skb->len - hdr_len;
3470 	void *data;
3471 	u8 *qc, *h_80211_src, *h_80211_dst;
3472 	const u8 *bssid;
3473 
3474 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3475 		return false;
3476 
3477 	if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3478 		return true;
3479 
3480 	if (!ieee80211_amsdu_realloc_pad(local, skb,
3481 					 sizeof(*amsdu_hdr) +
3482 					 local->hw.extra_tx_headroom))
3483 		return false;
3484 
3485 	data = skb_push(skb, sizeof(*amsdu_hdr));
3486 	memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3487 	hdr = data;
3488 	amsdu_hdr = data + hdr_len;
3489 	/* h_80211_src/dst is addr* field within hdr */
3490 	h_80211_src = data + fast_tx->sa_offs;
3491 	h_80211_dst = data + fast_tx->da_offs;
3492 
3493 	amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3494 	ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3495 	ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3496 
3497 	/* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3498 	 * fields needs to be changed to BSSID for A-MSDU frames depending
3499 	 * on FromDS/ToDS values.
3500 	 */
3501 	switch (sdata->vif.type) {
3502 	case NL80211_IFTYPE_STATION:
3503 		bssid = sdata->vif.cfg.ap_addr;
3504 		break;
3505 	case NL80211_IFTYPE_AP:
3506 	case NL80211_IFTYPE_AP_VLAN:
3507 		bssid = sdata->vif.addr;
3508 		break;
3509 	default:
3510 		bssid = NULL;
3511 	}
3512 
3513 	if (bssid && ieee80211_has_fromds(hdr->frame_control))
3514 		ether_addr_copy(h_80211_src, bssid);
3515 
3516 	if (bssid && ieee80211_has_tods(hdr->frame_control))
3517 		ether_addr_copy(h_80211_dst, bssid);
3518 
3519 	qc = ieee80211_get_qos_ctl(hdr);
3520 	*qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3521 
3522 	info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3523 
3524 	return true;
3525 }
3526 
3527 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3528 				      struct sta_info *sta,
3529 				      struct ieee80211_fast_tx *fast_tx,
3530 				      struct sk_buff *skb,
3531 				      const u8 *da, const u8 *sa)
3532 {
3533 	struct ieee80211_local *local = sdata->local;
3534 	struct fq *fq = &local->fq;
3535 	struct fq_tin *tin;
3536 	struct fq_flow *flow;
3537 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3538 	struct ieee80211_txq *txq = sta->sta.txq[tid];
3539 	struct txq_info *txqi;
3540 	struct sk_buff **frag_tail, *head;
3541 	int subframe_len = skb->len - ETH_ALEN;
3542 	u8 max_subframes = sta->sta.max_amsdu_subframes;
3543 	int max_frags = local->hw.max_tx_fragments;
3544 	int max_amsdu_len = sta->sta.cur->max_amsdu_len;
3545 	int orig_truesize;
3546 	u32 flow_idx;
3547 	__be16 len;
3548 	void *data;
3549 	bool ret = false;
3550 	unsigned int orig_len;
3551 	int n = 2, nfrags, pad = 0;
3552 	u16 hdrlen;
3553 
3554 	if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3555 		return false;
3556 
3557 	if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
3558 		return false;
3559 
3560 	if (ieee80211_vif_is_mesh(&sdata->vif))
3561 		return false;
3562 
3563 	if (skb_is_gso(skb))
3564 		return false;
3565 
3566 	if (!txq)
3567 		return false;
3568 
3569 	txqi = to_txq_info(txq);
3570 	if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3571 		return false;
3572 
3573 	if (sta->sta.cur->max_rc_amsdu_len)
3574 		max_amsdu_len = min_t(int, max_amsdu_len,
3575 				      sta->sta.cur->max_rc_amsdu_len);
3576 
3577 	if (sta->sta.cur->max_tid_amsdu_len[tid])
3578 		max_amsdu_len = min_t(int, max_amsdu_len,
3579 				      sta->sta.cur->max_tid_amsdu_len[tid]);
3580 
3581 	flow_idx = fq_flow_idx(fq, skb);
3582 
3583 	spin_lock_bh(&fq->lock);
3584 
3585 	/* TODO: Ideally aggregation should be done on dequeue to remain
3586 	 * responsive to environment changes.
3587 	 */
3588 
3589 	tin = &txqi->tin;
3590 	flow = fq_flow_classify(fq, tin, flow_idx, skb);
3591 	head = skb_peek_tail(&flow->queue);
3592 	if (!head || skb_is_gso(head))
3593 		goto out;
3594 
3595 	orig_truesize = head->truesize;
3596 	orig_len = head->len;
3597 
3598 	if (skb->len + head->len > max_amsdu_len)
3599 		goto out;
3600 
3601 	nfrags = 1 + skb_shinfo(skb)->nr_frags;
3602 	nfrags += 1 + skb_shinfo(head)->nr_frags;
3603 	frag_tail = &skb_shinfo(head)->frag_list;
3604 	while (*frag_tail) {
3605 		nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3606 		frag_tail = &(*frag_tail)->next;
3607 		n++;
3608 	}
3609 
3610 	if (max_subframes && n > max_subframes)
3611 		goto out;
3612 
3613 	if (max_frags && nfrags > max_frags)
3614 		goto out;
3615 
3616 	if (!drv_can_aggregate_in_amsdu(local, head, skb))
3617 		goto out;
3618 
3619 	if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3620 		goto out;
3621 
3622 	/* If n == 2, the "while (*frag_tail)" loop above didn't execute
3623 	 * and  frag_tail should be &skb_shinfo(head)->frag_list.
3624 	 * However, ieee80211_amsdu_prepare_head() can reallocate it.
3625 	 * Reload frag_tail to have it pointing to the correct place.
3626 	 */
3627 	if (n == 2)
3628 		frag_tail = &skb_shinfo(head)->frag_list;
3629 
3630 	/*
3631 	 * Pad out the previous subframe to a multiple of 4 by adding the
3632 	 * padding to the next one, that's being added. Note that head->len
3633 	 * is the length of the full A-MSDU, but that works since each time
3634 	 * we add a new subframe we pad out the previous one to a multiple
3635 	 * of 4 and thus it no longer matters in the next round.
3636 	 */
3637 	hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3638 	if ((head->len - hdrlen) & 3)
3639 		pad = 4 - ((head->len - hdrlen) & 3);
3640 
3641 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3642 						     2 + pad))
3643 		goto out_recalc;
3644 
3645 	ret = true;
3646 	data = skb_push(skb, ETH_ALEN + 2);
3647 	ether_addr_copy(data, da);
3648 	ether_addr_copy(data + ETH_ALEN, sa);
3649 
3650 	data += 2 * ETH_ALEN;
3651 	len = cpu_to_be16(subframe_len);
3652 	memcpy(data, &len, 2);
3653 	memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3654 
3655 	memset(skb_push(skb, pad), 0, pad);
3656 
3657 	head->len += skb->len;
3658 	head->data_len += skb->len;
3659 	*frag_tail = skb;
3660 
3661 out_recalc:
3662 	fq->memory_usage += head->truesize - orig_truesize;
3663 	if (head->len != orig_len) {
3664 		flow->backlog += head->len - orig_len;
3665 		tin->backlog_bytes += head->len - orig_len;
3666 	}
3667 out:
3668 	spin_unlock_bh(&fq->lock);
3669 
3670 	return ret;
3671 }
3672 
3673 /*
3674  * Can be called while the sta lock is held. Anything that can cause packets to
3675  * be generated will cause deadlock!
3676  */
3677 static ieee80211_tx_result
3678 ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3679 			   struct sta_info *sta, u8 pn_offs,
3680 			   struct ieee80211_key *key,
3681 			   struct ieee80211_tx_data *tx)
3682 {
3683 	struct sk_buff *skb = tx->skb;
3684 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3685 	struct ieee80211_hdr *hdr = (void *)skb->data;
3686 	u8 tid = IEEE80211_NUM_TIDS;
3687 
3688 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
3689 	    ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
3690 		return TX_DROP;
3691 
3692 	if (key)
3693 		info->control.hw_key = &key->conf;
3694 
3695 	dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
3696 
3697 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3698 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3699 		hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3700 	} else {
3701 		info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3702 		hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3703 		sdata->sequence_number += 0x10;
3704 	}
3705 
3706 	if (skb_shinfo(skb)->gso_size)
3707 		sta->deflink.tx_stats.msdu[tid] +=
3708 			DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3709 	else
3710 		sta->deflink.tx_stats.msdu[tid]++;
3711 
3712 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3713 
3714 	/* statistics normally done by ieee80211_tx_h_stats (but that
3715 	 * has to consider fragmentation, so is more complex)
3716 	 */
3717 	sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3718 	sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++;
3719 
3720 	if (pn_offs) {
3721 		u64 pn;
3722 		u8 *crypto_hdr = skb->data + pn_offs;
3723 
3724 		switch (key->conf.cipher) {
3725 		case WLAN_CIPHER_SUITE_CCMP:
3726 		case WLAN_CIPHER_SUITE_CCMP_256:
3727 		case WLAN_CIPHER_SUITE_GCMP:
3728 		case WLAN_CIPHER_SUITE_GCMP_256:
3729 			pn = atomic64_inc_return(&key->conf.tx_pn);
3730 			crypto_hdr[0] = pn;
3731 			crypto_hdr[1] = pn >> 8;
3732 			crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3733 			crypto_hdr[4] = pn >> 16;
3734 			crypto_hdr[5] = pn >> 24;
3735 			crypto_hdr[6] = pn >> 32;
3736 			crypto_hdr[7] = pn >> 40;
3737 			break;
3738 		}
3739 	}
3740 
3741 	return TX_CONTINUE;
3742 }
3743 
3744 static netdev_features_t
3745 ieee80211_sdata_netdev_features(struct ieee80211_sub_if_data *sdata)
3746 {
3747 	if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3748 		return sdata->vif.netdev_features;
3749 
3750 	if (!sdata->bss)
3751 		return 0;
3752 
3753 	sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
3754 	return sdata->vif.netdev_features;
3755 }
3756 
3757 static struct sk_buff *
3758 ieee80211_tx_skb_fixup(struct sk_buff *skb, netdev_features_t features)
3759 {
3760 	if (skb_is_gso(skb)) {
3761 		struct sk_buff *segs;
3762 
3763 		segs = skb_gso_segment(skb, features);
3764 		if (!segs)
3765 			return skb;
3766 		if (IS_ERR(segs))
3767 			goto free;
3768 
3769 		consume_skb(skb);
3770 		return segs;
3771 	}
3772 
3773 	if (skb_needs_linearize(skb, features) && __skb_linearize(skb))
3774 		goto free;
3775 
3776 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3777 		int ofs = skb_checksum_start_offset(skb);
3778 
3779 		if (skb->encapsulation)
3780 			skb_set_inner_transport_header(skb, ofs);
3781 		else
3782 			skb_set_transport_header(skb, ofs);
3783 
3784 		if (skb_csum_hwoffload_help(skb, features))
3785 			goto free;
3786 	}
3787 
3788 	skb_mark_not_on_list(skb);
3789 	return skb;
3790 
3791 free:
3792 	kfree_skb(skb);
3793 	return NULL;
3794 }
3795 
3796 void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3797 			   struct sta_info *sta,
3798 			   struct ieee80211_fast_tx *fast_tx,
3799 			   struct sk_buff *skb, bool ampdu,
3800 			   const u8 *da, const u8 *sa)
3801 {
3802 	struct ieee80211_local *local = sdata->local;
3803 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3804 	struct ieee80211_tx_info *info;
3805 	struct ieee80211_tx_data tx;
3806 	ieee80211_tx_result r;
3807 	int hw_headroom = sdata->local->hw.extra_tx_headroom;
3808 	int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3809 
3810 	skb = skb_share_check(skb, GFP_ATOMIC);
3811 	if (unlikely(!skb))
3812 		return;
3813 
3814 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3815 	    ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb, da, sa))
3816 		return;
3817 
3818 	/* will not be crypto-handled beyond what we do here, so use false
3819 	 * as the may-encrypt argument for the resize to not account for
3820 	 * more room than we already have in 'extra_head'
3821 	 */
3822 	if (unlikely(ieee80211_skb_resize(sdata, skb,
3823 					  max_t(int, extra_head + hw_headroom -
3824 						     skb_headroom(skb), 0),
3825 					  ENCRYPT_NO)))
3826 		goto free;
3827 
3828 	hdr = skb_push(skb, extra_head);
3829 	memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3830 	memcpy(skb->data + fast_tx->da_offs, da, ETH_ALEN);
3831 	memcpy(skb->data + fast_tx->sa_offs, sa, ETH_ALEN);
3832 
3833 	info = IEEE80211_SKB_CB(skb);
3834 	memset(info, 0, sizeof(*info));
3835 	info->band = fast_tx->band;
3836 	info->control.vif = &sdata->vif;
3837 	info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3838 		      IEEE80211_TX_CTL_DONTFRAG;
3839 	info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT |
3840 			      u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
3841 					      IEEE80211_TX_CTRL_MLO_LINK);
3842 
3843 #ifdef CONFIG_MAC80211_DEBUGFS
3844 	if (local->force_tx_status)
3845 		info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3846 #endif
3847 
3848 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3849 		u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3850 
3851 		*ieee80211_get_qos_ctl(hdr) = tid;
3852 	}
3853 
3854 	__skb_queue_head_init(&tx.skbs);
3855 
3856 	tx.flags = IEEE80211_TX_UNICAST;
3857 	tx.local = local;
3858 	tx.sdata = sdata;
3859 	tx.sta = sta;
3860 	tx.key = fast_tx->key;
3861 
3862 	if (ieee80211_queue_skb(local, sdata, sta, skb))
3863 		return;
3864 
3865 	tx.skb = skb;
3866 	r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3867 				       fast_tx->key, &tx);
3868 	tx.skb = NULL;
3869 	if (r == TX_DROP) {
3870 		tx.sdata->tx_handlers_drop++;
3871 		goto free;
3872 	}
3873 
3874 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3875 		sdata = container_of(sdata->bss,
3876 				     struct ieee80211_sub_if_data, u.ap);
3877 
3878 	__skb_queue_tail(&tx.skbs, skb);
3879 	ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3880 	return;
3881 
3882 free:
3883 	kfree_skb(skb);
3884 }
3885 
3886 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3887 				struct sta_info *sta,
3888 				struct ieee80211_fast_tx *fast_tx,
3889 				struct sk_buff *skb)
3890 {
3891 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3892 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3893 	struct tid_ampdu_tx *tid_tx = NULL;
3894 	struct sk_buff *next;
3895 	struct ethhdr eth;
3896 	u8 tid = IEEE80211_NUM_TIDS;
3897 
3898 	/* control port protocol needs a lot of special handling */
3899 	if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3900 		return false;
3901 
3902 	/* only RFC 1042 SNAP */
3903 	if (ethertype < ETH_P_802_3_MIN)
3904 		return false;
3905 
3906 	/* don't handle TX status request here either */
3907 	if (sk_requests_wifi_status(skb->sk))
3908 		return false;
3909 
3910 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3911 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3912 		tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3913 		if (tid_tx) {
3914 			if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3915 				return false;
3916 			if (tid_tx->timeout)
3917 				tid_tx->last_tx = jiffies;
3918 		}
3919 	}
3920 
3921 	memcpy(&eth, skb->data, ETH_HLEN - 2);
3922 
3923 	/* after this point (skb is modified) we cannot return false */
3924 	skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
3925 	if (!skb)
3926 		return true;
3927 
3928 	skb_list_walk_safe(skb, skb, next) {
3929 		skb_mark_not_on_list(skb);
3930 		__ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid_tx,
3931 				      eth.h_dest, eth.h_source);
3932 	}
3933 
3934 	return true;
3935 }
3936 
3937 static bool ieee80211_drop_unauth_port(struct ieee80211_tx_data *tx)
3938 {
3939 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
3940 	struct ieee80211_hdr *hdr;
3941 
3942 	if (likely(test_sta_flag(tx->sta, WLAN_STA_AUTHORIZED)))
3943 		return false;
3944 
3945 	if (info->flags & IEEE80211_TX_CTL_INJECTED)
3946 		return false;
3947 
3948 	if (ieee80211_vif_is_mesh(&tx->sdata->vif) ||
3949 	    tx->sdata->vif.type == NL80211_IFTYPE_OCB)
3950 		return false;
3951 
3952 	/* no need to check forwarding here */
3953 	if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
3954 		return !(info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO);
3955 
3956 	hdr = (struct ieee80211_hdr *)tx->skb->data;
3957 
3958 	if (!ieee80211_is_data_present(hdr->frame_control) ||
3959 	    is_multicast_ether_addr(hdr->addr1))
3960 		return false;
3961 
3962 	if ((info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) &&
3963 	    ieee80211_is_our_addr(tx->sdata, hdr->addr2, NULL))
3964 		return false;
3965 
3966 	return true;
3967 }
3968 
3969 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3970 				     struct ieee80211_txq *txq)
3971 {
3972 	struct ieee80211_local *local = hw_to_local(hw);
3973 	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3974 	struct ieee80211_hdr *hdr;
3975 	struct sk_buff *skb = NULL;
3976 	struct fq *fq = &local->fq;
3977 	struct fq_tin *tin = &txqi->tin;
3978 	struct ieee80211_tx_info *info;
3979 	struct ieee80211_tx_data tx;
3980 	ieee80211_tx_result r;
3981 	struct ieee80211_vif *vif = txq->vif;
3982 	int q = vif->hw_queue[txq->ac];
3983 	unsigned long flags;
3984 	bool q_stopped;
3985 
3986 	WARN_ON_ONCE(softirq_count() == 0);
3987 
3988 	if (!ieee80211_txq_airtime_check(hw, txq))
3989 		return NULL;
3990 
3991 begin:
3992 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3993 	q_stopped = local->queue_stop_reasons[q];
3994 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3995 
3996 	if (unlikely(q_stopped)) {
3997 		/* mark for waking later */
3998 		set_bit(IEEE80211_TXQ_DIRTY, &txqi->flags);
3999 		return NULL;
4000 	}
4001 
4002 	spin_lock_bh(&fq->lock);
4003 
4004 	/* Make sure fragments stay together. */
4005 	skb = __skb_dequeue(&txqi->frags);
4006 	if (unlikely(skb)) {
4007 		if (!(IEEE80211_SKB_CB(skb)->control.flags &
4008 				IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
4009 			goto out;
4010 		IEEE80211_SKB_CB(skb)->control.flags &=
4011 			~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
4012 	} else {
4013 		if (unlikely(test_bit(IEEE80211_TXQ_STOP, &txqi->flags)))
4014 			goto out;
4015 
4016 		skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
4017 	}
4018 
4019 	if (!skb)
4020 		goto out;
4021 
4022 	spin_unlock_bh(&fq->lock);
4023 
4024 	hdr = (struct ieee80211_hdr *)skb->data;
4025 	info = IEEE80211_SKB_CB(skb);
4026 
4027 	memset(&tx, 0, sizeof(tx));
4028 	__skb_queue_head_init(&tx.skbs);
4029 	tx.local = local;
4030 	tx.skb = skb;
4031 	tx.sdata = vif_to_sdata(info->control.vif);
4032 
4033 	if (txq->sta) {
4034 		tx.sta = container_of(txq->sta, struct sta_info, sta);
4035 
4036 		if (unlikely(ieee80211_drop_unauth_port(&tx))) {
4037 			I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
4038 			ieee80211_free_txskb(&local->hw, skb);
4039 			goto begin;
4040 		}
4041 	}
4042 
4043 	/*
4044 	 * The key can be removed while the packet was queued, so need to call
4045 	 * this here to get the current key.
4046 	 */
4047 	info->control.hw_key = NULL;
4048 	r = ieee80211_tx_h_select_key(&tx);
4049 	if (r != TX_CONTINUE) {
4050 		ieee80211_free_txskb(&local->hw, skb);
4051 		goto begin;
4052 	}
4053 
4054 	if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
4055 		info->flags |= (IEEE80211_TX_CTL_AMPDU |
4056 				IEEE80211_TX_CTL_DONTFRAG);
4057 
4058 	if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4059 		if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
4060 			r = ieee80211_tx_h_rate_ctrl(&tx);
4061 			if (r != TX_CONTINUE) {
4062 				ieee80211_free_txskb(&local->hw, skb);
4063 				goto begin;
4064 			}
4065 		}
4066 		goto encap_out;
4067 	}
4068 
4069 	if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
4070 		struct sta_info *sta = container_of(txq->sta, struct sta_info,
4071 						    sta);
4072 		u8 pn_offs = 0;
4073 
4074 		if (tx.key &&
4075 		    (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
4076 			pn_offs = ieee80211_hdrlen(hdr->frame_control);
4077 
4078 		r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
4079 					       tx.key, &tx);
4080 		if (r != TX_CONTINUE) {
4081 			ieee80211_free_txskb(&local->hw, skb);
4082 			goto begin;
4083 		}
4084 	} else {
4085 		if (invoke_tx_handlers_late(&tx))
4086 			goto begin;
4087 
4088 		skb = __skb_dequeue(&tx.skbs);
4089 		info = IEEE80211_SKB_CB(skb);
4090 
4091 		if (!skb_queue_empty(&tx.skbs)) {
4092 			spin_lock_bh(&fq->lock);
4093 			skb_queue_splice_tail(&tx.skbs, &txqi->frags);
4094 			spin_unlock_bh(&fq->lock);
4095 		}
4096 	}
4097 
4098 	if (skb_has_frag_list(skb) &&
4099 	    !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
4100 		if (skb_linearize(skb)) {
4101 			ieee80211_free_txskb(&local->hw, skb);
4102 			goto begin;
4103 		}
4104 	}
4105 
4106 	switch (tx.sdata->vif.type) {
4107 	case NL80211_IFTYPE_MONITOR:
4108 		if ((tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) ||
4109 		    ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) {
4110 			vif = &tx.sdata->vif;
4111 			break;
4112 		}
4113 		tx.sdata = rcu_dereference(local->monitor_sdata);
4114 		if (tx.sdata &&
4115 		    ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) {
4116 			vif = &tx.sdata->vif;
4117 			info->hw_queue =
4118 				vif->hw_queue[skb_get_queue_mapping(skb)];
4119 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
4120 			ieee80211_free_txskb(&local->hw, skb);
4121 			goto begin;
4122 		} else {
4123 			info->control.vif = NULL;
4124 			return skb;
4125 		}
4126 		break;
4127 	case NL80211_IFTYPE_AP_VLAN:
4128 		tx.sdata = container_of(tx.sdata->bss,
4129 					struct ieee80211_sub_if_data, u.ap);
4130 		fallthrough;
4131 	default:
4132 		vif = &tx.sdata->vif;
4133 		break;
4134 	}
4135 
4136 encap_out:
4137 	info->control.vif = vif;
4138 
4139 	if (wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
4140 		bool ampdu = txq->sta && txq->ac != IEEE80211_AC_VO;
4141 		u32 airtime;
4142 
4143 		airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
4144 							     skb->len, ampdu);
4145 		if (!airtime)
4146 			return skb;
4147 
4148 		airtime = ieee80211_info_set_tx_time_est(info, airtime);
4149 		info->tx_time_mc = !tx.sta;
4150 		ieee80211_sta_update_pending_airtime(local, tx.sta, txq->ac,
4151 						     airtime, false,
4152 						     info->tx_time_mc);
4153 	}
4154 
4155 	return skb;
4156 
4157 out:
4158 	spin_unlock_bh(&fq->lock);
4159 
4160 	return skb;
4161 }
4162 EXPORT_SYMBOL(ieee80211_tx_dequeue);
4163 
4164 static inline s32 ieee80211_sta_deficit(struct sta_info *sta, u8 ac)
4165 {
4166 	struct airtime_info *air_info = &sta->airtime[ac];
4167 
4168 	return air_info->deficit - atomic_read(&air_info->aql_tx_pending);
4169 }
4170 
4171 static void
4172 ieee80211_txq_set_active(struct txq_info *txqi)
4173 {
4174 	struct sta_info *sta;
4175 
4176 	if (!txqi->txq.sta)
4177 		return;
4178 
4179 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4180 	sta->airtime[txqi->txq.ac].last_active = jiffies;
4181 }
4182 
4183 static bool
4184 ieee80211_txq_keep_active(struct txq_info *txqi)
4185 {
4186 	struct sta_info *sta;
4187 
4188 	if (!txqi->txq.sta)
4189 		return false;
4190 
4191 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4192 	if (ieee80211_sta_deficit(sta, txqi->txq.ac) >= 0)
4193 		return false;
4194 
4195 	return ieee80211_sta_keep_active(sta, txqi->txq.ac);
4196 }
4197 
4198 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
4199 {
4200 	struct ieee80211_local *local = hw_to_local(hw);
4201 	struct ieee80211_txq *ret = NULL;
4202 	struct txq_info *txqi = NULL, *head = NULL;
4203 	bool found_eligible_txq = false;
4204 	bool aql_check;
4205 
4206 	spin_lock_bh(&local->active_txq_lock[ac]);
4207 
4208 	if (!local->schedule_open[ac])
4209 		goto out;
4210 
4211  begin:
4212 	txqi = list_first_entry_or_null(&local->active_txqs[ac],
4213 					struct txq_info,
4214 					schedule_order);
4215 	if (!txqi)
4216 		goto out;
4217 
4218 	if (txqi == head) {
4219 		if (!found_eligible_txq)
4220 			goto out;
4221 		else
4222 			found_eligible_txq = false;
4223 	}
4224 
4225 	if (!head)
4226 		head = txqi;
4227 
4228 	aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
4229 	if (aql_check)
4230 		found_eligible_txq = true;
4231 
4232 	if (txqi->txq.sta) {
4233 		struct sta_info *sta = container_of(txqi->txq.sta,
4234 						    struct sta_info, sta);
4235 
4236 		if (ieee80211_sta_deficit(sta, txqi->txq.ac) < 0) {
4237 			sta->airtime[txqi->txq.ac].deficit +=
4238 				sta->airtime_weight;
4239 
4240 			aql_check = false;
4241 		}
4242 	}
4243 
4244 	if (!aql_check) {
4245 		list_move_tail(&txqi->schedule_order,
4246 				   &local->active_txqs[txqi->txq.ac]);
4247 		goto begin;
4248 	}
4249 
4250 	if (txqi->schedule_round == local->schedule_round[ac])
4251 		goto out;
4252 
4253 	list_del_init(&txqi->schedule_order);
4254 	txqi->schedule_round = local->schedule_round[ac];
4255 	ret = &txqi->txq;
4256 
4257 out:
4258 	spin_unlock_bh(&local->active_txq_lock[ac]);
4259 	return ret;
4260 }
4261 EXPORT_SYMBOL(ieee80211_next_txq);
4262 
4263 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
4264 			      struct ieee80211_txq *txq,
4265 			      bool force)
4266 {
4267 	struct ieee80211_local *local = hw_to_local(hw);
4268 	struct txq_info *txqi = to_txq_info(txq);
4269 	bool has_queue;
4270 
4271 	spin_lock_bh(&local->active_txq_lock[txq->ac]);
4272 
4273 	has_queue = force ||
4274 		    (!test_bit(IEEE80211_TXQ_STOP, &txqi->flags) &&
4275 		     txq_has_queue(txq));
4276 	if (list_empty(&txqi->schedule_order) &&
4277 	    (has_queue || ieee80211_txq_keep_active(txqi))) {
4278 		/* If airtime accounting is active, always enqueue STAs at the
4279 		 * head of the list to ensure that they only get moved to the
4280 		 * back by the airtime DRR scheduler once they have a negative
4281 		 * deficit. A station that already has a negative deficit will
4282 		 * get immediately moved to the back of the list on the next
4283 		 * call to ieee80211_next_txq().
4284 		 */
4285 		if (txqi->txq.sta && local->airtime_flags && has_queue &&
4286 		    wiphy_ext_feature_isset(local->hw.wiphy,
4287 					    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
4288 			list_add(&txqi->schedule_order,
4289 				 &local->active_txqs[txq->ac]);
4290 		else
4291 			list_add_tail(&txqi->schedule_order,
4292 				      &local->active_txqs[txq->ac]);
4293 		if (has_queue)
4294 			ieee80211_txq_set_active(txqi);
4295 	}
4296 
4297 	spin_unlock_bh(&local->active_txq_lock[txq->ac]);
4298 }
4299 EXPORT_SYMBOL(__ieee80211_schedule_txq);
4300 
4301 DEFINE_STATIC_KEY_FALSE(aql_disable);
4302 
4303 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
4304 				 struct ieee80211_txq *txq)
4305 {
4306 	struct sta_info *sta;
4307 	struct ieee80211_local *local = hw_to_local(hw);
4308 
4309 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
4310 		return true;
4311 
4312 	if (static_branch_unlikely(&aql_disable))
4313 		return true;
4314 
4315 	if (!txq->sta)
4316 		return atomic_read(&local->aql_mc_pending_airtime) <
4317 		       local->aql_txq_limit_mc;
4318 
4319 	if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
4320 		return true;
4321 
4322 	sta = container_of(txq->sta, struct sta_info, sta);
4323 	if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4324 	    sta->airtime[txq->ac].aql_limit_low)
4325 		return true;
4326 
4327 	if (atomic_read(&local->aql_total_pending_airtime) <
4328 	    local->aql_threshold &&
4329 	    atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4330 	    sta->airtime[txq->ac].aql_limit_high)
4331 		return true;
4332 
4333 	return false;
4334 }
4335 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
4336 
4337 u32 ieee80211_txq_aql_pending(struct ieee80211_hw *hw,
4338 			      struct ieee80211_txq *txq)
4339 {
4340 	struct ieee80211_local *local = hw_to_local(hw);
4341 	struct sta_info *sta;
4342 
4343 	if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
4344 		return 0;
4345 
4346 	if (!txq->sta)
4347 		return atomic_read(&local->aql_mc_pending_airtime);
4348 
4349 	sta = container_of(txq->sta, struct sta_info, sta);
4350 
4351 	return atomic_read(&sta->airtime[txq->ac].aql_tx_pending);
4352 }
4353 EXPORT_SYMBOL(ieee80211_txq_aql_pending);
4354 
4355 static bool
4356 ieee80211_txq_schedule_airtime_check(struct ieee80211_local *local, u8 ac)
4357 {
4358 	unsigned int num_txq = 0;
4359 	struct txq_info *txq;
4360 	u32 aql_limit;
4361 
4362 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
4363 		return true;
4364 
4365 	list_for_each_entry(txq, &local->active_txqs[ac], schedule_order)
4366 		num_txq++;
4367 
4368 	aql_limit = (num_txq - 1) * local->aql_txq_limit_low[ac] / 2 +
4369 		    local->aql_txq_limit_high[ac];
4370 
4371 	return atomic_read(&local->aql_ac_pending_airtime[ac]) < aql_limit;
4372 }
4373 
4374 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
4375 				struct ieee80211_txq *txq)
4376 {
4377 	struct ieee80211_local *local = hw_to_local(hw);
4378 	struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
4379 	struct sta_info *sta;
4380 	u8 ac = txq->ac;
4381 
4382 	spin_lock_bh(&local->active_txq_lock[ac]);
4383 
4384 	if (list_empty(&txqi->schedule_order))
4385 		goto out;
4386 
4387 	if (!ieee80211_txq_schedule_airtime_check(local, ac))
4388 		goto out;
4389 
4390 	if (!txqi->txq.sta)
4391 		goto out;
4392 
4393 	list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
4394 				 schedule_order) {
4395 		if (iter == txqi)
4396 			break;
4397 
4398 		if (!iter->txq.sta) {
4399 			list_move_tail(&iter->schedule_order,
4400 				       &local->active_txqs[ac]);
4401 			continue;
4402 		}
4403 		sta = container_of(iter->txq.sta, struct sta_info, sta);
4404 		if (ieee80211_sta_deficit(sta, ac) < 0)
4405 			sta->airtime[ac].deficit += sta->airtime_weight;
4406 		list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
4407 	}
4408 
4409 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
4410 	if (sta->airtime[ac].deficit >= 0)
4411 		goto out;
4412 
4413 	sta->airtime[ac].deficit += sta->airtime_weight;
4414 	list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
4415 	spin_unlock_bh(&local->active_txq_lock[ac]);
4416 
4417 	return false;
4418 out:
4419 	if (!list_empty(&txqi->schedule_order))
4420 		list_del_init(&txqi->schedule_order);
4421 	spin_unlock_bh(&local->active_txq_lock[ac]);
4422 
4423 	return true;
4424 }
4425 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
4426 
4427 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
4428 {
4429 	struct ieee80211_local *local = hw_to_local(hw);
4430 
4431 	spin_lock_bh(&local->active_txq_lock[ac]);
4432 
4433 	local->schedule_open[ac] =
4434 		ieee80211_txq_schedule_airtime_check(local, ac);
4435 	if (local->schedule_open[ac]) {
4436 		local->schedule_round[ac]++;
4437 		if (!local->schedule_round[ac])
4438 			local->schedule_round[ac]++;
4439 	}
4440 
4441 	spin_unlock_bh(&local->active_txq_lock[ac]);
4442 }
4443 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
4444 
4445 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
4446 				  struct net_device *dev,
4447 				  u32 info_flags,
4448 				  u32 ctrl_flags)
4449 {
4450 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4451 	struct ieee80211_local *local = sdata->local;
4452 	struct ieee80211_tx_info *info;
4453 	struct sta_info *sta;
4454 	struct sk_buff *next;
4455 	bool group_addressed;
4456 	int len = skb->len;
4457 
4458 	if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
4459 		kfree_skb(skb);
4460 		return;
4461 	}
4462 
4463 	sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4464 
4465 	rcu_read_lock();
4466 
4467 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
4468 	    ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT) &&
4469 	    ieee80211_mesh_xmit_fast(sdata, skb, ctrl_flags))
4470 		goto out;
4471 
4472 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta,
4473 				    skb->protocol == sdata->control_port_protocol))
4474 		goto out_free;
4475 
4476 	group_addressed = IS_ERR(sta);
4477 	if (IS_ERR(sta))
4478 		sta = NULL;
4479 
4480 	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
4481 	ieee80211_aggr_check(sdata, sta, skb);
4482 
4483 	if (sta) {
4484 		struct ieee80211_fast_tx *fast_tx;
4485 
4486 		fast_tx = rcu_dereference(sta->fast_tx);
4487 
4488 		if (fast_tx &&
4489 		    ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
4490 			goto out;
4491 	}
4492 
4493 	/* the frame could be fragmented, software-encrypted, and other
4494 	 * things so we cannot really handle checksum or GSO offload.
4495 	 * fix it up in software before we handle anything else.
4496 	 */
4497 	skb = ieee80211_tx_skb_fixup(skb, 0);
4498 	if (!skb) {
4499 		len = 0;
4500 		goto out;
4501 	}
4502 
4503 	skb_list_walk_safe(skb, skb, next) {
4504 		skb_mark_not_on_list(skb);
4505 
4506 		if (skb->protocol == sdata->control_port_protocol)
4507 			ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4508 
4509 		skb = skb_share_check(skb, GFP_ATOMIC);
4510 		if (unlikely(!skb)) {
4511 			kfree_skb_list(next);
4512 			goto out;
4513 		}
4514 
4515 		info = IEEE80211_SKB_CB(skb);
4516 		memset(info, 0, sizeof(*info));
4517 		info->flags = info_flags;
4518 		info->control.flags = ctrl_flags;
4519 
4520 		if (unlikely(!group_addressed &&
4521 			     sk_requests_wifi_status(skb->sk)))
4522 			ieee80211_store_ack_skb(local, skb, 0);
4523 
4524 		skb = ieee80211_build_hdr(sdata, skb, sta);
4525 		if (IS_ERR(skb)) {
4526 			kfree_skb_list(next);
4527 			goto out;
4528 		}
4529 
4530 		dev_sw_netstats_tx_add(dev, 1, skb->len);
4531 
4532 		ieee80211_xmit(sdata, sta, skb);
4533 	}
4534 	goto out;
4535  out_free:
4536 	kfree_skb(skb);
4537 	len = 0;
4538  out:
4539 	if (len)
4540 		ieee80211_tpt_led_trig_tx(local, len);
4541 	rcu_read_unlock();
4542 }
4543 
4544 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4545 {
4546 	struct ethhdr *eth;
4547 	int err;
4548 
4549 	err = skb_ensure_writable(skb, ETH_HLEN);
4550 	if (unlikely(err))
4551 		return err;
4552 
4553 	eth = (void *)skb->data;
4554 	ether_addr_copy(eth->h_dest, sta->sta.addr);
4555 
4556 	return 0;
4557 }
4558 
4559 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4560 					   struct net_device *dev)
4561 {
4562 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4563 	const struct ethhdr *eth = (void *)skb->data;
4564 	const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4565 	__be16 ethertype;
4566 
4567 	switch (sdata->vif.type) {
4568 	case NL80211_IFTYPE_AP_VLAN:
4569 		if (sdata->u.vlan.sta)
4570 			return false;
4571 		if (sdata->wdev.use_4addr)
4572 			return false;
4573 		fallthrough;
4574 	case NL80211_IFTYPE_AP:
4575 		/* check runtime toggle for this bss */
4576 		if (!sdata->bss->multicast_to_unicast)
4577 			return false;
4578 		break;
4579 	default:
4580 		return false;
4581 	}
4582 
4583 	/* multicast to unicast conversion only for some payload */
4584 	ethertype = eth->h_proto;
4585 	if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4586 		ethertype = ethvlan->h_vlan_encapsulated_proto;
4587 	switch (ethertype) {
4588 	case htons(ETH_P_ARP):
4589 	case htons(ETH_P_IP):
4590 	case htons(ETH_P_IPV6):
4591 		break;
4592 	default:
4593 		return false;
4594 	}
4595 
4596 	return true;
4597 }
4598 
4599 static void
4600 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4601 			     struct sk_buff_head *queue)
4602 {
4603 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4604 	struct ieee80211_local *local = sdata->local;
4605 	const struct ethhdr *eth = (struct ethhdr *)skb->data;
4606 	struct sta_info *sta, *first = NULL;
4607 	struct sk_buff *cloned_skb;
4608 
4609 	rcu_read_lock();
4610 
4611 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
4612 		if (sdata != sta->sdata)
4613 			/* AP-VLAN mismatch */
4614 			continue;
4615 		if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4616 			/* do not send back to source */
4617 			continue;
4618 		if (!first) {
4619 			first = sta;
4620 			continue;
4621 		}
4622 		cloned_skb = skb_clone(skb, GFP_ATOMIC);
4623 		if (!cloned_skb)
4624 			goto multicast;
4625 		if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4626 			dev_kfree_skb(cloned_skb);
4627 			goto multicast;
4628 		}
4629 		__skb_queue_tail(queue, cloned_skb);
4630 	}
4631 
4632 	if (likely(first)) {
4633 		if (unlikely(ieee80211_change_da(skb, first)))
4634 			goto multicast;
4635 		__skb_queue_tail(queue, skb);
4636 	} else {
4637 		/* no STA connected, drop */
4638 		kfree_skb(skb);
4639 		skb = NULL;
4640 	}
4641 
4642 	goto out;
4643 multicast:
4644 	__skb_queue_purge(queue);
4645 	__skb_queue_tail(queue, skb);
4646 out:
4647 	rcu_read_unlock();
4648 }
4649 
4650 static void ieee80211_mlo_multicast_tx_one(struct ieee80211_sub_if_data *sdata,
4651 					   struct sk_buff *skb, u32 ctrl_flags,
4652 					   unsigned int link_id)
4653 {
4654 	struct sk_buff *out;
4655 
4656 	out = skb_copy(skb, GFP_ATOMIC);
4657 	if (!out)
4658 		return;
4659 
4660 	ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
4661 	__ieee80211_subif_start_xmit(out, sdata->dev, 0, ctrl_flags);
4662 }
4663 
4664 static void ieee80211_mlo_multicast_tx(struct net_device *dev,
4665 				       struct sk_buff *skb)
4666 {
4667 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4668 	unsigned long links = sdata->vif.active_links;
4669 	unsigned int link;
4670 	u32 ctrl_flags = IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX;
4671 
4672 	if (hweight16(links) == 1) {
4673 		ctrl_flags |= u32_encode_bits(__ffs(links),
4674 					      IEEE80211_TX_CTRL_MLO_LINK);
4675 
4676 		__ieee80211_subif_start_xmit(skb, sdata->dev, 0, ctrl_flags);
4677 		return;
4678 	}
4679 
4680 	for_each_set_bit(link, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4681 		ieee80211_mlo_multicast_tx_one(sdata, skb, ctrl_flags, link);
4682 		ctrl_flags = 0;
4683 	}
4684 	kfree_skb(skb);
4685 }
4686 
4687 /**
4688  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4689  * @skb: packet to be sent
4690  * @dev: incoming interface
4691  *
4692  * On failure skb will be freed.
4693  *
4694  * Returns: the netdev TX status (but really only %NETDEV_TX_OK)
4695  */
4696 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4697 				       struct net_device *dev)
4698 {
4699 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4700 	const struct ethhdr *eth = (void *)skb->data;
4701 
4702 	if (likely(!is_multicast_ether_addr(eth->h_dest)))
4703 		goto normal;
4704 
4705 	if (unlikely(!ieee80211_sdata_running(sdata))) {
4706 		kfree_skb(skb);
4707 		return NETDEV_TX_OK;
4708 	}
4709 
4710 	if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4711 		struct sk_buff_head queue;
4712 
4713 		__skb_queue_head_init(&queue);
4714 		ieee80211_convert_to_unicast(skb, dev, &queue);
4715 		while ((skb = __skb_dequeue(&queue)))
4716 			__ieee80211_subif_start_xmit(skb, dev, 0,
4717 						     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC);
4718 	} else if (ieee80211_vif_is_mld(&sdata->vif) &&
4719 		   ((sdata->vif.type == NL80211_IFTYPE_AP &&
4720 		     !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX)) ||
4721 		    (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4722 		     !sdata->wdev.use_4addr))) {
4723 		ieee80211_mlo_multicast_tx(dev, skb);
4724 	} else {
4725 normal:
4726 		__ieee80211_subif_start_xmit(skb, dev, 0,
4727 					     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC);
4728 	}
4729 
4730 	return NETDEV_TX_OK;
4731 }
4732 
4733 
4734 
4735 static bool __ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4736 				struct sk_buff *skb, struct sta_info *sta,
4737 				bool txpending)
4738 {
4739 	struct ieee80211_local *local = sdata->local;
4740 	struct ieee80211_tx_control control = {};
4741 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4742 	struct ieee80211_sta *pubsta = NULL;
4743 	unsigned long flags;
4744 	int q = info->hw_queue;
4745 
4746 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4747 
4748 	if (local->queue_stop_reasons[q] ||
4749 	    (!txpending && !skb_queue_empty(&local->pending[q]))) {
4750 		if (txpending)
4751 			skb_queue_head(&local->pending[q], skb);
4752 		else
4753 			skb_queue_tail(&local->pending[q], skb);
4754 
4755 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4756 
4757 		return false;
4758 	}
4759 
4760 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4761 
4762 	if (sta && sta->uploaded)
4763 		pubsta = &sta->sta;
4764 
4765 	control.sta = pubsta;
4766 
4767 	drv_tx(local, &control, skb);
4768 
4769 	return true;
4770 }
4771 
4772 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4773 			      struct sk_buff *skb, struct sta_info *sta,
4774 			      bool txpending)
4775 {
4776 	struct ieee80211_local *local = sdata->local;
4777 	struct sk_buff *next;
4778 	bool ret = true;
4779 
4780 	if (ieee80211_queue_skb(local, sdata, sta, skb))
4781 		return true;
4782 
4783 	skb_list_walk_safe(skb, skb, next) {
4784 		skb_mark_not_on_list(skb);
4785 		if (!__ieee80211_tx_8023(sdata, skb, sta, txpending))
4786 			ret = false;
4787 	}
4788 
4789 	return ret;
4790 }
4791 
4792 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4793 				struct net_device *dev, struct sta_info *sta,
4794 				struct ieee80211_key *key, struct sk_buff *skb)
4795 {
4796 	struct ieee80211_tx_info *info;
4797 	struct ieee80211_local *local = sdata->local;
4798 	struct ieee80211_chanctx_conf *chanctx_conf;
4799 	struct tid_ampdu_tx *tid_tx = NULL;
4800 	struct sk_buff *seg, *next;
4801 	unsigned int skbs = 0, len = 0;
4802 	u16 queue;
4803 	u8 tid;
4804 
4805 	queue = ieee80211_select_queue(sdata, sta, skb);
4806 	skb_set_queue_mapping(skb, queue);
4807 
4808 	skb = skb_share_check(skb, GFP_ATOMIC);
4809 	if (unlikely(!skb))
4810 		return;
4811 
4812 	ieee80211_aggr_check(sdata, sta, skb);
4813 
4814 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4815 
4816 	if (sta)
4817 		tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4818 	if (tid_tx) {
4819 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4820 			/* fall back to non-offload slow path */
4821 			__ieee80211_subif_start_xmit(skb, dev, 0,
4822 						     IEEE80211_TX_CTRL_MLO_LINK_UNSPEC);
4823 			return;
4824 		}
4825 
4826 		if (tid_tx->timeout)
4827 			tid_tx->last_tx = jiffies;
4828 	}
4829 
4830 	skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata));
4831 	if (!skb)
4832 		return;
4833 
4834 	info = IEEE80211_SKB_CB(skb);
4835 	memset(info, 0, sizeof(*info));
4836 
4837 	info->hw_queue = sdata->vif.hw_queue[queue];
4838 
4839 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4840 		sdata = container_of(sdata->bss,
4841 				     struct ieee80211_sub_if_data, u.ap);
4842 
4843 	/* MLD transmissions must not rely on the band */
4844 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
4845 		chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
4846 		if (unlikely(!chanctx_conf)) {
4847 			kfree_skb_list(skb);
4848 			return;
4849 		}
4850 		info->band = chanctx_conf->def.chan->band;
4851 	}
4852 
4853 	info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
4854 	info->control.vif = &sdata->vif;
4855 
4856 	if (key)
4857 		info->control.hw_key = &key->conf;
4858 
4859 	skb_list_walk_safe(skb, seg, next) {
4860 		skbs++;
4861 		len += seg->len;
4862 		if (seg != skb)
4863 			memcpy(IEEE80211_SKB_CB(seg), info, sizeof(*info));
4864 	}
4865 
4866 	if (unlikely(sk_requests_wifi_status(skb->sk)))
4867 		ieee80211_store_ack_skb(local, skb, 0);
4868 
4869 	dev_sw_netstats_tx_add(dev, skbs, len);
4870 
4871 	if (sta) {
4872 		sta->deflink.tx_stats.packets[queue] += skbs;
4873 		sta->deflink.tx_stats.bytes[queue] += len;
4874 	}
4875 
4876 	ieee80211_tpt_led_trig_tx(local, len);
4877 
4878 	ieee80211_tx_8023(sdata, skb, sta, false);
4879 }
4880 
4881 static bool ieee80211_check_mcast_offload(struct ieee80211_sub_if_data *sdata,
4882 					  struct sk_buff *skb)
4883 {
4884 	struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4885 
4886 	if ((ieee80211_vif_is_mld(&sdata->vif) &&
4887 	     (sdata->vif.type == NL80211_IFTYPE_AP &&
4888 	      !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX))) ||
4889 	    (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4890 	     !sdata->wdev.use_4addr))
4891 		return false;
4892 
4893 	if (!is_multicast_ether_addr(skb->data) ||
4894 	    !(sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_MCAST) ||
4895 	    sdata->control_port_protocol == ehdr->h_proto)
4896 		return false;
4897 
4898 	return true;
4899 }
4900 
4901 static void __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 	struct ieee80211_link_data *link;
4907 	struct ieee80211_key *key;
4908 	struct sta_info *sta;
4909 
4910 	rcu_read_lock();
4911 
4912 	if (unlikely(sdata->control_port_protocol == ehdr->h_proto))
4913 		goto skip_offload;
4914 
4915 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta, false)) {
4916 		kfree_skb(skb);
4917 		goto out;
4918 	}
4919 
4920 	/*
4921 	 * If STA is invalid, use the multicast offload path when applicable.
4922 	 * In AP mode, drop the frame if there are no associated stations;
4923 	 * otherwise use the default link and multicast key for transmission.
4924 	 */
4925 	if (unlikely(IS_ERR_OR_NULL(sta) &&
4926 		     ieee80211_check_mcast_offload(sdata, skb))) {
4927 		sta = NULL;
4928 		if (ieee80211_vif_get_num_mcast_if(sdata) <= 0) {
4929 			/* No associated STAs - no need to send multicast frames. */
4930 			kfree_skb(skb);
4931 			goto out;
4932 		}
4933 		link = &sdata->deflink;
4934 		key = rcu_dereference(link->default_multicast_key);
4935 	} else if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4936 		   !test_sta_flag(sta, WLAN_STA_AUTHORIZED))) {
4937 		goto skip_offload;
4938 	} else {
4939 		key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4940 		if (!key)
4941 			key = rcu_dereference(sdata->default_unicast_key);
4942 	}
4943 
4944 	if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4945 		    key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4946 		goto skip_offload;
4947 
4948 	sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4949 	ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4950 	goto out;
4951 
4952 skip_offload:
4953 	ieee80211_subif_start_xmit(skb, dev);
4954 out:
4955 	rcu_read_unlock();
4956 }
4957 
4958 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4959 					    struct net_device *dev)
4960 {
4961 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4962 	struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4963 
4964 	if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) {
4965 		kfree_skb(skb);
4966 		return NETDEV_TX_OK;
4967 	}
4968 
4969 	if (unlikely(is_multicast_ether_addr(ehdr->h_dest) &&
4970 		     ieee80211_multicast_to_unicast(skb, dev))) {
4971 		struct sk_buff_head queue;
4972 
4973 		__skb_queue_head_init(&queue);
4974 		ieee80211_convert_to_unicast(skb, dev, &queue);
4975 		while ((skb = __skb_dequeue(&queue)))
4976 			__ieee80211_subif_start_xmit_8023(skb, dev);
4977 	} else {
4978 		__ieee80211_subif_start_xmit_8023(skb, dev);
4979 	}
4980 
4981 	return NETDEV_TX_OK;
4982 }
4983 
4984 struct sk_buff *
4985 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4986 			      struct sk_buff *skb, u32 info_flags)
4987 {
4988 	struct ieee80211_hdr *hdr;
4989 	struct ieee80211_tx_data tx = {
4990 		.local = sdata->local,
4991 		.sdata = sdata,
4992 	};
4993 	struct ieee80211_tx_info *info;
4994 	struct sta_info *sta;
4995 
4996 	rcu_read_lock();
4997 
4998 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta, false)) {
4999 		kfree_skb(skb);
5000 		skb = ERR_PTR(-EINVAL);
5001 		goto out;
5002 	}
5003 
5004 	info = IEEE80211_SKB_CB(skb);
5005 	memset(info, 0, sizeof(*info));
5006 	info->flags = info_flags;
5007 	info->control.flags = IEEE80211_TX_CTRL_MLO_LINK_UNSPEC;
5008 
5009 	skb = ieee80211_build_hdr(sdata, skb, sta);
5010 	if (IS_ERR(skb))
5011 		goto out;
5012 
5013 	hdr = (void *)skb->data;
5014 	tx.sta = sta_info_get(sdata, hdr->addr1);
5015 	tx.skb = skb;
5016 
5017 	if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
5018 		rcu_read_unlock();
5019 		kfree_skb(skb);
5020 		return ERR_PTR(-EINVAL);
5021 	}
5022 
5023 out:
5024 	rcu_read_unlock();
5025 	return skb;
5026 }
5027 
5028 /*
5029  * ieee80211_clear_tx_pending may not be called in a context where
5030  * it is possible that it packets could come in again.
5031  */
5032 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
5033 {
5034 	struct sk_buff *skb;
5035 	int i;
5036 
5037 	for (i = 0; i < local->hw.queues; i++) {
5038 		while ((skb = skb_dequeue(&local->pending[i])) != NULL)
5039 			ieee80211_free_txskb(&local->hw, skb);
5040 	}
5041 }
5042 
5043 /*
5044  * Returns false if the frame couldn't be transmitted but was queued instead,
5045  * which in this case means re-queued -- take as an indication to stop sending
5046  * more pending frames.
5047  */
5048 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
5049 				     struct sk_buff *skb)
5050 {
5051 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5052 	struct ieee80211_sub_if_data *sdata;
5053 	struct sta_info *sta;
5054 	struct ieee80211_hdr *hdr;
5055 	bool result;
5056 	struct ieee80211_chanctx_conf *chanctx_conf;
5057 
5058 	sdata = vif_to_sdata(info->control.vif);
5059 
5060 	if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
5061 		/* update band only for non-MLD */
5062 		if (!ieee80211_vif_is_mld(&sdata->vif)) {
5063 			chanctx_conf =
5064 				rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
5065 			if (unlikely(!chanctx_conf)) {
5066 				dev_kfree_skb(skb);
5067 				return true;
5068 			}
5069 			info->band = chanctx_conf->def.chan->band;
5070 		}
5071 		result = ieee80211_tx(sdata, NULL, skb, true);
5072 	} else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
5073 		if (ieee80211_lookup_ra_sta(sdata, skb, &sta, true)) {
5074 			dev_kfree_skb(skb);
5075 			return true;
5076 		}
5077 
5078 		if (IS_ERR(sta) || (sta && !sta->uploaded))
5079 			sta = NULL;
5080 
5081 		result = ieee80211_tx_8023(sdata, skb, sta, true);
5082 	} else {
5083 		struct sk_buff_head skbs;
5084 
5085 		__skb_queue_head_init(&skbs);
5086 		__skb_queue_tail(&skbs, skb);
5087 
5088 		hdr = (struct ieee80211_hdr *)skb->data;
5089 		sta = sta_info_get(sdata, hdr->addr1);
5090 
5091 		result = __ieee80211_tx(local, &skbs, sta, true);
5092 	}
5093 
5094 	return result;
5095 }
5096 
5097 /*
5098  * Transmit all pending packets. Called from tasklet.
5099  */
5100 void ieee80211_tx_pending(struct tasklet_struct *t)
5101 {
5102 	struct ieee80211_local *local = from_tasklet(local, t,
5103 						     tx_pending_tasklet);
5104 	unsigned long flags;
5105 	int i;
5106 	bool txok;
5107 
5108 	rcu_read_lock();
5109 
5110 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
5111 	for (i = 0; i < local->hw.queues; i++) {
5112 		/*
5113 		 * If queue is stopped by something other than due to pending
5114 		 * frames, or we have no pending frames, proceed to next queue.
5115 		 */
5116 		if (local->queue_stop_reasons[i] ||
5117 		    skb_queue_empty(&local->pending[i]))
5118 			continue;
5119 
5120 		while (!skb_queue_empty(&local->pending[i])) {
5121 			struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
5122 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5123 
5124 			if (WARN_ON(!info->control.vif)) {
5125 				ieee80211_free_txskb(&local->hw, skb);
5126 				continue;
5127 			}
5128 
5129 			spin_unlock_irqrestore(&local->queue_stop_reason_lock,
5130 						flags);
5131 
5132 			txok = ieee80211_tx_pending_skb(local, skb);
5133 			spin_lock_irqsave(&local->queue_stop_reason_lock,
5134 					  flags);
5135 			if (!txok)
5136 				break;
5137 		}
5138 	}
5139 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
5140 
5141 	rcu_read_unlock();
5142 }
5143 
5144 /* functions for drivers to get certain frames */
5145 
5146 static void ieee80211_beacon_add_tim_pvb(struct ps_data *ps,
5147 					 struct sk_buff *skb,
5148 					 bool mcast_traffic)
5149 {
5150 	int i, n1 = 0, n2;
5151 
5152 	/*
5153 	 * Find largest even number N1 so that bits numbered 1 through
5154 	 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
5155 	 * (N2 + 1) x 8 through 2007 are 0.
5156 	 */
5157 	for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
5158 		if (ps->tim[i]) {
5159 			n1 = i & 0xfe;
5160 			break;
5161 		}
5162 	}
5163 	n2 = n1;
5164 	for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
5165 		if (ps->tim[i]) {
5166 			n2 = i;
5167 			break;
5168 		}
5169 	}
5170 
5171 	/* Bitmap control */
5172 	skb_put_u8(skb, n1 | mcast_traffic);
5173 	/* Part Virt Bitmap */
5174 	skb_put_data(skb, ps->tim + n1, n2 - n1 + 1);
5175 }
5176 
5177 /*
5178  * mac80211 currently supports encoding using block bitmap mode, non
5179  * inversed. The current implementation supports up to 1600 AIDs.
5180  *
5181  * Block bitmap encoding breaks down the AID bitmap into blocks of 64
5182  * AIDs. Each block contains between 0 and 8 subblocks. Each subblock
5183  * describes 8 AIDs and the presence of a subblock is determined by
5184  * the block bitmap.
5185  */
5186 static void ieee80211_s1g_beacon_add_tim_pvb(struct ps_data *ps,
5187 					     struct sk_buff *skb,
5188 					     bool mcast_traffic,
5189 					     bool ucast_traffic)
5190 {
5191 	int blk;
5192 
5193 	/*
5194 	 * if no unicast and multicast traffic don't emit a bitmap control
5195 	 * or pvb
5196 	 */
5197 	if (!mcast_traffic && !ucast_traffic)
5198 		return;
5199 
5200 	/*
5201 	 * Emit a bitmap control block with a page slice number of 31 and a
5202 	 * page index of 0 which indicates as per IEEE80211-2024 9.4.2.5.1
5203 	 * that the entire page (2048 bits) indicated by the page index
5204 	 * is encoded in the partial virtual bitmap.
5205 	 */
5206 	skb_put_u8(skb, mcast_traffic | (31 << 1));
5207 
5208 	/* If there's no unicast traffic we don't need to include a PVB. */
5209 	if (!ucast_traffic)
5210 		return;
5211 
5212 	/* Emit an encoded block for each non-zero sub-block */
5213 	for (blk = 0; blk < IEEE80211_MAX_SUPPORTED_S1G_TIM_BLOCKS; blk++) {
5214 		u8 blk_bmap = 0;
5215 		int sblk;
5216 
5217 		for (sblk = 0; sblk < 8; sblk++) {
5218 			int sblk_idx = blk * 8 + sblk;
5219 
5220 			/*
5221 			 * If the current subblock is non-zero, increase the
5222 			 * number of subblocks to emit for the current block.
5223 			 */
5224 			if (ps->tim[sblk_idx])
5225 				blk_bmap |= BIT(sblk);
5226 		}
5227 
5228 		/* If the current block contains no non-zero sublocks */
5229 		if (!blk_bmap)
5230 			continue;
5231 
5232 		/*
5233 		 * Emit a block control byte for the current encoded block
5234 		 * with an encoding mode of block bitmap (0x0), not inverse
5235 		 * (0x0) and the current block offset (5 bits)
5236 		 */
5237 		skb_put_u8(skb, blk << 3);
5238 
5239 		/*
5240 		 * Emit the block bitmap for the current encoded block which
5241 		 * contains the present subblocks.
5242 		 */
5243 		skb_put_u8(skb, blk_bmap);
5244 
5245 		/* Emit the present subblocks */
5246 		for (sblk = 0; sblk < 8; sblk++) {
5247 			int sblk_idx = blk * 8 + sblk;
5248 
5249 			if (!(blk_bmap & BIT(sblk)))
5250 				continue;
5251 
5252 			skb_put_u8(skb, ps->tim[sblk_idx]);
5253 		}
5254 	}
5255 }
5256 
5257 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
5258 				       struct ieee80211_link_data *link,
5259 				       struct ps_data *ps, struct sk_buff *skb,
5260 				       bool is_template)
5261 {
5262 	struct element *tim;
5263 	bool mcast_traffic = false, have_bits = false;
5264 	struct ieee80211_bss_conf *link_conf = link->conf;
5265 	bool s1g = ieee80211_get_link_sband(link)->band == NL80211_BAND_S1GHZ;
5266 
5267 	/* Generate bitmap for TIM only if there are any STAs in power save
5268 	 * mode. */
5269 	if (atomic_read(&ps->num_sta_ps) > 0)
5270 		/* in the hope that this is faster than
5271 		 * checking byte-for-byte */
5272 		have_bits = !bitmap_empty((unsigned long *)ps->tim,
5273 					  IEEE80211_MAX_AID + 1);
5274 
5275 	if (!is_template) {
5276 		if (ps->dtim_count == 0)
5277 			ps->dtim_count = link_conf->dtim_period - 1;
5278 		else
5279 			ps->dtim_count--;
5280 	}
5281 
5282 	/* Length is set after parsing the AID bitmap */
5283 	tim = skb_put(skb, sizeof(struct element));
5284 	tim->id = WLAN_EID_TIM;
5285 	skb_put_u8(skb, ps->dtim_count);
5286 	skb_put_u8(skb, link_conf->dtim_period);
5287 
5288 	if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
5289 		mcast_traffic = true;
5290 
5291 	ps->dtim_bc_mc = mcast_traffic;
5292 
5293 	if (s1g) {
5294 		ieee80211_s1g_beacon_add_tim_pvb(ps, skb, mcast_traffic,
5295 						 have_bits);
5296 	} else if (have_bits) {
5297 		ieee80211_beacon_add_tim_pvb(ps, skb, mcast_traffic);
5298 	} else {
5299 		/* Bitmap control */
5300 		skb_put_u8(skb, mcast_traffic);
5301 		/* Part Virt Bitmap */
5302 		skb_put_u8(skb, 0);
5303 	}
5304 
5305 	tim->datalen = skb_tail_pointer(skb) - tim->data;
5306 }
5307 
5308 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
5309 				    struct ieee80211_link_data *link,
5310 				    struct ps_data *ps, struct sk_buff *skb,
5311 				    bool is_template)
5312 {
5313 	struct ieee80211_local *local = sdata->local;
5314 
5315 	/*
5316 	 * Not very nice, but we want to allow the driver to call
5317 	 * ieee80211_beacon_get() as a response to the set_tim()
5318 	 * callback. That, however, is already invoked under the
5319 	 * sta_lock to guarantee consistent and race-free update
5320 	 * of the tim bitmap in mac80211 and the driver.
5321 	 */
5322 	if (local->tim_in_locked_section) {
5323 		__ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
5324 	} else {
5325 		spin_lock_bh(&local->tim_lock);
5326 		__ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
5327 		spin_unlock_bh(&local->tim_lock);
5328 	}
5329 
5330 	return 0;
5331 }
5332 
5333 static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
5334 					struct beacon_data *beacon,
5335 					struct ieee80211_link_data *link)
5336 {
5337 	u8 *beacon_data, count, max_count = 1;
5338 	struct probe_resp *resp;
5339 	size_t beacon_data_len;
5340 	u16 *bcn_offsets;
5341 	int i;
5342 
5343 	switch (sdata->vif.type) {
5344 	case NL80211_IFTYPE_AP:
5345 		beacon_data = beacon->tail;
5346 		beacon_data_len = beacon->tail_len;
5347 		break;
5348 	case NL80211_IFTYPE_ADHOC:
5349 		beacon_data = beacon->head;
5350 		beacon_data_len = beacon->head_len;
5351 		break;
5352 	case NL80211_IFTYPE_MESH_POINT:
5353 		beacon_data = beacon->head;
5354 		beacon_data_len = beacon->head_len;
5355 		break;
5356 	default:
5357 		return;
5358 	}
5359 
5360 	resp = rcu_dereference(link->u.ap.probe_resp);
5361 
5362 	bcn_offsets = beacon->cntdwn_counter_offsets;
5363 	count = beacon->cntdwn_current_counter;
5364 	if (link->conf->csa_active)
5365 		max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
5366 
5367 	for (i = 0; i < max_count; ++i) {
5368 		if (bcn_offsets[i]) {
5369 			if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len))
5370 				return;
5371 			beacon_data[bcn_offsets[i]] = count;
5372 		}
5373 
5374 		if (sdata->vif.type == NL80211_IFTYPE_AP && resp) {
5375 			u16 *resp_offsets = resp->cntdwn_counter_offsets;
5376 
5377 			if (resp_offsets[i])
5378 				resp->data[resp_offsets[i]] = count;
5379 		}
5380 	}
5381 }
5382 
5383 static u8 __ieee80211_beacon_update_cntdwn(struct ieee80211_link_data *link,
5384 					   struct beacon_data *beacon)
5385 {
5386 	if (beacon->cntdwn_current_counter == 1) {
5387 		/*
5388 		 * Channel switch handling is done by a worker thread while
5389 		 * beacons get pulled from hardware timers. It's therefore
5390 		 * possible that software threads are slow enough to not be
5391 		 * able to complete CSA handling in a single beacon interval,
5392 		 * in which case we get here. There isn't much to do about
5393 		 * it, other than letting the user know that the AP isn't
5394 		 * behaving correctly.
5395 		 */
5396 		link_err_once(link,
5397 			      "beacon TX faster than countdown (channel/color switch) completion\n");
5398 		return 0;
5399 	}
5400 
5401 	beacon->cntdwn_current_counter--;
5402 
5403 	return beacon->cntdwn_current_counter;
5404 }
5405 
5406 u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif, unsigned int link_id)
5407 {
5408 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5409 	struct ieee80211_link_data *link;
5410 	struct beacon_data *beacon = NULL;
5411 	u8 count = 0;
5412 
5413 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
5414 		return 0;
5415 
5416 	rcu_read_lock();
5417 
5418 	link = rcu_dereference(sdata->link[link_id]);
5419 	if (!link)
5420 		goto unlock;
5421 
5422 	if (sdata->vif.type == NL80211_IFTYPE_AP)
5423 		beacon = rcu_dereference(link->u.ap.beacon);
5424 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
5425 		beacon = rcu_dereference(sdata->u.ibss.presp);
5426 	else if (ieee80211_vif_is_mesh(&sdata->vif))
5427 		beacon = rcu_dereference(sdata->u.mesh.beacon);
5428 
5429 	if (!beacon)
5430 		goto unlock;
5431 
5432 	count = __ieee80211_beacon_update_cntdwn(link, beacon);
5433 
5434 unlock:
5435 	rcu_read_unlock();
5436 	return count;
5437 }
5438 EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
5439 
5440 void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
5441 {
5442 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5443 	struct beacon_data *beacon = NULL;
5444 
5445 	rcu_read_lock();
5446 
5447 	if (sdata->vif.type == NL80211_IFTYPE_AP)
5448 		beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
5449 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
5450 		beacon = rcu_dereference(sdata->u.ibss.presp);
5451 	else if (ieee80211_vif_is_mesh(&sdata->vif))
5452 		beacon = rcu_dereference(sdata->u.mesh.beacon);
5453 
5454 	if (!beacon)
5455 		goto unlock;
5456 
5457 	if (counter < beacon->cntdwn_current_counter)
5458 		beacon->cntdwn_current_counter = counter;
5459 
5460 unlock:
5461 	rcu_read_unlock();
5462 }
5463 EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
5464 
5465 bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif,
5466 					 unsigned int link_id)
5467 {
5468 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5469 	struct ieee80211_link_data *link;
5470 	struct beacon_data *beacon = NULL;
5471 	u8 *beacon_data;
5472 	size_t beacon_data_len;
5473 	int ret = false;
5474 
5475 	if (!ieee80211_sdata_running(sdata))
5476 		return false;
5477 
5478 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
5479 		return 0;
5480 
5481 	rcu_read_lock();
5482 
5483 	link = rcu_dereference(sdata->link[link_id]);
5484 	if (!link)
5485 		goto out;
5486 
5487 	if (vif->type == NL80211_IFTYPE_AP) {
5488 		beacon = rcu_dereference(link->u.ap.beacon);
5489 		if (WARN_ON(!beacon || !beacon->tail))
5490 			goto out;
5491 		beacon_data = beacon->tail;
5492 		beacon_data_len = beacon->tail_len;
5493 	} else if (vif->type == NL80211_IFTYPE_ADHOC) {
5494 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5495 
5496 		beacon = rcu_dereference(ifibss->presp);
5497 		if (!beacon)
5498 			goto out;
5499 
5500 		beacon_data = beacon->head;
5501 		beacon_data_len = beacon->head_len;
5502 	} else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
5503 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
5504 
5505 		beacon = rcu_dereference(ifmsh->beacon);
5506 		if (!beacon)
5507 			goto out;
5508 
5509 		beacon_data = beacon->head;
5510 		beacon_data_len = beacon->head_len;
5511 	} else {
5512 		WARN_ON(1);
5513 		goto out;
5514 	}
5515 
5516 	if (!beacon->cntdwn_counter_offsets[0])
5517 		goto out;
5518 
5519 	if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
5520 		goto out;
5521 
5522 	if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
5523 		ret = true;
5524 
5525  out:
5526 	rcu_read_unlock();
5527 
5528 	return ret;
5529 }
5530 EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
5531 
5532 static int ieee80211_beacon_protect(struct sk_buff *skb,
5533 				    struct ieee80211_local *local,
5534 				    struct ieee80211_sub_if_data *sdata,
5535 				    struct ieee80211_link_data *link)
5536 {
5537 	ieee80211_tx_result res;
5538 	struct ieee80211_tx_data tx;
5539 	struct sk_buff *check_skb;
5540 
5541 	memset(&tx, 0, sizeof(tx));
5542 	tx.key = rcu_dereference(link->default_beacon_key);
5543 	if (!tx.key)
5544 		return 0;
5545 
5546 	if (unlikely(tx.key->flags & KEY_FLAG_TAINTED)) {
5547 		tx.key = NULL;
5548 		return -EINVAL;
5549 	}
5550 
5551 	if (!(tx.key->conf.flags & IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
5552 	    tx.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
5553 		IEEE80211_SKB_CB(skb)->control.hw_key = &tx.key->conf;
5554 
5555 	tx.local = local;
5556 	tx.sdata = sdata;
5557 	__skb_queue_head_init(&tx.skbs);
5558 	__skb_queue_tail(&tx.skbs, skb);
5559 	res = ieee80211_tx_h_encrypt(&tx);
5560 	check_skb = __skb_dequeue(&tx.skbs);
5561 	/* we may crash after this, but it'd be a bug in crypto */
5562 	WARN_ON(check_skb != skb);
5563 	if (WARN_ON_ONCE(res != TX_CONTINUE))
5564 		return -EINVAL;
5565 
5566 	return 0;
5567 }
5568 
5569 int ieee80211_encrypt_tx_skb(struct sk_buff *skb)
5570 {
5571 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5572 	struct ieee80211_sub_if_data *sdata = NULL;
5573 	struct sk_buff *check_skb;
5574 	struct ieee80211_tx_data tx;
5575 	ieee80211_tx_result res;
5576 
5577 	if (!info->control.hw_key)
5578 		return 0;
5579 
5580 	memset(&tx, 0, sizeof(tx));
5581 	tx.key = container_of(info->control.hw_key, struct ieee80211_key, conf);
5582 	/* NULL it out now so we do full SW crypto */
5583 	info->control.hw_key = NULL;
5584 	__skb_queue_head_init(&tx.skbs);
5585 	__skb_queue_tail(&tx.skbs, skb);
5586 
5587 	if (skb->dev)
5588 		sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
5589 	else if (info->control.vif)
5590 		sdata = vif_to_sdata(info->control.vif);
5591 
5592 	if (WARN_ON(!sdata))
5593 		return -EINVAL;
5594 
5595 	tx.sdata = sdata;
5596 	tx.local = sdata->local;
5597 	res = ieee80211_tx_h_encrypt(&tx);
5598 	check_skb = __skb_dequeue(&tx.skbs);
5599 	/* we may crash after this, but it'd be a bug in crypto */
5600 	WARN_ON(check_skb != skb);
5601 	if (WARN_ON_ONCE(res != TX_CONTINUE))
5602 		return -EINVAL;
5603 
5604 	return 0;
5605 }
5606 EXPORT_SYMBOL_GPL(ieee80211_encrypt_tx_skb);
5607 
5608 static void
5609 ieee80211_beacon_get_finish(struct ieee80211_hw *hw,
5610 			    struct ieee80211_vif *vif,
5611 			    struct ieee80211_link_data *link,
5612 			    struct ieee80211_mutable_offsets *offs,
5613 			    struct beacon_data *beacon,
5614 			    struct sk_buff *skb,
5615 			    struct ieee80211_chanctx_conf *chanctx_conf,
5616 			    u16 csa_off_base)
5617 {
5618 	struct ieee80211_local *local = hw_to_local(hw);
5619 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5620 	struct ieee80211_tx_info *info;
5621 	enum nl80211_band band;
5622 	struct ieee80211_tx_rate_control txrc;
5623 
5624 	/* CSA offsets */
5625 	if (offs && beacon) {
5626 		u16 i;
5627 
5628 		for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
5629 			u16 csa_off = beacon->cntdwn_counter_offsets[i];
5630 
5631 			if (!csa_off)
5632 				continue;
5633 
5634 			offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
5635 		}
5636 	}
5637 
5638 	band = chanctx_conf->def.chan->band;
5639 	info = IEEE80211_SKB_CB(skb);
5640 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5641 	info->flags |= IEEE80211_TX_CTL_NO_ACK;
5642 	info->band = band;
5643 
5644 	memset(&txrc, 0, sizeof(txrc));
5645 	txrc.hw = hw;
5646 	txrc.sband = local->hw.wiphy->bands[band];
5647 	txrc.bss_conf = link->conf;
5648 	txrc.skb = skb;
5649 	txrc.reported_rate.idx = -1;
5650 	if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
5651 		txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
5652 	else
5653 		txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
5654 	txrc.bss = true;
5655 	rate_control_get_rate(sdata, NULL, &txrc);
5656 
5657 	info->control.vif = vif;
5658 	info->control.flags |= u32_encode_bits(link->link_id,
5659 					       IEEE80211_TX_CTRL_MLO_LINK);
5660 	info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
5661 		       IEEE80211_TX_CTL_ASSIGN_SEQ |
5662 		       IEEE80211_TX_CTL_FIRST_FRAGMENT;
5663 }
5664 
5665 static void
5666 ieee80211_beacon_add_mbssid(struct sk_buff *skb, struct beacon_data *beacon,
5667 			    u8 i)
5668 {
5669 	if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt ||
5670 	    i > beacon->mbssid_ies->cnt)
5671 		return;
5672 
5673 	if (i < beacon->mbssid_ies->cnt) {
5674 		skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
5675 			     beacon->mbssid_ies->elem[i].len);
5676 
5677 		if (beacon->rnr_ies && beacon->rnr_ies->cnt) {
5678 			skb_put_data(skb, beacon->rnr_ies->elem[i].data,
5679 				     beacon->rnr_ies->elem[i].len);
5680 
5681 			for (i = beacon->mbssid_ies->cnt; i < beacon->rnr_ies->cnt; i++)
5682 				skb_put_data(skb, beacon->rnr_ies->elem[i].data,
5683 					     beacon->rnr_ies->elem[i].len);
5684 		}
5685 		return;
5686 	}
5687 
5688 	/* i == beacon->mbssid_ies->cnt, include all MBSSID elements */
5689 	for (i = 0; i < beacon->mbssid_ies->cnt; i++)
5690 		skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
5691 			     beacon->mbssid_ies->elem[i].len);
5692 }
5693 
5694 static struct sk_buff *
5695 __ieee80211_beacon_get_ap(struct ieee80211_hw *hw,
5696 			  struct ieee80211_vif *vif,
5697 			  struct ieee80211_link_data *link,
5698 			  struct ieee80211_mutable_offsets *offs,
5699 			  bool is_template,
5700 			  struct beacon_data *beacon,
5701 			  struct ieee80211_chanctx_conf *chanctx_conf,
5702 			  u8 ema_index)
5703 {
5704 	struct ieee80211_local *local = hw_to_local(hw);
5705 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5706 	struct ieee80211_if_ap *ap = &sdata->u.ap;
5707 	struct sk_buff *skb = NULL;
5708 	u16 csa_off_base = 0;
5709 	int mbssid_len;
5710 
5711 	if (beacon->cntdwn_counter_offsets[0]) {
5712 		if (!is_template)
5713 			ieee80211_beacon_update_cntdwn(vif, link->link_id);
5714 
5715 		ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5716 	}
5717 
5718 	/* headroom, head length,
5719 	 * tail length, maximum TIM length and multiple BSSID length
5720 	 */
5721 	mbssid_len = ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies,
5722 						     beacon->rnr_ies,
5723 						     ema_index);
5724 
5725 	skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5726 			    beacon->tail_len + 256 +
5727 			    local->hw.extra_beacon_tailroom + mbssid_len +
5728 			    IEEE80211_ENCRYPT_TAILROOM);
5729 	if (!skb)
5730 		return NULL;
5731 
5732 	skb_reserve(skb, local->tx_headroom);
5733 	skb_put_data(skb, beacon->head, beacon->head_len);
5734 
5735 	ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template);
5736 
5737 	if (offs) {
5738 		offs->tim_offset = beacon->head_len;
5739 		offs->tim_length = skb->len - beacon->head_len;
5740 		offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0];
5741 
5742 		if (mbssid_len) {
5743 			ieee80211_beacon_add_mbssid(skb, beacon, ema_index);
5744 			offs->mbssid_off = skb->len - mbssid_len;
5745 		}
5746 
5747 		/* for AP the csa offsets are from tail */
5748 		csa_off_base = skb->len;
5749 	}
5750 
5751 	if (beacon->tail)
5752 		skb_put_data(skb, beacon->tail, beacon->tail_len);
5753 
5754 	if (ieee80211_beacon_protect(skb, local, sdata, link) < 0) {
5755 		dev_kfree_skb(skb);
5756 		return NULL;
5757 	}
5758 
5759 	ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5760 				    chanctx_conf, csa_off_base);
5761 	return skb;
5762 }
5763 
5764 static bool ieee80211_s1g_need_long_beacon(struct ieee80211_sub_if_data *sdata,
5765 					   struct ieee80211_link_data *link)
5766 {
5767 	struct ps_data *ps = &sdata->u.ap.ps;
5768 
5769 	if (ps->sb_count == 0)
5770 		ps->sb_count = link->conf->s1g_long_beacon_period - 1;
5771 	else
5772 		ps->sb_count--;
5773 
5774 	return ps->sb_count == 0;
5775 }
5776 
5777 static struct sk_buff *
5778 ieee80211_s1g_short_beacon_get(struct ieee80211_hw *hw,
5779 			       struct ieee80211_vif *vif,
5780 			       struct ieee80211_link_data *link,
5781 			       struct ieee80211_chanctx_conf *chanctx_conf,
5782 			       struct s1g_short_beacon_data *sb,
5783 			       bool is_template)
5784 {
5785 	struct ieee80211_local *local = hw_to_local(hw);
5786 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5787 	struct ieee80211_if_ap *ap = &sdata->u.ap;
5788 	struct sk_buff *skb;
5789 
5790 	skb = dev_alloc_skb(local->tx_headroom + sb->short_head_len +
5791 			    sb->short_tail_len + 256 +
5792 			    local->hw.extra_beacon_tailroom);
5793 	if (!skb)
5794 		return NULL;
5795 
5796 	skb_reserve(skb, local->tx_headroom);
5797 	skb_put_data(skb, sb->short_head, sb->short_head_len);
5798 
5799 	ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template);
5800 
5801 	if (sb->short_tail)
5802 		skb_put_data(skb, sb->short_tail, sb->short_tail_len);
5803 
5804 	ieee80211_beacon_get_finish(hw, vif, link, NULL, NULL, skb,
5805 				    chanctx_conf, 0);
5806 	return skb;
5807 }
5808 
5809 static struct sk_buff *
5810 ieee80211_beacon_get_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5811 			struct ieee80211_link_data *link,
5812 			struct ieee80211_mutable_offsets *offs,
5813 			bool is_template, struct beacon_data *beacon,
5814 			struct ieee80211_chanctx_conf *chanctx_conf,
5815 			u8 ema_index, struct s1g_short_beacon_data *s1g_sb)
5816 {
5817 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5818 
5819 	if (!sdata->vif.cfg.s1g || !s1g_sb ||
5820 	    ieee80211_s1g_need_long_beacon(sdata, link))
5821 		return __ieee80211_beacon_get_ap(hw, vif, link, offs,
5822 						 is_template, beacon,
5823 						 chanctx_conf, ema_index);
5824 
5825 	return ieee80211_s1g_short_beacon_get(hw, vif, link, chanctx_conf,
5826 					      s1g_sb, is_template);
5827 }
5828 
5829 static struct ieee80211_ema_beacons *
5830 ieee80211_beacon_get_ap_ema_list(struct ieee80211_hw *hw,
5831 				 struct ieee80211_vif *vif,
5832 				 struct ieee80211_link_data *link,
5833 				 struct ieee80211_mutable_offsets *offs,
5834 				 bool is_template, struct beacon_data *beacon,
5835 				 struct ieee80211_chanctx_conf *chanctx_conf)
5836 {
5837 	struct ieee80211_ema_beacons *ema = NULL;
5838 
5839 	if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt)
5840 		return NULL;
5841 
5842 	ema = kzalloc_flex(*ema, bcn, beacon->mbssid_ies->cnt, GFP_ATOMIC);
5843 	if (!ema)
5844 		return NULL;
5845 
5846 	for (ema->cnt = 0; ema->cnt < beacon->mbssid_ies->cnt; ema->cnt++) {
5847 		ema->bcn[ema->cnt].skb =
5848 			ieee80211_beacon_get_ap(hw, vif, link,
5849 						&ema->bcn[ema->cnt].offs,
5850 						is_template, beacon,
5851 						chanctx_conf, ema->cnt, NULL);
5852 		if (!ema->bcn[ema->cnt].skb)
5853 			break;
5854 	}
5855 
5856 	if (ema->cnt == beacon->mbssid_ies->cnt)
5857 		return ema;
5858 
5859 	ieee80211_beacon_free_ema_list(ema);
5860 	return NULL;
5861 }
5862 
5863 #define IEEE80211_INCLUDE_ALL_MBSSID_ELEMS -1
5864 
5865 static struct sk_buff *
5866 __ieee80211_beacon_get(struct ieee80211_hw *hw,
5867 		       struct ieee80211_vif *vif,
5868 		       struct ieee80211_mutable_offsets *offs,
5869 		       bool is_template,
5870 		       unsigned int link_id,
5871 		       int ema_index,
5872 		       struct ieee80211_ema_beacons **ema_beacons)
5873 {
5874 	struct ieee80211_local *local = hw_to_local(hw);
5875 	struct beacon_data *beacon = NULL;
5876 	struct sk_buff *skb = NULL;
5877 	struct ieee80211_sub_if_data *sdata = NULL;
5878 	struct ieee80211_chanctx_conf *chanctx_conf;
5879 	struct ieee80211_link_data *link;
5880 	struct s1g_short_beacon_data *s1g_short_bcn = NULL;
5881 
5882 	rcu_read_lock();
5883 
5884 	sdata = vif_to_sdata(vif);
5885 	link = rcu_dereference(sdata->link[link_id]);
5886 	if (!link)
5887 		goto out;
5888 	chanctx_conf =
5889 		rcu_dereference(link->conf->chanctx_conf);
5890 
5891 	if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
5892 		goto out;
5893 
5894 	if (offs)
5895 		memset(offs, 0, sizeof(*offs));
5896 
5897 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
5898 		beacon = rcu_dereference(link->u.ap.beacon);
5899 		if (!beacon)
5900 			goto out;
5901 
5902 		if (vif->cfg.s1g && link->u.ap.s1g_short_beacon) {
5903 			s1g_short_bcn =
5904 				rcu_dereference(link->u.ap.s1g_short_beacon);
5905 			if (!s1g_short_bcn)
5906 				goto out;
5907 		}
5908 
5909 		if (ema_beacons) {
5910 			*ema_beacons =
5911 				ieee80211_beacon_get_ap_ema_list(hw, vif, link,
5912 								 offs,
5913 								 is_template,
5914 								 beacon,
5915 								 chanctx_conf);
5916 		} else {
5917 			if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) {
5918 				if (ema_index >= beacon->mbssid_ies->cnt)
5919 					goto out; /* End of MBSSID elements */
5920 
5921 				if (ema_index <= IEEE80211_INCLUDE_ALL_MBSSID_ELEMS)
5922 					ema_index = beacon->mbssid_ies->cnt;
5923 			} else {
5924 				ema_index = 0;
5925 			}
5926 
5927 			skb = ieee80211_beacon_get_ap(hw, vif, link, offs,
5928 						      is_template, beacon,
5929 						      chanctx_conf, ema_index,
5930 						      s1g_short_bcn);
5931 		}
5932 	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
5933 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5934 		struct ieee80211_hdr *hdr;
5935 
5936 		beacon = rcu_dereference(ifibss->presp);
5937 		if (!beacon)
5938 			goto out;
5939 
5940 		if (beacon->cntdwn_counter_offsets[0]) {
5941 			if (!is_template)
5942 				__ieee80211_beacon_update_cntdwn(link, beacon);
5943 
5944 			ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5945 		}
5946 
5947 		skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5948 				    local->hw.extra_beacon_tailroom);
5949 		if (!skb)
5950 			goto out;
5951 		skb_reserve(skb, local->tx_headroom);
5952 		skb_put_data(skb, beacon->head, beacon->head_len);
5953 
5954 		hdr = (struct ieee80211_hdr *) skb->data;
5955 		hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5956 						 IEEE80211_STYPE_BEACON);
5957 
5958 		ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5959 					    chanctx_conf, 0);
5960 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5961 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
5962 
5963 		beacon = rcu_dereference(ifmsh->beacon);
5964 		if (!beacon)
5965 			goto out;
5966 
5967 		if (beacon->cntdwn_counter_offsets[0]) {
5968 			if (!is_template)
5969 				/* TODO: For mesh csa_counter is in TU, so
5970 				 * decrementing it by one isn't correct, but
5971 				 * for now we leave it consistent with overall
5972 				 * mac80211's behavior.
5973 				 */
5974 				__ieee80211_beacon_update_cntdwn(link, beacon);
5975 
5976 			ieee80211_set_beacon_cntdwn(sdata, beacon, link);
5977 		}
5978 
5979 		if (ifmsh->sync_ops)
5980 			ifmsh->sync_ops->adjust_tsf(sdata, beacon);
5981 
5982 		skb = dev_alloc_skb(local->tx_headroom +
5983 				    beacon->head_len +
5984 				    256 + /* TIM IE */
5985 				    beacon->tail_len +
5986 				    local->hw.extra_beacon_tailroom);
5987 		if (!skb)
5988 			goto out;
5989 		skb_reserve(skb, local->tx_headroom);
5990 		skb_put_data(skb, beacon->head, beacon->head_len);
5991 		ieee80211_beacon_add_tim(sdata, link, &ifmsh->ps, skb,
5992 					 is_template);
5993 
5994 		if (offs) {
5995 			offs->tim_offset = beacon->head_len;
5996 			offs->tim_length = skb->len - beacon->head_len;
5997 		}
5998 
5999 		skb_put_data(skb, beacon->tail, beacon->tail_len);
6000 		ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
6001 					    chanctx_conf, 0);
6002 	} else {
6003 		WARN_ON(1);
6004 		goto out;
6005 	}
6006 
6007  out:
6008 	rcu_read_unlock();
6009 	return skb;
6010 
6011 }
6012 
6013 struct sk_buff *
6014 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
6015 			      struct ieee80211_vif *vif,
6016 			      struct ieee80211_mutable_offsets *offs,
6017 			      unsigned int link_id)
6018 {
6019 	return __ieee80211_beacon_get(hw, vif, offs, true, link_id,
6020 				      IEEE80211_INCLUDE_ALL_MBSSID_ELEMS, NULL);
6021 }
6022 EXPORT_SYMBOL(ieee80211_beacon_get_template);
6023 
6024 struct sk_buff *
6025 ieee80211_beacon_get_template_ema_index(struct ieee80211_hw *hw,
6026 					struct ieee80211_vif *vif,
6027 					struct ieee80211_mutable_offsets *offs,
6028 					unsigned int link_id, u8 ema_index)
6029 {
6030 	return __ieee80211_beacon_get(hw, vif, offs, true, link_id, ema_index,
6031 				      NULL);
6032 }
6033 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_index);
6034 
6035 void ieee80211_beacon_free_ema_list(struct ieee80211_ema_beacons *ema_beacons)
6036 {
6037 	u8 i;
6038 
6039 	if (!ema_beacons)
6040 		return;
6041 
6042 	for (i = 0; i < ema_beacons->cnt; i++)
6043 		kfree_skb(ema_beacons->bcn[i].skb);
6044 
6045 	kfree(ema_beacons);
6046 }
6047 EXPORT_SYMBOL(ieee80211_beacon_free_ema_list);
6048 
6049 struct ieee80211_ema_beacons *
6050 ieee80211_beacon_get_template_ema_list(struct ieee80211_hw *hw,
6051 				       struct ieee80211_vif *vif,
6052 				       unsigned int link_id)
6053 {
6054 	struct ieee80211_ema_beacons *ema_beacons = NULL;
6055 
6056 	WARN_ON(__ieee80211_beacon_get(hw, vif, NULL, true, link_id, 0,
6057 				       &ema_beacons));
6058 
6059 	return ema_beacons;
6060 }
6061 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_list);
6062 
6063 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
6064 					 struct ieee80211_vif *vif,
6065 					 u16 *tim_offset, u16 *tim_length,
6066 					 unsigned int link_id)
6067 {
6068 	struct ieee80211_mutable_offsets offs = {};
6069 	struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false,
6070 						     link_id,
6071 						     IEEE80211_INCLUDE_ALL_MBSSID_ELEMS,
6072 						     NULL);
6073 	struct sk_buff *copy;
6074 
6075 	if (!bcn)
6076 		return bcn;
6077 
6078 	if (tim_offset)
6079 		*tim_offset = offs.tim_offset;
6080 
6081 	if (tim_length)
6082 		*tim_length = offs.tim_length;
6083 
6084 	if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
6085 	    !hw_to_local(hw)->monitors)
6086 		return bcn;
6087 
6088 	/* send a copy to monitor interfaces */
6089 	copy = skb_copy(bcn, GFP_ATOMIC);
6090 	if (!copy)
6091 		return bcn;
6092 
6093 	ieee80211_tx_monitor(hw_to_local(hw), copy, 1, NULL);
6094 
6095 	return bcn;
6096 }
6097 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
6098 
6099 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
6100 					struct ieee80211_vif *vif)
6101 {
6102 	struct sk_buff *skb = NULL;
6103 	struct probe_resp *presp = NULL;
6104 	struct ieee80211_hdr *hdr;
6105 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6106 
6107 	if (sdata->vif.type != NL80211_IFTYPE_AP)
6108 		return NULL;
6109 
6110 	rcu_read_lock();
6111 	presp = rcu_dereference(sdata->deflink.u.ap.probe_resp);
6112 	if (!presp)
6113 		goto out;
6114 
6115 	skb = dev_alloc_skb(presp->len);
6116 	if (!skb)
6117 		goto out;
6118 
6119 	skb_put_data(skb, presp->data, presp->len);
6120 
6121 	hdr = (struct ieee80211_hdr *) skb->data;
6122 	memset(hdr->addr1, 0, sizeof(hdr->addr1));
6123 
6124 out:
6125 	rcu_read_unlock();
6126 	return skb;
6127 }
6128 EXPORT_SYMBOL(ieee80211_proberesp_get);
6129 
6130 struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
6131 						  struct ieee80211_vif *vif,
6132 						  unsigned int link_id)
6133 {
6134 	struct sk_buff *skb = NULL;
6135 	struct fils_discovery_data *tmpl = NULL;
6136 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6137 	struct ieee80211_link_data *link;
6138 
6139 	if (sdata->vif.type != NL80211_IFTYPE_AP)
6140 		return NULL;
6141 
6142 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6143 		return NULL;
6144 
6145 	guard(rcu)();
6146 	link = rcu_dereference(sdata->link[link_id]);
6147 	if (!link)
6148 		return NULL;
6149 
6150 	tmpl = rcu_dereference(link->u.ap.fils_discovery);
6151 	if (!tmpl)
6152 		return NULL;
6153 
6154 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
6155 	if (skb) {
6156 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
6157 		skb_put_data(skb, tmpl->data, tmpl->len);
6158 	}
6159 
6160 	return skb;
6161 }
6162 EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
6163 
6164 struct sk_buff *
6165 ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
6166 					  struct ieee80211_vif *vif,
6167 					  unsigned int link_id)
6168 {
6169 	struct sk_buff *skb = NULL;
6170 	struct unsol_bcast_probe_resp_data *tmpl = NULL;
6171 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6172 	struct ieee80211_link_data *link;
6173 
6174 	if (sdata->vif.type != NL80211_IFTYPE_AP)
6175 		return NULL;
6176 
6177 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6178 		return NULL;
6179 
6180 	guard(rcu)();
6181 	link = rcu_dereference(sdata->link[link_id]);
6182 	if (!link)
6183 		return NULL;
6184 
6185 	tmpl = rcu_dereference(link->u.ap.unsol_bcast_probe_resp);
6186 	if (!tmpl)
6187 		return NULL;
6188 
6189 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
6190 	if (skb) {
6191 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
6192 		skb_put_data(skb, tmpl->data, tmpl->len);
6193 	}
6194 
6195 	return skb;
6196 }
6197 EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
6198 
6199 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
6200 				     struct ieee80211_vif *vif)
6201 {
6202 	struct ieee80211_sub_if_data *sdata;
6203 	struct ieee80211_pspoll *pspoll;
6204 	struct ieee80211_local *local;
6205 	struct sk_buff *skb;
6206 
6207 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
6208 		return NULL;
6209 
6210 	sdata = vif_to_sdata(vif);
6211 	local = sdata->local;
6212 
6213 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
6214 	if (!skb)
6215 		return NULL;
6216 
6217 	skb_reserve(skb, local->hw.extra_tx_headroom);
6218 
6219 	pspoll = skb_put_zero(skb, sizeof(*pspoll));
6220 	pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
6221 					    IEEE80211_STYPE_PSPOLL);
6222 	pspoll->aid = cpu_to_le16(sdata->vif.cfg.aid);
6223 
6224 	/* aid in PS-Poll has its two MSBs each set to 1 */
6225 	pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
6226 
6227 	memcpy(pspoll->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN);
6228 	memcpy(pspoll->ta, vif->addr, ETH_ALEN);
6229 
6230 	return skb;
6231 }
6232 EXPORT_SYMBOL(ieee80211_pspoll_get);
6233 
6234 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
6235 				       struct ieee80211_vif *vif,
6236 				       int link_id, bool qos_ok)
6237 {
6238 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
6239 	struct ieee80211_local *local = sdata->local;
6240 	struct ieee80211_link_data *link = NULL;
6241 	struct ieee80211_hdr_3addr *nullfunc;
6242 	struct sk_buff *skb;
6243 	bool qos = false;
6244 
6245 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
6246 		return NULL;
6247 
6248 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
6249 			    sizeof(*nullfunc) + 2);
6250 	if (!skb)
6251 		return NULL;
6252 
6253 	rcu_read_lock();
6254 	if (qos_ok) {
6255 		struct sta_info *sta;
6256 
6257 		sta = sta_info_get(sdata, vif->cfg.ap_addr);
6258 		qos = sta && sta->sta.wme;
6259 	}
6260 
6261 	if (link_id >= 0) {
6262 		link = rcu_dereference(sdata->link[link_id]);
6263 		if (WARN_ON_ONCE(!link)) {
6264 			rcu_read_unlock();
6265 			kfree_skb(skb);
6266 			return NULL;
6267 		}
6268 	}
6269 
6270 	skb_reserve(skb, local->hw.extra_tx_headroom);
6271 
6272 	nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
6273 	nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
6274 					      IEEE80211_STYPE_NULLFUNC |
6275 					      IEEE80211_FCTL_TODS);
6276 	if (qos) {
6277 		__le16 qoshdr = cpu_to_le16(7);
6278 
6279 		BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
6280 			      IEEE80211_STYPE_NULLFUNC) !=
6281 			     IEEE80211_STYPE_QOS_NULLFUNC);
6282 		nullfunc->frame_control |=
6283 			cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
6284 		skb->priority = 7;
6285 		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
6286 		skb_put_data(skb, &qoshdr, sizeof(qoshdr));
6287 	}
6288 
6289 	if (link) {
6290 		memcpy(nullfunc->addr1, link->conf->bssid, ETH_ALEN);
6291 		memcpy(nullfunc->addr2, link->conf->addr, ETH_ALEN);
6292 		memcpy(nullfunc->addr3, link->conf->bssid, ETH_ALEN);
6293 	} else {
6294 		memcpy(nullfunc->addr1, vif->cfg.ap_addr, ETH_ALEN);
6295 		memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
6296 		memcpy(nullfunc->addr3, vif->cfg.ap_addr, ETH_ALEN);
6297 	}
6298 	rcu_read_unlock();
6299 
6300 	return skb;
6301 }
6302 EXPORT_SYMBOL(ieee80211_nullfunc_get);
6303 
6304 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
6305 				       const u8 *src_addr,
6306 				       const u8 *ssid, size_t ssid_len,
6307 				       size_t tailroom)
6308 {
6309 	struct ieee80211_local *local = hw_to_local(hw);
6310 	struct ieee80211_hdr_3addr *hdr;
6311 	struct sk_buff *skb;
6312 	size_t ie_ssid_len;
6313 	u8 *pos;
6314 
6315 	ie_ssid_len = 2 + ssid_len;
6316 
6317 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
6318 			    ie_ssid_len + tailroom);
6319 	if (!skb)
6320 		return NULL;
6321 
6322 	skb_reserve(skb, local->hw.extra_tx_headroom);
6323 
6324 	hdr = skb_put_zero(skb, sizeof(*hdr));
6325 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
6326 					 IEEE80211_STYPE_PROBE_REQ);
6327 	eth_broadcast_addr(hdr->addr1);
6328 	memcpy(hdr->addr2, src_addr, ETH_ALEN);
6329 	eth_broadcast_addr(hdr->addr3);
6330 
6331 	pos = skb_put(skb, ie_ssid_len);
6332 	*pos++ = WLAN_EID_SSID;
6333 	*pos++ = ssid_len;
6334 	if (ssid_len)
6335 		memcpy(pos, ssid, ssid_len);
6336 	pos += ssid_len;
6337 
6338 	return skb;
6339 }
6340 EXPORT_SYMBOL(ieee80211_probereq_get);
6341 
6342 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6343 		       const void *frame, size_t frame_len,
6344 		       const struct ieee80211_tx_info *frame_txctl,
6345 		       struct ieee80211_rts *rts)
6346 {
6347 	const struct ieee80211_hdr *hdr = frame;
6348 
6349 	rts->frame_control =
6350 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
6351 	rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
6352 					       frame_txctl);
6353 	memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
6354 	memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
6355 }
6356 EXPORT_SYMBOL(ieee80211_rts_get);
6357 
6358 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6359 			     const void *frame, size_t frame_len,
6360 			     const struct ieee80211_tx_info *frame_txctl,
6361 			     struct ieee80211_cts *cts)
6362 {
6363 	const struct ieee80211_hdr *hdr = frame;
6364 
6365 	cts->frame_control =
6366 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
6367 	cts->duration = ieee80211_ctstoself_duration(hw, vif,
6368 						     frame_len, frame_txctl);
6369 	memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
6370 }
6371 EXPORT_SYMBOL(ieee80211_ctstoself_get);
6372 
6373 struct sk_buff *
6374 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
6375 			  struct ieee80211_vif *vif)
6376 {
6377 	struct ieee80211_local *local = hw_to_local(hw);
6378 	struct sk_buff *skb = NULL;
6379 	struct ieee80211_tx_data tx;
6380 	struct ieee80211_sub_if_data *sdata;
6381 	struct ps_data *ps;
6382 	struct ieee80211_tx_info *info;
6383 	struct ieee80211_chanctx_conf *chanctx_conf;
6384 
6385 	sdata = vif_to_sdata(vif);
6386 
6387 	rcu_read_lock();
6388 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
6389 
6390 	if (!chanctx_conf)
6391 		goto out;
6392 
6393 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
6394 		struct beacon_data *beacon =
6395 				rcu_dereference(sdata->deflink.u.ap.beacon);
6396 
6397 		if (!beacon || !beacon->head)
6398 			goto out;
6399 
6400 		ps = &sdata->u.ap.ps;
6401 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
6402 		ps = &sdata->u.mesh.ps;
6403 	} else {
6404 		goto out;
6405 	}
6406 
6407 	if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
6408 		goto out; /* send buffered bc/mc only after DTIM beacon */
6409 
6410 	while (1) {
6411 		skb = skb_dequeue(&ps->bc_buf);
6412 		if (!skb)
6413 			goto out;
6414 		local->total_ps_buffered--;
6415 
6416 		if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
6417 			struct ieee80211_hdr *hdr =
6418 				(struct ieee80211_hdr *) skb->data;
6419 			/* more buffered multicast/broadcast frames ==> set
6420 			 * MoreData flag in IEEE 802.11 header to inform PS
6421 			 * STAs */
6422 			hdr->frame_control |=
6423 				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
6424 		}
6425 
6426 		if (sdata->vif.type == NL80211_IFTYPE_AP)
6427 			sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
6428 		if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
6429 			break;
6430 		ieee80211_free_txskb(hw, skb);
6431 	}
6432 
6433 	info = IEEE80211_SKB_CB(skb);
6434 
6435 	tx.flags |= IEEE80211_TX_PS_BUFFERED;
6436 	info->band = chanctx_conf->def.chan->band;
6437 
6438 	if (invoke_tx_handlers(&tx))
6439 		skb = NULL;
6440  out:
6441 	rcu_read_unlock();
6442 
6443 	return skb;
6444 }
6445 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
6446 
6447 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
6448 {
6449 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
6450 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6451 	struct ieee80211_local *local = sdata->local;
6452 	int ret;
6453 	u32 queues;
6454 
6455 	lockdep_assert_wiphy(local->hw.wiphy);
6456 
6457 	/* only some cases are supported right now */
6458 	switch (sdata->vif.type) {
6459 	case NL80211_IFTYPE_STATION:
6460 	case NL80211_IFTYPE_AP:
6461 	case NL80211_IFTYPE_AP_VLAN:
6462 		break;
6463 	default:
6464 		WARN_ON(1);
6465 		return -EINVAL;
6466 	}
6467 
6468 	if (WARN_ON(tid >= IEEE80211_NUM_UPS))
6469 		return -EINVAL;
6470 
6471 	if (sta->reserved_tid == tid) {
6472 		ret = 0;
6473 		goto out;
6474 	}
6475 
6476 	if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
6477 		sdata_err(sdata, "TID reservation already active\n");
6478 		ret = -EALREADY;
6479 		goto out;
6480 	}
6481 
6482 	ieee80211_stop_vif_queues(sdata->local, sdata,
6483 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
6484 
6485 	synchronize_net();
6486 
6487 	/* Tear down BA sessions so we stop aggregating on this TID */
6488 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
6489 		set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6490 		__ieee80211_stop_tx_ba_session(sta, tid,
6491 					       AGG_STOP_LOCAL_REQUEST);
6492 	}
6493 
6494 	queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
6495 	__ieee80211_flush_queues(local, sdata, queues, false);
6496 
6497 	sta->reserved_tid = tid;
6498 
6499 	ieee80211_wake_vif_queues(local, sdata,
6500 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
6501 
6502 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
6503 		clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6504 
6505 	ret = 0;
6506  out:
6507 	return ret;
6508 }
6509 EXPORT_SYMBOL(ieee80211_reserve_tid);
6510 
6511 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
6512 {
6513 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
6514 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6515 
6516 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
6517 
6518 	/* only some cases are supported right now */
6519 	switch (sdata->vif.type) {
6520 	case NL80211_IFTYPE_STATION:
6521 	case NL80211_IFTYPE_AP:
6522 	case NL80211_IFTYPE_AP_VLAN:
6523 		break;
6524 	default:
6525 		WARN_ON(1);
6526 		return;
6527 	}
6528 
6529 	if (tid != sta->reserved_tid) {
6530 		sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
6531 		return;
6532 	}
6533 
6534 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
6535 }
6536 EXPORT_SYMBOL(ieee80211_unreserve_tid);
6537 
6538 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
6539 				 struct sk_buff *skb, struct sta_info *sta,
6540 				 int tid, int link_id,
6541 				 enum nl80211_band band)
6542 {
6543 	const struct ieee80211_hdr *hdr = (void *)skb->data;
6544 	int ac = ieee80211_ac_from_tid(tid);
6545 	unsigned int link;
6546 
6547 	skb_reset_mac_header(skb);
6548 	skb_set_queue_mapping(skb, ac);
6549 	skb->priority = tid;
6550 
6551 	skb->dev = sdata->dev;
6552 
6553 	BUILD_BUG_ON(IEEE80211_LINK_UNSPECIFIED < IEEE80211_MLD_MAX_NUM_LINKS);
6554 	BUILD_BUG_ON(!FIELD_FIT(IEEE80211_TX_CTRL_MLO_LINK,
6555 				IEEE80211_LINK_UNSPECIFIED));
6556 
6557 	if (!ieee80211_vif_is_mld(&sdata->vif)) {
6558 		link = 0;
6559 	} else if (link_id >= 0) {
6560 		link = link_id;
6561 	} else if (memcmp(sdata->vif.addr, hdr->addr2, ETH_ALEN) == 0) {
6562 		/* address from the MLD */
6563 		link = IEEE80211_LINK_UNSPECIFIED;
6564 	} else {
6565 		/* otherwise must be addressed from a link */
6566 		rcu_read_lock();
6567 		for (link = 0; link < ARRAY_SIZE(sdata->vif.link_conf); link++) {
6568 			struct ieee80211_bss_conf *link_conf;
6569 
6570 			link_conf = rcu_dereference(sdata->vif.link_conf[link]);
6571 			if (!link_conf)
6572 				continue;
6573 			if (memcmp(link_conf->addr, hdr->addr2, ETH_ALEN) == 0)
6574 				break;
6575 		}
6576 		rcu_read_unlock();
6577 
6578 		if (WARN_ON_ONCE(link == ARRAY_SIZE(sdata->vif.link_conf)))
6579 			link = ffs(sdata->vif.active_links) - 1;
6580 	}
6581 
6582 	IEEE80211_SKB_CB(skb)->control.flags |=
6583 		u32_encode_bits(link, IEEE80211_TX_CTRL_MLO_LINK);
6584 
6585 	/*
6586 	 * The other path calling ieee80211_xmit is from the tasklet,
6587 	 * and while we can handle concurrent transmissions locking
6588 	 * requirements are that we do not come into tx with bhs on.
6589 	 */
6590 	local_bh_disable();
6591 	IEEE80211_SKB_CB(skb)->band = band;
6592 	ieee80211_xmit(sdata, sta, skb);
6593 	local_bh_enable();
6594 }
6595 
6596 void ieee80211_tx_skb_tid(struct ieee80211_sub_if_data *sdata,
6597 			  struct sk_buff *skb, struct sta_info *sta,
6598 			  int tid, int link_id)
6599 {
6600 	struct ieee80211_chanctx_conf *chanctx_conf;
6601 	enum nl80211_band band;
6602 
6603 	rcu_read_lock();
6604 	if (sdata->vif.type == NL80211_IFTYPE_NAN ||
6605 	    sdata->vif.type == NL80211_IFTYPE_NAN_DATA) {
6606 		band = NUM_NL80211_BANDS;
6607 	} else if (!ieee80211_vif_is_mld(&sdata->vif)) {
6608 		WARN_ON(link_id >= 0);
6609 		chanctx_conf =
6610 			rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
6611 		if (WARN_ON(!chanctx_conf)) {
6612 			rcu_read_unlock();
6613 			ieee80211_free_txskb(&sdata->local->hw, skb);
6614 			return;
6615 		}
6616 		band = chanctx_conf->def.chan->band;
6617 	} else {
6618 		WARN_ON(link_id >= 0 &&
6619 			!(sdata->vif.active_links & BIT(link_id)));
6620 		/* MLD transmissions must not rely on the band */
6621 		band = 0;
6622 	}
6623 
6624 	__ieee80211_tx_skb_tid_band(sdata, skb, sta, tid, link_id, band);
6625 	rcu_read_unlock();
6626 }
6627 
6628 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
6629 			      const u8 *buf, size_t len,
6630 			      const u8 *dest, __be16 proto, bool unencrypted,
6631 			      int link_id, u64 cookie)
6632 {
6633 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
6634 	struct ieee80211_local *local = sdata->local;
6635 	struct ieee80211_tx_info *info;
6636 	bool group_addressed;
6637 	struct sta_info *sta;
6638 	struct sk_buff *skb;
6639 	struct ethhdr *ehdr;
6640 	u32 ctrl_flags = 0;
6641 	u32 flags = 0;
6642 	int err;
6643 
6644 	/* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
6645 	 * or Pre-Authentication
6646 	 */
6647 	if (proto != sdata->control_port_protocol &&
6648 	    proto != cpu_to_be16(ETH_P_PREAUTH))
6649 		return -EINVAL;
6650 
6651 	if (proto == sdata->control_port_protocol)
6652 		ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
6653 			      IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
6654 
6655 	if (unencrypted)
6656 		flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
6657 
6658 	flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
6659 
6660 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
6661 			    sizeof(struct ethhdr) + len);
6662 	if (!skb)
6663 		return -ENOMEM;
6664 
6665 	skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
6666 
6667 	skb_put_data(skb, buf, len);
6668 
6669 	ehdr = skb_push(skb, sizeof(struct ethhdr));
6670 	memcpy(ehdr->h_dest, dest, ETH_ALEN);
6671 
6672 	/* we may override the SA for MLO STA later */
6673 	if (link_id < 0) {
6674 		link_id = IEEE80211_LINK_UNSPECIFIED;
6675 		memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
6676 	} else {
6677 		struct ieee80211_bss_conf *link_conf;
6678 
6679 		rcu_read_lock();
6680 		link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
6681 		if (!link_conf) {
6682 			dev_kfree_skb(skb);
6683 			rcu_read_unlock();
6684 			return -ENOLINK;
6685 		}
6686 		memcpy(ehdr->h_source, link_conf->addr, ETH_ALEN);
6687 		rcu_read_unlock();
6688 	}
6689 
6690 	ehdr->h_proto = proto;
6691 
6692 	skb->dev = dev;
6693 	skb->protocol = proto;
6694 	skb_reset_network_header(skb);
6695 	skb_reset_mac_header(skb);
6696 
6697 	/* update QoS header to prioritize control port frames if possible,
6698 	 * prioritization also happens for control port frames send over
6699 	 * AF_PACKET
6700 	 */
6701 	rcu_read_lock();
6702 
6703 	/* do_stop() synchronizes RCU before the interface type can change */
6704 	if (!ieee80211_sdata_running(sdata)) {
6705 		dev_kfree_skb(skb);
6706 		rcu_read_unlock();
6707 		return -ENETDOWN;
6708 	}
6709 
6710 	err = ieee80211_lookup_ra_sta(sdata, skb, &sta, true);
6711 	if (err) {
6712 		dev_kfree_skb(skb);
6713 		rcu_read_unlock();
6714 		return err;
6715 	}
6716 
6717 	group_addressed = IS_ERR(sta);
6718 	if (IS_ERR(sta))
6719 		sta = NULL;
6720 
6721 	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb));
6722 
6723 	/*
6724 	 * for MLO STA, the SA should be the AP MLD address, but
6725 	 * the link ID has been selected already
6726 	 */
6727 	if (sta && sta->sta.mlo)
6728 		memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
6729 
6730 	info = IEEE80211_SKB_CB(skb);
6731 	memset(info, 0, sizeof(*info));
6732 	info->flags = flags;
6733 	ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
6734 	info->control.flags = ctrl_flags;
6735 
6736 	if (!group_addressed)
6737 		ieee80211_store_ack_skb(local, skb, cookie);
6738 
6739 	skb = ieee80211_build_hdr(sdata, skb, sta);
6740 	if (IS_ERR(skb)) {
6741 		rcu_read_unlock();
6742 		return PTR_ERR(skb);
6743 	}
6744 
6745 	if (link_id != u32_get_bits(info->control.flags,
6746 				    IEEE80211_TX_CTRL_MLO_LINK)) {
6747 		ieee80211_free_txskb(&local->hw, skb);
6748 		rcu_read_unlock();
6749 		return -EINVAL;
6750 	}
6751 
6752 	local_bh_disable();
6753 	dev_sw_netstats_tx_add(dev, 1, skb->len);
6754 	ieee80211_tpt_led_trig_tx(local, skb->len);
6755 	ieee80211_xmit(sdata, sta, skb);
6756 	local_bh_enable();
6757 	rcu_read_unlock();
6758 
6759 	return 0;
6760 }
6761 
6762 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
6763 			      const u8 *buf, size_t len)
6764 {
6765 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
6766 	struct ieee80211_local *local = sdata->local;
6767 	struct sk_buff *skb;
6768 
6769 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
6770 			    30 + /* header size */
6771 			    18); /* 11s header size */
6772 	if (!skb)
6773 		return -ENOMEM;
6774 
6775 	skb_reserve(skb, local->hw.extra_tx_headroom);
6776 	skb_put_data(skb, buf, len);
6777 
6778 	skb->dev = dev;
6779 	skb->protocol = htons(ETH_P_802_3);
6780 	skb_reset_network_header(skb);
6781 	skb_reset_mac_header(skb);
6782 
6783 	local_bh_disable();
6784 	__ieee80211_subif_start_xmit(skb, skb->dev, 0,
6785 				     IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);
6786 	local_bh_enable();
6787 
6788 	return 0;
6789 }
6790