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