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