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