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