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