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