xref: /openbmc/linux/net/mac80211/tx.c (revision 6cd70754)
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 (tx->sdata->control_port_protocol == tx->skb->protocol) {
1186 			tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1187 		}
1188 		if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1189 			tx->sta = sta_info_get(sdata, hdr->addr1);
1190 	}
1191 
1192 	if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1193 	    !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1194 	    ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1195 	    !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1196 		struct tid_ampdu_tx *tid_tx;
1197 
1198 		tid = ieee80211_get_tid(hdr);
1199 
1200 		tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1201 		if (tid_tx) {
1202 			bool queued;
1203 
1204 			queued = ieee80211_tx_prep_agg(tx, skb, info,
1205 						       tid_tx, tid);
1206 
1207 			if (unlikely(queued))
1208 				return TX_QUEUED;
1209 		}
1210 	}
1211 
1212 	if (is_multicast_ether_addr(hdr->addr1)) {
1213 		tx->flags &= ~IEEE80211_TX_UNICAST;
1214 		info->flags |= IEEE80211_TX_CTL_NO_ACK;
1215 	} else
1216 		tx->flags |= IEEE80211_TX_UNICAST;
1217 
1218 	if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1219 		if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1220 		    skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1221 		    info->flags & IEEE80211_TX_CTL_AMPDU)
1222 			info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1223 	}
1224 
1225 	if (!tx->sta)
1226 		info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1227 	else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1228 		info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1229 		ieee80211_check_fast_xmit(tx->sta);
1230 	}
1231 
1232 	info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1233 
1234 	return TX_CONTINUE;
1235 }
1236 
1237 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1238 					  struct ieee80211_vif *vif,
1239 					  struct sta_info *sta,
1240 					  struct sk_buff *skb)
1241 {
1242 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1243 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1244 	struct ieee80211_txq *txq = NULL;
1245 
1246 	if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1247 	    (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1248 		return NULL;
1249 
1250 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
1251 	    unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1252 		if ((!ieee80211_is_mgmt(hdr->frame_control) ||
1253 		     ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1254 		     vif->type == NL80211_IFTYPE_STATION) &&
1255 		    sta && sta->uploaded) {
1256 			/*
1257 			 * This will be NULL if the driver didn't set the
1258 			 * opt-in hardware flag.
1259 			 */
1260 			txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1261 		}
1262 	} else if (sta) {
1263 		u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1264 
1265 		if (!sta->uploaded)
1266 			return NULL;
1267 
1268 		txq = sta->sta.txq[tid];
1269 	} else if (vif) {
1270 		txq = vif->txq;
1271 	}
1272 
1273 	if (!txq)
1274 		return NULL;
1275 
1276 	return to_txq_info(txq);
1277 }
1278 
1279 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1280 {
1281 	IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1282 }
1283 
1284 static u32 codel_skb_len_func(const struct sk_buff *skb)
1285 {
1286 	return skb->len;
1287 }
1288 
1289 static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1290 {
1291 	const struct ieee80211_tx_info *info;
1292 
1293 	info = (const struct ieee80211_tx_info *)skb->cb;
1294 	return info->control.enqueue_time;
1295 }
1296 
1297 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1298 					  void *ctx)
1299 {
1300 	struct ieee80211_local *local;
1301 	struct txq_info *txqi;
1302 	struct fq *fq;
1303 	struct fq_flow *flow;
1304 
1305 	txqi = ctx;
1306 	local = vif_to_sdata(txqi->txq.vif)->local;
1307 	fq = &local->fq;
1308 
1309 	if (cvars == &txqi->def_cvars)
1310 		flow = &txqi->tin.default_flow;
1311 	else
1312 		flow = &fq->flows[cvars - local->cvars];
1313 
1314 	return fq_flow_dequeue(fq, flow);
1315 }
1316 
1317 static void codel_drop_func(struct sk_buff *skb,
1318 			    void *ctx)
1319 {
1320 	struct ieee80211_local *local;
1321 	struct ieee80211_hw *hw;
1322 	struct txq_info *txqi;
1323 
1324 	txqi = ctx;
1325 	local = vif_to_sdata(txqi->txq.vif)->local;
1326 	hw = &local->hw;
1327 
1328 	ieee80211_free_txskb(hw, skb);
1329 }
1330 
1331 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1332 					   struct fq_tin *tin,
1333 					   struct fq_flow *flow)
1334 {
1335 	struct ieee80211_local *local;
1336 	struct txq_info *txqi;
1337 	struct codel_vars *cvars;
1338 	struct codel_params *cparams;
1339 	struct codel_stats *cstats;
1340 
1341 	local = container_of(fq, struct ieee80211_local, fq);
1342 	txqi = container_of(tin, struct txq_info, tin);
1343 	cstats = &txqi->cstats;
1344 
1345 	if (txqi->txq.sta) {
1346 		struct sta_info *sta = container_of(txqi->txq.sta,
1347 						    struct sta_info, sta);
1348 		cparams = &sta->cparams;
1349 	} else {
1350 		cparams = &local->cparams;
1351 	}
1352 
1353 	if (flow == &tin->default_flow)
1354 		cvars = &txqi->def_cvars;
1355 	else
1356 		cvars = &local->cvars[flow - fq->flows];
1357 
1358 	return codel_dequeue(txqi,
1359 			     &flow->backlog,
1360 			     cparams,
1361 			     cvars,
1362 			     cstats,
1363 			     codel_skb_len_func,
1364 			     codel_skb_time_func,
1365 			     codel_drop_func,
1366 			     codel_dequeue_func);
1367 }
1368 
1369 static void fq_skb_free_func(struct fq *fq,
1370 			     struct fq_tin *tin,
1371 			     struct fq_flow *flow,
1372 			     struct sk_buff *skb)
1373 {
1374 	struct ieee80211_local *local;
1375 
1376 	local = container_of(fq, struct ieee80211_local, fq);
1377 	ieee80211_free_txskb(&local->hw, skb);
1378 }
1379 
1380 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1381 				  struct txq_info *txqi,
1382 				  struct sk_buff *skb)
1383 {
1384 	struct fq *fq = &local->fq;
1385 	struct fq_tin *tin = &txqi->tin;
1386 	u32 flow_idx = fq_flow_idx(fq, skb);
1387 
1388 	ieee80211_set_skb_enqueue_time(skb);
1389 
1390 	spin_lock_bh(&fq->lock);
1391 	fq_tin_enqueue(fq, tin, flow_idx, skb,
1392 		       fq_skb_free_func);
1393 	spin_unlock_bh(&fq->lock);
1394 }
1395 
1396 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1397 				struct fq_flow *flow, struct sk_buff *skb,
1398 				void *data)
1399 {
1400 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1401 
1402 	return info->control.vif == data;
1403 }
1404 
1405 void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1406 			       struct ieee80211_sub_if_data *sdata)
1407 {
1408 	struct fq *fq = &local->fq;
1409 	struct txq_info *txqi;
1410 	struct fq_tin *tin;
1411 	struct ieee80211_sub_if_data *ap;
1412 
1413 	if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1414 		return;
1415 
1416 	ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1417 
1418 	if (!ap->vif.txq)
1419 		return;
1420 
1421 	txqi = to_txq_info(ap->vif.txq);
1422 	tin = &txqi->tin;
1423 
1424 	spin_lock_bh(&fq->lock);
1425 	fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1426 		      fq_skb_free_func);
1427 	spin_unlock_bh(&fq->lock);
1428 }
1429 
1430 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1431 			struct sta_info *sta,
1432 			struct txq_info *txqi, int tid)
1433 {
1434 	fq_tin_init(&txqi->tin);
1435 	codel_vars_init(&txqi->def_cvars);
1436 	codel_stats_init(&txqi->cstats);
1437 	__skb_queue_head_init(&txqi->frags);
1438 	INIT_LIST_HEAD(&txqi->schedule_order);
1439 
1440 	txqi->txq.vif = &sdata->vif;
1441 
1442 	if (!sta) {
1443 		sdata->vif.txq = &txqi->txq;
1444 		txqi->txq.tid = 0;
1445 		txqi->txq.ac = IEEE80211_AC_BE;
1446 
1447 		return;
1448 	}
1449 
1450 	if (tid == IEEE80211_NUM_TIDS) {
1451 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1452 			/* Drivers need to opt in to the management MPDU TXQ */
1453 			if (!ieee80211_hw_check(&sdata->local->hw,
1454 						STA_MMPDU_TXQ))
1455 				return;
1456 		} else if (!ieee80211_hw_check(&sdata->local->hw,
1457 					       BUFF_MMPDU_TXQ)) {
1458 			/* Drivers need to opt in to the bufferable MMPDU TXQ */
1459 			return;
1460 		}
1461 		txqi->txq.ac = IEEE80211_AC_VO;
1462 	} else {
1463 		txqi->txq.ac = ieee80211_ac_from_tid(tid);
1464 	}
1465 
1466 	txqi->txq.sta = &sta->sta;
1467 	txqi->txq.tid = tid;
1468 	sta->sta.txq[tid] = &txqi->txq;
1469 }
1470 
1471 void ieee80211_txq_purge(struct ieee80211_local *local,
1472 			 struct txq_info *txqi)
1473 {
1474 	struct fq *fq = &local->fq;
1475 	struct fq_tin *tin = &txqi->tin;
1476 
1477 	spin_lock_bh(&fq->lock);
1478 	fq_tin_reset(fq, tin, fq_skb_free_func);
1479 	ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
1480 	spin_unlock_bh(&fq->lock);
1481 
1482 	spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1483 	list_del_init(&txqi->schedule_order);
1484 	spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
1485 }
1486 
1487 void ieee80211_txq_set_params(struct ieee80211_local *local)
1488 {
1489 	if (local->hw.wiphy->txq_limit)
1490 		local->fq.limit = local->hw.wiphy->txq_limit;
1491 	else
1492 		local->hw.wiphy->txq_limit = local->fq.limit;
1493 
1494 	if (local->hw.wiphy->txq_memory_limit)
1495 		local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1496 	else
1497 		local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1498 
1499 	if (local->hw.wiphy->txq_quantum)
1500 		local->fq.quantum = local->hw.wiphy->txq_quantum;
1501 	else
1502 		local->hw.wiphy->txq_quantum = local->fq.quantum;
1503 }
1504 
1505 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1506 {
1507 	struct fq *fq = &local->fq;
1508 	int ret;
1509 	int i;
1510 	bool supp_vht = false;
1511 	enum nl80211_band band;
1512 
1513 	if (!local->ops->wake_tx_queue)
1514 		return 0;
1515 
1516 	ret = fq_init(fq, 4096);
1517 	if (ret)
1518 		return ret;
1519 
1520 	/*
1521 	 * If the hardware doesn't support VHT, it is safe to limit the maximum
1522 	 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1523 	 */
1524 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1525 		struct ieee80211_supported_band *sband;
1526 
1527 		sband = local->hw.wiphy->bands[band];
1528 		if (!sband)
1529 			continue;
1530 
1531 		supp_vht = supp_vht || sband->vht_cap.vht_supported;
1532 	}
1533 
1534 	if (!supp_vht)
1535 		fq->memory_limit = 4 << 20; /* 4 Mbytes */
1536 
1537 	codel_params_init(&local->cparams);
1538 	local->cparams.interval = MS2TIME(100);
1539 	local->cparams.target = MS2TIME(20);
1540 	local->cparams.ecn = true;
1541 
1542 	local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1543 			       GFP_KERNEL);
1544 	if (!local->cvars) {
1545 		spin_lock_bh(&fq->lock);
1546 		fq_reset(fq, fq_skb_free_func);
1547 		spin_unlock_bh(&fq->lock);
1548 		return -ENOMEM;
1549 	}
1550 
1551 	for (i = 0; i < fq->flows_cnt; i++)
1552 		codel_vars_init(&local->cvars[i]);
1553 
1554 	ieee80211_txq_set_params(local);
1555 
1556 	return 0;
1557 }
1558 
1559 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1560 {
1561 	struct fq *fq = &local->fq;
1562 
1563 	if (!local->ops->wake_tx_queue)
1564 		return;
1565 
1566 	kfree(local->cvars);
1567 	local->cvars = NULL;
1568 
1569 	spin_lock_bh(&fq->lock);
1570 	fq_reset(fq, fq_skb_free_func);
1571 	spin_unlock_bh(&fq->lock);
1572 }
1573 
1574 static bool ieee80211_queue_skb(struct ieee80211_local *local,
1575 				struct ieee80211_sub_if_data *sdata,
1576 				struct sta_info *sta,
1577 				struct sk_buff *skb)
1578 {
1579 	struct ieee80211_vif *vif;
1580 	struct txq_info *txqi;
1581 
1582 	if (!local->ops->wake_tx_queue ||
1583 	    sdata->vif.type == NL80211_IFTYPE_MONITOR)
1584 		return false;
1585 
1586 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1587 		sdata = container_of(sdata->bss,
1588 				     struct ieee80211_sub_if_data, u.ap);
1589 
1590 	vif = &sdata->vif;
1591 	txqi = ieee80211_get_txq(local, vif, sta, skb);
1592 
1593 	if (!txqi)
1594 		return false;
1595 
1596 	ieee80211_txq_enqueue(local, txqi, skb);
1597 
1598 	schedule_and_wake_txq(local, txqi);
1599 
1600 	return true;
1601 }
1602 
1603 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1604 			       struct ieee80211_vif *vif,
1605 			       struct sta_info *sta,
1606 			       struct sk_buff_head *skbs,
1607 			       bool txpending)
1608 {
1609 	struct ieee80211_tx_control control = {};
1610 	struct sk_buff *skb, *tmp;
1611 	unsigned long flags;
1612 
1613 	skb_queue_walk_safe(skbs, skb, tmp) {
1614 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1615 		int q = info->hw_queue;
1616 
1617 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1618 		if (WARN_ON_ONCE(q >= local->hw.queues)) {
1619 			__skb_unlink(skb, skbs);
1620 			ieee80211_free_txskb(&local->hw, skb);
1621 			continue;
1622 		}
1623 #endif
1624 
1625 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1626 		if (local->queue_stop_reasons[q] ||
1627 		    (!txpending && !skb_queue_empty(&local->pending[q]))) {
1628 			if (unlikely(info->flags &
1629 				     IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1630 				if (local->queue_stop_reasons[q] &
1631 				    ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1632 					/*
1633 					 * Drop off-channel frames if queues
1634 					 * are stopped for any reason other
1635 					 * than off-channel operation. Never
1636 					 * queue them.
1637 					 */
1638 					spin_unlock_irqrestore(
1639 						&local->queue_stop_reason_lock,
1640 						flags);
1641 					ieee80211_purge_tx_queue(&local->hw,
1642 								 skbs);
1643 					return true;
1644 				}
1645 			} else {
1646 
1647 				/*
1648 				 * Since queue is stopped, queue up frames for
1649 				 * later transmission from the tx-pending
1650 				 * tasklet when the queue is woken again.
1651 				 */
1652 				if (txpending)
1653 					skb_queue_splice_init(skbs,
1654 							      &local->pending[q]);
1655 				else
1656 					skb_queue_splice_tail_init(skbs,
1657 								   &local->pending[q]);
1658 
1659 				spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1660 						       flags);
1661 				return false;
1662 			}
1663 		}
1664 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1665 
1666 		info->control.vif = vif;
1667 		control.sta = sta ? &sta->sta : NULL;
1668 
1669 		__skb_unlink(skb, skbs);
1670 		drv_tx(local, &control, skb);
1671 	}
1672 
1673 	return true;
1674 }
1675 
1676 /*
1677  * Returns false if the frame couldn't be transmitted but was queued instead.
1678  */
1679 static bool __ieee80211_tx(struct ieee80211_local *local,
1680 			   struct sk_buff_head *skbs, int led_len,
1681 			   struct sta_info *sta, bool txpending)
1682 {
1683 	struct ieee80211_tx_info *info;
1684 	struct ieee80211_sub_if_data *sdata;
1685 	struct ieee80211_vif *vif;
1686 	struct sk_buff *skb;
1687 	bool result = true;
1688 	__le16 fc;
1689 
1690 	if (WARN_ON(skb_queue_empty(skbs)))
1691 		return true;
1692 
1693 	skb = skb_peek(skbs);
1694 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1695 	info = IEEE80211_SKB_CB(skb);
1696 	sdata = vif_to_sdata(info->control.vif);
1697 	if (sta && !sta->uploaded)
1698 		sta = NULL;
1699 
1700 	switch (sdata->vif.type) {
1701 	case NL80211_IFTYPE_MONITOR:
1702 		if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1703 			vif = &sdata->vif;
1704 			break;
1705 		}
1706 		sdata = rcu_dereference(local->monitor_sdata);
1707 		if (sdata) {
1708 			vif = &sdata->vif;
1709 			info->hw_queue =
1710 				vif->hw_queue[skb_get_queue_mapping(skb)];
1711 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1712 			ieee80211_purge_tx_queue(&local->hw, skbs);
1713 			return true;
1714 		} else
1715 			vif = NULL;
1716 		break;
1717 	case NL80211_IFTYPE_AP_VLAN:
1718 		sdata = container_of(sdata->bss,
1719 				     struct ieee80211_sub_if_data, u.ap);
1720 		fallthrough;
1721 	default:
1722 		vif = &sdata->vif;
1723 		break;
1724 	}
1725 
1726 	result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
1727 
1728 	ieee80211_tpt_led_trig_tx(local, fc, led_len);
1729 
1730 	WARN_ON_ONCE(!skb_queue_empty(skbs));
1731 
1732 	return result;
1733 }
1734 
1735 /*
1736  * Invoke TX handlers, return 0 on success and non-zero if the
1737  * frame was dropped or queued.
1738  *
1739  * The handlers are split into an early and late part. The latter is everything
1740  * that can be sensitive to reordering, and will be deferred to after packets
1741  * are dequeued from the intermediate queues (when they are enabled).
1742  */
1743 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
1744 {
1745 	ieee80211_tx_result res = TX_DROP;
1746 
1747 #define CALL_TXH(txh) \
1748 	do {				\
1749 		res = txh(tx);		\
1750 		if (res != TX_CONTINUE)	\
1751 			goto txh_done;	\
1752 	} while (0)
1753 
1754 	CALL_TXH(ieee80211_tx_h_dynamic_ps);
1755 	CALL_TXH(ieee80211_tx_h_check_assoc);
1756 	CALL_TXH(ieee80211_tx_h_ps_buf);
1757 	CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1758 	CALL_TXH(ieee80211_tx_h_select_key);
1759 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1760 		CALL_TXH(ieee80211_tx_h_rate_ctrl);
1761 
1762  txh_done:
1763 	if (unlikely(res == TX_DROP)) {
1764 		I802_DEBUG_INC(tx->local->tx_handlers_drop);
1765 		if (tx->skb)
1766 			ieee80211_free_txskb(&tx->local->hw, tx->skb);
1767 		else
1768 			ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1769 		return -1;
1770 	} else if (unlikely(res == TX_QUEUED)) {
1771 		I802_DEBUG_INC(tx->local->tx_handlers_queued);
1772 		return -1;
1773 	}
1774 
1775 	return 0;
1776 }
1777 
1778 /*
1779  * Late handlers can be called while the sta lock is held. Handlers that can
1780  * cause packets to be generated will cause deadlock!
1781  */
1782 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1783 {
1784 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1785 	ieee80211_tx_result res = TX_CONTINUE;
1786 
1787 	if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1788 		__skb_queue_tail(&tx->skbs, tx->skb);
1789 		tx->skb = NULL;
1790 		goto txh_done;
1791 	}
1792 
1793 	CALL_TXH(ieee80211_tx_h_michael_mic_add);
1794 	CALL_TXH(ieee80211_tx_h_sequence);
1795 	CALL_TXH(ieee80211_tx_h_fragment);
1796 	/* handlers after fragment must be aware of tx info fragmentation! */
1797 	CALL_TXH(ieee80211_tx_h_stats);
1798 	CALL_TXH(ieee80211_tx_h_encrypt);
1799 	if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1800 		CALL_TXH(ieee80211_tx_h_calculate_duration);
1801 #undef CALL_TXH
1802 
1803  txh_done:
1804 	if (unlikely(res == TX_DROP)) {
1805 		I802_DEBUG_INC(tx->local->tx_handlers_drop);
1806 		if (tx->skb)
1807 			ieee80211_free_txskb(&tx->local->hw, tx->skb);
1808 		else
1809 			ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1810 		return -1;
1811 	} else if (unlikely(res == TX_QUEUED)) {
1812 		I802_DEBUG_INC(tx->local->tx_handlers_queued);
1813 		return -1;
1814 	}
1815 
1816 	return 0;
1817 }
1818 
1819 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1820 {
1821 	int r = invoke_tx_handlers_early(tx);
1822 
1823 	if (r)
1824 		return r;
1825 	return invoke_tx_handlers_late(tx);
1826 }
1827 
1828 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1829 			      struct ieee80211_vif *vif, struct sk_buff *skb,
1830 			      int band, struct ieee80211_sta **sta)
1831 {
1832 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1833 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1834 	struct ieee80211_tx_data tx;
1835 	struct sk_buff *skb2;
1836 
1837 	if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1838 		return false;
1839 
1840 	info->band = band;
1841 	info->control.vif = vif;
1842 	info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1843 
1844 	if (invoke_tx_handlers(&tx))
1845 		return false;
1846 
1847 	if (sta) {
1848 		if (tx.sta)
1849 			*sta = &tx.sta->sta;
1850 		else
1851 			*sta = NULL;
1852 	}
1853 
1854 	/* this function isn't suitable for fragmented data frames */
1855 	skb2 = __skb_dequeue(&tx.skbs);
1856 	if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1857 		ieee80211_free_txskb(hw, skb2);
1858 		ieee80211_purge_tx_queue(hw, &tx.skbs);
1859 		return false;
1860 	}
1861 
1862 	return true;
1863 }
1864 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1865 
1866 /*
1867  * Returns false if the frame couldn't be transmitted but was queued instead.
1868  */
1869 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1870 			 struct sta_info *sta, struct sk_buff *skb,
1871 			 bool txpending)
1872 {
1873 	struct ieee80211_local *local = sdata->local;
1874 	struct ieee80211_tx_data tx;
1875 	ieee80211_tx_result res_prepare;
1876 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1877 	bool result = true;
1878 	int led_len;
1879 
1880 	if (unlikely(skb->len < 10)) {
1881 		dev_kfree_skb(skb);
1882 		return true;
1883 	}
1884 
1885 	/* initialises tx */
1886 	led_len = skb->len;
1887 	res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1888 
1889 	if (unlikely(res_prepare == TX_DROP)) {
1890 		ieee80211_free_txskb(&local->hw, skb);
1891 		return true;
1892 	} else if (unlikely(res_prepare == TX_QUEUED)) {
1893 		return true;
1894 	}
1895 
1896 	/* set up hw_queue value early */
1897 	if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1898 	    !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1899 		info->hw_queue =
1900 			sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1901 
1902 	if (invoke_tx_handlers_early(&tx))
1903 		return true;
1904 
1905 	if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1906 		return true;
1907 
1908 	if (!invoke_tx_handlers_late(&tx))
1909 		result = __ieee80211_tx(local, &tx.skbs, led_len,
1910 					tx.sta, txpending);
1911 
1912 	return result;
1913 }
1914 
1915 /* device xmit handlers */
1916 
1917 enum ieee80211_encrypt {
1918 	ENCRYPT_NO,
1919 	ENCRYPT_MGMT,
1920 	ENCRYPT_DATA,
1921 };
1922 
1923 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1924 				struct sk_buff *skb,
1925 				int head_need,
1926 				enum ieee80211_encrypt encrypt)
1927 {
1928 	struct ieee80211_local *local = sdata->local;
1929 	bool enc_tailroom;
1930 	int tail_need = 0;
1931 
1932 	enc_tailroom = encrypt == ENCRYPT_MGMT ||
1933 		       (encrypt == ENCRYPT_DATA &&
1934 			sdata->crypto_tx_tailroom_needed_cnt);
1935 
1936 	if (enc_tailroom) {
1937 		tail_need = IEEE80211_ENCRYPT_TAILROOM;
1938 		tail_need -= skb_tailroom(skb);
1939 		tail_need = max_t(int, tail_need, 0);
1940 	}
1941 
1942 	if (skb_cloned(skb) &&
1943 	    (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1944 	     !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
1945 		I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1946 	else if (head_need || tail_need)
1947 		I802_DEBUG_INC(local->tx_expand_skb_head);
1948 	else
1949 		return 0;
1950 
1951 	if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1952 		wiphy_debug(local->hw.wiphy,
1953 			    "failed to reallocate TX buffer\n");
1954 		return -ENOMEM;
1955 	}
1956 
1957 	return 0;
1958 }
1959 
1960 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1961 		    struct sta_info *sta, struct sk_buff *skb)
1962 {
1963 	struct ieee80211_local *local = sdata->local;
1964 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1965 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1966 	int headroom;
1967 	enum ieee80211_encrypt encrypt;
1968 
1969 	if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
1970 		encrypt = ENCRYPT_NO;
1971 	else if (ieee80211_is_mgmt(hdr->frame_control))
1972 		encrypt = ENCRYPT_MGMT;
1973 	else
1974 		encrypt = ENCRYPT_DATA;
1975 
1976 	headroom = local->tx_headroom;
1977 	if (encrypt != ENCRYPT_NO)
1978 		headroom += sdata->encrypt_headroom;
1979 	headroom -= skb_headroom(skb);
1980 	headroom = max_t(int, 0, headroom);
1981 
1982 	if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
1983 		ieee80211_free_txskb(&local->hw, skb);
1984 		return;
1985 	}
1986 
1987 	/* reload after potential resize */
1988 	hdr = (struct ieee80211_hdr *) skb->data;
1989 	info->control.vif = &sdata->vif;
1990 
1991 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1992 		if (ieee80211_is_data(hdr->frame_control) &&
1993 		    is_unicast_ether_addr(hdr->addr1)) {
1994 			if (mesh_nexthop_resolve(sdata, skb))
1995 				return; /* skb queued: don't free */
1996 		} else {
1997 			ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1998 		}
1999 	}
2000 
2001 	ieee80211_set_qos_hdr(sdata, skb);
2002 	ieee80211_tx(sdata, sta, skb, false);
2003 }
2004 
2005 bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2006 				 struct net_device *dev)
2007 {
2008 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2009 	struct ieee80211_radiotap_iterator iterator;
2010 	struct ieee80211_radiotap_header *rthdr =
2011 		(struct ieee80211_radiotap_header *) skb->data;
2012 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2013 	struct ieee80211_supported_band *sband =
2014 		local->hw.wiphy->bands[info->band];
2015 	int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2016 						   NULL);
2017 	u16 txflags;
2018 	u16 rate = 0;
2019 	bool rate_found = false;
2020 	u8 rate_retries = 0;
2021 	u16 rate_flags = 0;
2022 	u8 mcs_known, mcs_flags, mcs_bw;
2023 	u16 vht_known;
2024 	u8 vht_mcs = 0, vht_nss = 0;
2025 	int i;
2026 
2027 	/* check for not even having the fixed radiotap header part */
2028 	if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2029 		return false; /* too short to be possibly valid */
2030 
2031 	/* is it a header version we can trust to find length from? */
2032 	if (unlikely(rthdr->it_version))
2033 		return false; /* only version 0 is supported */
2034 
2035 	/* does the skb contain enough to deliver on the alleged length? */
2036 	if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2037 		return false; /* skb too short for claimed rt header extent */
2038 
2039 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2040 		       IEEE80211_TX_CTL_DONTFRAG;
2041 
2042 	/*
2043 	 * for every radiotap entry that is present
2044 	 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2045 	 * entries present, or -EINVAL on error)
2046 	 */
2047 
2048 	while (!ret) {
2049 		ret = ieee80211_radiotap_iterator_next(&iterator);
2050 
2051 		if (ret)
2052 			continue;
2053 
2054 		/* see if this argument is something we can use */
2055 		switch (iterator.this_arg_index) {
2056 		/*
2057 		 * You must take care when dereferencing iterator.this_arg
2058 		 * for multibyte types... the pointer is not aligned.  Use
2059 		 * get_unaligned((type *)iterator.this_arg) to dereference
2060 		 * iterator.this_arg for type "type" safely on all arches.
2061 		*/
2062 		case IEEE80211_RADIOTAP_FLAGS:
2063 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2064 				/*
2065 				 * this indicates that the skb we have been
2066 				 * handed has the 32-bit FCS CRC at the end...
2067 				 * we should react to that by snipping it off
2068 				 * because it will be recomputed and added
2069 				 * on transmission
2070 				 */
2071 				if (skb->len < (iterator._max_length + FCS_LEN))
2072 					return false;
2073 
2074 				skb_trim(skb, skb->len - FCS_LEN);
2075 			}
2076 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2077 				info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2078 			if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2079 				info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2080 			break;
2081 
2082 		case IEEE80211_RADIOTAP_TX_FLAGS:
2083 			txflags = get_unaligned_le16(iterator.this_arg);
2084 			if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2085 				info->flags |= IEEE80211_TX_CTL_NO_ACK;
2086 			if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2087 				info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
2088 			if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2089 				info->control.flags |=
2090 					IEEE80211_TX_CTRL_DONT_REORDER;
2091 			break;
2092 
2093 		case IEEE80211_RADIOTAP_RATE:
2094 			rate = *iterator.this_arg;
2095 			rate_flags = 0;
2096 			rate_found = true;
2097 			break;
2098 
2099 		case IEEE80211_RADIOTAP_DATA_RETRIES:
2100 			rate_retries = *iterator.this_arg;
2101 			break;
2102 
2103 		case IEEE80211_RADIOTAP_MCS:
2104 			mcs_known = iterator.this_arg[0];
2105 			mcs_flags = iterator.this_arg[1];
2106 			if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2107 				break;
2108 
2109 			rate_found = true;
2110 			rate = iterator.this_arg[2];
2111 			rate_flags = IEEE80211_TX_RC_MCS;
2112 
2113 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2114 			    mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2115 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2116 
2117 			mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2118 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2119 			    mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2120 				rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2121 
2122 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2123 			    mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2124 				info->flags |= IEEE80211_TX_CTL_LDPC;
2125 
2126 			if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2127 				u8 stbc = u8_get_bits(mcs_flags,
2128 						      IEEE80211_RADIOTAP_MCS_STBC_MASK);
2129 
2130 				info->flags |=
2131 					u32_encode_bits(stbc,
2132 							IEEE80211_TX_CTL_STBC);
2133 			}
2134 			break;
2135 
2136 		case IEEE80211_RADIOTAP_VHT:
2137 			vht_known = get_unaligned_le16(iterator.this_arg);
2138 			rate_found = true;
2139 
2140 			rate_flags = IEEE80211_TX_RC_VHT_MCS;
2141 			if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2142 			    (iterator.this_arg[2] &
2143 			     IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2144 				rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2145 			if (vht_known &
2146 			    IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2147 				if (iterator.this_arg[3] == 1)
2148 					rate_flags |=
2149 						IEEE80211_TX_RC_40_MHZ_WIDTH;
2150 				else if (iterator.this_arg[3] == 4)
2151 					rate_flags |=
2152 						IEEE80211_TX_RC_80_MHZ_WIDTH;
2153 				else if (iterator.this_arg[3] == 11)
2154 					rate_flags |=
2155 						IEEE80211_TX_RC_160_MHZ_WIDTH;
2156 			}
2157 
2158 			vht_mcs = iterator.this_arg[4] >> 4;
2159 			vht_nss = iterator.this_arg[4] & 0xF;
2160 			break;
2161 
2162 		/*
2163 		 * Please update the file
2164 		 * Documentation/networking/mac80211-injection.rst
2165 		 * when parsing new fields here.
2166 		 */
2167 
2168 		default:
2169 			break;
2170 		}
2171 	}
2172 
2173 	if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2174 		return false;
2175 
2176 	if (rate_found) {
2177 		info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2178 
2179 		for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2180 			info->control.rates[i].idx = -1;
2181 			info->control.rates[i].flags = 0;
2182 			info->control.rates[i].count = 0;
2183 		}
2184 
2185 		if (rate_flags & IEEE80211_TX_RC_MCS) {
2186 			info->control.rates[0].idx = rate;
2187 		} else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2188 			ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2189 					       vht_nss);
2190 		} else {
2191 			for (i = 0; i < sband->n_bitrates; i++) {
2192 				if (rate * 5 != sband->bitrates[i].bitrate)
2193 					continue;
2194 
2195 				info->control.rates[0].idx = i;
2196 				break;
2197 			}
2198 		}
2199 
2200 		if (info->control.rates[0].idx < 0)
2201 			info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2202 
2203 		info->control.rates[0].flags = rate_flags;
2204 		info->control.rates[0].count = min_t(u8, rate_retries + 1,
2205 						     local->hw.max_rate_tries);
2206 	}
2207 
2208 	return true;
2209 }
2210 
2211 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2212 					 struct net_device *dev)
2213 {
2214 	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2215 	struct ieee80211_chanctx_conf *chanctx_conf;
2216 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2217 	struct ieee80211_hdr *hdr;
2218 	struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2219 	struct cfg80211_chan_def *chandef;
2220 	u16 len_rthdr;
2221 	int hdrlen;
2222 
2223 	memset(info, 0, sizeof(*info));
2224 	info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2225 		      IEEE80211_TX_CTL_INJECTED;
2226 
2227 	/* Sanity-check and process the injection radiotap header */
2228 	if (!ieee80211_parse_tx_radiotap(skb, dev))
2229 		goto fail;
2230 
2231 	/* we now know there is a radiotap header with a length we can use */
2232 	len_rthdr = ieee80211_get_radiotap_len(skb->data);
2233 
2234 	/*
2235 	 * fix up the pointers accounting for the radiotap
2236 	 * header still being in there.  We are being given
2237 	 * a precooked IEEE80211 header so no need for
2238 	 * normal processing
2239 	 */
2240 	skb_set_mac_header(skb, len_rthdr);
2241 	/*
2242 	 * these are just fixed to the end of the rt area since we
2243 	 * don't have any better information and at this point, nobody cares
2244 	 */
2245 	skb_set_network_header(skb, len_rthdr);
2246 	skb_set_transport_header(skb, len_rthdr);
2247 
2248 	if (skb->len < len_rthdr + 2)
2249 		goto fail;
2250 
2251 	hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2252 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
2253 
2254 	if (skb->len < len_rthdr + hdrlen)
2255 		goto fail;
2256 
2257 	/*
2258 	 * Initialize skb->protocol if the injected frame is a data frame
2259 	 * carrying a rfc1042 header
2260 	 */
2261 	if (ieee80211_is_data(hdr->frame_control) &&
2262 	    skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2263 		u8 *payload = (u8 *)hdr + hdrlen;
2264 
2265 		if (ether_addr_equal(payload, rfc1042_header))
2266 			skb->protocol = cpu_to_be16((payload[6] << 8) |
2267 						    payload[7]);
2268 	}
2269 
2270 	/* Initialize skb->priority for QoS frames. If the DONT_REORDER flag
2271 	 * is set, stick to the default value for skb->priority to assure
2272 	 * frames injected with this flag are not reordered relative to each
2273 	 * other.
2274 	 */
2275 	if (ieee80211_is_data_qos(hdr->frame_control) &&
2276 	    !(info->control.flags & IEEE80211_TX_CTRL_DONT_REORDER)) {
2277 		u8 *p = ieee80211_get_qos_ctl(hdr);
2278 		skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2279 	}
2280 
2281 	rcu_read_lock();
2282 
2283 	/*
2284 	 * We process outgoing injected frames that have a local address
2285 	 * we handle as though they are non-injected frames.
2286 	 * This code here isn't entirely correct, the local MAC address
2287 	 * isn't always enough to find the interface to use; for proper
2288 	 * VLAN support we have an nl80211-based mechanism.
2289 	 *
2290 	 * This is necessary, for example, for old hostapd versions that
2291 	 * don't use nl80211-based management TX/RX.
2292 	 */
2293 	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2294 
2295 	list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2296 		if (!ieee80211_sdata_running(tmp_sdata))
2297 			continue;
2298 		if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2299 		    tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2300 			continue;
2301 		if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2302 			sdata = tmp_sdata;
2303 			break;
2304 		}
2305 	}
2306 
2307 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2308 	if (!chanctx_conf) {
2309 		tmp_sdata = rcu_dereference(local->monitor_sdata);
2310 		if (tmp_sdata)
2311 			chanctx_conf =
2312 				rcu_dereference(tmp_sdata->vif.chanctx_conf);
2313 	}
2314 
2315 	if (chanctx_conf)
2316 		chandef = &chanctx_conf->def;
2317 	else if (!local->use_chanctx)
2318 		chandef = &local->_oper_chandef;
2319 	else
2320 		goto fail_rcu;
2321 
2322 	/*
2323 	 * Frame injection is not allowed if beaconing is not allowed
2324 	 * or if we need radar detection. Beaconing is usually not allowed when
2325 	 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2326 	 * Passive scan is also used in world regulatory domains where
2327 	 * your country is not known and as such it should be treated as
2328 	 * NO TX unless the channel is explicitly allowed in which case
2329 	 * your current regulatory domain would not have the passive scan
2330 	 * flag.
2331 	 *
2332 	 * Since AP mode uses monitor interfaces to inject/TX management
2333 	 * frames we can make AP mode the exception to this rule once it
2334 	 * supports radar detection as its implementation can deal with
2335 	 * radar detection by itself. We can do that later by adding a
2336 	 * monitor flag interfaces used for AP support.
2337 	 */
2338 	if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2339 				     sdata->vif.type))
2340 		goto fail_rcu;
2341 
2342 	info->band = chandef->chan->band;
2343 
2344 	/* remove the injection radiotap header */
2345 	skb_pull(skb, len_rthdr);
2346 
2347 	ieee80211_xmit(sdata, NULL, skb);
2348 	rcu_read_unlock();
2349 
2350 	return NETDEV_TX_OK;
2351 
2352 fail_rcu:
2353 	rcu_read_unlock();
2354 fail:
2355 	dev_kfree_skb(skb);
2356 	return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2357 }
2358 
2359 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2360 {
2361 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2362 
2363 	return ethertype == ETH_P_TDLS &&
2364 	       skb->len > 14 &&
2365 	       skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2366 }
2367 
2368 int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2369 			    struct sk_buff *skb,
2370 			    struct sta_info **sta_out)
2371 {
2372 	struct sta_info *sta;
2373 
2374 	switch (sdata->vif.type) {
2375 	case NL80211_IFTYPE_AP_VLAN:
2376 		sta = rcu_dereference(sdata->u.vlan.sta);
2377 		if (sta) {
2378 			*sta_out = sta;
2379 			return 0;
2380 		} else if (sdata->wdev.use_4addr) {
2381 			return -ENOLINK;
2382 		}
2383 		fallthrough;
2384 	case NL80211_IFTYPE_AP:
2385 	case NL80211_IFTYPE_OCB:
2386 	case NL80211_IFTYPE_ADHOC:
2387 		if (is_multicast_ether_addr(skb->data)) {
2388 			*sta_out = ERR_PTR(-ENOENT);
2389 			return 0;
2390 		}
2391 		sta = sta_info_get_bss(sdata, skb->data);
2392 		break;
2393 #ifdef CONFIG_MAC80211_MESH
2394 	case NL80211_IFTYPE_MESH_POINT:
2395 		/* determined much later */
2396 		*sta_out = NULL;
2397 		return 0;
2398 #endif
2399 	case NL80211_IFTYPE_STATION:
2400 		if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2401 			sta = sta_info_get(sdata, skb->data);
2402 			if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2403 				if (test_sta_flag(sta,
2404 						  WLAN_STA_TDLS_PEER_AUTH)) {
2405 					*sta_out = sta;
2406 					return 0;
2407 				}
2408 
2409 				/*
2410 				 * TDLS link during setup - throw out frames to
2411 				 * peer. Allow TDLS-setup frames to unauthorized
2412 				 * peers for the special case of a link teardown
2413 				 * after a TDLS sta is removed due to being
2414 				 * unreachable.
2415 				 */
2416 				if (!ieee80211_is_tdls_setup(skb))
2417 					return -EINVAL;
2418 			}
2419 
2420 		}
2421 
2422 		sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2423 		if (!sta)
2424 			return -ENOLINK;
2425 		break;
2426 	default:
2427 		return -EINVAL;
2428 	}
2429 
2430 	*sta_out = sta ?: ERR_PTR(-ENOENT);
2431 	return 0;
2432 }
2433 
2434 static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
2435 				   struct sk_buff *skb,
2436 				   u32 *info_flags,
2437 				   u64 *cookie)
2438 {
2439 	struct sk_buff *ack_skb;
2440 	u16 info_id = 0;
2441 
2442 	if (skb->sk)
2443 		ack_skb = skb_clone_sk(skb);
2444 	else
2445 		ack_skb = skb_clone(skb, GFP_ATOMIC);
2446 
2447 	if (ack_skb) {
2448 		unsigned long flags;
2449 		int id;
2450 
2451 		spin_lock_irqsave(&local->ack_status_lock, flags);
2452 		id = idr_alloc(&local->ack_status_frames, ack_skb,
2453 			       1, 0x2000, GFP_ATOMIC);
2454 		spin_unlock_irqrestore(&local->ack_status_lock, flags);
2455 
2456 		if (id >= 0) {
2457 			info_id = id;
2458 			*info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2459 			if (cookie) {
2460 				*cookie = ieee80211_mgmt_tx_cookie(local);
2461 				IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2462 			}
2463 		} else {
2464 			kfree_skb(ack_skb);
2465 		}
2466 	}
2467 
2468 	return info_id;
2469 }
2470 
2471 /**
2472  * ieee80211_build_hdr - build 802.11 header in the given frame
2473  * @sdata: virtual interface to build the header for
2474  * @skb: the skb to build the header in
2475  * @info_flags: skb flags to set
2476  * @sta: the station pointer
2477  * @ctrl_flags: info control flags to set
2478  * @cookie: cookie pointer to fill (if not %NULL)
2479  *
2480  * This function takes the skb with 802.3 header and reformats the header to
2481  * the appropriate IEEE 802.11 header based on which interface the packet is
2482  * being transmitted on.
2483  *
2484  * Note that this function also takes care of the TX status request and
2485  * potential unsharing of the SKB - this needs to be interleaved with the
2486  * header building.
2487  *
2488  * The function requires the read-side RCU lock held
2489  *
2490  * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2491  */
2492 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2493 					   struct sk_buff *skb, u32 info_flags,
2494 					   struct sta_info *sta, u32 ctrl_flags,
2495 					   u64 *cookie)
2496 {
2497 	struct ieee80211_local *local = sdata->local;
2498 	struct ieee80211_tx_info *info;
2499 	int head_need;
2500 	u16 ethertype, hdrlen,  meshhdrlen = 0;
2501 	__le16 fc;
2502 	struct ieee80211_hdr hdr;
2503 	struct ieee80211s_hdr mesh_hdr __maybe_unused;
2504 	struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2505 	const u8 *encaps_data;
2506 	int encaps_len, skip_header_bytes;
2507 	bool wme_sta = false, authorized = false;
2508 	bool tdls_peer;
2509 	bool multicast;
2510 	u16 info_id = 0;
2511 	struct ieee80211_chanctx_conf *chanctx_conf;
2512 	struct ieee80211_sub_if_data *ap_sdata;
2513 	enum nl80211_band band;
2514 	int ret;
2515 
2516 	if (IS_ERR(sta))
2517 		sta = NULL;
2518 
2519 #ifdef CONFIG_MAC80211_DEBUGFS
2520 	if (local->force_tx_status)
2521 		info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2522 #endif
2523 
2524 	/* convert Ethernet header to proper 802.11 header (based on
2525 	 * operation mode) */
2526 	ethertype = (skb->data[12] << 8) | skb->data[13];
2527 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2528 
2529 	switch (sdata->vif.type) {
2530 	case NL80211_IFTYPE_AP_VLAN:
2531 		if (sdata->wdev.use_4addr) {
2532 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2533 			/* RA TA DA SA */
2534 			memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2535 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2536 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2537 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2538 			hdrlen = 30;
2539 			authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2540 			wme_sta = sta->sta.wme;
2541 		}
2542 		ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2543 					u.ap);
2544 		chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2545 		if (!chanctx_conf) {
2546 			ret = -ENOTCONN;
2547 			goto free;
2548 		}
2549 		band = chanctx_conf->def.chan->band;
2550 		if (sdata->wdev.use_4addr)
2551 			break;
2552 		fallthrough;
2553 	case NL80211_IFTYPE_AP:
2554 		if (sdata->vif.type == NL80211_IFTYPE_AP)
2555 			chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2556 		if (!chanctx_conf) {
2557 			ret = -ENOTCONN;
2558 			goto free;
2559 		}
2560 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2561 		/* DA BSSID SA */
2562 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2563 		memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2564 		memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2565 		hdrlen = 24;
2566 		band = chanctx_conf->def.chan->band;
2567 		break;
2568 #ifdef CONFIG_MAC80211_MESH
2569 	case NL80211_IFTYPE_MESH_POINT:
2570 		if (!is_multicast_ether_addr(skb->data)) {
2571 			struct sta_info *next_hop;
2572 			bool mpp_lookup = true;
2573 
2574 			mpath = mesh_path_lookup(sdata, skb->data);
2575 			if (mpath) {
2576 				mpp_lookup = false;
2577 				next_hop = rcu_dereference(mpath->next_hop);
2578 				if (!next_hop ||
2579 				    !(mpath->flags & (MESH_PATH_ACTIVE |
2580 						      MESH_PATH_RESOLVING)))
2581 					mpp_lookup = true;
2582 			}
2583 
2584 			if (mpp_lookup) {
2585 				mppath = mpp_path_lookup(sdata, skb->data);
2586 				if (mppath)
2587 					mppath->exp_time = jiffies;
2588 			}
2589 
2590 			if (mppath && mpath)
2591 				mesh_path_del(sdata, mpath->dst);
2592 		}
2593 
2594 		/*
2595 		 * Use address extension if it is a packet from
2596 		 * another interface or if we know the destination
2597 		 * is being proxied by a portal (i.e. portal address
2598 		 * differs from proxied address)
2599 		 */
2600 		if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2601 		    !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2602 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2603 					skb->data, skb->data + ETH_ALEN);
2604 			meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2605 							       NULL, NULL);
2606 		} else {
2607 			/* DS -> MBSS (802.11-2012 13.11.3.3).
2608 			 * For unicast with unknown forwarding information,
2609 			 * destination might be in the MBSS or if that fails
2610 			 * forwarded to another mesh gate. In either case
2611 			 * resolution will be handled in ieee80211_xmit(), so
2612 			 * leave the original DA. This also works for mcast */
2613 			const u8 *mesh_da = skb->data;
2614 
2615 			if (mppath)
2616 				mesh_da = mppath->mpp;
2617 			else if (mpath)
2618 				mesh_da = mpath->dst;
2619 
2620 			hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2621 					mesh_da, sdata->vif.addr);
2622 			if (is_multicast_ether_addr(mesh_da))
2623 				/* DA TA mSA AE:SA */
2624 				meshhdrlen = ieee80211_new_mesh_header(
2625 						sdata, &mesh_hdr,
2626 						skb->data + ETH_ALEN, NULL);
2627 			else
2628 				/* RA TA mDA mSA AE:DA SA */
2629 				meshhdrlen = ieee80211_new_mesh_header(
2630 						sdata, &mesh_hdr, skb->data,
2631 						skb->data + ETH_ALEN);
2632 
2633 		}
2634 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2635 		if (!chanctx_conf) {
2636 			ret = -ENOTCONN;
2637 			goto free;
2638 		}
2639 		band = chanctx_conf->def.chan->band;
2640 
2641 		/* For injected frames, fill RA right away as nexthop lookup
2642 		 * will be skipped.
2643 		 */
2644 		if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2645 		    is_zero_ether_addr(hdr.addr1))
2646 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2647 		break;
2648 #endif
2649 	case NL80211_IFTYPE_STATION:
2650 		/* we already did checks when looking up the RA STA */
2651 		tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2652 
2653 		if (tdls_peer) {
2654 			/* DA SA BSSID */
2655 			memcpy(hdr.addr1, skb->data, ETH_ALEN);
2656 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2657 			memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2658 			hdrlen = 24;
2659 		}  else if (sdata->u.mgd.use_4addr &&
2660 			    cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2661 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2662 					  IEEE80211_FCTL_TODS);
2663 			/* RA TA DA SA */
2664 			memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2665 			memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2666 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2667 			memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2668 			hdrlen = 30;
2669 		} else {
2670 			fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2671 			/* BSSID SA DA */
2672 			memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2673 			memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2674 			memcpy(hdr.addr3, skb->data, ETH_ALEN);
2675 			hdrlen = 24;
2676 		}
2677 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2678 		if (!chanctx_conf) {
2679 			ret = -ENOTCONN;
2680 			goto free;
2681 		}
2682 		band = chanctx_conf->def.chan->band;
2683 		break;
2684 	case NL80211_IFTYPE_OCB:
2685 		/* DA SA BSSID */
2686 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2687 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2688 		eth_broadcast_addr(hdr.addr3);
2689 		hdrlen = 24;
2690 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2691 		if (!chanctx_conf) {
2692 			ret = -ENOTCONN;
2693 			goto free;
2694 		}
2695 		band = chanctx_conf->def.chan->band;
2696 		break;
2697 	case NL80211_IFTYPE_ADHOC:
2698 		/* DA SA BSSID */
2699 		memcpy(hdr.addr1, skb->data, ETH_ALEN);
2700 		memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2701 		memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2702 		hdrlen = 24;
2703 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2704 		if (!chanctx_conf) {
2705 			ret = -ENOTCONN;
2706 			goto free;
2707 		}
2708 		band = chanctx_conf->def.chan->band;
2709 		break;
2710 	default:
2711 		ret = -EINVAL;
2712 		goto free;
2713 	}
2714 
2715 	multicast = is_multicast_ether_addr(hdr.addr1);
2716 
2717 	/* sta is always NULL for mesh */
2718 	if (sta) {
2719 		authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2720 		wme_sta = sta->sta.wme;
2721 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2722 		/* For mesh, the use of the QoS header is mandatory */
2723 		wme_sta = true;
2724 	}
2725 
2726 	/* receiver does QoS (which also means we do) use it */
2727 	if (wme_sta) {
2728 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2729 		hdrlen += 2;
2730 	}
2731 
2732 	/*
2733 	 * Drop unicast frames to unauthorised stations unless they are
2734 	 * EAPOL frames from the local station.
2735 	 */
2736 	if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2737 		     (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2738 		     !multicast && !authorized &&
2739 		     (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2740 		      !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2741 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2742 		net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2743 				    sdata->name, hdr.addr1);
2744 #endif
2745 
2746 		I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2747 
2748 		ret = -EPERM;
2749 		goto free;
2750 	}
2751 
2752 	if (unlikely(!multicast && ((skb->sk &&
2753 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2754 		     ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2755 		info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2756 						  cookie);
2757 
2758 	/*
2759 	 * If the skb is shared we need to obtain our own copy.
2760 	 */
2761 	if (skb_shared(skb)) {
2762 		struct sk_buff *tmp_skb = skb;
2763 
2764 		/* can't happen -- skb is a clone if info_id != 0 */
2765 		WARN_ON(info_id);
2766 
2767 		skb = skb_clone(skb, GFP_ATOMIC);
2768 		kfree_skb(tmp_skb);
2769 
2770 		if (!skb) {
2771 			ret = -ENOMEM;
2772 			goto free;
2773 		}
2774 	}
2775 
2776 	hdr.frame_control = fc;
2777 	hdr.duration_id = 0;
2778 	hdr.seq_ctrl = 0;
2779 
2780 	skip_header_bytes = ETH_HLEN;
2781 	if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2782 		encaps_data = bridge_tunnel_header;
2783 		encaps_len = sizeof(bridge_tunnel_header);
2784 		skip_header_bytes -= 2;
2785 	} else if (ethertype >= ETH_P_802_3_MIN) {
2786 		encaps_data = rfc1042_header;
2787 		encaps_len = sizeof(rfc1042_header);
2788 		skip_header_bytes -= 2;
2789 	} else {
2790 		encaps_data = NULL;
2791 		encaps_len = 0;
2792 	}
2793 
2794 	skb_pull(skb, skip_header_bytes);
2795 	head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2796 
2797 	/*
2798 	 * So we need to modify the skb header and hence need a copy of
2799 	 * that. The head_need variable above doesn't, so far, include
2800 	 * the needed header space that we don't need right away. If we
2801 	 * can, then we don't reallocate right now but only after the
2802 	 * frame arrives at the master device (if it does...)
2803 	 *
2804 	 * If we cannot, however, then we will reallocate to include all
2805 	 * the ever needed space. Also, if we need to reallocate it anyway,
2806 	 * make it big enough for everything we may ever need.
2807 	 */
2808 
2809 	if (head_need > 0 || skb_cloned(skb)) {
2810 		head_need += sdata->encrypt_headroom;
2811 		head_need += local->tx_headroom;
2812 		head_need = max_t(int, 0, head_need);
2813 		if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
2814 			ieee80211_free_txskb(&local->hw, skb);
2815 			skb = NULL;
2816 			return ERR_PTR(-ENOMEM);
2817 		}
2818 	}
2819 
2820 	if (encaps_data)
2821 		memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2822 
2823 #ifdef CONFIG_MAC80211_MESH
2824 	if (meshhdrlen > 0)
2825 		memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2826 #endif
2827 
2828 	if (ieee80211_is_data_qos(fc)) {
2829 		__le16 *qos_control;
2830 
2831 		qos_control = skb_push(skb, 2);
2832 		memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2833 		/*
2834 		 * Maybe we could actually set some fields here, for now just
2835 		 * initialise to zero to indicate no special operation.
2836 		 */
2837 		*qos_control = 0;
2838 	} else
2839 		memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2840 
2841 	skb_reset_mac_header(skb);
2842 
2843 	info = IEEE80211_SKB_CB(skb);
2844 	memset(info, 0, sizeof(*info));
2845 
2846 	info->flags = info_flags;
2847 	info->ack_frame_id = info_id;
2848 	info->band = band;
2849 	info->control.flags = ctrl_flags;
2850 
2851 	return skb;
2852  free:
2853 	kfree_skb(skb);
2854 	return ERR_PTR(ret);
2855 }
2856 
2857 /*
2858  * fast-xmit overview
2859  *
2860  * The core idea of this fast-xmit is to remove per-packet checks by checking
2861  * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2862  * checks that are needed to get the sta->fast_tx pointer assigned, after which
2863  * much less work can be done per packet. For example, fragmentation must be
2864  * disabled or the fast_tx pointer will not be set. All the conditions are seen
2865  * in the code here.
2866  *
2867  * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2868  * header and other data to aid packet processing in ieee80211_xmit_fast().
2869  *
2870  * The most difficult part of this is that when any of these assumptions
2871  * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2872  * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2873  * since the per-packet code no longer checks the conditions. This is reflected
2874  * by the calls to these functions throughout the rest of the code, and must be
2875  * maintained if any of the TX path checks change.
2876  */
2877 
2878 void ieee80211_check_fast_xmit(struct sta_info *sta)
2879 {
2880 	struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2881 	struct ieee80211_local *local = sta->local;
2882 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2883 	struct ieee80211_hdr *hdr = (void *)build.hdr;
2884 	struct ieee80211_chanctx_conf *chanctx_conf;
2885 	__le16 fc;
2886 
2887 	if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2888 		return;
2889 
2890 	/* Locking here protects both the pointer itself, and against concurrent
2891 	 * invocations winning data access races to, e.g., the key pointer that
2892 	 * is used.
2893 	 * Without it, the invocation of this function right after the key
2894 	 * pointer changes wouldn't be sufficient, as another CPU could access
2895 	 * the pointer, then stall, and then do the cache update after the CPU
2896 	 * that invalidated the key.
2897 	 * With the locking, such scenarios cannot happen as the check for the
2898 	 * key and the fast-tx assignment are done atomically, so the CPU that
2899 	 * modifies the key will either wait or other one will see the key
2900 	 * cleared/changed already.
2901 	 */
2902 	spin_lock_bh(&sta->lock);
2903 	if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2904 	    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2905 	    sdata->vif.type == NL80211_IFTYPE_STATION)
2906 		goto out;
2907 
2908 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2909 		goto out;
2910 
2911 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2912 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2913 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2914 	    test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2915 		goto out;
2916 
2917 	if (sdata->noack_map)
2918 		goto out;
2919 
2920 	/* fast-xmit doesn't handle fragmentation at all */
2921 	if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2922 	    !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
2923 		goto out;
2924 
2925 	rcu_read_lock();
2926 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2927 	if (!chanctx_conf) {
2928 		rcu_read_unlock();
2929 		goto out;
2930 	}
2931 	build.band = chanctx_conf->def.chan->band;
2932 	rcu_read_unlock();
2933 
2934 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2935 
2936 	switch (sdata->vif.type) {
2937 	case NL80211_IFTYPE_ADHOC:
2938 		/* DA SA BSSID */
2939 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2940 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2941 		memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2942 		build.hdr_len = 24;
2943 		break;
2944 	case NL80211_IFTYPE_STATION:
2945 		if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2946 			/* DA SA BSSID */
2947 			build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2948 			build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2949 			memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2950 			build.hdr_len = 24;
2951 			break;
2952 		}
2953 
2954 		if (sdata->u.mgd.use_4addr) {
2955 			/* non-regular ethertype cannot use the fastpath */
2956 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2957 					  IEEE80211_FCTL_TODS);
2958 			/* RA TA DA SA */
2959 			memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2960 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2961 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2962 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2963 			build.hdr_len = 30;
2964 			break;
2965 		}
2966 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2967 		/* BSSID SA DA */
2968 		memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2969 		build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2970 		build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2971 		build.hdr_len = 24;
2972 		break;
2973 	case NL80211_IFTYPE_AP_VLAN:
2974 		if (sdata->wdev.use_4addr) {
2975 			fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2976 					  IEEE80211_FCTL_TODS);
2977 			/* RA TA DA SA */
2978 			memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2979 			memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2980 			build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2981 			build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2982 			build.hdr_len = 30;
2983 			break;
2984 		}
2985 		fallthrough;
2986 	case NL80211_IFTYPE_AP:
2987 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2988 		/* DA BSSID SA */
2989 		build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2990 		memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2991 		build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2992 		build.hdr_len = 24;
2993 		break;
2994 	default:
2995 		/* not handled on fast-xmit */
2996 		goto out;
2997 	}
2998 
2999 	if (sta->sta.wme) {
3000 		build.hdr_len += 2;
3001 		fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3002 	}
3003 
3004 	/* We store the key here so there's no point in using rcu_dereference()
3005 	 * but that's fine because the code that changes the pointers will call
3006 	 * this function after doing so. For a single CPU that would be enough,
3007 	 * for multiple see the comment above.
3008 	 */
3009 	build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3010 	if (!build.key)
3011 		build.key = rcu_access_pointer(sdata->default_unicast_key);
3012 	if (build.key) {
3013 		bool gen_iv, iv_spc, mmic;
3014 
3015 		gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3016 		iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3017 		mmic = build.key->conf.flags &
3018 			(IEEE80211_KEY_FLAG_GENERATE_MMIC |
3019 			 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3020 
3021 		/* don't handle software crypto */
3022 		if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3023 			goto out;
3024 
3025 		/* Key is being removed */
3026 		if (build.key->flags & KEY_FLAG_TAINTED)
3027 			goto out;
3028 
3029 		switch (build.key->conf.cipher) {
3030 		case WLAN_CIPHER_SUITE_CCMP:
3031 		case WLAN_CIPHER_SUITE_CCMP_256:
3032 			if (gen_iv)
3033 				build.pn_offs = build.hdr_len;
3034 			if (gen_iv || iv_spc)
3035 				build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3036 			break;
3037 		case WLAN_CIPHER_SUITE_GCMP:
3038 		case WLAN_CIPHER_SUITE_GCMP_256:
3039 			if (gen_iv)
3040 				build.pn_offs = build.hdr_len;
3041 			if (gen_iv || iv_spc)
3042 				build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3043 			break;
3044 		case WLAN_CIPHER_SUITE_TKIP:
3045 			/* cannot handle MMIC or IV generation in xmit-fast */
3046 			if (mmic || gen_iv)
3047 				goto out;
3048 			if (iv_spc)
3049 				build.hdr_len += IEEE80211_TKIP_IV_LEN;
3050 			break;
3051 		case WLAN_CIPHER_SUITE_WEP40:
3052 		case WLAN_CIPHER_SUITE_WEP104:
3053 			/* cannot handle IV generation in fast-xmit */
3054 			if (gen_iv)
3055 				goto out;
3056 			if (iv_spc)
3057 				build.hdr_len += IEEE80211_WEP_IV_LEN;
3058 			break;
3059 		case WLAN_CIPHER_SUITE_AES_CMAC:
3060 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3061 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3062 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3063 			WARN(1,
3064 			     "management cipher suite 0x%x enabled for data\n",
3065 			     build.key->conf.cipher);
3066 			goto out;
3067 		default:
3068 			/* we don't know how to generate IVs for this at all */
3069 			if (WARN_ON(gen_iv))
3070 				goto out;
3071 			/* pure hardware keys are OK, of course */
3072 			if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3073 				break;
3074 			/* cipher scheme might require space allocation */
3075 			if (iv_spc &&
3076 			    build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3077 				goto out;
3078 			if (iv_spc)
3079 				build.hdr_len += build.key->conf.iv_len;
3080 		}
3081 
3082 		fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3083 	}
3084 
3085 	hdr->frame_control = fc;
3086 
3087 	memcpy(build.hdr + build.hdr_len,
3088 	       rfc1042_header,  sizeof(rfc1042_header));
3089 	build.hdr_len += sizeof(rfc1042_header);
3090 
3091 	fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3092 	/* if the kmemdup fails, continue w/o fast_tx */
3093 	if (!fast_tx)
3094 		goto out;
3095 
3096  out:
3097 	/* we might have raced against another call to this function */
3098 	old = rcu_dereference_protected(sta->fast_tx,
3099 					lockdep_is_held(&sta->lock));
3100 	rcu_assign_pointer(sta->fast_tx, fast_tx);
3101 	if (old)
3102 		kfree_rcu(old, rcu_head);
3103 	spin_unlock_bh(&sta->lock);
3104 }
3105 
3106 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3107 {
3108 	struct sta_info *sta;
3109 
3110 	rcu_read_lock();
3111 	list_for_each_entry_rcu(sta, &local->sta_list, list)
3112 		ieee80211_check_fast_xmit(sta);
3113 	rcu_read_unlock();
3114 }
3115 
3116 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3117 {
3118 	struct ieee80211_local *local = sdata->local;
3119 	struct sta_info *sta;
3120 
3121 	rcu_read_lock();
3122 
3123 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
3124 		if (sdata != sta->sdata &&
3125 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3126 			continue;
3127 		ieee80211_check_fast_xmit(sta);
3128 	}
3129 
3130 	rcu_read_unlock();
3131 }
3132 
3133 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3134 {
3135 	struct ieee80211_fast_tx *fast_tx;
3136 
3137 	spin_lock_bh(&sta->lock);
3138 	fast_tx = rcu_dereference_protected(sta->fast_tx,
3139 					    lockdep_is_held(&sta->lock));
3140 	RCU_INIT_POINTER(sta->fast_tx, NULL);
3141 	spin_unlock_bh(&sta->lock);
3142 
3143 	if (fast_tx)
3144 		kfree_rcu(fast_tx, rcu_head);
3145 }
3146 
3147 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3148 					struct sk_buff *skb, int headroom)
3149 {
3150 	if (skb_headroom(skb) < headroom) {
3151 		I802_DEBUG_INC(local->tx_expand_skb_head);
3152 
3153 		if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3154 			wiphy_debug(local->hw.wiphy,
3155 				    "failed to reallocate TX buffer\n");
3156 			return false;
3157 		}
3158 	}
3159 
3160 	return true;
3161 }
3162 
3163 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3164 					 struct ieee80211_fast_tx *fast_tx,
3165 					 struct sk_buff *skb)
3166 {
3167 	struct ieee80211_local *local = sdata->local;
3168 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3169 	struct ieee80211_hdr *hdr;
3170 	struct ethhdr *amsdu_hdr;
3171 	int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3172 	int subframe_len = skb->len - hdr_len;
3173 	void *data;
3174 	u8 *qc, *h_80211_src, *h_80211_dst;
3175 	const u8 *bssid;
3176 
3177 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3178 		return false;
3179 
3180 	if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3181 		return true;
3182 
3183 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
3184 		return false;
3185 
3186 	data = skb_push(skb, sizeof(*amsdu_hdr));
3187 	memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3188 	hdr = data;
3189 	amsdu_hdr = data + hdr_len;
3190 	/* h_80211_src/dst is addr* field within hdr */
3191 	h_80211_src = data + fast_tx->sa_offs;
3192 	h_80211_dst = data + fast_tx->da_offs;
3193 
3194 	amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3195 	ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3196 	ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3197 
3198 	/* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3199 	 * fields needs to be changed to BSSID for A-MSDU frames depending
3200 	 * on FromDS/ToDS values.
3201 	 */
3202 	switch (sdata->vif.type) {
3203 	case NL80211_IFTYPE_STATION:
3204 		bssid = sdata->u.mgd.bssid;
3205 		break;
3206 	case NL80211_IFTYPE_AP:
3207 	case NL80211_IFTYPE_AP_VLAN:
3208 		bssid = sdata->vif.addr;
3209 		break;
3210 	default:
3211 		bssid = NULL;
3212 	}
3213 
3214 	if (bssid && ieee80211_has_fromds(hdr->frame_control))
3215 		ether_addr_copy(h_80211_src, bssid);
3216 
3217 	if (bssid && ieee80211_has_tods(hdr->frame_control))
3218 		ether_addr_copy(h_80211_dst, bssid);
3219 
3220 	qc = ieee80211_get_qos_ctl(hdr);
3221 	*qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3222 
3223 	info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3224 
3225 	return true;
3226 }
3227 
3228 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3229 				      struct sta_info *sta,
3230 				      struct ieee80211_fast_tx *fast_tx,
3231 				      struct sk_buff *skb)
3232 {
3233 	struct ieee80211_local *local = sdata->local;
3234 	struct fq *fq = &local->fq;
3235 	struct fq_tin *tin;
3236 	struct fq_flow *flow;
3237 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3238 	struct ieee80211_txq *txq = sta->sta.txq[tid];
3239 	struct txq_info *txqi;
3240 	struct sk_buff **frag_tail, *head;
3241 	int subframe_len = skb->len - ETH_ALEN;
3242 	u8 max_subframes = sta->sta.max_amsdu_subframes;
3243 	int max_frags = local->hw.max_tx_fragments;
3244 	int max_amsdu_len = sta->sta.max_amsdu_len;
3245 	int orig_truesize;
3246 	u32 flow_idx;
3247 	__be16 len;
3248 	void *data;
3249 	bool ret = false;
3250 	unsigned int orig_len;
3251 	int n = 2, nfrags, pad = 0;
3252 	u16 hdrlen;
3253 
3254 	if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3255 		return false;
3256 
3257 	if (skb_is_gso(skb))
3258 		return false;
3259 
3260 	if (!txq)
3261 		return false;
3262 
3263 	txqi = to_txq_info(txq);
3264 	if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3265 		return false;
3266 
3267 	if (sta->sta.max_rc_amsdu_len)
3268 		max_amsdu_len = min_t(int, max_amsdu_len,
3269 				      sta->sta.max_rc_amsdu_len);
3270 
3271 	if (sta->sta.max_tid_amsdu_len[tid])
3272 		max_amsdu_len = min_t(int, max_amsdu_len,
3273 				      sta->sta.max_tid_amsdu_len[tid]);
3274 
3275 	flow_idx = fq_flow_idx(fq, skb);
3276 
3277 	spin_lock_bh(&fq->lock);
3278 
3279 	/* TODO: Ideally aggregation should be done on dequeue to remain
3280 	 * responsive to environment changes.
3281 	 */
3282 
3283 	tin = &txqi->tin;
3284 	flow = fq_flow_classify(fq, tin, flow_idx, skb);
3285 	head = skb_peek_tail(&flow->queue);
3286 	if (!head || skb_is_gso(head))
3287 		goto out;
3288 
3289 	orig_truesize = head->truesize;
3290 	orig_len = head->len;
3291 
3292 	if (skb->len + head->len > max_amsdu_len)
3293 		goto out;
3294 
3295 	nfrags = 1 + skb_shinfo(skb)->nr_frags;
3296 	nfrags += 1 + skb_shinfo(head)->nr_frags;
3297 	frag_tail = &skb_shinfo(head)->frag_list;
3298 	while (*frag_tail) {
3299 		nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3300 		frag_tail = &(*frag_tail)->next;
3301 		n++;
3302 	}
3303 
3304 	if (max_subframes && n > max_subframes)
3305 		goto out;
3306 
3307 	if (max_frags && nfrags > max_frags)
3308 		goto out;
3309 
3310 	if (!drv_can_aggregate_in_amsdu(local, head, skb))
3311 		goto out;
3312 
3313 	if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3314 		goto out;
3315 
3316 	/*
3317 	 * Pad out the previous subframe to a multiple of 4 by adding the
3318 	 * padding to the next one, that's being added. Note that head->len
3319 	 * is the length of the full A-MSDU, but that works since each time
3320 	 * we add a new subframe we pad out the previous one to a multiple
3321 	 * of 4 and thus it no longer matters in the next round.
3322 	 */
3323 	hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3324 	if ((head->len - hdrlen) & 3)
3325 		pad = 4 - ((head->len - hdrlen) & 3);
3326 
3327 	if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3328 						     2 + pad))
3329 		goto out_recalc;
3330 
3331 	ret = true;
3332 	data = skb_push(skb, ETH_ALEN + 2);
3333 	memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3334 
3335 	data += 2 * ETH_ALEN;
3336 	len = cpu_to_be16(subframe_len);
3337 	memcpy(data, &len, 2);
3338 	memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3339 
3340 	memset(skb_push(skb, pad), 0, pad);
3341 
3342 	head->len += skb->len;
3343 	head->data_len += skb->len;
3344 	*frag_tail = skb;
3345 
3346 out_recalc:
3347 	fq->memory_usage += head->truesize - orig_truesize;
3348 	if (head->len != orig_len) {
3349 		flow->backlog += head->len - orig_len;
3350 		tin->backlog_bytes += head->len - orig_len;
3351 	}
3352 out:
3353 	spin_unlock_bh(&fq->lock);
3354 
3355 	return ret;
3356 }
3357 
3358 /*
3359  * Can be called while the sta lock is held. Anything that can cause packets to
3360  * be generated will cause deadlock!
3361  */
3362 static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3363 				       struct sta_info *sta, u8 pn_offs,
3364 				       struct ieee80211_key *key,
3365 				       struct sk_buff *skb)
3366 {
3367 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3368 	struct ieee80211_hdr *hdr = (void *)skb->data;
3369 	u8 tid = IEEE80211_NUM_TIDS;
3370 
3371 	if (key)
3372 		info->control.hw_key = &key->conf;
3373 
3374 	dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
3375 
3376 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3377 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3378 		hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3379 	} else {
3380 		info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3381 		hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3382 		sdata->sequence_number += 0x10;
3383 	}
3384 
3385 	if (skb_shinfo(skb)->gso_size)
3386 		sta->tx_stats.msdu[tid] +=
3387 			DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3388 	else
3389 		sta->tx_stats.msdu[tid]++;
3390 
3391 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3392 
3393 	/* statistics normally done by ieee80211_tx_h_stats (but that
3394 	 * has to consider fragmentation, so is more complex)
3395 	 */
3396 	sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3397 	sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3398 
3399 	if (pn_offs) {
3400 		u64 pn;
3401 		u8 *crypto_hdr = skb->data + pn_offs;
3402 
3403 		switch (key->conf.cipher) {
3404 		case WLAN_CIPHER_SUITE_CCMP:
3405 		case WLAN_CIPHER_SUITE_CCMP_256:
3406 		case WLAN_CIPHER_SUITE_GCMP:
3407 		case WLAN_CIPHER_SUITE_GCMP_256:
3408 			pn = atomic64_inc_return(&key->conf.tx_pn);
3409 			crypto_hdr[0] = pn;
3410 			crypto_hdr[1] = pn >> 8;
3411 			crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3412 			crypto_hdr[4] = pn >> 16;
3413 			crypto_hdr[5] = pn >> 24;
3414 			crypto_hdr[6] = pn >> 32;
3415 			crypto_hdr[7] = pn >> 40;
3416 			break;
3417 		}
3418 	}
3419 }
3420 
3421 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3422 				struct sta_info *sta,
3423 				struct ieee80211_fast_tx *fast_tx,
3424 				struct sk_buff *skb)
3425 {
3426 	struct ieee80211_local *local = sdata->local;
3427 	u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3428 	int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3429 	int hw_headroom = sdata->local->hw.extra_tx_headroom;
3430 	struct ethhdr eth;
3431 	struct ieee80211_tx_info *info;
3432 	struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3433 	struct ieee80211_tx_data tx;
3434 	ieee80211_tx_result r;
3435 	struct tid_ampdu_tx *tid_tx = NULL;
3436 	u8 tid = IEEE80211_NUM_TIDS;
3437 
3438 	/* control port protocol needs a lot of special handling */
3439 	if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3440 		return false;
3441 
3442 	/* only RFC 1042 SNAP */
3443 	if (ethertype < ETH_P_802_3_MIN)
3444 		return false;
3445 
3446 	/* don't handle TX status request here either */
3447 	if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3448 		return false;
3449 
3450 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3451 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3452 		tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3453 		if (tid_tx) {
3454 			if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3455 				return false;
3456 			if (tid_tx->timeout)
3457 				tid_tx->last_tx = jiffies;
3458 		}
3459 	}
3460 
3461 	/* after this point (skb is modified) we cannot return false */
3462 
3463 	if (skb_shared(skb)) {
3464 		struct sk_buff *tmp_skb = skb;
3465 
3466 		skb = skb_clone(skb, GFP_ATOMIC);
3467 		kfree_skb(tmp_skb);
3468 
3469 		if (!skb)
3470 			return true;
3471 	}
3472 
3473 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3474 	    ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3475 		return true;
3476 
3477 	/* will not be crypto-handled beyond what we do here, so use false
3478 	 * as the may-encrypt argument for the resize to not account for
3479 	 * more room than we already have in 'extra_head'
3480 	 */
3481 	if (unlikely(ieee80211_skb_resize(sdata, skb,
3482 					  max_t(int, extra_head + hw_headroom -
3483 						     skb_headroom(skb), 0),
3484 					  ENCRYPT_NO))) {
3485 		kfree_skb(skb);
3486 		return true;
3487 	}
3488 
3489 	memcpy(&eth, skb->data, ETH_HLEN - 2);
3490 	hdr = skb_push(skb, extra_head);
3491 	memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3492 	memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3493 	memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3494 
3495 	info = IEEE80211_SKB_CB(skb);
3496 	memset(info, 0, sizeof(*info));
3497 	info->band = fast_tx->band;
3498 	info->control.vif = &sdata->vif;
3499 	info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3500 		      IEEE80211_TX_CTL_DONTFRAG |
3501 		      (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3502 	info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
3503 
3504 #ifdef CONFIG_MAC80211_DEBUGFS
3505 	if (local->force_tx_status)
3506 		info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3507 #endif
3508 
3509 	if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3510 		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3511 		*ieee80211_get_qos_ctl(hdr) = tid;
3512 	}
3513 
3514 	__skb_queue_head_init(&tx.skbs);
3515 
3516 	tx.flags = IEEE80211_TX_UNICAST;
3517 	tx.local = local;
3518 	tx.sdata = sdata;
3519 	tx.sta = sta;
3520 	tx.key = fast_tx->key;
3521 
3522 	if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3523 		tx.skb = skb;
3524 		r = ieee80211_tx_h_rate_ctrl(&tx);
3525 		skb = tx.skb;
3526 		tx.skb = NULL;
3527 
3528 		if (r != TX_CONTINUE) {
3529 			if (r != TX_QUEUED)
3530 				kfree_skb(skb);
3531 			return true;
3532 		}
3533 	}
3534 
3535 	if (ieee80211_queue_skb(local, sdata, sta, skb))
3536 		return true;
3537 
3538 	ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3539 				   fast_tx->key, skb);
3540 
3541 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3542 		sdata = container_of(sdata->bss,
3543 				     struct ieee80211_sub_if_data, u.ap);
3544 
3545 	__skb_queue_tail(&tx.skbs, skb);
3546 	ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3547 	return true;
3548 }
3549 
3550 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3551 				     struct ieee80211_txq *txq)
3552 {
3553 	struct ieee80211_local *local = hw_to_local(hw);
3554 	struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3555 	struct ieee80211_hdr *hdr;
3556 	struct sk_buff *skb = NULL;
3557 	struct fq *fq = &local->fq;
3558 	struct fq_tin *tin = &txqi->tin;
3559 	struct ieee80211_tx_info *info;
3560 	struct ieee80211_tx_data tx;
3561 	ieee80211_tx_result r;
3562 	struct ieee80211_vif *vif = txq->vif;
3563 
3564 	WARN_ON_ONCE(softirq_count() == 0);
3565 
3566 	if (!ieee80211_txq_airtime_check(hw, txq))
3567 		return NULL;
3568 
3569 begin:
3570 	spin_lock_bh(&fq->lock);
3571 
3572 	if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3573 	    test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
3574 		goto out;
3575 
3576 	if (vif->txqs_stopped[txq->ac]) {
3577 		set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3578 		goto out;
3579 	}
3580 
3581 	/* Make sure fragments stay together. */
3582 	skb = __skb_dequeue(&txqi->frags);
3583 	if (skb)
3584 		goto out;
3585 
3586 	skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3587 	if (!skb)
3588 		goto out;
3589 
3590 	spin_unlock_bh(&fq->lock);
3591 
3592 	hdr = (struct ieee80211_hdr *)skb->data;
3593 	info = IEEE80211_SKB_CB(skb);
3594 
3595 	memset(&tx, 0, sizeof(tx));
3596 	__skb_queue_head_init(&tx.skbs);
3597 	tx.local = local;
3598 	tx.skb = skb;
3599 	tx.sdata = vif_to_sdata(info->control.vif);
3600 
3601 	if (txq->sta) {
3602 		tx.sta = container_of(txq->sta, struct sta_info, sta);
3603 		/*
3604 		 * Drop unicast frames to unauthorised stations unless they are
3605 		 * injected frames or EAPOL frames from the local station.
3606 		 */
3607 		if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3608 			     ieee80211_is_data(hdr->frame_control) &&
3609 			     !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
3610 			     tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3611 			     !is_multicast_ether_addr(hdr->addr1) &&
3612 			     !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3613 			     (!(info->control.flags &
3614 				IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3615 			      !ether_addr_equal(tx.sdata->vif.addr,
3616 						hdr->addr2)))) {
3617 			I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3618 			ieee80211_free_txskb(&local->hw, skb);
3619 			goto begin;
3620 		}
3621 	}
3622 
3623 	/*
3624 	 * The key can be removed while the packet was queued, so need to call
3625 	 * this here to get the current key.
3626 	 */
3627 	r = ieee80211_tx_h_select_key(&tx);
3628 	if (r != TX_CONTINUE) {
3629 		ieee80211_free_txskb(&local->hw, skb);
3630 		goto begin;
3631 	}
3632 
3633 	if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3634 		info->flags |= IEEE80211_TX_CTL_AMPDU;
3635 	else
3636 		info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3637 
3638 	if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
3639 		goto encap_out;
3640 
3641 	if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3642 		struct sta_info *sta = container_of(txq->sta, struct sta_info,
3643 						    sta);
3644 		u8 pn_offs = 0;
3645 
3646 		if (tx.key &&
3647 		    (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3648 			pn_offs = ieee80211_hdrlen(hdr->frame_control);
3649 
3650 		ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3651 					   tx.key, skb);
3652 	} else {
3653 		if (invoke_tx_handlers_late(&tx))
3654 			goto begin;
3655 
3656 		skb = __skb_dequeue(&tx.skbs);
3657 
3658 		if (!skb_queue_empty(&tx.skbs)) {
3659 			spin_lock_bh(&fq->lock);
3660 			skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3661 			spin_unlock_bh(&fq->lock);
3662 		}
3663 	}
3664 
3665 	if (skb_has_frag_list(skb) &&
3666 	    !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3667 		if (skb_linearize(skb)) {
3668 			ieee80211_free_txskb(&local->hw, skb);
3669 			goto begin;
3670 		}
3671 	}
3672 
3673 	switch (tx.sdata->vif.type) {
3674 	case NL80211_IFTYPE_MONITOR:
3675 		if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3676 			vif = &tx.sdata->vif;
3677 			break;
3678 		}
3679 		tx.sdata = rcu_dereference(local->monitor_sdata);
3680 		if (tx.sdata) {
3681 			vif = &tx.sdata->vif;
3682 			info->hw_queue =
3683 				vif->hw_queue[skb_get_queue_mapping(skb)];
3684 		} else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3685 			ieee80211_free_txskb(&local->hw, skb);
3686 			goto begin;
3687 		} else {
3688 			vif = NULL;
3689 		}
3690 		break;
3691 	case NL80211_IFTYPE_AP_VLAN:
3692 		tx.sdata = container_of(tx.sdata->bss,
3693 					struct ieee80211_sub_if_data, u.ap);
3694 		fallthrough;
3695 	default:
3696 		vif = &tx.sdata->vif;
3697 		break;
3698 	}
3699 
3700 encap_out:
3701 	IEEE80211_SKB_CB(skb)->control.vif = vif;
3702 
3703 	if (vif &&
3704 	    wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
3705 		bool ampdu = txq->ac != IEEE80211_AC_VO;
3706 		u32 airtime;
3707 
3708 		airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3709 							     skb->len, ampdu);
3710 		if (airtime) {
3711 			airtime = ieee80211_info_set_tx_time_est(info, airtime);
3712 			ieee80211_sta_update_pending_airtime(local, tx.sta,
3713 							     txq->ac,
3714 							     airtime,
3715 							     false);
3716 		}
3717 	}
3718 
3719 	return skb;
3720 
3721 out:
3722 	spin_unlock_bh(&fq->lock);
3723 
3724 	return skb;
3725 }
3726 EXPORT_SYMBOL(ieee80211_tx_dequeue);
3727 
3728 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3729 {
3730 	struct ieee80211_local *local = hw_to_local(hw);
3731 	struct ieee80211_txq *ret = NULL;
3732 	struct txq_info *txqi = NULL, *head = NULL;
3733 	bool found_eligible_txq = false;
3734 
3735 	spin_lock_bh(&local->active_txq_lock[ac]);
3736 
3737  begin:
3738 	txqi = list_first_entry_or_null(&local->active_txqs[ac],
3739 					struct txq_info,
3740 					schedule_order);
3741 	if (!txqi)
3742 		goto out;
3743 
3744 	if (txqi == head) {
3745 		if (!found_eligible_txq)
3746 			goto out;
3747 		else
3748 			found_eligible_txq = false;
3749 	}
3750 
3751 	if (!head)
3752 		head = txqi;
3753 
3754 	if (txqi->txq.sta) {
3755 		struct sta_info *sta = container_of(txqi->txq.sta,
3756 						    struct sta_info, sta);
3757 		bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3758 		s64 deficit = sta->airtime[txqi->txq.ac].deficit;
3759 
3760 		if (aql_check)
3761 			found_eligible_txq = true;
3762 
3763 		if (deficit < 0)
3764 			sta->airtime[txqi->txq.ac].deficit +=
3765 				sta->airtime_weight;
3766 
3767 		if (deficit < 0 || !aql_check) {
3768 			list_move_tail(&txqi->schedule_order,
3769 				       &local->active_txqs[txqi->txq.ac]);
3770 			goto begin;
3771 		}
3772 	}
3773 
3774 
3775 	if (txqi->schedule_round == local->schedule_round[ac])
3776 		goto out;
3777 
3778 	list_del_init(&txqi->schedule_order);
3779 	txqi->schedule_round = local->schedule_round[ac];
3780 	ret = &txqi->txq;
3781 
3782 out:
3783 	spin_unlock_bh(&local->active_txq_lock[ac]);
3784 	return ret;
3785 }
3786 EXPORT_SYMBOL(ieee80211_next_txq);
3787 
3788 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3789 			      struct ieee80211_txq *txq,
3790 			      bool force)
3791 {
3792 	struct ieee80211_local *local = hw_to_local(hw);
3793 	struct txq_info *txqi = to_txq_info(txq);
3794 
3795 	spin_lock_bh(&local->active_txq_lock[txq->ac]);
3796 
3797 	if (list_empty(&txqi->schedule_order) &&
3798 	    (force || !skb_queue_empty(&txqi->frags) ||
3799 	     txqi->tin.backlog_packets)) {
3800 		/* If airtime accounting is active, always enqueue STAs at the
3801 		 * head of the list to ensure that they only get moved to the
3802 		 * back by the airtime DRR scheduler once they have a negative
3803 		 * deficit. A station that already has a negative deficit will
3804 		 * get immediately moved to the back of the list on the next
3805 		 * call to ieee80211_next_txq().
3806 		 */
3807 		if (txqi->txq.sta && local->airtime_flags &&
3808 		    wiphy_ext_feature_isset(local->hw.wiphy,
3809 					    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3810 			list_add(&txqi->schedule_order,
3811 				 &local->active_txqs[txq->ac]);
3812 		else
3813 			list_add_tail(&txqi->schedule_order,
3814 				      &local->active_txqs[txq->ac]);
3815 	}
3816 
3817 	spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3818 }
3819 EXPORT_SYMBOL(__ieee80211_schedule_txq);
3820 
3821 DEFINE_STATIC_KEY_FALSE(aql_disable);
3822 
3823 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3824 				 struct ieee80211_txq *txq)
3825 {
3826 	struct sta_info *sta;
3827 	struct ieee80211_local *local = hw_to_local(hw);
3828 
3829 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
3830 		return true;
3831 
3832 	if (static_branch_unlikely(&aql_disable))
3833 		return true;
3834 
3835 	if (!txq->sta)
3836 		return true;
3837 
3838 	sta = container_of(txq->sta, struct sta_info, sta);
3839 	if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3840 	    sta->airtime[txq->ac].aql_limit_low)
3841 		return true;
3842 
3843 	if (atomic_read(&local->aql_total_pending_airtime) <
3844 	    local->aql_threshold &&
3845 	    atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3846 	    sta->airtime[txq->ac].aql_limit_high)
3847 		return true;
3848 
3849 	return false;
3850 }
3851 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3852 
3853 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3854 				struct ieee80211_txq *txq)
3855 {
3856 	struct ieee80211_local *local = hw_to_local(hw);
3857 	struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3858 	struct sta_info *sta;
3859 	u8 ac = txq->ac;
3860 
3861 	spin_lock_bh(&local->active_txq_lock[ac]);
3862 
3863 	if (!txqi->txq.sta)
3864 		goto out;
3865 
3866 	if (list_empty(&txqi->schedule_order))
3867 		goto out;
3868 
3869 	list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3870 				 schedule_order) {
3871 		if (iter == txqi)
3872 			break;
3873 
3874 		if (!iter->txq.sta) {
3875 			list_move_tail(&iter->schedule_order,
3876 				       &local->active_txqs[ac]);
3877 			continue;
3878 		}
3879 		sta = container_of(iter->txq.sta, struct sta_info, sta);
3880 		if (sta->airtime[ac].deficit < 0)
3881 			sta->airtime[ac].deficit += sta->airtime_weight;
3882 		list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3883 	}
3884 
3885 	sta = container_of(txqi->txq.sta, struct sta_info, sta);
3886 	if (sta->airtime[ac].deficit >= 0)
3887 		goto out;
3888 
3889 	sta->airtime[ac].deficit += sta->airtime_weight;
3890 	list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
3891 	spin_unlock_bh(&local->active_txq_lock[ac]);
3892 
3893 	return false;
3894 out:
3895 	if (!list_empty(&txqi->schedule_order))
3896 		list_del_init(&txqi->schedule_order);
3897 	spin_unlock_bh(&local->active_txq_lock[ac]);
3898 
3899 	return true;
3900 }
3901 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3902 
3903 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
3904 {
3905 	struct ieee80211_local *local = hw_to_local(hw);
3906 
3907 	spin_lock_bh(&local->active_txq_lock[ac]);
3908 	local->schedule_round[ac]++;
3909 	spin_unlock_bh(&local->active_txq_lock[ac]);
3910 }
3911 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
3912 
3913 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3914 				  struct net_device *dev,
3915 				  u32 info_flags,
3916 				  u32 ctrl_flags,
3917 				  u64 *cookie)
3918 {
3919 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3920 	struct ieee80211_local *local = sdata->local;
3921 	struct sta_info *sta;
3922 	struct sk_buff *next;
3923 
3924 	if (unlikely(skb->len < ETH_HLEN)) {
3925 		kfree_skb(skb);
3926 		return;
3927 	}
3928 
3929 	rcu_read_lock();
3930 
3931 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3932 		goto out_free;
3933 
3934 	if (IS_ERR(sta))
3935 		sta = NULL;
3936 
3937 	if (local->ops->wake_tx_queue) {
3938 		u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3939 		skb_set_queue_mapping(skb, queue);
3940 		skb_get_hash(skb);
3941 	}
3942 
3943 	if (sta) {
3944 		struct ieee80211_fast_tx *fast_tx;
3945 
3946 		sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
3947 
3948 		fast_tx = rcu_dereference(sta->fast_tx);
3949 
3950 		if (fast_tx &&
3951 		    ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
3952 			goto out;
3953 	}
3954 
3955 	if (skb_is_gso(skb)) {
3956 		struct sk_buff *segs;
3957 
3958 		segs = skb_gso_segment(skb, 0);
3959 		if (IS_ERR(segs)) {
3960 			goto out_free;
3961 		} else if (segs) {
3962 			consume_skb(skb);
3963 			skb = segs;
3964 		}
3965 	} else {
3966 		/* we cannot process non-linear frames on this path */
3967 		if (skb_linearize(skb)) {
3968 			kfree_skb(skb);
3969 			goto out;
3970 		}
3971 
3972 		/* the frame could be fragmented, software-encrypted, and other
3973 		 * things so we cannot really handle checksum offload with it -
3974 		 * fix it up in software before we handle anything else.
3975 		 */
3976 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
3977 			skb_set_transport_header(skb,
3978 						 skb_checksum_start_offset(skb));
3979 			if (skb_checksum_help(skb))
3980 				goto out_free;
3981 		}
3982 	}
3983 
3984 	skb_list_walk_safe(skb, skb, next) {
3985 		skb_mark_not_on_list(skb);
3986 
3987 		if (skb->protocol == sdata->control_port_protocol)
3988 			ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
3989 
3990 		skb = ieee80211_build_hdr(sdata, skb, info_flags,
3991 					  sta, ctrl_flags, cookie);
3992 		if (IS_ERR(skb)) {
3993 			kfree_skb_list(next);
3994 			goto out;
3995 		}
3996 
3997 		dev_sw_netstats_tx_add(dev, 1, skb->len);
3998 
3999 		ieee80211_xmit(sdata, sta, skb);
4000 	}
4001 	goto out;
4002  out_free:
4003 	kfree_skb(skb);
4004  out:
4005 	rcu_read_unlock();
4006 }
4007 
4008 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4009 {
4010 	struct ethhdr *eth;
4011 	int err;
4012 
4013 	err = skb_ensure_writable(skb, ETH_HLEN);
4014 	if (unlikely(err))
4015 		return err;
4016 
4017 	eth = (void *)skb->data;
4018 	ether_addr_copy(eth->h_dest, sta->sta.addr);
4019 
4020 	return 0;
4021 }
4022 
4023 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4024 					   struct net_device *dev)
4025 {
4026 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4027 	const struct ethhdr *eth = (void *)skb->data;
4028 	const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4029 	__be16 ethertype;
4030 
4031 	if (likely(!is_multicast_ether_addr(eth->h_dest)))
4032 		return false;
4033 
4034 	switch (sdata->vif.type) {
4035 	case NL80211_IFTYPE_AP_VLAN:
4036 		if (sdata->u.vlan.sta)
4037 			return false;
4038 		if (sdata->wdev.use_4addr)
4039 			return false;
4040 		fallthrough;
4041 	case NL80211_IFTYPE_AP:
4042 		/* check runtime toggle for this bss */
4043 		if (!sdata->bss->multicast_to_unicast)
4044 			return false;
4045 		break;
4046 	default:
4047 		return false;
4048 	}
4049 
4050 	/* multicast to unicast conversion only for some payload */
4051 	ethertype = eth->h_proto;
4052 	if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4053 		ethertype = ethvlan->h_vlan_encapsulated_proto;
4054 	switch (ethertype) {
4055 	case htons(ETH_P_ARP):
4056 	case htons(ETH_P_IP):
4057 	case htons(ETH_P_IPV6):
4058 		break;
4059 	default:
4060 		return false;
4061 	}
4062 
4063 	return true;
4064 }
4065 
4066 static void
4067 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4068 			     struct sk_buff_head *queue)
4069 {
4070 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4071 	struct ieee80211_local *local = sdata->local;
4072 	const struct ethhdr *eth = (struct ethhdr *)skb->data;
4073 	struct sta_info *sta, *first = NULL;
4074 	struct sk_buff *cloned_skb;
4075 
4076 	rcu_read_lock();
4077 
4078 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
4079 		if (sdata != sta->sdata)
4080 			/* AP-VLAN mismatch */
4081 			continue;
4082 		if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4083 			/* do not send back to source */
4084 			continue;
4085 		if (!first) {
4086 			first = sta;
4087 			continue;
4088 		}
4089 		cloned_skb = skb_clone(skb, GFP_ATOMIC);
4090 		if (!cloned_skb)
4091 			goto multicast;
4092 		if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4093 			dev_kfree_skb(cloned_skb);
4094 			goto multicast;
4095 		}
4096 		__skb_queue_tail(queue, cloned_skb);
4097 	}
4098 
4099 	if (likely(first)) {
4100 		if (unlikely(ieee80211_change_da(skb, first)))
4101 			goto multicast;
4102 		__skb_queue_tail(queue, skb);
4103 	} else {
4104 		/* no STA connected, drop */
4105 		kfree_skb(skb);
4106 		skb = NULL;
4107 	}
4108 
4109 	goto out;
4110 multicast:
4111 	__skb_queue_purge(queue);
4112 	__skb_queue_tail(queue, skb);
4113 out:
4114 	rcu_read_unlock();
4115 }
4116 
4117 /**
4118  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4119  * @skb: packet to be sent
4120  * @dev: incoming interface
4121  *
4122  * On failure skb will be freed.
4123  */
4124 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4125 				       struct net_device *dev)
4126 {
4127 	if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4128 		struct sk_buff_head queue;
4129 
4130 		__skb_queue_head_init(&queue);
4131 		ieee80211_convert_to_unicast(skb, dev, &queue);
4132 		while ((skb = __skb_dequeue(&queue)))
4133 			__ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4134 	} else {
4135 		__ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4136 	}
4137 
4138 	return NETDEV_TX_OK;
4139 }
4140 
4141 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4142 			      struct sk_buff *skb, int led_len,
4143 			      struct sta_info *sta,
4144 			      bool txpending)
4145 {
4146 	struct ieee80211_local *local = sdata->local;
4147 	struct ieee80211_tx_control control = {};
4148 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4149 	struct ieee80211_sta *pubsta = NULL;
4150 	unsigned long flags;
4151 	int q = info->hw_queue;
4152 
4153 	if (ieee80211_queue_skb(local, sdata, sta, skb))
4154 		return true;
4155 
4156 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4157 
4158 	if (local->queue_stop_reasons[q] ||
4159 	    (!txpending && !skb_queue_empty(&local->pending[q]))) {
4160 		if (txpending)
4161 			skb_queue_head(&local->pending[q], skb);
4162 		else
4163 			skb_queue_tail(&local->pending[q], skb);
4164 
4165 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4166 
4167 		return false;
4168 	}
4169 
4170 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4171 
4172 	if (sta && sta->uploaded)
4173 		pubsta = &sta->sta;
4174 
4175 	control.sta = pubsta;
4176 
4177 	drv_tx(local, &control, skb);
4178 
4179 	return true;
4180 }
4181 
4182 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4183 				struct net_device *dev, struct sta_info *sta,
4184 				struct ieee80211_key *key, struct sk_buff *skb)
4185 {
4186 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4187 	struct ieee80211_local *local = sdata->local;
4188 	struct tid_ampdu_tx *tid_tx;
4189 	u8 tid;
4190 
4191 	if (local->ops->wake_tx_queue) {
4192 		u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4193 		skb_set_queue_mapping(skb, queue);
4194 		skb_get_hash(skb);
4195 	}
4196 
4197 	if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4198 	    test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4199 		goto out_free;
4200 
4201 	memset(info, 0, sizeof(*info));
4202 
4203 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4204 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4205 	if (tid_tx) {
4206 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4207 			/* fall back to non-offload slow path */
4208 			__ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4209 			return;
4210 		}
4211 
4212 		info->flags |= IEEE80211_TX_CTL_AMPDU;
4213 		if (tid_tx->timeout)
4214 			tid_tx->last_tx = jiffies;
4215 	}
4216 
4217 	if (unlikely(skb->sk &&
4218 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
4219 		info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4220 							     &info->flags, NULL);
4221 
4222 	info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4223 
4224 	dev_sw_netstats_tx_add(dev, 1, skb->len);
4225 
4226 	sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4227 	sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
4228 
4229 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4230 		sdata = container_of(sdata->bss,
4231 				     struct ieee80211_sub_if_data, u.ap);
4232 
4233 	info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
4234 	info->control.vif = &sdata->vif;
4235 
4236 	if (key)
4237 		info->control.hw_key = &key->conf;
4238 
4239 	ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4240 
4241 	return;
4242 
4243 out_free:
4244 	kfree_skb(skb);
4245 }
4246 
4247 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4248 					    struct net_device *dev)
4249 {
4250 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4251 	struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4252 	struct ieee80211_key *key;
4253 	struct sta_info *sta;
4254 
4255 	if (unlikely(skb->len < ETH_HLEN)) {
4256 		kfree_skb(skb);
4257 		return NETDEV_TX_OK;
4258 	}
4259 
4260 	rcu_read_lock();
4261 
4262 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4263 		kfree_skb(skb);
4264 		goto out;
4265 	}
4266 
4267 	if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4268 	    !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
4269 	    sdata->control_port_protocol == ehdr->h_proto))
4270 		goto skip_offload;
4271 
4272 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4273 	if (!key)
4274 		key = rcu_dereference(sdata->default_unicast_key);
4275 
4276 	if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4277 		    key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4278 		goto skip_offload;
4279 
4280 	ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4281 	goto out;
4282 
4283 skip_offload:
4284 	ieee80211_subif_start_xmit(skb, dev);
4285 out:
4286 	rcu_read_unlock();
4287 
4288 	return NETDEV_TX_OK;
4289 }
4290 
4291 struct sk_buff *
4292 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4293 			      struct sk_buff *skb, u32 info_flags)
4294 {
4295 	struct ieee80211_hdr *hdr;
4296 	struct ieee80211_tx_data tx = {
4297 		.local = sdata->local,
4298 		.sdata = sdata,
4299 	};
4300 	struct sta_info *sta;
4301 
4302 	rcu_read_lock();
4303 
4304 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4305 		kfree_skb(skb);
4306 		skb = ERR_PTR(-EINVAL);
4307 		goto out;
4308 	}
4309 
4310 	skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
4311 	if (IS_ERR(skb))
4312 		goto out;
4313 
4314 	hdr = (void *)skb->data;
4315 	tx.sta = sta_info_get(sdata, hdr->addr1);
4316 	tx.skb = skb;
4317 
4318 	if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4319 		rcu_read_unlock();
4320 		kfree_skb(skb);
4321 		return ERR_PTR(-EINVAL);
4322 	}
4323 
4324 out:
4325 	rcu_read_unlock();
4326 	return skb;
4327 }
4328 
4329 /*
4330  * ieee80211_clear_tx_pending may not be called in a context where
4331  * it is possible that it packets could come in again.
4332  */
4333 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4334 {
4335 	struct sk_buff *skb;
4336 	int i;
4337 
4338 	for (i = 0; i < local->hw.queues; i++) {
4339 		while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4340 			ieee80211_free_txskb(&local->hw, skb);
4341 	}
4342 }
4343 
4344 /*
4345  * Returns false if the frame couldn't be transmitted but was queued instead,
4346  * which in this case means re-queued -- take as an indication to stop sending
4347  * more pending frames.
4348  */
4349 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4350 				     struct sk_buff *skb)
4351 {
4352 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4353 	struct ieee80211_sub_if_data *sdata;
4354 	struct sta_info *sta;
4355 	struct ieee80211_hdr *hdr;
4356 	bool result;
4357 	struct ieee80211_chanctx_conf *chanctx_conf;
4358 
4359 	sdata = vif_to_sdata(info->control.vif);
4360 
4361 	if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
4362 		chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4363 		if (unlikely(!chanctx_conf)) {
4364 			dev_kfree_skb(skb);
4365 			return true;
4366 		}
4367 		info->band = chanctx_conf->def.chan->band;
4368 		result = ieee80211_tx(sdata, NULL, skb, true);
4369 	} else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4370 		if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4371 			dev_kfree_skb(skb);
4372 			return true;
4373 		}
4374 
4375 		if (IS_ERR(sta) || (sta && !sta->uploaded))
4376 			sta = NULL;
4377 
4378 		result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
4379 	} else {
4380 		struct sk_buff_head skbs;
4381 
4382 		__skb_queue_head_init(&skbs);
4383 		__skb_queue_tail(&skbs, skb);
4384 
4385 		hdr = (struct ieee80211_hdr *)skb->data;
4386 		sta = sta_info_get(sdata, hdr->addr1);
4387 
4388 		result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
4389 	}
4390 
4391 	return result;
4392 }
4393 
4394 /*
4395  * Transmit all pending packets. Called from tasklet.
4396  */
4397 void ieee80211_tx_pending(struct tasklet_struct *t)
4398 {
4399 	struct ieee80211_local *local = from_tasklet(local, t,
4400 						     tx_pending_tasklet);
4401 	unsigned long flags;
4402 	int i;
4403 	bool txok;
4404 
4405 	rcu_read_lock();
4406 
4407 	spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4408 	for (i = 0; i < local->hw.queues; i++) {
4409 		/*
4410 		 * If queue is stopped by something other than due to pending
4411 		 * frames, or we have no pending frames, proceed to next queue.
4412 		 */
4413 		if (local->queue_stop_reasons[i] ||
4414 		    skb_queue_empty(&local->pending[i]))
4415 			continue;
4416 
4417 		while (!skb_queue_empty(&local->pending[i])) {
4418 			struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
4419 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4420 
4421 			if (WARN_ON(!info->control.vif)) {
4422 				ieee80211_free_txskb(&local->hw, skb);
4423 				continue;
4424 			}
4425 
4426 			spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4427 						flags);
4428 
4429 			txok = ieee80211_tx_pending_skb(local, skb);
4430 			spin_lock_irqsave(&local->queue_stop_reason_lock,
4431 					  flags);
4432 			if (!txok)
4433 				break;
4434 		}
4435 
4436 		if (skb_queue_empty(&local->pending[i]))
4437 			ieee80211_propagate_queue_wake(local, i);
4438 	}
4439 	spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4440 
4441 	rcu_read_unlock();
4442 }
4443 
4444 /* functions for drivers to get certain frames */
4445 
4446 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4447 				       struct ps_data *ps, struct sk_buff *skb,
4448 				       bool is_template)
4449 {
4450 	u8 *pos, *tim;
4451 	int aid0 = 0;
4452 	int i, have_bits = 0, n1, n2;
4453 
4454 	/* Generate bitmap for TIM only if there are any STAs in power save
4455 	 * mode. */
4456 	if (atomic_read(&ps->num_sta_ps) > 0)
4457 		/* in the hope that this is faster than
4458 		 * checking byte-for-byte */
4459 		have_bits = !bitmap_empty((unsigned long *)ps->tim,
4460 					  IEEE80211_MAX_AID+1);
4461 	if (!is_template) {
4462 		if (ps->dtim_count == 0)
4463 			ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4464 		else
4465 			ps->dtim_count--;
4466 	}
4467 
4468 	tim = pos = skb_put(skb, 6);
4469 	*pos++ = WLAN_EID_TIM;
4470 	*pos++ = 4;
4471 	*pos++ = ps->dtim_count;
4472 	*pos++ = sdata->vif.bss_conf.dtim_period;
4473 
4474 	if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4475 		aid0 = 1;
4476 
4477 	ps->dtim_bc_mc = aid0 == 1;
4478 
4479 	if (have_bits) {
4480 		/* Find largest even number N1 so that bits numbered 1 through
4481 		 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4482 		 * (N2 + 1) x 8 through 2007 are 0. */
4483 		n1 = 0;
4484 		for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4485 			if (ps->tim[i]) {
4486 				n1 = i & 0xfe;
4487 				break;
4488 			}
4489 		}
4490 		n2 = n1;
4491 		for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4492 			if (ps->tim[i]) {
4493 				n2 = i;
4494 				break;
4495 			}
4496 		}
4497 
4498 		/* Bitmap control */
4499 		*pos++ = n1 | aid0;
4500 		/* Part Virt Bitmap */
4501 		skb_put(skb, n2 - n1);
4502 		memcpy(pos, ps->tim + n1, n2 - n1 + 1);
4503 
4504 		tim[1] = n2 - n1 + 4;
4505 	} else {
4506 		*pos++ = aid0; /* Bitmap control */
4507 		*pos++ = 0; /* Part Virt Bitmap */
4508 	}
4509 }
4510 
4511 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4512 				    struct ps_data *ps, struct sk_buff *skb,
4513 				    bool is_template)
4514 {
4515 	struct ieee80211_local *local = sdata->local;
4516 
4517 	/*
4518 	 * Not very nice, but we want to allow the driver to call
4519 	 * ieee80211_beacon_get() as a response to the set_tim()
4520 	 * callback. That, however, is already invoked under the
4521 	 * sta_lock to guarantee consistent and race-free update
4522 	 * of the tim bitmap in mac80211 and the driver.
4523 	 */
4524 	if (local->tim_in_locked_section) {
4525 		__ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4526 	} else {
4527 		spin_lock_bh(&local->tim_lock);
4528 		__ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4529 		spin_unlock_bh(&local->tim_lock);
4530 	}
4531 
4532 	return 0;
4533 }
4534 
4535 static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4536 					struct beacon_data *beacon)
4537 {
4538 	struct probe_resp *resp;
4539 	u8 *beacon_data;
4540 	size_t beacon_data_len;
4541 	int i;
4542 	u8 count = beacon->cntdwn_current_counter;
4543 
4544 	switch (sdata->vif.type) {
4545 	case NL80211_IFTYPE_AP:
4546 		beacon_data = beacon->tail;
4547 		beacon_data_len = beacon->tail_len;
4548 		break;
4549 	case NL80211_IFTYPE_ADHOC:
4550 		beacon_data = beacon->head;
4551 		beacon_data_len = beacon->head_len;
4552 		break;
4553 	case NL80211_IFTYPE_MESH_POINT:
4554 		beacon_data = beacon->head;
4555 		beacon_data_len = beacon->head_len;
4556 		break;
4557 	default:
4558 		return;
4559 	}
4560 
4561 	rcu_read_lock();
4562 	for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; ++i) {
4563 		resp = rcu_dereference(sdata->u.ap.probe_resp);
4564 
4565 		if (beacon->cntdwn_counter_offsets[i]) {
4566 			if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[i] >=
4567 					 beacon_data_len)) {
4568 				rcu_read_unlock();
4569 				return;
4570 			}
4571 
4572 			beacon_data[beacon->cntdwn_counter_offsets[i]] = count;
4573 		}
4574 
4575 		if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4576 			resp->data[resp->cntdwn_counter_offsets[i]] = count;
4577 	}
4578 	rcu_read_unlock();
4579 }
4580 
4581 static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
4582 {
4583 	beacon->cntdwn_current_counter--;
4584 
4585 	/* the counter should never reach 0 */
4586 	WARN_ON_ONCE(!beacon->cntdwn_current_counter);
4587 
4588 	return beacon->cntdwn_current_counter;
4589 }
4590 
4591 u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
4592 {
4593 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4594 	struct beacon_data *beacon = NULL;
4595 	u8 count = 0;
4596 
4597 	rcu_read_lock();
4598 
4599 	if (sdata->vif.type == NL80211_IFTYPE_AP)
4600 		beacon = rcu_dereference(sdata->u.ap.beacon);
4601 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4602 		beacon = rcu_dereference(sdata->u.ibss.presp);
4603 	else if (ieee80211_vif_is_mesh(&sdata->vif))
4604 		beacon = rcu_dereference(sdata->u.mesh.beacon);
4605 
4606 	if (!beacon)
4607 		goto unlock;
4608 
4609 	count = __ieee80211_beacon_update_cntdwn(beacon);
4610 
4611 unlock:
4612 	rcu_read_unlock();
4613 	return count;
4614 }
4615 EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
4616 
4617 void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
4618 {
4619 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4620 	struct beacon_data *beacon = NULL;
4621 
4622 	rcu_read_lock();
4623 
4624 	if (sdata->vif.type == NL80211_IFTYPE_AP)
4625 		beacon = rcu_dereference(sdata->u.ap.beacon);
4626 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4627 		beacon = rcu_dereference(sdata->u.ibss.presp);
4628 	else if (ieee80211_vif_is_mesh(&sdata->vif))
4629 		beacon = rcu_dereference(sdata->u.mesh.beacon);
4630 
4631 	if (!beacon)
4632 		goto unlock;
4633 
4634 	if (counter < beacon->cntdwn_current_counter)
4635 		beacon->cntdwn_current_counter = counter;
4636 
4637 unlock:
4638 	rcu_read_unlock();
4639 }
4640 EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
4641 
4642 bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
4643 {
4644 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4645 	struct beacon_data *beacon = NULL;
4646 	u8 *beacon_data;
4647 	size_t beacon_data_len;
4648 	int ret = false;
4649 
4650 	if (!ieee80211_sdata_running(sdata))
4651 		return false;
4652 
4653 	rcu_read_lock();
4654 	if (vif->type == NL80211_IFTYPE_AP) {
4655 		struct ieee80211_if_ap *ap = &sdata->u.ap;
4656 
4657 		beacon = rcu_dereference(ap->beacon);
4658 		if (WARN_ON(!beacon || !beacon->tail))
4659 			goto out;
4660 		beacon_data = beacon->tail;
4661 		beacon_data_len = beacon->tail_len;
4662 	} else if (vif->type == NL80211_IFTYPE_ADHOC) {
4663 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4664 
4665 		beacon = rcu_dereference(ifibss->presp);
4666 		if (!beacon)
4667 			goto out;
4668 
4669 		beacon_data = beacon->head;
4670 		beacon_data_len = beacon->head_len;
4671 	} else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4672 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4673 
4674 		beacon = rcu_dereference(ifmsh->beacon);
4675 		if (!beacon)
4676 			goto out;
4677 
4678 		beacon_data = beacon->head;
4679 		beacon_data_len = beacon->head_len;
4680 	} else {
4681 		WARN_ON(1);
4682 		goto out;
4683 	}
4684 
4685 	if (!beacon->cntdwn_counter_offsets[0])
4686 		goto out;
4687 
4688 	if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
4689 		goto out;
4690 
4691 	if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
4692 		ret = true;
4693 
4694  out:
4695 	rcu_read_unlock();
4696 
4697 	return ret;
4698 }
4699 EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
4700 
4701 static int ieee80211_beacon_protect(struct sk_buff *skb,
4702 				    struct ieee80211_local *local,
4703 				    struct ieee80211_sub_if_data *sdata)
4704 {
4705 	ieee80211_tx_result res;
4706 	struct ieee80211_tx_data tx;
4707 	struct sk_buff *check_skb;
4708 
4709 	memset(&tx, 0, sizeof(tx));
4710 	tx.key = rcu_dereference(sdata->default_beacon_key);
4711 	if (!tx.key)
4712 		return 0;
4713 	tx.local = local;
4714 	tx.sdata = sdata;
4715 	__skb_queue_head_init(&tx.skbs);
4716 	__skb_queue_tail(&tx.skbs, skb);
4717 	res = ieee80211_tx_h_encrypt(&tx);
4718 	check_skb = __skb_dequeue(&tx.skbs);
4719 	/* we may crash after this, but it'd be a bug in crypto */
4720 	WARN_ON(check_skb != skb);
4721 	if (WARN_ON_ONCE(res != TX_CONTINUE))
4722 		return -EINVAL;
4723 
4724 	return 0;
4725 }
4726 
4727 static struct sk_buff *
4728 __ieee80211_beacon_get(struct ieee80211_hw *hw,
4729 		       struct ieee80211_vif *vif,
4730 		       struct ieee80211_mutable_offsets *offs,
4731 		       bool is_template)
4732 {
4733 	struct ieee80211_local *local = hw_to_local(hw);
4734 	struct beacon_data *beacon = NULL;
4735 	struct sk_buff *skb = NULL;
4736 	struct ieee80211_tx_info *info;
4737 	struct ieee80211_sub_if_data *sdata = NULL;
4738 	enum nl80211_band band;
4739 	struct ieee80211_tx_rate_control txrc;
4740 	struct ieee80211_chanctx_conf *chanctx_conf;
4741 	int csa_off_base = 0;
4742 
4743 	rcu_read_lock();
4744 
4745 	sdata = vif_to_sdata(vif);
4746 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4747 
4748 	if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
4749 		goto out;
4750 
4751 	if (offs)
4752 		memset(offs, 0, sizeof(*offs));
4753 
4754 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
4755 		struct ieee80211_if_ap *ap = &sdata->u.ap;
4756 
4757 		beacon = rcu_dereference(ap->beacon);
4758 		if (beacon) {
4759 			if (beacon->cntdwn_counter_offsets[0]) {
4760 				if (!is_template)
4761 					ieee80211_beacon_update_cntdwn(vif);
4762 
4763 				ieee80211_set_beacon_cntdwn(sdata, beacon);
4764 			}
4765 
4766 			/*
4767 			 * headroom, head length,
4768 			 * tail length and maximum TIM length
4769 			 */
4770 			skb = dev_alloc_skb(local->tx_headroom +
4771 					    beacon->head_len +
4772 					    beacon->tail_len + 256 +
4773 					    local->hw.extra_beacon_tailroom);
4774 			if (!skb)
4775 				goto out;
4776 
4777 			skb_reserve(skb, local->tx_headroom);
4778 			skb_put_data(skb, beacon->head, beacon->head_len);
4779 
4780 			ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4781 						 is_template);
4782 
4783 			if (offs) {
4784 				offs->tim_offset = beacon->head_len;
4785 				offs->tim_length = skb->len - beacon->head_len;
4786 
4787 				/* for AP the csa offsets are from tail */
4788 				csa_off_base = skb->len;
4789 			}
4790 
4791 			if (beacon->tail)
4792 				skb_put_data(skb, beacon->tail,
4793 					     beacon->tail_len);
4794 
4795 			if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4796 				goto out;
4797 		} else
4798 			goto out;
4799 	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
4800 		struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4801 		struct ieee80211_hdr *hdr;
4802 
4803 		beacon = rcu_dereference(ifibss->presp);
4804 		if (!beacon)
4805 			goto out;
4806 
4807 		if (beacon->cntdwn_counter_offsets[0]) {
4808 			if (!is_template)
4809 				__ieee80211_beacon_update_cntdwn(beacon);
4810 
4811 			ieee80211_set_beacon_cntdwn(sdata, beacon);
4812 		}
4813 
4814 		skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
4815 				    local->hw.extra_beacon_tailroom);
4816 		if (!skb)
4817 			goto out;
4818 		skb_reserve(skb, local->tx_headroom);
4819 		skb_put_data(skb, beacon->head, beacon->head_len);
4820 
4821 		hdr = (struct ieee80211_hdr *) skb->data;
4822 		hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4823 						 IEEE80211_STYPE_BEACON);
4824 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4825 		struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4826 
4827 		beacon = rcu_dereference(ifmsh->beacon);
4828 		if (!beacon)
4829 			goto out;
4830 
4831 		if (beacon->cntdwn_counter_offsets[0]) {
4832 			if (!is_template)
4833 				/* TODO: For mesh csa_counter is in TU, so
4834 				 * decrementing it by one isn't correct, but
4835 				 * for now we leave it consistent with overall
4836 				 * mac80211's behavior.
4837 				 */
4838 				__ieee80211_beacon_update_cntdwn(beacon);
4839 
4840 			ieee80211_set_beacon_cntdwn(sdata, beacon);
4841 		}
4842 
4843 		if (ifmsh->sync_ops)
4844 			ifmsh->sync_ops->adjust_tsf(sdata, beacon);
4845 
4846 		skb = dev_alloc_skb(local->tx_headroom +
4847 				    beacon->head_len +
4848 				    256 + /* TIM IE */
4849 				    beacon->tail_len +
4850 				    local->hw.extra_beacon_tailroom);
4851 		if (!skb)
4852 			goto out;
4853 		skb_reserve(skb, local->tx_headroom);
4854 		skb_put_data(skb, beacon->head, beacon->head_len);
4855 		ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4856 
4857 		if (offs) {
4858 			offs->tim_offset = beacon->head_len;
4859 			offs->tim_length = skb->len - beacon->head_len;
4860 		}
4861 
4862 		skb_put_data(skb, beacon->tail, beacon->tail_len);
4863 	} else {
4864 		WARN_ON(1);
4865 		goto out;
4866 	}
4867 
4868 	/* CSA offsets */
4869 	if (offs && beacon) {
4870 		int i;
4871 
4872 		for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
4873 			u16 csa_off = beacon->cntdwn_counter_offsets[i];
4874 
4875 			if (!csa_off)
4876 				continue;
4877 
4878 			offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
4879 		}
4880 	}
4881 
4882 	band = chanctx_conf->def.chan->band;
4883 
4884 	info = IEEE80211_SKB_CB(skb);
4885 
4886 	info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4887 	info->flags |= IEEE80211_TX_CTL_NO_ACK;
4888 	info->band = band;
4889 
4890 	memset(&txrc, 0, sizeof(txrc));
4891 	txrc.hw = hw;
4892 	txrc.sband = local->hw.wiphy->bands[band];
4893 	txrc.bss_conf = &sdata->vif.bss_conf;
4894 	txrc.skb = skb;
4895 	txrc.reported_rate.idx = -1;
4896 	if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
4897 		txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
4898 	else
4899 		txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
4900 	txrc.bss = true;
4901 	rate_control_get_rate(sdata, NULL, &txrc);
4902 
4903 	info->control.vif = vif;
4904 
4905 	info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4906 			IEEE80211_TX_CTL_ASSIGN_SEQ |
4907 			IEEE80211_TX_CTL_FIRST_FRAGMENT;
4908  out:
4909 	rcu_read_unlock();
4910 	return skb;
4911 
4912 }
4913 
4914 struct sk_buff *
4915 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4916 			      struct ieee80211_vif *vif,
4917 			      struct ieee80211_mutable_offsets *offs)
4918 {
4919 	return __ieee80211_beacon_get(hw, vif, offs, true);
4920 }
4921 EXPORT_SYMBOL(ieee80211_beacon_get_template);
4922 
4923 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4924 					 struct ieee80211_vif *vif,
4925 					 u16 *tim_offset, u16 *tim_length)
4926 {
4927 	struct ieee80211_mutable_offsets offs = {};
4928 	struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
4929 	struct sk_buff *copy;
4930 	struct ieee80211_supported_band *sband;
4931 	int shift;
4932 
4933 	if (!bcn)
4934 		return bcn;
4935 
4936 	if (tim_offset)
4937 		*tim_offset = offs.tim_offset;
4938 
4939 	if (tim_length)
4940 		*tim_length = offs.tim_length;
4941 
4942 	if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4943 	    !hw_to_local(hw)->monitors)
4944 		return bcn;
4945 
4946 	/* send a copy to monitor interfaces */
4947 	copy = skb_copy(bcn, GFP_ATOMIC);
4948 	if (!copy)
4949 		return bcn;
4950 
4951 	shift = ieee80211_vif_get_shift(vif);
4952 	sband = ieee80211_get_sband(vif_to_sdata(vif));
4953 	if (!sband)
4954 		return bcn;
4955 
4956 	ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4957 			     NULL);
4958 
4959 	return bcn;
4960 }
4961 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
4962 
4963 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4964 					struct ieee80211_vif *vif)
4965 {
4966 	struct ieee80211_if_ap *ap = NULL;
4967 	struct sk_buff *skb = NULL;
4968 	struct probe_resp *presp = NULL;
4969 	struct ieee80211_hdr *hdr;
4970 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4971 
4972 	if (sdata->vif.type != NL80211_IFTYPE_AP)
4973 		return NULL;
4974 
4975 	rcu_read_lock();
4976 
4977 	ap = &sdata->u.ap;
4978 	presp = rcu_dereference(ap->probe_resp);
4979 	if (!presp)
4980 		goto out;
4981 
4982 	skb = dev_alloc_skb(presp->len);
4983 	if (!skb)
4984 		goto out;
4985 
4986 	skb_put_data(skb, presp->data, presp->len);
4987 
4988 	hdr = (struct ieee80211_hdr *) skb->data;
4989 	memset(hdr->addr1, 0, sizeof(hdr->addr1));
4990 
4991 out:
4992 	rcu_read_unlock();
4993 	return skb;
4994 }
4995 EXPORT_SYMBOL(ieee80211_proberesp_get);
4996 
4997 struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
4998 						  struct ieee80211_vif *vif)
4999 {
5000 	struct sk_buff *skb = NULL;
5001 	struct fils_discovery_data *tmpl = NULL;
5002 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5003 
5004 	if (sdata->vif.type != NL80211_IFTYPE_AP)
5005 		return NULL;
5006 
5007 	rcu_read_lock();
5008 	tmpl = rcu_dereference(sdata->u.ap.fils_discovery);
5009 	if (!tmpl) {
5010 		rcu_read_unlock();
5011 		return NULL;
5012 	}
5013 
5014 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5015 	if (skb) {
5016 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5017 		skb_put_data(skb, tmpl->data, tmpl->len);
5018 	}
5019 
5020 	rcu_read_unlock();
5021 	return skb;
5022 }
5023 EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5024 
5025 struct sk_buff *
5026 ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5027 					  struct ieee80211_vif *vif)
5028 {
5029 	struct sk_buff *skb = NULL;
5030 	struct unsol_bcast_probe_resp_data *tmpl = NULL;
5031 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5032 
5033 	if (sdata->vif.type != NL80211_IFTYPE_AP)
5034 		return NULL;
5035 
5036 	rcu_read_lock();
5037 	tmpl = rcu_dereference(sdata->u.ap.unsol_bcast_probe_resp);
5038 	if (!tmpl) {
5039 		rcu_read_unlock();
5040 		return NULL;
5041 	}
5042 
5043 	skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5044 	if (skb) {
5045 		skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5046 		skb_put_data(skb, tmpl->data, tmpl->len);
5047 	}
5048 
5049 	rcu_read_unlock();
5050 	return skb;
5051 }
5052 EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5053 
5054 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5055 				     struct ieee80211_vif *vif)
5056 {
5057 	struct ieee80211_sub_if_data *sdata;
5058 	struct ieee80211_if_managed *ifmgd;
5059 	struct ieee80211_pspoll *pspoll;
5060 	struct ieee80211_local *local;
5061 	struct sk_buff *skb;
5062 
5063 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5064 		return NULL;
5065 
5066 	sdata = vif_to_sdata(vif);
5067 	ifmgd = &sdata->u.mgd;
5068 	local = sdata->local;
5069 
5070 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
5071 	if (!skb)
5072 		return NULL;
5073 
5074 	skb_reserve(skb, local->hw.extra_tx_headroom);
5075 
5076 	pspoll = skb_put_zero(skb, sizeof(*pspoll));
5077 	pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5078 					    IEEE80211_STYPE_PSPOLL);
5079 	pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
5080 
5081 	/* aid in PS-Poll has its two MSBs each set to 1 */
5082 	pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5083 
5084 	memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5085 	memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5086 
5087 	return skb;
5088 }
5089 EXPORT_SYMBOL(ieee80211_pspoll_get);
5090 
5091 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5092 				       struct ieee80211_vif *vif,
5093 				       bool qos_ok)
5094 {
5095 	struct ieee80211_hdr_3addr *nullfunc;
5096 	struct ieee80211_sub_if_data *sdata;
5097 	struct ieee80211_if_managed *ifmgd;
5098 	struct ieee80211_local *local;
5099 	struct sk_buff *skb;
5100 	bool qos = false;
5101 
5102 	if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5103 		return NULL;
5104 
5105 	sdata = vif_to_sdata(vif);
5106 	ifmgd = &sdata->u.mgd;
5107 	local = sdata->local;
5108 
5109 	if (qos_ok) {
5110 		struct sta_info *sta;
5111 
5112 		rcu_read_lock();
5113 		sta = sta_info_get(sdata, ifmgd->bssid);
5114 		qos = sta && sta->sta.wme;
5115 		rcu_read_unlock();
5116 	}
5117 
5118 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5119 			    sizeof(*nullfunc) + 2);
5120 	if (!skb)
5121 		return NULL;
5122 
5123 	skb_reserve(skb, local->hw.extra_tx_headroom);
5124 
5125 	nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
5126 	nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5127 					      IEEE80211_STYPE_NULLFUNC |
5128 					      IEEE80211_FCTL_TODS);
5129 	if (qos) {
5130 		__le16 qoshdr = cpu_to_le16(7);
5131 
5132 		BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5133 			      IEEE80211_STYPE_NULLFUNC) !=
5134 			     IEEE80211_STYPE_QOS_NULLFUNC);
5135 		nullfunc->frame_control |=
5136 			cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5137 		skb->priority = 7;
5138 		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
5139 		skb_put_data(skb, &qoshdr, sizeof(qoshdr));
5140 	}
5141 
5142 	memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5143 	memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5144 	memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5145 
5146 	return skb;
5147 }
5148 EXPORT_SYMBOL(ieee80211_nullfunc_get);
5149 
5150 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
5151 				       const u8 *src_addr,
5152 				       const u8 *ssid, size_t ssid_len,
5153 				       size_t tailroom)
5154 {
5155 	struct ieee80211_local *local = hw_to_local(hw);
5156 	struct ieee80211_hdr_3addr *hdr;
5157 	struct sk_buff *skb;
5158 	size_t ie_ssid_len;
5159 	u8 *pos;
5160 
5161 	ie_ssid_len = 2 + ssid_len;
5162 
5163 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
5164 			    ie_ssid_len + tailroom);
5165 	if (!skb)
5166 		return NULL;
5167 
5168 	skb_reserve(skb, local->hw.extra_tx_headroom);
5169 
5170 	hdr = skb_put_zero(skb, sizeof(*hdr));
5171 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5172 					 IEEE80211_STYPE_PROBE_REQ);
5173 	eth_broadcast_addr(hdr->addr1);
5174 	memcpy(hdr->addr2, src_addr, ETH_ALEN);
5175 	eth_broadcast_addr(hdr->addr3);
5176 
5177 	pos = skb_put(skb, ie_ssid_len);
5178 	*pos++ = WLAN_EID_SSID;
5179 	*pos++ = ssid_len;
5180 	if (ssid_len)
5181 		memcpy(pos, ssid, ssid_len);
5182 	pos += ssid_len;
5183 
5184 	return skb;
5185 }
5186 EXPORT_SYMBOL(ieee80211_probereq_get);
5187 
5188 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5189 		       const void *frame, size_t frame_len,
5190 		       const struct ieee80211_tx_info *frame_txctl,
5191 		       struct ieee80211_rts *rts)
5192 {
5193 	const struct ieee80211_hdr *hdr = frame;
5194 
5195 	rts->frame_control =
5196 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
5197 	rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5198 					       frame_txctl);
5199 	memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5200 	memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5201 }
5202 EXPORT_SYMBOL(ieee80211_rts_get);
5203 
5204 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5205 			     const void *frame, size_t frame_len,
5206 			     const struct ieee80211_tx_info *frame_txctl,
5207 			     struct ieee80211_cts *cts)
5208 {
5209 	const struct ieee80211_hdr *hdr = frame;
5210 
5211 	cts->frame_control =
5212 	    cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
5213 	cts->duration = ieee80211_ctstoself_duration(hw, vif,
5214 						     frame_len, frame_txctl);
5215 	memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5216 }
5217 EXPORT_SYMBOL(ieee80211_ctstoself_get);
5218 
5219 struct sk_buff *
5220 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
5221 			  struct ieee80211_vif *vif)
5222 {
5223 	struct ieee80211_local *local = hw_to_local(hw);
5224 	struct sk_buff *skb = NULL;
5225 	struct ieee80211_tx_data tx;
5226 	struct ieee80211_sub_if_data *sdata;
5227 	struct ps_data *ps;
5228 	struct ieee80211_tx_info *info;
5229 	struct ieee80211_chanctx_conf *chanctx_conf;
5230 
5231 	sdata = vif_to_sdata(vif);
5232 
5233 	rcu_read_lock();
5234 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
5235 
5236 	if (!chanctx_conf)
5237 		goto out;
5238 
5239 	if (sdata->vif.type == NL80211_IFTYPE_AP) {
5240 		struct beacon_data *beacon =
5241 				rcu_dereference(sdata->u.ap.beacon);
5242 
5243 		if (!beacon || !beacon->head)
5244 			goto out;
5245 
5246 		ps = &sdata->u.ap.ps;
5247 	} else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5248 		ps = &sdata->u.mesh.ps;
5249 	} else {
5250 		goto out;
5251 	}
5252 
5253 	if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
5254 		goto out; /* send buffered bc/mc only after DTIM beacon */
5255 
5256 	while (1) {
5257 		skb = skb_dequeue(&ps->bc_buf);
5258 		if (!skb)
5259 			goto out;
5260 		local->total_ps_buffered--;
5261 
5262 		if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
5263 			struct ieee80211_hdr *hdr =
5264 				(struct ieee80211_hdr *) skb->data;
5265 			/* more buffered multicast/broadcast frames ==> set
5266 			 * MoreData flag in IEEE 802.11 header to inform PS
5267 			 * STAs */
5268 			hdr->frame_control |=
5269 				cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5270 		}
5271 
5272 		if (sdata->vif.type == NL80211_IFTYPE_AP)
5273 			sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
5274 		if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
5275 			break;
5276 		ieee80211_free_txskb(hw, skb);
5277 	}
5278 
5279 	info = IEEE80211_SKB_CB(skb);
5280 
5281 	tx.flags |= IEEE80211_TX_PS_BUFFERED;
5282 	info->band = chanctx_conf->def.chan->band;
5283 
5284 	if (invoke_tx_handlers(&tx))
5285 		skb = NULL;
5286  out:
5287 	rcu_read_unlock();
5288 
5289 	return skb;
5290 }
5291 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
5292 
5293 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5294 {
5295 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5296 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5297 	struct ieee80211_local *local = sdata->local;
5298 	int ret;
5299 	u32 queues;
5300 
5301 	lockdep_assert_held(&local->sta_mtx);
5302 
5303 	/* only some cases are supported right now */
5304 	switch (sdata->vif.type) {
5305 	case NL80211_IFTYPE_STATION:
5306 	case NL80211_IFTYPE_AP:
5307 	case NL80211_IFTYPE_AP_VLAN:
5308 		break;
5309 	default:
5310 		WARN_ON(1);
5311 		return -EINVAL;
5312 	}
5313 
5314 	if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5315 		return -EINVAL;
5316 
5317 	if (sta->reserved_tid == tid) {
5318 		ret = 0;
5319 		goto out;
5320 	}
5321 
5322 	if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5323 		sdata_err(sdata, "TID reservation already active\n");
5324 		ret = -EALREADY;
5325 		goto out;
5326 	}
5327 
5328 	ieee80211_stop_vif_queues(sdata->local, sdata,
5329 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5330 
5331 	synchronize_net();
5332 
5333 	/* Tear down BA sessions so we stop aggregating on this TID */
5334 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
5335 		set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5336 		__ieee80211_stop_tx_ba_session(sta, tid,
5337 					       AGG_STOP_LOCAL_REQUEST);
5338 	}
5339 
5340 	queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
5341 	__ieee80211_flush_queues(local, sdata, queues, false);
5342 
5343 	sta->reserved_tid = tid;
5344 
5345 	ieee80211_wake_vif_queues(local, sdata,
5346 				  IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5347 
5348 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
5349 		clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5350 
5351 	ret = 0;
5352  out:
5353 	return ret;
5354 }
5355 EXPORT_SYMBOL(ieee80211_reserve_tid);
5356 
5357 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5358 {
5359 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5360 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5361 
5362 	lockdep_assert_held(&sdata->local->sta_mtx);
5363 
5364 	/* only some cases are supported right now */
5365 	switch (sdata->vif.type) {
5366 	case NL80211_IFTYPE_STATION:
5367 	case NL80211_IFTYPE_AP:
5368 	case NL80211_IFTYPE_AP_VLAN:
5369 		break;
5370 	default:
5371 		WARN_ON(1);
5372 		return;
5373 	}
5374 
5375 	if (tid != sta->reserved_tid) {
5376 		sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5377 		return;
5378 	}
5379 
5380 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5381 }
5382 EXPORT_SYMBOL(ieee80211_unreserve_tid);
5383 
5384 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5385 				 struct sk_buff *skb, int tid,
5386 				 enum nl80211_band band)
5387 {
5388 	int ac = ieee80211_ac_from_tid(tid);
5389 
5390 	skb_reset_mac_header(skb);
5391 	skb_set_queue_mapping(skb, ac);
5392 	skb->priority = tid;
5393 
5394 	skb->dev = sdata->dev;
5395 
5396 	/*
5397 	 * The other path calling ieee80211_xmit is from the tasklet,
5398 	 * and while we can handle concurrent transmissions locking
5399 	 * requirements are that we do not come into tx with bhs on.
5400 	 */
5401 	local_bh_disable();
5402 	IEEE80211_SKB_CB(skb)->band = band;
5403 	ieee80211_xmit(sdata, NULL, skb);
5404 	local_bh_enable();
5405 }
5406 
5407 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5408 			      const u8 *buf, size_t len,
5409 			      const u8 *dest, __be16 proto, bool unencrypted,
5410 			      u64 *cookie)
5411 {
5412 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5413 	struct ieee80211_local *local = sdata->local;
5414 	struct sta_info *sta;
5415 	struct sk_buff *skb;
5416 	struct ethhdr *ehdr;
5417 	u32 ctrl_flags = 0;
5418 	u32 flags = 0;
5419 
5420 	/* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5421 	 * or Pre-Authentication
5422 	 */
5423 	if (proto != sdata->control_port_protocol &&
5424 	    proto != cpu_to_be16(ETH_P_PREAUTH))
5425 		return -EINVAL;
5426 
5427 	if (proto == sdata->control_port_protocol)
5428 		ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5429 			      IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
5430 
5431 	if (unencrypted)
5432 		flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5433 
5434 	if (cookie)
5435 		ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5436 
5437 	flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
5438 
5439 	skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5440 			    sizeof(struct ethhdr) + len);
5441 	if (!skb)
5442 		return -ENOMEM;
5443 
5444 	skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5445 
5446 	skb_put_data(skb, buf, len);
5447 
5448 	ehdr = skb_push(skb, sizeof(struct ethhdr));
5449 	memcpy(ehdr->h_dest, dest, ETH_ALEN);
5450 	memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5451 	ehdr->h_proto = proto;
5452 
5453 	skb->dev = dev;
5454 	skb->protocol = proto;
5455 	skb_reset_network_header(skb);
5456 	skb_reset_mac_header(skb);
5457 
5458 	/* update QoS header to prioritize control port frames if possible,
5459 	 * priorization also happens for control port frames send over
5460 	 * AF_PACKET
5461 	 */
5462 	rcu_read_lock();
5463 
5464 	if (ieee80211_lookup_ra_sta(sdata, skb, &sta) == 0 && !IS_ERR(sta)) {
5465 		u16 queue = __ieee80211_select_queue(sdata, sta, skb);
5466 
5467 		skb_set_queue_mapping(skb, queue);
5468 		skb_get_hash(skb);
5469 	}
5470 
5471 	rcu_read_unlock();
5472 
5473 	/* mutex lock is only needed for incrementing the cookie counter */
5474 	mutex_lock(&local->mtx);
5475 
5476 	local_bh_disable();
5477 	__ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
5478 	local_bh_enable();
5479 
5480 	mutex_unlock(&local->mtx);
5481 
5482 	return 0;
5483 }
5484 
5485 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5486 			      const u8 *buf, size_t len)
5487 {
5488 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5489 	struct ieee80211_local *local = sdata->local;
5490 	struct sk_buff *skb;
5491 
5492 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5493 			    30 + /* header size */
5494 			    18); /* 11s header size */
5495 	if (!skb)
5496 		return -ENOMEM;
5497 
5498 	skb_reserve(skb, local->hw.extra_tx_headroom);
5499 	skb_put_data(skb, buf, len);
5500 
5501 	skb->dev = dev;
5502 	skb->protocol = htons(ETH_P_802_3);
5503 	skb_reset_network_header(skb);
5504 	skb_reset_mac_header(skb);
5505 
5506 	local_bh_disable();
5507 	__ieee80211_subif_start_xmit(skb, skb->dev, 0,
5508 				     IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5509 				     NULL);
5510 	local_bh_enable();
5511 
5512 	return 0;
5513 }
5514