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