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