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