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