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