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