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