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