ipv6 sit: work around bogus gcc-8 -Wrestrict warning
[linux/fpc-iii.git] / net / mac80211 / tx.c
blob655abb9aa59715614c6707b3f009c58d484d9996
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 * Transmit and frame generation functions.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/bitmap.h>
21 #include <linux/rcupdate.h>
22 #include <linux/export.h>
23 #include <linux/time.h>
24 #include <net/net_namespace.h>
25 #include <net/ieee80211_radiotap.h>
26 #include <net/cfg80211.h>
27 #include <net/mac80211.h>
28 #include <asm/unaligned.h>
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "led.h"
33 #include "mesh.h"
34 #include "wep.h"
35 #include "wpa.h"
36 #include "wme.h"
37 #include "rate.h"
39 /* misc utils */
41 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
42 struct sk_buff *skb, int group_addr,
43 int next_frag_len)
45 int rate, mrate, erp, dur, i, shift = 0;
46 struct ieee80211_rate *txrate;
47 struct ieee80211_local *local = tx->local;
48 struct ieee80211_supported_band *sband;
49 struct ieee80211_hdr *hdr;
50 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
51 struct ieee80211_chanctx_conf *chanctx_conf;
52 u32 rate_flags = 0;
54 rcu_read_lock();
55 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
56 if (chanctx_conf) {
57 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
58 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
60 rcu_read_unlock();
62 /* assume HW handles this */
63 if (tx->rate.flags & IEEE80211_TX_RC_MCS)
64 return 0;
66 /* uh huh? */
67 if (WARN_ON_ONCE(tx->rate.idx < 0))
68 return 0;
70 sband = local->hw.wiphy->bands[info->band];
71 txrate = &sband->bitrates[tx->rate.idx];
73 erp = txrate->flags & IEEE80211_RATE_ERP_G;
76 * data and mgmt (except PS Poll):
77 * - during CFP: 32768
78 * - during contention period:
79 * if addr1 is group address: 0
80 * if more fragments = 0 and addr1 is individual address: time to
81 * transmit one ACK plus SIFS
82 * if more fragments = 1 and addr1 is individual address: time to
83 * transmit next fragment plus 2 x ACK plus 3 x SIFS
85 * IEEE 802.11, 9.6:
86 * - control response frame (CTS or ACK) shall be transmitted using the
87 * same rate as the immediately previous frame in the frame exchange
88 * sequence, if this rate belongs to the PHY mandatory rates, or else
89 * at the highest possible rate belonging to the PHY rates in the
90 * BSSBasicRateSet
92 hdr = (struct ieee80211_hdr *)skb->data;
93 if (ieee80211_is_ctl(hdr->frame_control)) {
94 /* TODO: These control frames are not currently sent by
95 * mac80211, but should they be implemented, this function
96 * needs to be updated to support duration field calculation.
98 * RTS: time needed to transmit pending data/mgmt frame plus
99 * one CTS frame plus one ACK frame plus 3 x SIFS
100 * CTS: duration of immediately previous RTS minus time
101 * required to transmit CTS and its SIFS
102 * ACK: 0 if immediately previous directed data/mgmt had
103 * more=0, with more=1 duration in ACK frame is duration
104 * from previous frame minus time needed to transmit ACK
105 * and its SIFS
106 * PS Poll: BIT(15) | BIT(14) | aid
108 return 0;
111 /* data/mgmt */
112 if (0 /* FIX: data/mgmt during CFP */)
113 return cpu_to_le16(32768);
115 if (group_addr) /* Group address as the destination - no ACK */
116 return 0;
118 /* Individual destination address:
119 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
120 * CTS and ACK frames shall be transmitted using the highest rate in
121 * basic rate set that is less than or equal to the rate of the
122 * immediately previous frame and that is using the same modulation
123 * (CCK or OFDM). If no basic rate set matches with these requirements,
124 * the highest mandatory rate of the PHY that is less than or equal to
125 * the rate of the previous frame is used.
126 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
128 rate = -1;
129 /* use lowest available if everything fails */
130 mrate = sband->bitrates[0].bitrate;
131 for (i = 0; i < sband->n_bitrates; i++) {
132 struct ieee80211_rate *r = &sband->bitrates[i];
134 if (r->bitrate > txrate->bitrate)
135 break;
137 if ((rate_flags & r->flags) != rate_flags)
138 continue;
140 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
141 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
143 switch (sband->band) {
144 case IEEE80211_BAND_2GHZ: {
145 u32 flag;
146 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
147 flag = IEEE80211_RATE_MANDATORY_G;
148 else
149 flag = IEEE80211_RATE_MANDATORY_B;
150 if (r->flags & flag)
151 mrate = r->bitrate;
152 break;
154 case IEEE80211_BAND_5GHZ:
155 if (r->flags & IEEE80211_RATE_MANDATORY_A)
156 mrate = r->bitrate;
157 break;
158 case IEEE80211_BAND_60GHZ:
159 /* TODO, for now fall through */
160 case IEEE80211_NUM_BANDS:
161 WARN_ON(1);
162 break;
165 if (rate == -1) {
166 /* No matching basic rate found; use highest suitable mandatory
167 * PHY rate */
168 rate = DIV_ROUND_UP(mrate, 1 << shift);
171 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
172 if (ieee80211_is_data_qos(hdr->frame_control) &&
173 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
174 dur = 0;
175 else
176 /* Time needed to transmit ACK
177 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
178 * to closest integer */
179 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
180 tx->sdata->vif.bss_conf.use_short_preamble,
181 shift);
183 if (next_frag_len) {
184 /* Frame is fragmented: duration increases with time needed to
185 * transmit next fragment plus ACK and 2 x SIFS. */
186 dur *= 2; /* ACK + SIFS */
187 /* next fragment */
188 dur += ieee80211_frame_duration(sband->band, next_frag_len,
189 txrate->bitrate, erp,
190 tx->sdata->vif.bss_conf.use_short_preamble,
191 shift);
194 return cpu_to_le16(dur);
197 /* tx handlers */
198 static ieee80211_tx_result debug_noinline
199 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
201 struct ieee80211_local *local = tx->local;
202 struct ieee80211_if_managed *ifmgd;
204 /* driver doesn't support power save */
205 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
206 return TX_CONTINUE;
208 /* hardware does dynamic power save */
209 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
210 return TX_CONTINUE;
212 /* dynamic power save disabled */
213 if (local->hw.conf.dynamic_ps_timeout <= 0)
214 return TX_CONTINUE;
216 /* we are scanning, don't enable power save */
217 if (local->scanning)
218 return TX_CONTINUE;
220 if (!local->ps_sdata)
221 return TX_CONTINUE;
223 /* No point if we're going to suspend */
224 if (local->quiescing)
225 return TX_CONTINUE;
227 /* dynamic ps is supported only in managed mode */
228 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
229 return TX_CONTINUE;
231 ifmgd = &tx->sdata->u.mgd;
234 * Don't wakeup from power save if u-apsd is enabled, voip ac has
235 * u-apsd enabled and the frame is in voip class. This effectively
236 * means that even if all access categories have u-apsd enabled, in
237 * practise u-apsd is only used with the voip ac. This is a
238 * workaround for the case when received voip class packets do not
239 * have correct qos tag for some reason, due the network or the
240 * peer application.
242 * Note: ifmgd->uapsd_queues access is racy here. If the value is
243 * changed via debugfs, user needs to reassociate manually to have
244 * everything in sync.
246 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
247 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
248 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
249 return TX_CONTINUE;
251 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
252 ieee80211_stop_queues_by_reason(&local->hw,
253 IEEE80211_MAX_QUEUE_MAP,
254 IEEE80211_QUEUE_STOP_REASON_PS,
255 false);
256 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
257 ieee80211_queue_work(&local->hw,
258 &local->dynamic_ps_disable_work);
261 /* Don't restart the timer if we're not disassociated */
262 if (!ifmgd->associated)
263 return TX_CONTINUE;
265 mod_timer(&local->dynamic_ps_timer, jiffies +
266 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
268 return TX_CONTINUE;
271 static ieee80211_tx_result debug_noinline
272 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
275 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
276 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
277 bool assoc = false;
279 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
280 return TX_CONTINUE;
282 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
283 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
284 !ieee80211_is_probe_req(hdr->frame_control) &&
285 !ieee80211_is_nullfunc(hdr->frame_control))
287 * When software scanning only nullfunc frames (to notify
288 * the sleep state to the AP) and probe requests (for the
289 * active scan) are allowed, all other frames should not be
290 * sent and we should not get here, but if we do
291 * nonetheless, drop them to avoid sending them
292 * off-channel. See the link below and
293 * ieee80211_start_scan() for more.
295 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
297 return TX_DROP;
299 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
300 return TX_CONTINUE;
302 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
303 return TX_CONTINUE;
305 if (tx->sta)
306 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
308 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
309 if (unlikely(!assoc &&
310 ieee80211_is_data(hdr->frame_control))) {
311 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
312 sdata_info(tx->sdata,
313 "dropped data frame to not associated station %pM\n",
314 hdr->addr1);
315 #endif
316 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
317 return TX_DROP;
319 } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
320 ieee80211_is_data(hdr->frame_control) &&
321 !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
323 * No associated STAs - no need to send multicast
324 * frames.
326 return TX_DROP;
329 return TX_CONTINUE;
332 /* This function is called whenever the AP is about to exceed the maximum limit
333 * of buffered frames for power saving STAs. This situation should not really
334 * happen often during normal operation, so dropping the oldest buffered packet
335 * from each queue should be OK to make some room for new frames. */
336 static void purge_old_ps_buffers(struct ieee80211_local *local)
338 int total = 0, purged = 0;
339 struct sk_buff *skb;
340 struct ieee80211_sub_if_data *sdata;
341 struct sta_info *sta;
343 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
344 struct ps_data *ps;
346 if (sdata->vif.type == NL80211_IFTYPE_AP)
347 ps = &sdata->u.ap.ps;
348 else if (ieee80211_vif_is_mesh(&sdata->vif))
349 ps = &sdata->u.mesh.ps;
350 else
351 continue;
353 skb = skb_dequeue(&ps->bc_buf);
354 if (skb) {
355 purged++;
356 ieee80211_free_txskb(&local->hw, skb);
358 total += skb_queue_len(&ps->bc_buf);
362 * Drop one frame from each station from the lowest-priority
363 * AC that has frames at all.
365 list_for_each_entry_rcu(sta, &local->sta_list, list) {
366 int ac;
368 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
369 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
370 total += skb_queue_len(&sta->ps_tx_buf[ac]);
371 if (skb) {
372 purged++;
373 ieee80211_free_txskb(&local->hw, skb);
374 break;
379 local->total_ps_buffered = total;
380 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
383 static ieee80211_tx_result
384 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
386 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
387 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
388 struct ps_data *ps;
391 * broadcast/multicast frame
393 * If any of the associated/peer stations is in power save mode,
394 * the frame is buffered to be sent after DTIM beacon frame.
395 * This is done either by the hardware or us.
398 /* powersaving STAs currently only in AP/VLAN/mesh mode */
399 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
400 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
401 if (!tx->sdata->bss)
402 return TX_CONTINUE;
404 ps = &tx->sdata->bss->ps;
405 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
406 ps = &tx->sdata->u.mesh.ps;
407 } else {
408 return TX_CONTINUE;
412 /* no buffering for ordered frames */
413 if (ieee80211_has_order(hdr->frame_control))
414 return TX_CONTINUE;
416 if (ieee80211_is_probe_req(hdr->frame_control))
417 return TX_CONTINUE;
419 if (tx->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
420 info->hw_queue = tx->sdata->vif.cab_queue;
422 /* no stations in PS mode */
423 if (!atomic_read(&ps->num_sta_ps))
424 return TX_CONTINUE;
426 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
428 /* device releases frame after DTIM beacon */
429 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
430 return TX_CONTINUE;
432 /* buffered in mac80211 */
433 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
434 purge_old_ps_buffers(tx->local);
436 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
437 ps_dbg(tx->sdata,
438 "BC TX buffer full - dropping the oldest frame\n");
439 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
440 } else
441 tx->local->total_ps_buffered++;
443 skb_queue_tail(&ps->bc_buf, tx->skb);
445 return TX_QUEUED;
448 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
449 struct sk_buff *skb)
451 if (!ieee80211_is_mgmt(fc))
452 return 0;
454 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
455 return 0;
457 if (!ieee80211_is_robust_mgmt_frame(skb))
458 return 0;
460 return 1;
463 static ieee80211_tx_result
464 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
466 struct sta_info *sta = tx->sta;
467 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
468 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
469 struct ieee80211_local *local = tx->local;
471 if (unlikely(!sta))
472 return TX_CONTINUE;
474 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
475 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
476 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
477 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
478 int ac = skb_get_queue_mapping(tx->skb);
480 if (ieee80211_is_mgmt(hdr->frame_control) &&
481 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
482 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
483 return TX_CONTINUE;
486 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
487 sta->sta.addr, sta->sta.aid, ac);
488 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
489 purge_old_ps_buffers(tx->local);
491 /* sync with ieee80211_sta_ps_deliver_wakeup */
492 spin_lock(&sta->ps_lock);
494 * STA woke up the meantime and all the frames on ps_tx_buf have
495 * been queued to pending queue. No reordering can happen, go
496 * ahead and Tx the packet.
498 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
499 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
500 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
501 spin_unlock(&sta->ps_lock);
502 return TX_CONTINUE;
505 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
506 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
507 ps_dbg(tx->sdata,
508 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
509 sta->sta.addr, ac);
510 ieee80211_free_txskb(&local->hw, old);
511 } else
512 tx->local->total_ps_buffered++;
514 info->control.jiffies = jiffies;
515 info->control.vif = &tx->sdata->vif;
516 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
517 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
518 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
519 spin_unlock(&sta->ps_lock);
521 if (!timer_pending(&local->sta_cleanup))
522 mod_timer(&local->sta_cleanup,
523 round_jiffies(jiffies +
524 STA_INFO_CLEANUP_INTERVAL));
527 * We queued up some frames, so the TIM bit might
528 * need to be set, recalculate it.
530 sta_info_recalc_tim(sta);
532 return TX_QUEUED;
533 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
534 ps_dbg(tx->sdata,
535 "STA %pM in PS mode, but polling/in SP -> send frame\n",
536 sta->sta.addr);
539 return TX_CONTINUE;
542 static ieee80211_tx_result debug_noinline
543 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
545 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
546 return TX_CONTINUE;
548 if (tx->flags & IEEE80211_TX_UNICAST)
549 return ieee80211_tx_h_unicast_ps_buf(tx);
550 else
551 return ieee80211_tx_h_multicast_ps_buf(tx);
554 static ieee80211_tx_result debug_noinline
555 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
557 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
559 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
560 if (tx->sdata->control_port_no_encrypt)
561 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
562 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
563 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
566 return TX_CONTINUE;
569 static ieee80211_tx_result debug_noinline
570 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
572 struct ieee80211_key *key;
573 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
574 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
576 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
577 tx->key = NULL;
578 else if (tx->sta &&
579 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
580 tx->key = key;
581 else if (ieee80211_is_mgmt(hdr->frame_control) &&
582 is_multicast_ether_addr(hdr->addr1) &&
583 ieee80211_is_robust_mgmt_frame(tx->skb) &&
584 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
585 tx->key = key;
586 else if (is_multicast_ether_addr(hdr->addr1) &&
587 (key = rcu_dereference(tx->sdata->default_multicast_key)))
588 tx->key = key;
589 else if (!is_multicast_ether_addr(hdr->addr1) &&
590 (key = rcu_dereference(tx->sdata->default_unicast_key)))
591 tx->key = key;
592 else if (info->flags & IEEE80211_TX_CTL_INJECTED)
593 tx->key = NULL;
594 else if (!tx->sdata->drop_unencrypted)
595 tx->key = NULL;
596 else if (tx->skb->protocol == tx->sdata->control_port_protocol)
597 tx->key = NULL;
598 else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
599 !(ieee80211_is_action(hdr->frame_control) &&
600 tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
601 tx->key = NULL;
602 else if (ieee80211_is_mgmt(hdr->frame_control) &&
603 !ieee80211_is_robust_mgmt_frame(tx->skb))
604 tx->key = NULL;
605 else {
606 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
607 return TX_DROP;
610 if (tx->key) {
611 bool skip_hw = false;
613 tx->key->tx_rx_count++;
614 /* TODO: add threshold stuff again */
616 switch (tx->key->conf.cipher) {
617 case WLAN_CIPHER_SUITE_WEP40:
618 case WLAN_CIPHER_SUITE_WEP104:
619 case WLAN_CIPHER_SUITE_TKIP:
620 if (!ieee80211_is_data_present(hdr->frame_control))
621 tx->key = NULL;
622 break;
623 case WLAN_CIPHER_SUITE_CCMP:
624 if (!ieee80211_is_data_present(hdr->frame_control) &&
625 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
626 tx->skb))
627 tx->key = NULL;
628 else
629 skip_hw = (tx->key->conf.flags &
630 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
631 ieee80211_is_mgmt(hdr->frame_control);
632 break;
633 case WLAN_CIPHER_SUITE_AES_CMAC:
634 if (!ieee80211_is_mgmt(hdr->frame_control))
635 tx->key = NULL;
636 break;
639 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
640 !ieee80211_is_deauth(hdr->frame_control)))
641 return TX_DROP;
643 if (!skip_hw && tx->key &&
644 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
645 info->control.hw_key = &tx->key->conf;
648 return TX_CONTINUE;
651 static ieee80211_tx_result debug_noinline
652 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
654 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
655 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
656 struct ieee80211_supported_band *sband;
657 u32 len;
658 struct ieee80211_tx_rate_control txrc;
659 struct ieee80211_sta_rates *ratetbl = NULL;
660 bool assoc = false;
662 memset(&txrc, 0, sizeof(txrc));
664 sband = tx->local->hw.wiphy->bands[info->band];
666 len = min_t(u32, tx->skb->len + FCS_LEN,
667 tx->local->hw.wiphy->frag_threshold);
669 /* set up the tx rate control struct we give the RC algo */
670 txrc.hw = &tx->local->hw;
671 txrc.sband = sband;
672 txrc.bss_conf = &tx->sdata->vif.bss_conf;
673 txrc.skb = tx->skb;
674 txrc.reported_rate.idx = -1;
675 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
676 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
677 txrc.max_rate_idx = -1;
678 else
679 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
681 if (tx->sdata->rc_has_mcs_mask[info->band])
682 txrc.rate_idx_mcs_mask =
683 tx->sdata->rc_rateidx_mcs_mask[info->band];
685 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
686 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
687 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
689 /* set up RTS protection if desired */
690 if (len > tx->local->hw.wiphy->rts_threshold) {
691 txrc.rts = true;
694 info->control.use_rts = txrc.rts;
695 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
698 * Use short preamble if the BSS can handle it, but not for
699 * management frames unless we know the receiver can handle
700 * that -- the management frame might be to a station that
701 * just wants a probe response.
703 if (tx->sdata->vif.bss_conf.use_short_preamble &&
704 (ieee80211_is_data(hdr->frame_control) ||
705 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
706 txrc.short_preamble = true;
708 info->control.short_preamble = txrc.short_preamble;
710 if (tx->sta)
711 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
714 * Lets not bother rate control if we're associated and cannot
715 * talk to the sta. This should not happen.
717 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
718 !rate_usable_index_exists(sband, &tx->sta->sta),
719 "%s: Dropped data frame as no usable bitrate found while "
720 "scanning and associated. Target station: "
721 "%pM on %d GHz band\n",
722 tx->sdata->name, hdr->addr1,
723 info->band ? 5 : 2))
724 return TX_DROP;
727 * If we're associated with the sta at this point we know we can at
728 * least send the frame at the lowest bit rate.
730 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
732 if (tx->sta && !info->control.skip_table)
733 ratetbl = rcu_dereference(tx->sta->sta.rates);
735 if (unlikely(info->control.rates[0].idx < 0)) {
736 if (ratetbl) {
737 struct ieee80211_tx_rate rate = {
738 .idx = ratetbl->rate[0].idx,
739 .flags = ratetbl->rate[0].flags,
740 .count = ratetbl->rate[0].count
743 if (ratetbl->rate[0].idx < 0)
744 return TX_DROP;
746 tx->rate = rate;
747 } else {
748 return TX_DROP;
750 } else {
751 tx->rate = info->control.rates[0];
754 if (txrc.reported_rate.idx < 0) {
755 txrc.reported_rate = tx->rate;
756 if (tx->sta && ieee80211_is_data(hdr->frame_control))
757 tx->sta->last_tx_rate = txrc.reported_rate;
758 } else if (tx->sta)
759 tx->sta->last_tx_rate = txrc.reported_rate;
761 if (ratetbl)
762 return TX_CONTINUE;
764 if (unlikely(!info->control.rates[0].count))
765 info->control.rates[0].count = 1;
767 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
768 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
769 info->control.rates[0].count = 1;
771 return TX_CONTINUE;
774 static ieee80211_tx_result debug_noinline
775 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
777 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
778 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
779 u16 *seq;
780 u8 *qc;
781 int tid;
784 * Packet injection may want to control the sequence
785 * number, if we have no matching interface then we
786 * neither assign one ourselves nor ask the driver to.
788 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
789 return TX_CONTINUE;
791 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
792 return TX_CONTINUE;
794 if (ieee80211_hdrlen(hdr->frame_control) < 24)
795 return TX_CONTINUE;
797 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
798 return TX_CONTINUE;
801 * Anything but QoS data that has a sequence number field
802 * (is long enough) gets a sequence number from the global
803 * counter. QoS data frames with a multicast destination
804 * also use the global counter (802.11-2012 9.3.2.10).
806 if (!ieee80211_is_data_qos(hdr->frame_control) ||
807 is_multicast_ether_addr(hdr->addr1)) {
808 /* driver should assign sequence number */
809 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
810 /* for pure STA mode without beacons, we can do it */
811 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
812 tx->sdata->sequence_number += 0x10;
813 return TX_CONTINUE;
817 * This should be true for injected/management frames only, for
818 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
819 * above since they are not QoS-data frames.
821 if (!tx->sta)
822 return TX_CONTINUE;
824 /* include per-STA, per-TID sequence counter */
826 qc = ieee80211_get_qos_ctl(hdr);
827 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
828 seq = &tx->sta->tid_seq[tid];
830 hdr->seq_ctrl = cpu_to_le16(*seq);
832 /* Increase the sequence number. */
833 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
835 return TX_CONTINUE;
838 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
839 struct sk_buff *skb, int hdrlen,
840 int frag_threshold)
842 struct ieee80211_local *local = tx->local;
843 struct ieee80211_tx_info *info;
844 struct sk_buff *tmp;
845 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
846 int pos = hdrlen + per_fragm;
847 int rem = skb->len - hdrlen - per_fragm;
849 if (WARN_ON(rem < 0))
850 return -EINVAL;
852 /* first fragment was already added to queue by caller */
854 while (rem) {
855 int fraglen = per_fragm;
857 if (fraglen > rem)
858 fraglen = rem;
859 rem -= fraglen;
860 tmp = dev_alloc_skb(local->tx_headroom +
861 frag_threshold +
862 tx->sdata->encrypt_headroom +
863 IEEE80211_ENCRYPT_TAILROOM);
864 if (!tmp)
865 return -ENOMEM;
867 __skb_queue_tail(&tx->skbs, tmp);
869 skb_reserve(tmp,
870 local->tx_headroom + tx->sdata->encrypt_headroom);
872 /* copy control information */
873 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
875 info = IEEE80211_SKB_CB(tmp);
876 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
877 IEEE80211_TX_CTL_FIRST_FRAGMENT);
879 if (rem)
880 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
882 skb_copy_queue_mapping(tmp, skb);
883 tmp->priority = skb->priority;
884 tmp->dev = skb->dev;
886 /* copy header and data */
887 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
888 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
890 pos += fraglen;
893 /* adjust first fragment's length */
894 skb_trim(skb, hdrlen + per_fragm);
895 return 0;
898 static ieee80211_tx_result debug_noinline
899 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
901 struct sk_buff *skb = tx->skb;
902 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
903 struct ieee80211_hdr *hdr = (void *)skb->data;
904 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
905 int hdrlen;
906 int fragnum;
908 /* no matter what happens, tx->skb moves to tx->skbs */
909 __skb_queue_tail(&tx->skbs, skb);
910 tx->skb = NULL;
912 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
913 return TX_CONTINUE;
915 if (tx->local->ops->set_frag_threshold)
916 return TX_CONTINUE;
919 * Warn when submitting a fragmented A-MPDU frame and drop it.
920 * This scenario is handled in ieee80211_tx_prepare but extra
921 * caution taken here as fragmented ampdu may cause Tx stop.
923 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
924 return TX_DROP;
926 hdrlen = ieee80211_hdrlen(hdr->frame_control);
928 /* internal error, why isn't DONTFRAG set? */
929 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
930 return TX_DROP;
933 * Now fragment the frame. This will allocate all the fragments and
934 * chain them (using skb as the first fragment) to skb->next.
935 * During transmission, we will remove the successfully transmitted
936 * fragments from this list. When the low-level driver rejects one
937 * of the fragments then we will simply pretend to accept the skb
938 * but store it away as pending.
940 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
941 return TX_DROP;
943 /* update duration/seq/flags of fragments */
944 fragnum = 0;
946 skb_queue_walk(&tx->skbs, skb) {
947 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
949 hdr = (void *)skb->data;
950 info = IEEE80211_SKB_CB(skb);
952 if (!skb_queue_is_last(&tx->skbs, skb)) {
953 hdr->frame_control |= morefrags;
955 * No multi-rate retries for fragmented frames, that
956 * would completely throw off the NAV at other STAs.
958 info->control.rates[1].idx = -1;
959 info->control.rates[2].idx = -1;
960 info->control.rates[3].idx = -1;
961 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
962 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
963 } else {
964 hdr->frame_control &= ~morefrags;
966 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
967 fragnum++;
970 return TX_CONTINUE;
973 static ieee80211_tx_result debug_noinline
974 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
976 struct sk_buff *skb;
977 int ac = -1;
979 if (!tx->sta)
980 return TX_CONTINUE;
982 skb_queue_walk(&tx->skbs, skb) {
983 ac = skb_get_queue_mapping(skb);
984 tx->sta->tx_fragments++;
985 tx->sta->tx_bytes[ac] += skb->len;
987 if (ac >= 0)
988 tx->sta->tx_packets[ac]++;
990 return TX_CONTINUE;
993 static ieee80211_tx_result debug_noinline
994 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
996 if (!tx->key)
997 return TX_CONTINUE;
999 switch (tx->key->conf.cipher) {
1000 case WLAN_CIPHER_SUITE_WEP40:
1001 case WLAN_CIPHER_SUITE_WEP104:
1002 return ieee80211_crypto_wep_encrypt(tx);
1003 case WLAN_CIPHER_SUITE_TKIP:
1004 return ieee80211_crypto_tkip_encrypt(tx);
1005 case WLAN_CIPHER_SUITE_CCMP:
1006 return ieee80211_crypto_ccmp_encrypt(tx);
1007 case WLAN_CIPHER_SUITE_AES_CMAC:
1008 return ieee80211_crypto_aes_cmac_encrypt(tx);
1009 default:
1010 return ieee80211_crypto_hw_encrypt(tx);
1013 return TX_DROP;
1016 static ieee80211_tx_result debug_noinline
1017 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1019 struct sk_buff *skb;
1020 struct ieee80211_hdr *hdr;
1021 int next_len;
1022 bool group_addr;
1024 skb_queue_walk(&tx->skbs, skb) {
1025 hdr = (void *) skb->data;
1026 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1027 break; /* must not overwrite AID */
1028 if (!skb_queue_is_last(&tx->skbs, skb)) {
1029 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1030 next_len = next->len;
1031 } else
1032 next_len = 0;
1033 group_addr = is_multicast_ether_addr(hdr->addr1);
1035 hdr->duration_id =
1036 ieee80211_duration(tx, skb, group_addr, next_len);
1039 return TX_CONTINUE;
1042 /* actual transmit path */
1044 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1045 struct sk_buff *skb,
1046 struct ieee80211_tx_info *info,
1047 struct tid_ampdu_tx *tid_tx,
1048 int tid)
1050 bool queued = false;
1051 bool reset_agg_timer = false;
1052 struct sk_buff *purge_skb = NULL;
1054 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1055 info->flags |= IEEE80211_TX_CTL_AMPDU;
1056 reset_agg_timer = true;
1057 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1059 * nothing -- this aggregation session is being started
1060 * but that might still fail with the driver
1062 } else {
1063 spin_lock(&tx->sta->lock);
1065 * Need to re-check now, because we may get here
1067 * 1) in the window during which the setup is actually
1068 * already done, but not marked yet because not all
1069 * packets are spliced over to the driver pending
1070 * queue yet -- if this happened we acquire the lock
1071 * either before or after the splice happens, but
1072 * need to recheck which of these cases happened.
1074 * 2) during session teardown, if the OPERATIONAL bit
1075 * was cleared due to the teardown but the pointer
1076 * hasn't been assigned NULL yet (or we loaded it
1077 * before it was assigned) -- in this case it may
1078 * now be NULL which means we should just let the
1079 * packet pass through because splicing the frames
1080 * back is already done.
1082 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1084 if (!tid_tx) {
1085 /* do nothing, let packet pass through */
1086 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1087 info->flags |= IEEE80211_TX_CTL_AMPDU;
1088 reset_agg_timer = true;
1089 } else {
1090 queued = true;
1091 info->control.vif = &tx->sdata->vif;
1092 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1093 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1094 __skb_queue_tail(&tid_tx->pending, skb);
1095 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1096 purge_skb = __skb_dequeue(&tid_tx->pending);
1098 spin_unlock(&tx->sta->lock);
1100 if (purge_skb)
1101 ieee80211_free_txskb(&tx->local->hw, purge_skb);
1104 /* reset session timer */
1105 if (reset_agg_timer && tid_tx->timeout)
1106 tid_tx->last_tx = jiffies;
1108 return queued;
1112 * initialises @tx
1114 static ieee80211_tx_result
1115 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1116 struct ieee80211_tx_data *tx,
1117 struct sk_buff *skb)
1119 struct ieee80211_local *local = sdata->local;
1120 struct ieee80211_hdr *hdr;
1121 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1122 int tid;
1123 u8 *qc;
1125 memset(tx, 0, sizeof(*tx));
1126 tx->skb = skb;
1127 tx->local = local;
1128 tx->sdata = sdata;
1129 __skb_queue_head_init(&tx->skbs);
1132 * If this flag is set to true anywhere, and we get here,
1133 * we are doing the needed processing, so remove the flag
1134 * now.
1136 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1138 hdr = (struct ieee80211_hdr *) skb->data;
1140 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1141 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1142 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1143 return TX_DROP;
1144 } else if (info->flags & (IEEE80211_TX_CTL_INJECTED |
1145 IEEE80211_TX_INTFL_NL80211_FRAME_TX) ||
1146 tx->sdata->control_port_protocol == tx->skb->protocol) {
1147 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1149 if (!tx->sta)
1150 tx->sta = sta_info_get(sdata, hdr->addr1);
1152 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1153 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1154 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
1155 !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
1156 struct tid_ampdu_tx *tid_tx;
1158 qc = ieee80211_get_qos_ctl(hdr);
1159 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1161 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1162 if (tid_tx) {
1163 bool queued;
1165 queued = ieee80211_tx_prep_agg(tx, skb, info,
1166 tid_tx, tid);
1168 if (unlikely(queued))
1169 return TX_QUEUED;
1173 if (is_multicast_ether_addr(hdr->addr1)) {
1174 tx->flags &= ~IEEE80211_TX_UNICAST;
1175 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1176 } else
1177 tx->flags |= IEEE80211_TX_UNICAST;
1179 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1180 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1181 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1182 info->flags & IEEE80211_TX_CTL_AMPDU)
1183 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1186 if (!tx->sta)
1187 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1188 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1189 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1191 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1193 return TX_CONTINUE;
1196 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1197 struct ieee80211_vif *vif,
1198 struct ieee80211_sta *sta,
1199 struct sk_buff_head *skbs,
1200 bool txpending)
1202 struct ieee80211_tx_control control;
1203 struct sk_buff *skb, *tmp;
1204 unsigned long flags;
1206 skb_queue_walk_safe(skbs, skb, tmp) {
1207 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1208 int q = info->hw_queue;
1210 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1211 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1212 __skb_unlink(skb, skbs);
1213 ieee80211_free_txskb(&local->hw, skb);
1214 continue;
1216 #endif
1218 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1219 if (local->queue_stop_reasons[q] ||
1220 (!txpending && !skb_queue_empty(&local->pending[q]))) {
1221 if (unlikely(info->flags &
1222 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1223 if (local->queue_stop_reasons[q] &
1224 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1226 * Drop off-channel frames if queues
1227 * are stopped for any reason other
1228 * than off-channel operation. Never
1229 * queue them.
1231 spin_unlock_irqrestore(
1232 &local->queue_stop_reason_lock,
1233 flags);
1234 ieee80211_purge_tx_queue(&local->hw,
1235 skbs);
1236 return true;
1238 } else {
1241 * Since queue is stopped, queue up frames for
1242 * later transmission from the tx-pending
1243 * tasklet when the queue is woken again.
1245 if (txpending)
1246 skb_queue_splice_init(skbs,
1247 &local->pending[q]);
1248 else
1249 skb_queue_splice_tail_init(skbs,
1250 &local->pending[q]);
1252 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1253 flags);
1254 return false;
1257 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1259 info->control.vif = vif;
1260 control.sta = sta;
1262 __skb_unlink(skb, skbs);
1263 drv_tx(local, &control, skb);
1266 return true;
1270 * Returns false if the frame couldn't be transmitted but was queued instead.
1272 static bool __ieee80211_tx(struct ieee80211_local *local,
1273 struct sk_buff_head *skbs, int led_len,
1274 struct sta_info *sta, bool txpending)
1276 struct ieee80211_tx_info *info;
1277 struct ieee80211_sub_if_data *sdata;
1278 struct ieee80211_vif *vif;
1279 struct ieee80211_sta *pubsta;
1280 struct sk_buff *skb;
1281 bool result = true;
1282 __le16 fc;
1284 if (WARN_ON(skb_queue_empty(skbs)))
1285 return true;
1287 skb = skb_peek(skbs);
1288 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1289 info = IEEE80211_SKB_CB(skb);
1290 sdata = vif_to_sdata(info->control.vif);
1291 if (sta && !sta->uploaded)
1292 sta = NULL;
1294 if (sta)
1295 pubsta = &sta->sta;
1296 else
1297 pubsta = NULL;
1299 switch (sdata->vif.type) {
1300 case NL80211_IFTYPE_MONITOR:
1301 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1302 vif = &sdata->vif;
1303 break;
1305 sdata = rcu_dereference(local->monitor_sdata);
1306 if (sdata) {
1307 vif = &sdata->vif;
1308 info->hw_queue =
1309 vif->hw_queue[skb_get_queue_mapping(skb)];
1310 } else if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
1311 dev_kfree_skb(skb);
1312 return true;
1313 } else
1314 vif = NULL;
1315 break;
1316 case NL80211_IFTYPE_AP_VLAN:
1317 sdata = container_of(sdata->bss,
1318 struct ieee80211_sub_if_data, u.ap);
1319 /* fall through */
1320 default:
1321 vif = &sdata->vif;
1322 break;
1325 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1326 txpending);
1328 ieee80211_tpt_led_trig_tx(local, fc, led_len);
1330 WARN_ON_ONCE(!skb_queue_empty(skbs));
1332 return result;
1336 * Invoke TX handlers, return 0 on success and non-zero if the
1337 * frame was dropped or queued.
1339 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1341 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1342 ieee80211_tx_result res = TX_DROP;
1344 #define CALL_TXH(txh) \
1345 do { \
1346 res = txh(tx); \
1347 if (res != TX_CONTINUE) \
1348 goto txh_done; \
1349 } while (0)
1351 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1352 CALL_TXH(ieee80211_tx_h_check_assoc);
1353 CALL_TXH(ieee80211_tx_h_ps_buf);
1354 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1355 CALL_TXH(ieee80211_tx_h_select_key);
1356 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1357 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1359 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1360 __skb_queue_tail(&tx->skbs, tx->skb);
1361 tx->skb = NULL;
1362 goto txh_done;
1365 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1366 CALL_TXH(ieee80211_tx_h_sequence);
1367 CALL_TXH(ieee80211_tx_h_fragment);
1368 /* handlers after fragment must be aware of tx info fragmentation! */
1369 CALL_TXH(ieee80211_tx_h_stats);
1370 CALL_TXH(ieee80211_tx_h_encrypt);
1371 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1372 CALL_TXH(ieee80211_tx_h_calculate_duration);
1373 #undef CALL_TXH
1375 txh_done:
1376 if (unlikely(res == TX_DROP)) {
1377 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1378 if (tx->skb)
1379 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1380 else
1381 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1382 return -1;
1383 } else if (unlikely(res == TX_QUEUED)) {
1384 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1385 return -1;
1388 return 0;
1391 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1392 struct ieee80211_vif *vif, struct sk_buff *skb,
1393 int band, struct ieee80211_sta **sta)
1395 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1396 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1397 struct ieee80211_tx_data tx;
1399 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1400 return false;
1402 info->band = band;
1403 info->control.vif = vif;
1404 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1406 if (invoke_tx_handlers(&tx))
1407 return false;
1409 if (sta) {
1410 if (tx.sta)
1411 *sta = &tx.sta->sta;
1412 else
1413 *sta = NULL;
1416 return true;
1418 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1421 * Returns false if the frame couldn't be transmitted but was queued instead.
1423 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1424 struct sk_buff *skb, bool txpending,
1425 enum ieee80211_band band)
1427 struct ieee80211_local *local = sdata->local;
1428 struct ieee80211_tx_data tx;
1429 ieee80211_tx_result res_prepare;
1430 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1431 bool result = true;
1432 int led_len;
1434 if (unlikely(skb->len < 10)) {
1435 dev_kfree_skb(skb);
1436 return true;
1439 /* initialises tx */
1440 led_len = skb->len;
1441 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
1443 if (unlikely(res_prepare == TX_DROP)) {
1444 ieee80211_free_txskb(&local->hw, skb);
1445 return true;
1446 } else if (unlikely(res_prepare == TX_QUEUED)) {
1447 return true;
1450 info->band = band;
1452 /* set up hw_queue value early */
1453 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1454 !(local->hw.flags & IEEE80211_HW_QUEUE_CONTROL))
1455 info->hw_queue =
1456 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1458 if (!invoke_tx_handlers(&tx))
1459 result = __ieee80211_tx(local, &tx.skbs, led_len,
1460 tx.sta, txpending);
1462 return result;
1465 /* device xmit handlers */
1467 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1468 struct sk_buff *skb,
1469 int head_need, bool may_encrypt)
1471 struct ieee80211_local *local = sdata->local;
1472 int tail_need = 0;
1474 if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
1475 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1476 tail_need -= skb_tailroom(skb);
1477 tail_need = max_t(int, tail_need, 0);
1480 if (skb_cloned(skb) &&
1481 (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CLONED_SKBS) ||
1482 !skb_clone_writable(skb, ETH_HLEN) ||
1483 sdata->crypto_tx_tailroom_needed_cnt))
1484 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1485 else if (head_need || tail_need)
1486 I802_DEBUG_INC(local->tx_expand_skb_head);
1487 else
1488 return 0;
1490 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1491 wiphy_debug(local->hw.wiphy,
1492 "failed to reallocate TX buffer\n");
1493 return -ENOMEM;
1496 return 0;
1499 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
1500 enum ieee80211_band band)
1502 struct ieee80211_local *local = sdata->local;
1503 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1504 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1505 int headroom;
1506 bool may_encrypt;
1508 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1510 headroom = local->tx_headroom;
1511 if (may_encrypt)
1512 headroom += sdata->encrypt_headroom;
1513 headroom -= skb_headroom(skb);
1514 headroom = max_t(int, 0, headroom);
1516 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1517 ieee80211_free_txskb(&local->hw, skb);
1518 return;
1521 hdr = (struct ieee80211_hdr *) skb->data;
1522 info->control.vif = &sdata->vif;
1524 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1525 if (ieee80211_is_data(hdr->frame_control) &&
1526 is_unicast_ether_addr(hdr->addr1)) {
1527 if (mesh_nexthop_resolve(sdata, skb))
1528 return; /* skb queued: don't free */
1529 } else {
1530 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1534 ieee80211_set_qos_hdr(sdata, skb);
1535 ieee80211_tx(sdata, skb, false, band);
1538 static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
1540 struct ieee80211_radiotap_iterator iterator;
1541 struct ieee80211_radiotap_header *rthdr =
1542 (struct ieee80211_radiotap_header *) skb->data;
1543 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1544 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1545 NULL);
1546 u16 txflags;
1548 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1549 IEEE80211_TX_CTL_DONTFRAG;
1552 * for every radiotap entry that is present
1553 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1554 * entries present, or -EINVAL on error)
1557 while (!ret) {
1558 ret = ieee80211_radiotap_iterator_next(&iterator);
1560 if (ret)
1561 continue;
1563 /* see if this argument is something we can use */
1564 switch (iterator.this_arg_index) {
1566 * You must take care when dereferencing iterator.this_arg
1567 * for multibyte types... the pointer is not aligned. Use
1568 * get_unaligned((type *)iterator.this_arg) to dereference
1569 * iterator.this_arg for type "type" safely on all arches.
1571 case IEEE80211_RADIOTAP_FLAGS:
1572 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1574 * this indicates that the skb we have been
1575 * handed has the 32-bit FCS CRC at the end...
1576 * we should react to that by snipping it off
1577 * because it will be recomputed and added
1578 * on transmission
1580 if (skb->len < (iterator._max_length + FCS_LEN))
1581 return false;
1583 skb_trim(skb, skb->len - FCS_LEN);
1585 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1586 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1587 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1588 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1589 break;
1591 case IEEE80211_RADIOTAP_TX_FLAGS:
1592 txflags = get_unaligned_le16(iterator.this_arg);
1593 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1594 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1595 break;
1598 * Please update the file
1599 * Documentation/networking/mac80211-injection.txt
1600 * when parsing new fields here.
1603 default:
1604 break;
1608 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1609 return false;
1612 * remove the radiotap header
1613 * iterator->_max_length was sanity-checked against
1614 * skb->len by iterator init
1616 skb_pull(skb, iterator._max_length);
1618 return true;
1621 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1622 struct net_device *dev)
1624 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1625 struct ieee80211_chanctx_conf *chanctx_conf;
1626 struct ieee80211_radiotap_header *prthdr =
1627 (struct ieee80211_radiotap_header *)skb->data;
1628 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1629 struct ieee80211_hdr *hdr;
1630 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
1631 struct cfg80211_chan_def *chandef;
1632 u16 len_rthdr;
1633 int hdrlen;
1635 /* check for not even having the fixed radiotap header part */
1636 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1637 goto fail; /* too short to be possibly valid */
1639 /* is it a header version we can trust to find length from? */
1640 if (unlikely(prthdr->it_version))
1641 goto fail; /* only version 0 is supported */
1643 /* then there must be a radiotap header with a length we can use */
1644 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1646 /* does the skb contain enough to deliver on the alleged length? */
1647 if (unlikely(skb->len < len_rthdr))
1648 goto fail; /* skb too short for claimed rt header extent */
1651 * fix up the pointers accounting for the radiotap
1652 * header still being in there. We are being given
1653 * a precooked IEEE80211 header so no need for
1654 * normal processing
1656 skb_set_mac_header(skb, len_rthdr);
1658 * these are just fixed to the end of the rt area since we
1659 * don't have any better information and at this point, nobody cares
1661 skb_set_network_header(skb, len_rthdr);
1662 skb_set_transport_header(skb, len_rthdr);
1664 if (skb->len < len_rthdr + 2)
1665 goto fail;
1667 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1668 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1670 if (skb->len < len_rthdr + hdrlen)
1671 goto fail;
1674 * Initialize skb->protocol if the injected frame is a data frame
1675 * carrying a rfc1042 header
1677 if (ieee80211_is_data(hdr->frame_control) &&
1678 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
1679 u8 *payload = (u8 *)hdr + hdrlen;
1681 if (ether_addr_equal(payload, rfc1042_header))
1682 skb->protocol = cpu_to_be16((payload[6] << 8) |
1683 payload[7]);
1686 memset(info, 0, sizeof(*info));
1688 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
1689 IEEE80211_TX_CTL_INJECTED;
1691 /* process and remove the injection radiotap header */
1692 if (!ieee80211_parse_tx_radiotap(skb))
1693 goto fail;
1695 rcu_read_lock();
1698 * We process outgoing injected frames that have a local address
1699 * we handle as though they are non-injected frames.
1700 * This code here isn't entirely correct, the local MAC address
1701 * isn't always enough to find the interface to use; for proper
1702 * VLAN/WDS support we will need a different mechanism (which
1703 * likely isn't going to be monitor interfaces).
1705 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1707 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
1708 if (!ieee80211_sdata_running(tmp_sdata))
1709 continue;
1710 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1711 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1712 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
1713 continue;
1714 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
1715 sdata = tmp_sdata;
1716 break;
1720 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1721 if (!chanctx_conf) {
1722 tmp_sdata = rcu_dereference(local->monitor_sdata);
1723 if (tmp_sdata)
1724 chanctx_conf =
1725 rcu_dereference(tmp_sdata->vif.chanctx_conf);
1728 if (chanctx_conf)
1729 chandef = &chanctx_conf->def;
1730 else if (!local->use_chanctx)
1731 chandef = &local->_oper_chandef;
1732 else
1733 goto fail_rcu;
1736 * Frame injection is not allowed if beaconing is not allowed
1737 * or if we need radar detection. Beaconing is usually not allowed when
1738 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1739 * Passive scan is also used in world regulatory domains where
1740 * your country is not known and as such it should be treated as
1741 * NO TX unless the channel is explicitly allowed in which case
1742 * your current regulatory domain would not have the passive scan
1743 * flag.
1745 * Since AP mode uses monitor interfaces to inject/TX management
1746 * frames we can make AP mode the exception to this rule once it
1747 * supports radar detection as its implementation can deal with
1748 * radar detection by itself. We can do that later by adding a
1749 * monitor flag interfaces used for AP support.
1751 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
1752 sdata->vif.type))
1753 goto fail_rcu;
1755 ieee80211_xmit(sdata, skb, chandef->chan->band);
1756 rcu_read_unlock();
1758 return NETDEV_TX_OK;
1760 fail_rcu:
1761 rcu_read_unlock();
1762 fail:
1763 dev_kfree_skb(skb);
1764 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1768 * Measure Tx frame arrival time for Tx latency statistics calculation
1769 * A single Tx frame latency should be measured from when it is entering the
1770 * Kernel until we receive Tx complete confirmation indication and the skb is
1771 * freed.
1773 static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
1774 struct sk_buff *skb)
1776 struct ieee80211_tx_latency_bin_ranges *tx_latency;
1778 tx_latency = rcu_dereference(local->tx_latency);
1779 if (!tx_latency)
1780 return;
1781 skb->tstamp = ktime_get();
1785 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1786 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1787 * @skb: packet to be sent
1788 * @dev: incoming interface
1790 * Returns: NETDEV_TX_OK both on success and on failure. On failure skb will
1791 * be freed.
1793 * This function takes in an Ethernet header and encapsulates it with suitable
1794 * IEEE 802.11 header based on which interface the packet is coming in. The
1795 * encapsulated packet will then be passed to master interface, wlan#.11, for
1796 * transmission (through low-level driver).
1798 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1799 struct net_device *dev)
1801 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1802 struct ieee80211_local *local = sdata->local;
1803 struct ieee80211_tx_info *info;
1804 int head_need;
1805 u16 ethertype, hdrlen, meshhdrlen = 0;
1806 __le16 fc;
1807 struct ieee80211_hdr hdr;
1808 struct ieee80211s_hdr mesh_hdr __maybe_unused;
1809 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
1810 const u8 *encaps_data;
1811 int encaps_len, skip_header_bytes;
1812 int nh_pos, h_pos;
1813 struct sta_info *sta = NULL;
1814 bool wme_sta = false, authorized = false, tdls_auth = false;
1815 bool tdls_peer = false, tdls_setup_frame = false;
1816 bool multicast;
1817 u32 info_flags = 0;
1818 u16 info_id = 0;
1819 struct ieee80211_chanctx_conf *chanctx_conf;
1820 struct ieee80211_sub_if_data *ap_sdata;
1821 enum ieee80211_band band;
1823 if (unlikely(skb->len < ETH_HLEN))
1824 goto fail;
1826 /* convert Ethernet header to proper 802.11 header (based on
1827 * operation mode) */
1828 ethertype = (skb->data[12] << 8) | skb->data[13];
1829 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
1831 rcu_read_lock();
1833 /* Measure frame arrival for Tx latency statistics calculation */
1834 ieee80211_tx_latency_start_msrmnt(local, skb);
1836 switch (sdata->vif.type) {
1837 case NL80211_IFTYPE_AP_VLAN:
1838 sta = rcu_dereference(sdata->u.vlan.sta);
1839 if (sta) {
1840 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1841 /* RA TA DA SA */
1842 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
1843 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1844 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1845 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1846 hdrlen = 30;
1847 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1848 wme_sta = sta->sta.wme;
1850 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1851 u.ap);
1852 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
1853 if (!chanctx_conf)
1854 goto fail_rcu;
1855 band = chanctx_conf->def.chan->band;
1856 if (sta)
1857 break;
1858 /* fall through */
1859 case NL80211_IFTYPE_AP:
1860 if (sdata->vif.type == NL80211_IFTYPE_AP)
1861 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1862 if (!chanctx_conf)
1863 goto fail_rcu;
1864 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
1865 /* DA BSSID SA */
1866 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1867 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1868 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1869 hdrlen = 24;
1870 band = chanctx_conf->def.chan->band;
1871 break;
1872 case NL80211_IFTYPE_WDS:
1873 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1874 /* RA TA DA SA */
1875 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1876 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1877 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1878 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1879 hdrlen = 30;
1881 * This is the exception! WDS style interfaces are prohibited
1882 * when channel contexts are in used so this must be valid
1884 band = local->hw.conf.chandef.chan->band;
1885 break;
1886 #ifdef CONFIG_MAC80211_MESH
1887 case NL80211_IFTYPE_MESH_POINT:
1888 if (!is_multicast_ether_addr(skb->data)) {
1889 struct sta_info *next_hop;
1890 bool mpp_lookup = true;
1892 mpath = mesh_path_lookup(sdata, skb->data);
1893 if (mpath) {
1894 mpp_lookup = false;
1895 next_hop = rcu_dereference(mpath->next_hop);
1896 if (!next_hop ||
1897 !(mpath->flags & (MESH_PATH_ACTIVE |
1898 MESH_PATH_RESOLVING)))
1899 mpp_lookup = true;
1902 if (mpp_lookup)
1903 mppath = mpp_path_lookup(sdata, skb->data);
1905 if (mppath && mpath)
1906 mesh_path_del(mpath->sdata, mpath->dst);
1910 * Use address extension if it is a packet from
1911 * another interface or if we know the destination
1912 * is being proxied by a portal (i.e. portal address
1913 * differs from proxied address)
1915 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
1916 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
1917 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1918 skb->data, skb->data + ETH_ALEN);
1919 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
1920 NULL, NULL);
1921 } else {
1922 /* DS -> MBSS (802.11-2012 13.11.3.3).
1923 * For unicast with unknown forwarding information,
1924 * destination might be in the MBSS or if that fails
1925 * forwarded to another mesh gate. In either case
1926 * resolution will be handled in ieee80211_xmit(), so
1927 * leave the original DA. This also works for mcast */
1928 const u8 *mesh_da = skb->data;
1930 if (mppath)
1931 mesh_da = mppath->mpp;
1932 else if (mpath)
1933 mesh_da = mpath->dst;
1935 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1936 mesh_da, sdata->vif.addr);
1937 if (is_multicast_ether_addr(mesh_da))
1938 /* DA TA mSA AE:SA */
1939 meshhdrlen = ieee80211_new_mesh_header(
1940 sdata, &mesh_hdr,
1941 skb->data + ETH_ALEN, NULL);
1942 else
1943 /* RA TA mDA mSA AE:DA SA */
1944 meshhdrlen = ieee80211_new_mesh_header(
1945 sdata, &mesh_hdr, skb->data,
1946 skb->data + ETH_ALEN);
1949 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1950 if (!chanctx_conf)
1951 goto fail_rcu;
1952 band = chanctx_conf->def.chan->band;
1953 break;
1954 #endif
1955 case NL80211_IFTYPE_STATION:
1956 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1957 sta = sta_info_get(sdata, skb->data);
1958 if (sta) {
1959 authorized = test_sta_flag(sta,
1960 WLAN_STA_AUTHORIZED);
1961 wme_sta = sta->sta.wme;
1962 tdls_peer = test_sta_flag(sta,
1963 WLAN_STA_TDLS_PEER);
1964 tdls_auth = test_sta_flag(sta,
1965 WLAN_STA_TDLS_PEER_AUTH);
1968 if (tdls_peer)
1969 tdls_setup_frame =
1970 ethertype == ETH_P_TDLS &&
1971 skb->len > 14 &&
1972 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
1976 * TDLS link during setup - throw out frames to peer. We allow
1977 * TDLS-setup frames to unauthorized peers for the special case
1978 * of a link teardown after a TDLS sta is removed due to being
1979 * unreachable.
1981 if (tdls_peer && !tdls_auth && !tdls_setup_frame)
1982 goto fail_rcu;
1984 /* send direct packets to authorized TDLS peers */
1985 if (tdls_peer && tdls_auth) {
1986 /* DA SA BSSID */
1987 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1988 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1989 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
1990 hdrlen = 24;
1991 } else if (sdata->u.mgd.use_4addr &&
1992 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
1993 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
1994 IEEE80211_FCTL_TODS);
1995 /* RA TA DA SA */
1996 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
1997 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1998 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1999 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2000 hdrlen = 30;
2001 } else {
2002 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2003 /* BSSID SA DA */
2004 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2005 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2006 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2007 hdrlen = 24;
2009 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2010 if (!chanctx_conf)
2011 goto fail_rcu;
2012 band = chanctx_conf->def.chan->band;
2013 break;
2014 case NL80211_IFTYPE_ADHOC:
2015 /* DA SA BSSID */
2016 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2017 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2018 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2019 hdrlen = 24;
2020 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2021 if (!chanctx_conf)
2022 goto fail_rcu;
2023 band = chanctx_conf->def.chan->band;
2024 break;
2025 default:
2026 goto fail_rcu;
2030 * There's no need to try to look up the destination
2031 * if it is a multicast address (which can only happen
2032 * in AP mode)
2034 multicast = is_multicast_ether_addr(hdr.addr1);
2035 if (!multicast) {
2036 sta = sta_info_get(sdata, hdr.addr1);
2037 if (sta) {
2038 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2039 wme_sta = sta->sta.wme;
2043 /* For mesh, the use of the QoS header is mandatory */
2044 if (ieee80211_vif_is_mesh(&sdata->vif))
2045 wme_sta = true;
2047 /* receiver and we are QoS enabled, use a QoS type frame */
2048 if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) {
2049 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2050 hdrlen += 2;
2054 * Drop unicast frames to unauthorised stations unless they are
2055 * EAPOL frames from the local station.
2057 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2058 !multicast && !authorized &&
2059 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2060 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2061 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2062 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2063 dev->name, hdr.addr1);
2064 #endif
2066 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2068 goto fail_rcu;
2071 if (unlikely(!multicast && skb->sk &&
2072 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2073 struct sk_buff *ack_skb = skb_clone_sk(skb);
2075 if (ack_skb) {
2076 unsigned long flags;
2077 int id;
2079 spin_lock_irqsave(&local->ack_status_lock, flags);
2080 id = idr_alloc(&local->ack_status_frames, ack_skb,
2081 1, 0x10000, GFP_ATOMIC);
2082 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2084 if (id >= 0) {
2085 info_id = id;
2086 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2087 } else {
2088 kfree_skb(ack_skb);
2094 * If the skb is shared we need to obtain our own copy.
2096 if (skb_shared(skb)) {
2097 struct sk_buff *tmp_skb = skb;
2099 /* can't happen -- skb is a clone if info_id != 0 */
2100 WARN_ON(info_id);
2102 skb = skb_clone(skb, GFP_ATOMIC);
2103 kfree_skb(tmp_skb);
2105 if (!skb)
2106 goto fail_rcu;
2109 hdr.frame_control = fc;
2110 hdr.duration_id = 0;
2111 hdr.seq_ctrl = 0;
2113 skip_header_bytes = ETH_HLEN;
2114 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2115 encaps_data = bridge_tunnel_header;
2116 encaps_len = sizeof(bridge_tunnel_header);
2117 skip_header_bytes -= 2;
2118 } else if (ethertype >= ETH_P_802_3_MIN) {
2119 encaps_data = rfc1042_header;
2120 encaps_len = sizeof(rfc1042_header);
2121 skip_header_bytes -= 2;
2122 } else {
2123 encaps_data = NULL;
2124 encaps_len = 0;
2127 nh_pos = skb_network_header(skb) - skb->data;
2128 h_pos = skb_transport_header(skb) - skb->data;
2130 skb_pull(skb, skip_header_bytes);
2131 nh_pos -= skip_header_bytes;
2132 h_pos -= skip_header_bytes;
2134 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2137 * So we need to modify the skb header and hence need a copy of
2138 * that. The head_need variable above doesn't, so far, include
2139 * the needed header space that we don't need right away. If we
2140 * can, then we don't reallocate right now but only after the
2141 * frame arrives at the master device (if it does...)
2143 * If we cannot, however, then we will reallocate to include all
2144 * the ever needed space. Also, if we need to reallocate it anyway,
2145 * make it big enough for everything we may ever need.
2148 if (head_need > 0 || skb_cloned(skb)) {
2149 head_need += sdata->encrypt_headroom;
2150 head_need += local->tx_headroom;
2151 head_need = max_t(int, 0, head_need);
2152 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2153 ieee80211_free_txskb(&local->hw, skb);
2154 skb = NULL;
2155 goto fail_rcu;
2159 if (encaps_data) {
2160 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2161 nh_pos += encaps_len;
2162 h_pos += encaps_len;
2165 #ifdef CONFIG_MAC80211_MESH
2166 if (meshhdrlen > 0) {
2167 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2168 nh_pos += meshhdrlen;
2169 h_pos += meshhdrlen;
2171 #endif
2173 if (ieee80211_is_data_qos(fc)) {
2174 __le16 *qos_control;
2176 qos_control = (__le16 *) skb_push(skb, 2);
2177 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2179 * Maybe we could actually set some fields here, for now just
2180 * initialise to zero to indicate no special operation.
2182 *qos_control = 0;
2183 } else
2184 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2186 nh_pos += hdrlen;
2187 h_pos += hdrlen;
2189 dev->stats.tx_packets++;
2190 dev->stats.tx_bytes += skb->len;
2192 /* Update skb pointers to various headers since this modified frame
2193 * is going to go through Linux networking code that may potentially
2194 * need things like pointer to IP header. */
2195 skb_set_mac_header(skb, 0);
2196 skb_set_network_header(skb, nh_pos);
2197 skb_set_transport_header(skb, h_pos);
2199 info = IEEE80211_SKB_CB(skb);
2200 memset(info, 0, sizeof(*info));
2202 dev->trans_start = jiffies;
2204 info->flags = info_flags;
2205 info->ack_frame_id = info_id;
2207 ieee80211_xmit(sdata, skb, band);
2208 rcu_read_unlock();
2210 return NETDEV_TX_OK;
2212 fail_rcu:
2213 rcu_read_unlock();
2214 fail:
2215 dev_kfree_skb(skb);
2216 return NETDEV_TX_OK;
2221 * ieee80211_clear_tx_pending may not be called in a context where
2222 * it is possible that it packets could come in again.
2224 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
2226 struct sk_buff *skb;
2227 int i;
2229 for (i = 0; i < local->hw.queues; i++) {
2230 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
2231 ieee80211_free_txskb(&local->hw, skb);
2236 * Returns false if the frame couldn't be transmitted but was queued instead,
2237 * which in this case means re-queued -- take as an indication to stop sending
2238 * more pending frames.
2240 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2241 struct sk_buff *skb)
2243 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2244 struct ieee80211_sub_if_data *sdata;
2245 struct sta_info *sta;
2246 struct ieee80211_hdr *hdr;
2247 bool result;
2248 struct ieee80211_chanctx_conf *chanctx_conf;
2250 sdata = vif_to_sdata(info->control.vif);
2252 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
2253 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2254 if (unlikely(!chanctx_conf)) {
2255 dev_kfree_skb(skb);
2256 return true;
2258 result = ieee80211_tx(sdata, skb, true,
2259 chanctx_conf->def.chan->band);
2260 } else {
2261 struct sk_buff_head skbs;
2263 __skb_queue_head_init(&skbs);
2264 __skb_queue_tail(&skbs, skb);
2266 hdr = (struct ieee80211_hdr *)skb->data;
2267 sta = sta_info_get(sdata, hdr->addr1);
2269 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
2272 return result;
2276 * Transmit all pending packets. Called from tasklet.
2278 void ieee80211_tx_pending(unsigned long data)
2280 struct ieee80211_local *local = (struct ieee80211_local *)data;
2281 unsigned long flags;
2282 int i;
2283 bool txok;
2285 rcu_read_lock();
2287 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2288 for (i = 0; i < local->hw.queues; i++) {
2290 * If queue is stopped by something other than due to pending
2291 * frames, or we have no pending frames, proceed to next queue.
2293 if (local->queue_stop_reasons[i] ||
2294 skb_queue_empty(&local->pending[i]))
2295 continue;
2297 while (!skb_queue_empty(&local->pending[i])) {
2298 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
2299 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2301 if (WARN_ON(!info->control.vif)) {
2302 ieee80211_free_txskb(&local->hw, skb);
2303 continue;
2306 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2307 flags);
2309 txok = ieee80211_tx_pending_skb(local, skb);
2310 spin_lock_irqsave(&local->queue_stop_reason_lock,
2311 flags);
2312 if (!txok)
2313 break;
2316 if (skb_queue_empty(&local->pending[i]))
2317 ieee80211_propagate_queue_wake(local, i);
2319 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2321 rcu_read_unlock();
2324 /* functions for drivers to get certain frames */
2326 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2327 struct ps_data *ps, struct sk_buff *skb,
2328 bool is_template)
2330 u8 *pos, *tim;
2331 int aid0 = 0;
2332 int i, have_bits = 0, n1, n2;
2334 /* Generate bitmap for TIM only if there are any STAs in power save
2335 * mode. */
2336 if (atomic_read(&ps->num_sta_ps) > 0)
2337 /* in the hope that this is faster than
2338 * checking byte-for-byte */
2339 have_bits = !bitmap_empty((unsigned long *)ps->tim,
2340 IEEE80211_MAX_AID+1);
2341 if (!is_template) {
2342 if (ps->dtim_count == 0)
2343 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
2344 else
2345 ps->dtim_count--;
2348 tim = pos = (u8 *) skb_put(skb, 6);
2349 *pos++ = WLAN_EID_TIM;
2350 *pos++ = 4;
2351 *pos++ = ps->dtim_count;
2352 *pos++ = sdata->vif.bss_conf.dtim_period;
2354 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
2355 aid0 = 1;
2357 ps->dtim_bc_mc = aid0 == 1;
2359 if (have_bits) {
2360 /* Find largest even number N1 so that bits numbered 1 through
2361 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2362 * (N2 + 1) x 8 through 2007 are 0. */
2363 n1 = 0;
2364 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2365 if (ps->tim[i]) {
2366 n1 = i & 0xfe;
2367 break;
2370 n2 = n1;
2371 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2372 if (ps->tim[i]) {
2373 n2 = i;
2374 break;
2378 /* Bitmap control */
2379 *pos++ = n1 | aid0;
2380 /* Part Virt Bitmap */
2381 skb_put(skb, n2 - n1);
2382 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
2384 tim[1] = n2 - n1 + 4;
2385 } else {
2386 *pos++ = aid0; /* Bitmap control */
2387 *pos++ = 0; /* Part Virt Bitmap */
2391 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2392 struct ps_data *ps, struct sk_buff *skb,
2393 bool is_template)
2395 struct ieee80211_local *local = sdata->local;
2398 * Not very nice, but we want to allow the driver to call
2399 * ieee80211_beacon_get() as a response to the set_tim()
2400 * callback. That, however, is already invoked under the
2401 * sta_lock to guarantee consistent and race-free update
2402 * of the tim bitmap in mac80211 and the driver.
2404 if (local->tim_in_locked_section) {
2405 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2406 } else {
2407 spin_lock_bh(&local->tim_lock);
2408 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2409 spin_unlock_bh(&local->tim_lock);
2412 return 0;
2415 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
2416 struct beacon_data *beacon)
2418 struct probe_resp *resp;
2419 u8 *beacon_data;
2420 size_t beacon_data_len;
2421 int i;
2422 u8 count = beacon->csa_current_counter;
2424 switch (sdata->vif.type) {
2425 case NL80211_IFTYPE_AP:
2426 beacon_data = beacon->tail;
2427 beacon_data_len = beacon->tail_len;
2428 break;
2429 case NL80211_IFTYPE_ADHOC:
2430 beacon_data = beacon->head;
2431 beacon_data_len = beacon->head_len;
2432 break;
2433 case NL80211_IFTYPE_MESH_POINT:
2434 beacon_data = beacon->head;
2435 beacon_data_len = beacon->head_len;
2436 break;
2437 default:
2438 return;
2441 rcu_read_lock();
2442 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
2443 resp = rcu_dereference(sdata->u.ap.probe_resp);
2445 if (beacon->csa_counter_offsets[i]) {
2446 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
2447 beacon_data_len)) {
2448 rcu_read_unlock();
2449 return;
2452 beacon_data[beacon->csa_counter_offsets[i]] = count;
2455 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
2456 resp->data[resp->csa_counter_offsets[i]] = count;
2458 rcu_read_unlock();
2461 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
2463 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2464 struct beacon_data *beacon = NULL;
2465 u8 count = 0;
2467 rcu_read_lock();
2469 if (sdata->vif.type == NL80211_IFTYPE_AP)
2470 beacon = rcu_dereference(sdata->u.ap.beacon);
2471 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2472 beacon = rcu_dereference(sdata->u.ibss.presp);
2473 else if (ieee80211_vif_is_mesh(&sdata->vif))
2474 beacon = rcu_dereference(sdata->u.mesh.beacon);
2476 if (!beacon)
2477 goto unlock;
2479 beacon->csa_current_counter--;
2481 /* the counter should never reach 0 */
2482 WARN_ON_ONCE(!beacon->csa_current_counter);
2483 count = beacon->csa_current_counter;
2485 unlock:
2486 rcu_read_unlock();
2487 return count;
2489 EXPORT_SYMBOL(ieee80211_csa_update_counter);
2491 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2493 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2494 struct beacon_data *beacon = NULL;
2495 u8 *beacon_data;
2496 size_t beacon_data_len;
2497 int ret = false;
2499 if (!ieee80211_sdata_running(sdata))
2500 return false;
2502 rcu_read_lock();
2503 if (vif->type == NL80211_IFTYPE_AP) {
2504 struct ieee80211_if_ap *ap = &sdata->u.ap;
2506 beacon = rcu_dereference(ap->beacon);
2507 if (WARN_ON(!beacon || !beacon->tail))
2508 goto out;
2509 beacon_data = beacon->tail;
2510 beacon_data_len = beacon->tail_len;
2511 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2512 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2514 beacon = rcu_dereference(ifibss->presp);
2515 if (!beacon)
2516 goto out;
2518 beacon_data = beacon->head;
2519 beacon_data_len = beacon->head_len;
2520 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2521 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2523 beacon = rcu_dereference(ifmsh->beacon);
2524 if (!beacon)
2525 goto out;
2527 beacon_data = beacon->head;
2528 beacon_data_len = beacon->head_len;
2529 } else {
2530 WARN_ON(1);
2531 goto out;
2534 if (!beacon->csa_counter_offsets[0])
2535 goto out;
2537 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
2538 goto out;
2540 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
2541 ret = true;
2542 out:
2543 rcu_read_unlock();
2545 return ret;
2547 EXPORT_SYMBOL(ieee80211_csa_is_complete);
2549 static struct sk_buff *
2550 __ieee80211_beacon_get(struct ieee80211_hw *hw,
2551 struct ieee80211_vif *vif,
2552 struct ieee80211_mutable_offsets *offs,
2553 bool is_template)
2555 struct ieee80211_local *local = hw_to_local(hw);
2556 struct beacon_data *beacon = NULL;
2557 struct sk_buff *skb = NULL;
2558 struct ieee80211_tx_info *info;
2559 struct ieee80211_sub_if_data *sdata = NULL;
2560 enum ieee80211_band band;
2561 struct ieee80211_tx_rate_control txrc;
2562 struct ieee80211_chanctx_conf *chanctx_conf;
2563 int csa_off_base = 0;
2565 rcu_read_lock();
2567 sdata = vif_to_sdata(vif);
2568 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2570 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
2571 goto out;
2573 if (offs)
2574 memset(offs, 0, sizeof(*offs));
2576 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2577 struct ieee80211_if_ap *ap = &sdata->u.ap;
2579 beacon = rcu_dereference(ap->beacon);
2580 if (beacon) {
2581 if (beacon->csa_counter_offsets[0]) {
2582 if (!is_template)
2583 ieee80211_csa_update_counter(vif);
2585 ieee80211_set_csa(sdata, beacon);
2589 * headroom, head length,
2590 * tail length and maximum TIM length
2592 skb = dev_alloc_skb(local->tx_headroom +
2593 beacon->head_len +
2594 beacon->tail_len + 256 +
2595 local->hw.extra_beacon_tailroom);
2596 if (!skb)
2597 goto out;
2599 skb_reserve(skb, local->tx_headroom);
2600 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2601 beacon->head_len);
2603 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
2604 is_template);
2606 if (offs) {
2607 offs->tim_offset = beacon->head_len;
2608 offs->tim_length = skb->len - beacon->head_len;
2610 /* for AP the csa offsets are from tail */
2611 csa_off_base = skb->len;
2614 if (beacon->tail)
2615 memcpy(skb_put(skb, beacon->tail_len),
2616 beacon->tail, beacon->tail_len);
2617 } else
2618 goto out;
2619 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2620 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2621 struct ieee80211_hdr *hdr;
2623 beacon = rcu_dereference(ifibss->presp);
2624 if (!beacon)
2625 goto out;
2627 if (beacon->csa_counter_offsets[0]) {
2628 if (!is_template)
2629 ieee80211_csa_update_counter(vif);
2631 ieee80211_set_csa(sdata, beacon);
2634 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
2635 local->hw.extra_beacon_tailroom);
2636 if (!skb)
2637 goto out;
2638 skb_reserve(skb, local->tx_headroom);
2639 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2640 beacon->head_len);
2642 hdr = (struct ieee80211_hdr *) skb->data;
2643 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2644 IEEE80211_STYPE_BEACON);
2645 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2646 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2648 beacon = rcu_dereference(ifmsh->beacon);
2649 if (!beacon)
2650 goto out;
2652 if (beacon->csa_counter_offsets[0]) {
2653 if (!is_template)
2654 /* TODO: For mesh csa_counter is in TU, so
2655 * decrementing it by one isn't correct, but
2656 * for now we leave it consistent with overall
2657 * mac80211's behavior.
2659 ieee80211_csa_update_counter(vif);
2661 ieee80211_set_csa(sdata, beacon);
2664 if (ifmsh->sync_ops)
2665 ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
2667 skb = dev_alloc_skb(local->tx_headroom +
2668 beacon->head_len +
2669 256 + /* TIM IE */
2670 beacon->tail_len +
2671 local->hw.extra_beacon_tailroom);
2672 if (!skb)
2673 goto out;
2674 skb_reserve(skb, local->tx_headroom);
2675 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2676 beacon->head_len);
2677 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
2679 if (offs) {
2680 offs->tim_offset = beacon->head_len;
2681 offs->tim_length = skb->len - beacon->head_len;
2684 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
2685 beacon->tail_len);
2686 } else {
2687 WARN_ON(1);
2688 goto out;
2691 /* CSA offsets */
2692 if (offs && beacon) {
2693 int i;
2695 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
2696 u16 csa_off = beacon->csa_counter_offsets[i];
2698 if (!csa_off)
2699 continue;
2701 offs->csa_counter_offs[i] = csa_off_base + csa_off;
2705 band = chanctx_conf->def.chan->band;
2707 info = IEEE80211_SKB_CB(skb);
2709 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2710 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2711 info->band = band;
2713 memset(&txrc, 0, sizeof(txrc));
2714 txrc.hw = hw;
2715 txrc.sband = local->hw.wiphy->bands[band];
2716 txrc.bss_conf = &sdata->vif.bss_conf;
2717 txrc.skb = skb;
2718 txrc.reported_rate.idx = -1;
2719 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2720 if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
2721 txrc.max_rate_idx = -1;
2722 else
2723 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
2724 txrc.bss = true;
2725 rate_control_get_rate(sdata, NULL, &txrc);
2727 info->control.vif = vif;
2729 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
2730 IEEE80211_TX_CTL_ASSIGN_SEQ |
2731 IEEE80211_TX_CTL_FIRST_FRAGMENT;
2732 out:
2733 rcu_read_unlock();
2734 return skb;
2738 struct sk_buff *
2739 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
2740 struct ieee80211_vif *vif,
2741 struct ieee80211_mutable_offsets *offs)
2743 return __ieee80211_beacon_get(hw, vif, offs, true);
2745 EXPORT_SYMBOL(ieee80211_beacon_get_template);
2747 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2748 struct ieee80211_vif *vif,
2749 u16 *tim_offset, u16 *tim_length)
2751 struct ieee80211_mutable_offsets offs = {};
2752 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
2754 if (tim_offset)
2755 *tim_offset = offs.tim_offset;
2757 if (tim_length)
2758 *tim_length = offs.tim_length;
2760 return bcn;
2762 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
2764 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
2765 struct ieee80211_vif *vif)
2767 struct ieee80211_if_ap *ap = NULL;
2768 struct sk_buff *skb = NULL;
2769 struct probe_resp *presp = NULL;
2770 struct ieee80211_hdr *hdr;
2771 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2773 if (sdata->vif.type != NL80211_IFTYPE_AP)
2774 return NULL;
2776 rcu_read_lock();
2778 ap = &sdata->u.ap;
2779 presp = rcu_dereference(ap->probe_resp);
2780 if (!presp)
2781 goto out;
2783 skb = dev_alloc_skb(presp->len);
2784 if (!skb)
2785 goto out;
2787 memcpy(skb_put(skb, presp->len), presp->data, presp->len);
2789 hdr = (struct ieee80211_hdr *) skb->data;
2790 memset(hdr->addr1, 0, sizeof(hdr->addr1));
2792 out:
2793 rcu_read_unlock();
2794 return skb;
2796 EXPORT_SYMBOL(ieee80211_proberesp_get);
2798 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2799 struct ieee80211_vif *vif)
2801 struct ieee80211_sub_if_data *sdata;
2802 struct ieee80211_if_managed *ifmgd;
2803 struct ieee80211_pspoll *pspoll;
2804 struct ieee80211_local *local;
2805 struct sk_buff *skb;
2807 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2808 return NULL;
2810 sdata = vif_to_sdata(vif);
2811 ifmgd = &sdata->u.mgd;
2812 local = sdata->local;
2814 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2815 if (!skb)
2816 return NULL;
2818 skb_reserve(skb, local->hw.extra_tx_headroom);
2820 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2821 memset(pspoll, 0, sizeof(*pspoll));
2822 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2823 IEEE80211_STYPE_PSPOLL);
2824 pspoll->aid = cpu_to_le16(ifmgd->aid);
2826 /* aid in PS-Poll has its two MSBs each set to 1 */
2827 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2829 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2830 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2832 return skb;
2834 EXPORT_SYMBOL(ieee80211_pspoll_get);
2836 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2837 struct ieee80211_vif *vif)
2839 struct ieee80211_hdr_3addr *nullfunc;
2840 struct ieee80211_sub_if_data *sdata;
2841 struct ieee80211_if_managed *ifmgd;
2842 struct ieee80211_local *local;
2843 struct sk_buff *skb;
2845 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2846 return NULL;
2848 sdata = vif_to_sdata(vif);
2849 ifmgd = &sdata->u.mgd;
2850 local = sdata->local;
2852 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2853 if (!skb)
2854 return NULL;
2856 skb_reserve(skb, local->hw.extra_tx_headroom);
2858 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2859 sizeof(*nullfunc));
2860 memset(nullfunc, 0, sizeof(*nullfunc));
2861 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2862 IEEE80211_STYPE_NULLFUNC |
2863 IEEE80211_FCTL_TODS);
2864 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2865 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2866 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2868 return skb;
2870 EXPORT_SYMBOL(ieee80211_nullfunc_get);
2872 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2873 struct ieee80211_vif *vif,
2874 const u8 *ssid, size_t ssid_len,
2875 size_t tailroom)
2877 struct ieee80211_sub_if_data *sdata;
2878 struct ieee80211_local *local;
2879 struct ieee80211_hdr_3addr *hdr;
2880 struct sk_buff *skb;
2881 size_t ie_ssid_len;
2882 u8 *pos;
2884 sdata = vif_to_sdata(vif);
2885 local = sdata->local;
2886 ie_ssid_len = 2 + ssid_len;
2888 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2889 ie_ssid_len + tailroom);
2890 if (!skb)
2891 return NULL;
2893 skb_reserve(skb, local->hw.extra_tx_headroom);
2895 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2896 memset(hdr, 0, sizeof(*hdr));
2897 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2898 IEEE80211_STYPE_PROBE_REQ);
2899 eth_broadcast_addr(hdr->addr1);
2900 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2901 eth_broadcast_addr(hdr->addr3);
2903 pos = skb_put(skb, ie_ssid_len);
2904 *pos++ = WLAN_EID_SSID;
2905 *pos++ = ssid_len;
2906 if (ssid_len)
2907 memcpy(pos, ssid, ssid_len);
2908 pos += ssid_len;
2910 return skb;
2912 EXPORT_SYMBOL(ieee80211_probereq_get);
2914 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2915 const void *frame, size_t frame_len,
2916 const struct ieee80211_tx_info *frame_txctl,
2917 struct ieee80211_rts *rts)
2919 const struct ieee80211_hdr *hdr = frame;
2921 rts->frame_control =
2922 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
2923 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2924 frame_txctl);
2925 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2926 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2928 EXPORT_SYMBOL(ieee80211_rts_get);
2930 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2931 const void *frame, size_t frame_len,
2932 const struct ieee80211_tx_info *frame_txctl,
2933 struct ieee80211_cts *cts)
2935 const struct ieee80211_hdr *hdr = frame;
2937 cts->frame_control =
2938 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
2939 cts->duration = ieee80211_ctstoself_duration(hw, vif,
2940 frame_len, frame_txctl);
2941 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2943 EXPORT_SYMBOL(ieee80211_ctstoself_get);
2945 struct sk_buff *
2946 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
2947 struct ieee80211_vif *vif)
2949 struct ieee80211_local *local = hw_to_local(hw);
2950 struct sk_buff *skb = NULL;
2951 struct ieee80211_tx_data tx;
2952 struct ieee80211_sub_if_data *sdata;
2953 struct ps_data *ps;
2954 struct ieee80211_tx_info *info;
2955 struct ieee80211_chanctx_conf *chanctx_conf;
2957 sdata = vif_to_sdata(vif);
2959 rcu_read_lock();
2960 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2962 if (!chanctx_conf)
2963 goto out;
2965 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2966 struct beacon_data *beacon =
2967 rcu_dereference(sdata->u.ap.beacon);
2969 if (!beacon || !beacon->head)
2970 goto out;
2972 ps = &sdata->u.ap.ps;
2973 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2974 ps = &sdata->u.mesh.ps;
2975 } else {
2976 goto out;
2979 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
2980 goto out; /* send buffered bc/mc only after DTIM beacon */
2982 while (1) {
2983 skb = skb_dequeue(&ps->bc_buf);
2984 if (!skb)
2985 goto out;
2986 local->total_ps_buffered--;
2988 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
2989 struct ieee80211_hdr *hdr =
2990 (struct ieee80211_hdr *) skb->data;
2991 /* more buffered multicast/broadcast frames ==> set
2992 * MoreData flag in IEEE 802.11 header to inform PS
2993 * STAs */
2994 hdr->frame_control |=
2995 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2998 if (sdata->vif.type == NL80211_IFTYPE_AP)
2999 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
3000 if (!ieee80211_tx_prepare(sdata, &tx, skb))
3001 break;
3002 ieee80211_free_txskb(hw, skb);
3005 info = IEEE80211_SKB_CB(skb);
3007 tx.flags |= IEEE80211_TX_PS_BUFFERED;
3008 info->band = chanctx_conf->def.chan->band;
3010 if (invoke_tx_handlers(&tx))
3011 skb = NULL;
3012 out:
3013 rcu_read_unlock();
3015 return skb;
3017 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
3019 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
3020 struct sk_buff *skb, int tid,
3021 enum ieee80211_band band)
3023 int ac = ieee802_1d_to_ac[tid & 7];
3025 skb_set_mac_header(skb, 0);
3026 skb_set_network_header(skb, 0);
3027 skb_set_transport_header(skb, 0);
3029 skb_set_queue_mapping(skb, ac);
3030 skb->priority = tid;
3032 skb->dev = sdata->dev;
3035 * The other path calling ieee80211_xmit is from the tasklet,
3036 * and while we can handle concurrent transmissions locking
3037 * requirements are that we do not come into tx with bhs on.
3039 local_bh_disable();
3040 ieee80211_xmit(sdata, skb, band);
3041 local_bh_enable();