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