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