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