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