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