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