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