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