]> git.ipfire.org Git - people/ms/linux.git/blob - net/mac80211/tx.c
mac80211: Send EAPOL frames at lowest rate
[people/ms/linux.git] / net / mac80211 / tx.c
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
7 *
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.
11 *
12 *
13 * Transmit and frame generation functions.
14 */
15
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>
29
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "led.h"
33 #include "mesh.h"
34 #include "wep.h"
35 #include "wpa.h"
36 #include "wme.h"
37 #include "rate.h"
38
39 /* misc utils */
40
41 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
42 struct sk_buff *skb, int group_addr,
43 int next_frag_len)
44 {
45 int rate, mrate, erp, dur, i, shift = 0;
46 struct ieee80211_rate *txrate;
47 struct ieee80211_local *local = tx->local;
48 struct ieee80211_supported_band *sband;
49 struct ieee80211_hdr *hdr;
50 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
51 struct ieee80211_chanctx_conf *chanctx_conf;
52 u32 rate_flags = 0;
53
54 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);
59 }
60 rcu_read_unlock();
61
62 /* assume HW handles this */
63 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
64 return 0;
65
66 /* uh huh? */
67 if (WARN_ON_ONCE(tx->rate.idx < 0))
68 return 0;
69
70 sband = local->hw.wiphy->bands[info->band];
71 txrate = &sband->bitrates[tx->rate.idx];
72
73 erp = txrate->flags & IEEE80211_RATE_ERP_G;
74
75 /*
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
84 *
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
91 */
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.
97 *
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
107 */
108 return 0;
109 }
110
111 /* data/mgmt */
112 if (0 /* FIX: data/mgmt during CFP */)
113 return cpu_to_le16(32768);
114
115 if (group_addr) /* Group address as the destination - no ACK */
116 return 0;
117
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
127 */
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];
133
134 if (r->bitrate > txrate->bitrate)
135 break;
136
137 if ((rate_flags & r->flags) != rate_flags)
138 continue;
139
140 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
141 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
142
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;
153 }
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;
163 }
164 }
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);
169 }
170
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);
182
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);
192 }
193
194 return cpu_to_le16(dur);
195 }
196
197 /* tx handlers */
198 static ieee80211_tx_result debug_noinline
199 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
200 {
201 struct ieee80211_local *local = tx->local;
202 struct ieee80211_if_managed *ifmgd;
203
204 /* driver doesn't support power save */
205 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
206 return TX_CONTINUE;
207
208 /* hardware does dynamic power save */
209 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
210 return TX_CONTINUE;
211
212 /* dynamic power save disabled */
213 if (local->hw.conf.dynamic_ps_timeout <= 0)
214 return TX_CONTINUE;
215
216 /* we are scanning, don't enable power save */
217 if (local->scanning)
218 return TX_CONTINUE;
219
220 if (!local->ps_sdata)
221 return TX_CONTINUE;
222
223 /* No point if we're going to suspend */
224 if (local->quiescing)
225 return TX_CONTINUE;
226
227 /* dynamic ps is supported only in managed mode */
228 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
229 return TX_CONTINUE;
230
231 ifmgd = &tx->sdata->u.mgd;
232
233 /*
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.
241 *
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.
245 */
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;
250
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);
259 }
260
261 /* Don't restart the timer if we're not disassociated */
262 if (!ifmgd->associated)
263 return TX_CONTINUE;
264
265 mod_timer(&local->dynamic_ps_timer, jiffies +
266 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
267
268 return TX_CONTINUE;
269 }
270
271 static ieee80211_tx_result debug_noinline
272 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
273 {
274
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;
278
279 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
280 return TX_CONTINUE;
281
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))
286 /*
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.
294 *
295 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
296 */
297 return TX_DROP;
298
299 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
300 return TX_CONTINUE;
301
302 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
303 return TX_CONTINUE;
304
305 if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
306 return TX_CONTINUE;
307
308 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
309 return TX_CONTINUE;
310
311 if (tx->sta)
312 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
313
314 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
315 if (unlikely(!assoc &&
316 ieee80211_is_data(hdr->frame_control))) {
317 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
318 sdata_info(tx->sdata,
319 "dropped data frame to not associated station %pM\n",
320 hdr->addr1);
321 #endif
322 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
323 return TX_DROP;
324 }
325 } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
326 ieee80211_is_data(hdr->frame_control) &&
327 !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
328 /*
329 * No associated STAs - no need to send multicast
330 * frames.
331 */
332 return TX_DROP;
333 }
334
335 return TX_CONTINUE;
336 }
337
338 /* This function is called whenever the AP is about to exceed the maximum limit
339 * of buffered frames for power saving STAs. This situation should not really
340 * happen often during normal operation, so dropping the oldest buffered packet
341 * from each queue should be OK to make some room for new frames. */
342 static void purge_old_ps_buffers(struct ieee80211_local *local)
343 {
344 int total = 0, purged = 0;
345 struct sk_buff *skb;
346 struct ieee80211_sub_if_data *sdata;
347 struct sta_info *sta;
348
349 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
350 struct ps_data *ps;
351
352 if (sdata->vif.type == NL80211_IFTYPE_AP)
353 ps = &sdata->u.ap.ps;
354 else if (ieee80211_vif_is_mesh(&sdata->vif))
355 ps = &sdata->u.mesh.ps;
356 else
357 continue;
358
359 skb = skb_dequeue(&ps->bc_buf);
360 if (skb) {
361 purged++;
362 dev_kfree_skb(skb);
363 }
364 total += skb_queue_len(&ps->bc_buf);
365 }
366
367 /*
368 * Drop one frame from each station from the lowest-priority
369 * AC that has frames at all.
370 */
371 list_for_each_entry_rcu(sta, &local->sta_list, list) {
372 int ac;
373
374 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
375 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
376 total += skb_queue_len(&sta->ps_tx_buf[ac]);
377 if (skb) {
378 purged++;
379 ieee80211_free_txskb(&local->hw, skb);
380 break;
381 }
382 }
383 }
384
385 local->total_ps_buffered = total;
386 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
387 }
388
389 static ieee80211_tx_result
390 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
391 {
392 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
393 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
394 struct ps_data *ps;
395
396 /*
397 * broadcast/multicast frame
398 *
399 * If any of the associated/peer stations is in power save mode,
400 * the frame is buffered to be sent after DTIM beacon frame.
401 * This is done either by the hardware or us.
402 */
403
404 /* powersaving STAs currently only in AP/VLAN/mesh mode */
405 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
406 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
407 if (!tx->sdata->bss)
408 return TX_CONTINUE;
409
410 ps = &tx->sdata->bss->ps;
411 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
412 ps = &tx->sdata->u.mesh.ps;
413 } else {
414 return TX_CONTINUE;
415 }
416
417
418 /* no buffering for ordered frames */
419 if (ieee80211_has_order(hdr->frame_control))
420 return TX_CONTINUE;
421
422 if (ieee80211_is_probe_req(hdr->frame_control))
423 return TX_CONTINUE;
424
425 if (tx->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
426 info->hw_queue = tx->sdata->vif.cab_queue;
427
428 /* no stations in PS mode */
429 if (!atomic_read(&ps->num_sta_ps))
430 return TX_CONTINUE;
431
432 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
433
434 /* device releases frame after DTIM beacon */
435 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
436 return TX_CONTINUE;
437
438 /* buffered in mac80211 */
439 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
440 purge_old_ps_buffers(tx->local);
441
442 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
443 ps_dbg(tx->sdata,
444 "BC TX buffer full - dropping the oldest frame\n");
445 dev_kfree_skb(skb_dequeue(&ps->bc_buf));
446 } else
447 tx->local->total_ps_buffered++;
448
449 skb_queue_tail(&ps->bc_buf, tx->skb);
450
451 return TX_QUEUED;
452 }
453
454 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
455 struct sk_buff *skb)
456 {
457 if (!ieee80211_is_mgmt(fc))
458 return 0;
459
460 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
461 return 0;
462
463 if (!ieee80211_is_robust_mgmt_frame(skb))
464 return 0;
465
466 return 1;
467 }
468
469 static ieee80211_tx_result
470 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
471 {
472 struct sta_info *sta = tx->sta;
473 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
474 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
475 struct ieee80211_local *local = tx->local;
476
477 if (unlikely(!sta))
478 return TX_CONTINUE;
479
480 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
481 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
482 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
483 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
484 int ac = skb_get_queue_mapping(tx->skb);
485
486 if (ieee80211_is_mgmt(hdr->frame_control) &&
487 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
488 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
489 return TX_CONTINUE;
490 }
491
492 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
493 sta->sta.addr, sta->sta.aid, ac);
494 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
495 purge_old_ps_buffers(tx->local);
496
497 /* sync with ieee80211_sta_ps_deliver_wakeup */
498 spin_lock(&sta->ps_lock);
499 /*
500 * STA woke up the meantime and all the frames on ps_tx_buf have
501 * been queued to pending queue. No reordering can happen, go
502 * ahead and Tx the packet.
503 */
504 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
505 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
506 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
507 spin_unlock(&sta->ps_lock);
508 return TX_CONTINUE;
509 }
510
511 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
512 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
513 ps_dbg(tx->sdata,
514 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
515 sta->sta.addr, ac);
516 ieee80211_free_txskb(&local->hw, old);
517 } else
518 tx->local->total_ps_buffered++;
519
520 info->control.jiffies = jiffies;
521 info->control.vif = &tx->sdata->vif;
522 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
523 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
524 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
525 spin_unlock(&sta->ps_lock);
526
527 if (!timer_pending(&local->sta_cleanup))
528 mod_timer(&local->sta_cleanup,
529 round_jiffies(jiffies +
530 STA_INFO_CLEANUP_INTERVAL));
531
532 /*
533 * We queued up some frames, so the TIM bit might
534 * need to be set, recalculate it.
535 */
536 sta_info_recalc_tim(sta);
537
538 return TX_QUEUED;
539 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
540 ps_dbg(tx->sdata,
541 "STA %pM in PS mode, but polling/in SP -> send frame\n",
542 sta->sta.addr);
543 }
544
545 return TX_CONTINUE;
546 }
547
548 static ieee80211_tx_result debug_noinline
549 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
550 {
551 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
552 return TX_CONTINUE;
553
554 if (tx->flags & IEEE80211_TX_UNICAST)
555 return ieee80211_tx_h_unicast_ps_buf(tx);
556 else
557 return ieee80211_tx_h_multicast_ps_buf(tx);
558 }
559
560 static ieee80211_tx_result debug_noinline
561 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
562 {
563 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
564
565 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
566 if (tx->sdata->control_port_no_encrypt)
567 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
568 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
569 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
570 }
571
572 return TX_CONTINUE;
573 }
574
575 static ieee80211_tx_result debug_noinline
576 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
577 {
578 struct ieee80211_key *key;
579 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
580 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
581
582 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
583 tx->key = NULL;
584 else if (tx->sta &&
585 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
586 tx->key = key;
587 else if (ieee80211_is_mgmt(hdr->frame_control) &&
588 is_multicast_ether_addr(hdr->addr1) &&
589 ieee80211_is_robust_mgmt_frame(tx->skb) &&
590 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
591 tx->key = key;
592 else if (is_multicast_ether_addr(hdr->addr1) &&
593 (key = rcu_dereference(tx->sdata->default_multicast_key)))
594 tx->key = key;
595 else if (!is_multicast_ether_addr(hdr->addr1) &&
596 (key = rcu_dereference(tx->sdata->default_unicast_key)))
597 tx->key = key;
598 else if (info->flags & IEEE80211_TX_CTL_INJECTED)
599 tx->key = NULL;
600 else if (!tx->sdata->drop_unencrypted)
601 tx->key = NULL;
602 else if (tx->skb->protocol == tx->sdata->control_port_protocol)
603 tx->key = NULL;
604 else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
605 !(ieee80211_is_action(hdr->frame_control) &&
606 tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
607 tx->key = NULL;
608 else if (ieee80211_is_mgmt(hdr->frame_control) &&
609 !ieee80211_is_robust_mgmt_frame(tx->skb))
610 tx->key = NULL;
611 else {
612 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
613 return TX_DROP;
614 }
615
616 if (tx->key) {
617 bool skip_hw = false;
618
619 tx->key->tx_rx_count++;
620 /* TODO: add threshold stuff again */
621
622 switch (tx->key->conf.cipher) {
623 case WLAN_CIPHER_SUITE_WEP40:
624 case WLAN_CIPHER_SUITE_WEP104:
625 case WLAN_CIPHER_SUITE_TKIP:
626 if (!ieee80211_is_data_present(hdr->frame_control))
627 tx->key = NULL;
628 break;
629 case WLAN_CIPHER_SUITE_CCMP:
630 if (!ieee80211_is_data_present(hdr->frame_control) &&
631 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
632 tx->skb))
633 tx->key = NULL;
634 else
635 skip_hw = (tx->key->conf.flags &
636 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
637 ieee80211_is_mgmt(hdr->frame_control);
638 break;
639 case WLAN_CIPHER_SUITE_AES_CMAC:
640 if (!ieee80211_is_mgmt(hdr->frame_control))
641 tx->key = NULL;
642 break;
643 }
644
645 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
646 !ieee80211_is_deauth(hdr->frame_control)))
647 return TX_DROP;
648
649 if (!skip_hw && tx->key &&
650 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
651 info->control.hw_key = &tx->key->conf;
652 }
653
654 return TX_CONTINUE;
655 }
656
657 static ieee80211_tx_result debug_noinline
658 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
659 {
660 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
661 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
662 struct ieee80211_supported_band *sband;
663 u32 len;
664 struct ieee80211_tx_rate_control txrc;
665 struct ieee80211_sta_rates *ratetbl = NULL;
666 bool assoc = false;
667
668 memset(&txrc, 0, sizeof(txrc));
669
670 sband = tx->local->hw.wiphy->bands[info->band];
671
672 len = min_t(u32, tx->skb->len + FCS_LEN,
673 tx->local->hw.wiphy->frag_threshold);
674
675 /* set up the tx rate control struct we give the RC algo */
676 txrc.hw = &tx->local->hw;
677 txrc.sband = sband;
678 txrc.bss_conf = &tx->sdata->vif.bss_conf;
679 txrc.skb = tx->skb;
680 txrc.reported_rate.idx = -1;
681 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
682 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
683 txrc.max_rate_idx = -1;
684 else
685 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
686
687 if (tx->sdata->rc_has_mcs_mask[info->band])
688 txrc.rate_idx_mcs_mask =
689 tx->sdata->rc_rateidx_mcs_mask[info->band];
690
691 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
692 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
693 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
694
695 /* set up RTS protection if desired */
696 if (len > tx->local->hw.wiphy->rts_threshold) {
697 txrc.rts = true;
698 }
699
700 info->control.use_rts = txrc.rts;
701 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
702
703 /*
704 * Use short preamble if the BSS can handle it, but not for
705 * management frames unless we know the receiver can handle
706 * that -- the management frame might be to a station that
707 * just wants a probe response.
708 */
709 if (tx->sdata->vif.bss_conf.use_short_preamble &&
710 (ieee80211_is_data(hdr->frame_control) ||
711 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
712 txrc.short_preamble = true;
713
714 info->control.short_preamble = txrc.short_preamble;
715
716 if (tx->sta)
717 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
718
719 /*
720 * Lets not bother rate control if we're associated and cannot
721 * talk to the sta. This should not happen.
722 */
723 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
724 !rate_usable_index_exists(sband, &tx->sta->sta),
725 "%s: Dropped data frame as no usable bitrate found while "
726 "scanning and associated. Target station: "
727 "%pM on %d GHz band\n",
728 tx->sdata->name, hdr->addr1,
729 info->band ? 5 : 2))
730 return TX_DROP;
731
732 /*
733 * If we're associated with the sta at this point we know we can at
734 * least send the frame at the lowest bit rate.
735 */
736 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
737
738 if (tx->sta && !info->control.skip_table)
739 ratetbl = rcu_dereference(tx->sta->sta.rates);
740
741 if (unlikely(info->control.rates[0].idx < 0)) {
742 if (ratetbl) {
743 struct ieee80211_tx_rate rate = {
744 .idx = ratetbl->rate[0].idx,
745 .flags = ratetbl->rate[0].flags,
746 .count = ratetbl->rate[0].count
747 };
748
749 if (ratetbl->rate[0].idx < 0)
750 return TX_DROP;
751
752 tx->rate = rate;
753 } else {
754 return TX_DROP;
755 }
756 } else {
757 tx->rate = info->control.rates[0];
758 }
759
760 if (txrc.reported_rate.idx < 0) {
761 txrc.reported_rate = tx->rate;
762 if (tx->sta && ieee80211_is_data(hdr->frame_control))
763 tx->sta->last_tx_rate = txrc.reported_rate;
764 } else if (tx->sta)
765 tx->sta->last_tx_rate = txrc.reported_rate;
766
767 if (ratetbl)
768 return TX_CONTINUE;
769
770 if (unlikely(!info->control.rates[0].count))
771 info->control.rates[0].count = 1;
772
773 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
774 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
775 info->control.rates[0].count = 1;
776
777 return TX_CONTINUE;
778 }
779
780 static ieee80211_tx_result debug_noinline
781 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
782 {
783 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
784 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
785 u16 *seq;
786 u8 *qc;
787 int tid;
788
789 /*
790 * Packet injection may want to control the sequence
791 * number, if we have no matching interface then we
792 * neither assign one ourselves nor ask the driver to.
793 */
794 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
795 return TX_CONTINUE;
796
797 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
798 return TX_CONTINUE;
799
800 if (ieee80211_hdrlen(hdr->frame_control) < 24)
801 return TX_CONTINUE;
802
803 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
804 return TX_CONTINUE;
805
806 /*
807 * Anything but QoS data that has a sequence number field
808 * (is long enough) gets a sequence number from the global
809 * counter. QoS data frames with a multicast destination
810 * also use the global counter (802.11-2012 9.3.2.10).
811 */
812 if (!ieee80211_is_data_qos(hdr->frame_control) ||
813 is_multicast_ether_addr(hdr->addr1)) {
814 /* driver should assign sequence number */
815 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
816 /* for pure STA mode without beacons, we can do it */
817 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
818 tx->sdata->sequence_number += 0x10;
819 return TX_CONTINUE;
820 }
821
822 /*
823 * This should be true for injected/management frames only, for
824 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
825 * above since they are not QoS-data frames.
826 */
827 if (!tx->sta)
828 return TX_CONTINUE;
829
830 /* include per-STA, per-TID sequence counter */
831
832 qc = ieee80211_get_qos_ctl(hdr);
833 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
834 seq = &tx->sta->tid_seq[tid];
835
836 hdr->seq_ctrl = cpu_to_le16(*seq);
837
838 /* Increase the sequence number. */
839 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
840
841 return TX_CONTINUE;
842 }
843
844 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
845 struct sk_buff *skb, int hdrlen,
846 int frag_threshold)
847 {
848 struct ieee80211_local *local = tx->local;
849 struct ieee80211_tx_info *info;
850 struct sk_buff *tmp;
851 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
852 int pos = hdrlen + per_fragm;
853 int rem = skb->len - hdrlen - per_fragm;
854
855 if (WARN_ON(rem < 0))
856 return -EINVAL;
857
858 /* first fragment was already added to queue by caller */
859
860 while (rem) {
861 int fraglen = per_fragm;
862
863 if (fraglen > rem)
864 fraglen = rem;
865 rem -= fraglen;
866 tmp = dev_alloc_skb(local->tx_headroom +
867 frag_threshold +
868 tx->sdata->encrypt_headroom +
869 IEEE80211_ENCRYPT_TAILROOM);
870 if (!tmp)
871 return -ENOMEM;
872
873 __skb_queue_tail(&tx->skbs, tmp);
874
875 skb_reserve(tmp,
876 local->tx_headroom + tx->sdata->encrypt_headroom);
877
878 /* copy control information */
879 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
880
881 info = IEEE80211_SKB_CB(tmp);
882 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
883 IEEE80211_TX_CTL_FIRST_FRAGMENT);
884
885 if (rem)
886 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
887
888 skb_copy_queue_mapping(tmp, skb);
889 tmp->priority = skb->priority;
890 tmp->dev = skb->dev;
891
892 /* copy header and data */
893 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
894 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
895
896 pos += fraglen;
897 }
898
899 /* adjust first fragment's length */
900 skb_trim(skb, hdrlen + per_fragm);
901 return 0;
902 }
903
904 static ieee80211_tx_result debug_noinline
905 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
906 {
907 struct sk_buff *skb = tx->skb;
908 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
909 struct ieee80211_hdr *hdr = (void *)skb->data;
910 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
911 int hdrlen;
912 int fragnum;
913
914 /* no matter what happens, tx->skb moves to tx->skbs */
915 __skb_queue_tail(&tx->skbs, skb);
916 tx->skb = NULL;
917
918 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
919 return TX_CONTINUE;
920
921 if (tx->local->ops->set_frag_threshold)
922 return TX_CONTINUE;
923
924 /*
925 * Warn when submitting a fragmented A-MPDU frame and drop it.
926 * This scenario is handled in ieee80211_tx_prepare but extra
927 * caution taken here as fragmented ampdu may cause Tx stop.
928 */
929 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
930 return TX_DROP;
931
932 hdrlen = ieee80211_hdrlen(hdr->frame_control);
933
934 /* internal error, why isn't DONTFRAG set? */
935 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
936 return TX_DROP;
937
938 /*
939 * Now fragment the frame. This will allocate all the fragments and
940 * chain them (using skb as the first fragment) to skb->next.
941 * During transmission, we will remove the successfully transmitted
942 * fragments from this list. When the low-level driver rejects one
943 * of the fragments then we will simply pretend to accept the skb
944 * but store it away as pending.
945 */
946 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
947 return TX_DROP;
948
949 /* update duration/seq/flags of fragments */
950 fragnum = 0;
951
952 skb_queue_walk(&tx->skbs, skb) {
953 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
954
955 hdr = (void *)skb->data;
956 info = IEEE80211_SKB_CB(skb);
957
958 if (!skb_queue_is_last(&tx->skbs, skb)) {
959 hdr->frame_control |= morefrags;
960 /*
961 * No multi-rate retries for fragmented frames, that
962 * would completely throw off the NAV at other STAs.
963 */
964 info->control.rates[1].idx = -1;
965 info->control.rates[2].idx = -1;
966 info->control.rates[3].idx = -1;
967 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
968 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
969 } else {
970 hdr->frame_control &= ~morefrags;
971 }
972 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
973 fragnum++;
974 }
975
976 return TX_CONTINUE;
977 }
978
979 static ieee80211_tx_result debug_noinline
980 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
981 {
982 struct sk_buff *skb;
983 int ac = -1;
984
985 if (!tx->sta)
986 return TX_CONTINUE;
987
988 skb_queue_walk(&tx->skbs, skb) {
989 ac = skb_get_queue_mapping(skb);
990 tx->sta->tx_fragments++;
991 tx->sta->tx_bytes[ac] += skb->len;
992 }
993 if (ac >= 0)
994 tx->sta->tx_packets[ac]++;
995
996 return TX_CONTINUE;
997 }
998
999 static ieee80211_tx_result debug_noinline
1000 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1001 {
1002 if (!tx->key)
1003 return TX_CONTINUE;
1004
1005 switch (tx->key->conf.cipher) {
1006 case WLAN_CIPHER_SUITE_WEP40:
1007 case WLAN_CIPHER_SUITE_WEP104:
1008 return ieee80211_crypto_wep_encrypt(tx);
1009 case WLAN_CIPHER_SUITE_TKIP:
1010 return ieee80211_crypto_tkip_encrypt(tx);
1011 case WLAN_CIPHER_SUITE_CCMP:
1012 return ieee80211_crypto_ccmp_encrypt(tx);
1013 case WLAN_CIPHER_SUITE_AES_CMAC:
1014 return ieee80211_crypto_aes_cmac_encrypt(tx);
1015 default:
1016 return ieee80211_crypto_hw_encrypt(tx);
1017 }
1018
1019 return TX_DROP;
1020 }
1021
1022 static ieee80211_tx_result debug_noinline
1023 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1024 {
1025 struct sk_buff *skb;
1026 struct ieee80211_hdr *hdr;
1027 int next_len;
1028 bool group_addr;
1029
1030 skb_queue_walk(&tx->skbs, skb) {
1031 hdr = (void *) skb->data;
1032 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1033 break; /* must not overwrite AID */
1034 if (!skb_queue_is_last(&tx->skbs, skb)) {
1035 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1036 next_len = next->len;
1037 } else
1038 next_len = 0;
1039 group_addr = is_multicast_ether_addr(hdr->addr1);
1040
1041 hdr->duration_id =
1042 ieee80211_duration(tx, skb, group_addr, next_len);
1043 }
1044
1045 return TX_CONTINUE;
1046 }
1047
1048 /* actual transmit path */
1049
1050 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1051 struct sk_buff *skb,
1052 struct ieee80211_tx_info *info,
1053 struct tid_ampdu_tx *tid_tx,
1054 int tid)
1055 {
1056 bool queued = false;
1057 bool reset_agg_timer = false;
1058 struct sk_buff *purge_skb = NULL;
1059
1060 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1061 info->flags |= IEEE80211_TX_CTL_AMPDU;
1062 reset_agg_timer = true;
1063 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1064 /*
1065 * nothing -- this aggregation session is being started
1066 * but that might still fail with the driver
1067 */
1068 } else {
1069 spin_lock(&tx->sta->lock);
1070 /*
1071 * Need to re-check now, because we may get here
1072 *
1073 * 1) in the window during which the setup is actually
1074 * already done, but not marked yet because not all
1075 * packets are spliced over to the driver pending
1076 * queue yet -- if this happened we acquire the lock
1077 * either before or after the splice happens, but
1078 * need to recheck which of these cases happened.
1079 *
1080 * 2) during session teardown, if the OPERATIONAL bit
1081 * was cleared due to the teardown but the pointer
1082 * hasn't been assigned NULL yet (or we loaded it
1083 * before it was assigned) -- in this case it may
1084 * now be NULL which means we should just let the
1085 * packet pass through because splicing the frames
1086 * back is already done.
1087 */
1088 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1089
1090 if (!tid_tx) {
1091 /* do nothing, let packet pass through */
1092 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1093 info->flags |= IEEE80211_TX_CTL_AMPDU;
1094 reset_agg_timer = true;
1095 } else {
1096 queued = true;
1097 info->control.vif = &tx->sdata->vif;
1098 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1099 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1100 __skb_queue_tail(&tid_tx->pending, skb);
1101 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1102 purge_skb = __skb_dequeue(&tid_tx->pending);
1103 }
1104 spin_unlock(&tx->sta->lock);
1105
1106 if (purge_skb)
1107 ieee80211_free_txskb(&tx->local->hw, purge_skb);
1108 }
1109
1110 /* reset session timer */
1111 if (reset_agg_timer && tid_tx->timeout)
1112 tid_tx->last_tx = jiffies;
1113
1114 return queued;
1115 }
1116
1117 /*
1118 * initialises @tx
1119 */
1120 static ieee80211_tx_result
1121 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1122 struct ieee80211_tx_data *tx,
1123 struct sk_buff *skb)
1124 {
1125 struct ieee80211_local *local = sdata->local;
1126 struct ieee80211_hdr *hdr;
1127 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1128 int tid;
1129 u8 *qc;
1130
1131 memset(tx, 0, sizeof(*tx));
1132 tx->skb = skb;
1133 tx->local = local;
1134 tx->sdata = sdata;
1135 __skb_queue_head_init(&tx->skbs);
1136
1137 /*
1138 * If this flag is set to true anywhere, and we get here,
1139 * we are doing the needed processing, so remove the flag
1140 * now.
1141 */
1142 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1143
1144 hdr = (struct ieee80211_hdr *) skb->data;
1145
1146 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1147 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1148 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1149 return TX_DROP;
1150 } else if (info->flags & (IEEE80211_TX_CTL_INJECTED |
1151 IEEE80211_TX_INTFL_NL80211_FRAME_TX) ||
1152 tx->sdata->control_port_protocol == tx->skb->protocol) {
1153 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1154 }
1155 if (!tx->sta)
1156 tx->sta = sta_info_get(sdata, hdr->addr1);
1157
1158 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1159 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1160 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
1161 !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
1162 struct tid_ampdu_tx *tid_tx;
1163
1164 qc = ieee80211_get_qos_ctl(hdr);
1165 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1166
1167 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1168 if (tid_tx) {
1169 bool queued;
1170
1171 queued = ieee80211_tx_prep_agg(tx, skb, info,
1172 tid_tx, tid);
1173
1174 if (unlikely(queued))
1175 return TX_QUEUED;
1176 }
1177 }
1178
1179 if (is_multicast_ether_addr(hdr->addr1)) {
1180 tx->flags &= ~IEEE80211_TX_UNICAST;
1181 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1182 } else
1183 tx->flags |= IEEE80211_TX_UNICAST;
1184
1185 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1186 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1187 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1188 info->flags & IEEE80211_TX_CTL_AMPDU)
1189 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1190 }
1191
1192 if (!tx->sta)
1193 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1194 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1195 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1196
1197 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1198
1199 return TX_CONTINUE;
1200 }
1201
1202 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1203 struct ieee80211_vif *vif,
1204 struct ieee80211_sta *sta,
1205 struct sk_buff_head *skbs,
1206 bool txpending)
1207 {
1208 struct ieee80211_tx_control control;
1209 struct sk_buff *skb, *tmp;
1210 unsigned long flags;
1211
1212 skb_queue_walk_safe(skbs, skb, tmp) {
1213 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1214 int q = info->hw_queue;
1215
1216 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1217 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1218 __skb_unlink(skb, skbs);
1219 ieee80211_free_txskb(&local->hw, skb);
1220 continue;
1221 }
1222 #endif
1223
1224 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1225 if (local->queue_stop_reasons[q] ||
1226 (!txpending && !skb_queue_empty(&local->pending[q]))) {
1227 if (unlikely(info->flags &
1228 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1229 if (local->queue_stop_reasons[q] &
1230 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1231 /*
1232 * Drop off-channel frames if queues
1233 * are stopped for any reason other
1234 * than off-channel operation. Never
1235 * queue them.
1236 */
1237 spin_unlock_irqrestore(
1238 &local->queue_stop_reason_lock,
1239 flags);
1240 ieee80211_purge_tx_queue(&local->hw,
1241 skbs);
1242 return true;
1243 }
1244 } else {
1245
1246 /*
1247 * Since queue is stopped, queue up frames for
1248 * later transmission from the tx-pending
1249 * tasklet when the queue is woken again.
1250 */
1251 if (txpending)
1252 skb_queue_splice_init(skbs,
1253 &local->pending[q]);
1254 else
1255 skb_queue_splice_tail_init(skbs,
1256 &local->pending[q]);
1257
1258 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1259 flags);
1260 return false;
1261 }
1262 }
1263 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1264
1265 info->control.vif = vif;
1266 control.sta = sta;
1267
1268 __skb_unlink(skb, skbs);
1269 drv_tx(local, &control, skb);
1270 }
1271
1272 return true;
1273 }
1274
1275 /*
1276 * Returns false if the frame couldn't be transmitted but was queued instead.
1277 */
1278 static bool __ieee80211_tx(struct ieee80211_local *local,
1279 struct sk_buff_head *skbs, int led_len,
1280 struct sta_info *sta, bool txpending)
1281 {
1282 struct ieee80211_tx_info *info;
1283 struct ieee80211_sub_if_data *sdata;
1284 struct ieee80211_vif *vif;
1285 struct ieee80211_sta *pubsta;
1286 struct sk_buff *skb;
1287 bool result = true;
1288 __le16 fc;
1289
1290 if (WARN_ON(skb_queue_empty(skbs)))
1291 return true;
1292
1293 skb = skb_peek(skbs);
1294 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1295 info = IEEE80211_SKB_CB(skb);
1296 sdata = vif_to_sdata(info->control.vif);
1297 if (sta && !sta->uploaded)
1298 sta = NULL;
1299
1300 if (sta)
1301 pubsta = &sta->sta;
1302 else
1303 pubsta = NULL;
1304
1305 switch (sdata->vif.type) {
1306 case NL80211_IFTYPE_MONITOR:
1307 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1308 vif = &sdata->vif;
1309 break;
1310 }
1311 sdata = rcu_dereference(local->monitor_sdata);
1312 if (sdata) {
1313 vif = &sdata->vif;
1314 info->hw_queue =
1315 vif->hw_queue[skb_get_queue_mapping(skb)];
1316 } else if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
1317 dev_kfree_skb(skb);
1318 return true;
1319 } else
1320 vif = NULL;
1321 break;
1322 case NL80211_IFTYPE_AP_VLAN:
1323 sdata = container_of(sdata->bss,
1324 struct ieee80211_sub_if_data, u.ap);
1325 /* fall through */
1326 default:
1327 vif = &sdata->vif;
1328 break;
1329 }
1330
1331 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1332 txpending);
1333
1334 ieee80211_tpt_led_trig_tx(local, fc, led_len);
1335
1336 WARN_ON_ONCE(!skb_queue_empty(skbs));
1337
1338 return result;
1339 }
1340
1341 /*
1342 * Invoke TX handlers, return 0 on success and non-zero if the
1343 * frame was dropped or queued.
1344 */
1345 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1346 {
1347 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1348 ieee80211_tx_result res = TX_DROP;
1349
1350 #define CALL_TXH(txh) \
1351 do { \
1352 res = txh(tx); \
1353 if (res != TX_CONTINUE) \
1354 goto txh_done; \
1355 } while (0)
1356
1357 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1358 CALL_TXH(ieee80211_tx_h_check_assoc);
1359 CALL_TXH(ieee80211_tx_h_ps_buf);
1360 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1361 CALL_TXH(ieee80211_tx_h_select_key);
1362 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1363 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1364
1365 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1366 __skb_queue_tail(&tx->skbs, tx->skb);
1367 tx->skb = NULL;
1368 goto txh_done;
1369 }
1370
1371 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1372 CALL_TXH(ieee80211_tx_h_sequence);
1373 CALL_TXH(ieee80211_tx_h_fragment);
1374 /* handlers after fragment must be aware of tx info fragmentation! */
1375 CALL_TXH(ieee80211_tx_h_stats);
1376 CALL_TXH(ieee80211_tx_h_encrypt);
1377 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1378 CALL_TXH(ieee80211_tx_h_calculate_duration);
1379 #undef CALL_TXH
1380
1381 txh_done:
1382 if (unlikely(res == TX_DROP)) {
1383 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1384 if (tx->skb)
1385 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1386 else
1387 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1388 return -1;
1389 } else if (unlikely(res == TX_QUEUED)) {
1390 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1391 return -1;
1392 }
1393
1394 return 0;
1395 }
1396
1397 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1398 struct ieee80211_vif *vif, struct sk_buff *skb,
1399 int band, struct ieee80211_sta **sta)
1400 {
1401 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1402 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1403 struct ieee80211_tx_data tx;
1404
1405 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1406 return false;
1407
1408 info->band = band;
1409 info->control.vif = vif;
1410 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1411
1412 if (invoke_tx_handlers(&tx))
1413 return false;
1414
1415 if (sta) {
1416 if (tx.sta)
1417 *sta = &tx.sta->sta;
1418 else
1419 *sta = NULL;
1420 }
1421
1422 return true;
1423 }
1424 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1425
1426 /*
1427 * Returns false if the frame couldn't be transmitted but was queued instead.
1428 */
1429 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1430 struct sk_buff *skb, bool txpending)
1431 {
1432 struct ieee80211_local *local = sdata->local;
1433 struct ieee80211_tx_data tx;
1434 ieee80211_tx_result res_prepare;
1435 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1436 bool result = true;
1437 int led_len;
1438
1439 if (unlikely(skb->len < 10)) {
1440 dev_kfree_skb(skb);
1441 return true;
1442 }
1443
1444 /* initialises tx */
1445 led_len = skb->len;
1446 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
1447
1448 if (unlikely(res_prepare == TX_DROP)) {
1449 ieee80211_free_txskb(&local->hw, skb);
1450 return true;
1451 } else if (unlikely(res_prepare == TX_QUEUED)) {
1452 return true;
1453 }
1454
1455 /* set up hw_queue value early */
1456 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1457 !(local->hw.flags & IEEE80211_HW_QUEUE_CONTROL))
1458 info->hw_queue =
1459 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1460
1461 if (!invoke_tx_handlers(&tx))
1462 result = __ieee80211_tx(local, &tx.skbs, led_len,
1463 tx.sta, txpending);
1464
1465 return result;
1466 }
1467
1468 /* device xmit handlers */
1469
1470 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1471 struct sk_buff *skb,
1472 int head_need, bool may_encrypt)
1473 {
1474 struct ieee80211_local *local = sdata->local;
1475 int tail_need = 0;
1476
1477 if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
1478 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1479 tail_need -= skb_tailroom(skb);
1480 tail_need = max_t(int, tail_need, 0);
1481 }
1482
1483 if (skb_cloned(skb) &&
1484 (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CLONED_SKBS) ||
1485 !skb_clone_writable(skb, ETH_HLEN) ||
1486 sdata->crypto_tx_tailroom_needed_cnt))
1487 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1488 else if (head_need || tail_need)
1489 I802_DEBUG_INC(local->tx_expand_skb_head);
1490 else
1491 return 0;
1492
1493 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1494 wiphy_debug(local->hw.wiphy,
1495 "failed to reallocate TX buffer\n");
1496 return -ENOMEM;
1497 }
1498
1499 return 0;
1500 }
1501
1502 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
1503 {
1504 struct ieee80211_local *local = sdata->local;
1505 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1506 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1507 int headroom;
1508 bool may_encrypt;
1509
1510 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1511
1512 headroom = local->tx_headroom;
1513 if (may_encrypt)
1514 headroom += sdata->encrypt_headroom;
1515 headroom -= skb_headroom(skb);
1516 headroom = max_t(int, 0, headroom);
1517
1518 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1519 ieee80211_free_txskb(&local->hw, skb);
1520 return;
1521 }
1522
1523 hdr = (struct ieee80211_hdr *) skb->data;
1524 info->control.vif = &sdata->vif;
1525
1526 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1527 if (ieee80211_is_data(hdr->frame_control) &&
1528 is_unicast_ether_addr(hdr->addr1)) {
1529 if (mesh_nexthop_resolve(sdata, skb))
1530 return; /* skb queued: don't free */
1531 } else {
1532 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1533 }
1534 }
1535
1536 ieee80211_set_qos_hdr(sdata, skb);
1537 ieee80211_tx(sdata, skb, false);
1538 }
1539
1540 static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
1541 {
1542 struct ieee80211_radiotap_iterator iterator;
1543 struct ieee80211_radiotap_header *rthdr =
1544 (struct ieee80211_radiotap_header *) skb->data;
1545 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1546 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1547 NULL);
1548 u16 txflags;
1549
1550 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1551 IEEE80211_TX_CTL_DONTFRAG;
1552
1553 /*
1554 * for every radiotap entry that is present
1555 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1556 * entries present, or -EINVAL on error)
1557 */
1558
1559 while (!ret) {
1560 ret = ieee80211_radiotap_iterator_next(&iterator);
1561
1562 if (ret)
1563 continue;
1564
1565 /* see if this argument is something we can use */
1566 switch (iterator.this_arg_index) {
1567 /*
1568 * You must take care when dereferencing iterator.this_arg
1569 * for multibyte types... the pointer is not aligned. Use
1570 * get_unaligned((type *)iterator.this_arg) to dereference
1571 * iterator.this_arg for type "type" safely on all arches.
1572 */
1573 case IEEE80211_RADIOTAP_FLAGS:
1574 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1575 /*
1576 * this indicates that the skb we have been
1577 * handed has the 32-bit FCS CRC at the end...
1578 * we should react to that by snipping it off
1579 * because it will be recomputed and added
1580 * on transmission
1581 */
1582 if (skb->len < (iterator._max_length + FCS_LEN))
1583 return false;
1584
1585 skb_trim(skb, skb->len - FCS_LEN);
1586 }
1587 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1588 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1589 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1590 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1591 break;
1592
1593 case IEEE80211_RADIOTAP_TX_FLAGS:
1594 txflags = get_unaligned_le16(iterator.this_arg);
1595 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1596 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1597 break;
1598
1599 /*
1600 * Please update the file
1601 * Documentation/networking/mac80211-injection.txt
1602 * when parsing new fields here.
1603 */
1604
1605 default:
1606 break;
1607 }
1608 }
1609
1610 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1611 return false;
1612
1613 /*
1614 * remove the radiotap header
1615 * iterator->_max_length was sanity-checked against
1616 * skb->len by iterator init
1617 */
1618 skb_pull(skb, iterator._max_length);
1619
1620 return true;
1621 }
1622
1623 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1624 struct net_device *dev)
1625 {
1626 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1627 struct ieee80211_chanctx_conf *chanctx_conf;
1628 struct ieee80211_radiotap_header *prthdr =
1629 (struct ieee80211_radiotap_header *)skb->data;
1630 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1631 struct ieee80211_hdr *hdr;
1632 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
1633 struct cfg80211_chan_def *chandef;
1634 u16 len_rthdr;
1635 int hdrlen;
1636
1637 /* check for not even having the fixed radiotap header part */
1638 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1639 goto fail; /* too short to be possibly valid */
1640
1641 /* is it a header version we can trust to find length from? */
1642 if (unlikely(prthdr->it_version))
1643 goto fail; /* only version 0 is supported */
1644
1645 /* then there must be a radiotap header with a length we can use */
1646 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1647
1648 /* does the skb contain enough to deliver on the alleged length? */
1649 if (unlikely(skb->len < len_rthdr))
1650 goto fail; /* skb too short for claimed rt header extent */
1651
1652 /*
1653 * fix up the pointers accounting for the radiotap
1654 * header still being in there. We are being given
1655 * a precooked IEEE80211 header so no need for
1656 * normal processing
1657 */
1658 skb_set_mac_header(skb, len_rthdr);
1659 /*
1660 * these are just fixed to the end of the rt area since we
1661 * don't have any better information and at this point, nobody cares
1662 */
1663 skb_set_network_header(skb, len_rthdr);
1664 skb_set_transport_header(skb, len_rthdr);
1665
1666 if (skb->len < len_rthdr + 2)
1667 goto fail;
1668
1669 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1670 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1671
1672 if (skb->len < len_rthdr + hdrlen)
1673 goto fail;
1674
1675 /*
1676 * Initialize skb->protocol if the injected frame is a data frame
1677 * carrying a rfc1042 header
1678 */
1679 if (ieee80211_is_data(hdr->frame_control) &&
1680 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
1681 u8 *payload = (u8 *)hdr + hdrlen;
1682
1683 if (ether_addr_equal(payload, rfc1042_header))
1684 skb->protocol = cpu_to_be16((payload[6] << 8) |
1685 payload[7]);
1686 }
1687
1688 memset(info, 0, sizeof(*info));
1689
1690 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
1691 IEEE80211_TX_CTL_INJECTED;
1692
1693 /* process and remove the injection radiotap header */
1694 if (!ieee80211_parse_tx_radiotap(skb))
1695 goto fail;
1696
1697 rcu_read_lock();
1698
1699 /*
1700 * We process outgoing injected frames that have a local address
1701 * we handle as though they are non-injected frames.
1702 * This code here isn't entirely correct, the local MAC address
1703 * isn't always enough to find the interface to use; for proper
1704 * VLAN/WDS support we will need a different mechanism (which
1705 * likely isn't going to be monitor interfaces).
1706 */
1707 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1708
1709 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
1710 if (!ieee80211_sdata_running(tmp_sdata))
1711 continue;
1712 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1713 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1714 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
1715 continue;
1716 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
1717 sdata = tmp_sdata;
1718 break;
1719 }
1720 }
1721
1722 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1723 if (!chanctx_conf) {
1724 tmp_sdata = rcu_dereference(local->monitor_sdata);
1725 if (tmp_sdata)
1726 chanctx_conf =
1727 rcu_dereference(tmp_sdata->vif.chanctx_conf);
1728 }
1729
1730 if (chanctx_conf)
1731 chandef = &chanctx_conf->def;
1732 else if (!local->use_chanctx)
1733 chandef = &local->_oper_chandef;
1734 else
1735 goto fail_rcu;
1736
1737 /*
1738 * Frame injection is not allowed if beaconing is not allowed
1739 * or if we need radar detection. Beaconing is usually not allowed when
1740 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1741 * Passive scan is also used in world regulatory domains where
1742 * your country is not known and as such it should be treated as
1743 * NO TX unless the channel is explicitly allowed in which case
1744 * your current regulatory domain would not have the passive scan
1745 * flag.
1746 *
1747 * Since AP mode uses monitor interfaces to inject/TX management
1748 * frames we can make AP mode the exception to this rule once it
1749 * supports radar detection as its implementation can deal with
1750 * radar detection by itself. We can do that later by adding a
1751 * monitor flag interfaces used for AP support.
1752 */
1753 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
1754 sdata->vif.type))
1755 goto fail_rcu;
1756
1757 info->band = chandef->chan->band;
1758 ieee80211_xmit(sdata, skb);
1759 rcu_read_unlock();
1760
1761 return NETDEV_TX_OK;
1762
1763 fail_rcu:
1764 rcu_read_unlock();
1765 fail:
1766 dev_kfree_skb(skb);
1767 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1768 }
1769
1770 /*
1771 * Measure Tx frame arrival time for Tx latency statistics calculation
1772 * A single Tx frame latency should be measured from when it is entering the
1773 * Kernel until we receive Tx complete confirmation indication and the skb is
1774 * freed.
1775 */
1776 static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
1777 struct sk_buff *skb)
1778 {
1779 struct ieee80211_tx_latency_bin_ranges *tx_latency;
1780
1781 tx_latency = rcu_dereference(local->tx_latency);
1782 if (!tx_latency)
1783 return;
1784 skb->tstamp = ktime_get();
1785 }
1786
1787 /**
1788 * ieee80211_build_hdr - build 802.11 header in the given frame
1789 * @sdata: virtual interface to build the header for
1790 * @skb: the skb to build the header in
1791 * @info_flags: skb flags to set
1792 *
1793 * This function takes the skb with 802.3 header and reformats the header to
1794 * the appropriate IEEE 802.11 header based on which interface the packet is
1795 * being transmitted on.
1796 *
1797 * Note that this function also takes care of the TX status request and
1798 * potential unsharing of the SKB - this needs to be interleaved with the
1799 * header building.
1800 *
1801 * The function requires the read-side RCU lock held
1802 *
1803 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
1804 */
1805 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1806 struct sk_buff *skb, u32 info_flags)
1807 {
1808 struct ieee80211_local *local = sdata->local;
1809 struct ieee80211_tx_info *info;
1810 int head_need;
1811 u16 ethertype, hdrlen, meshhdrlen = 0;
1812 __le16 fc;
1813 struct ieee80211_hdr hdr;
1814 struct ieee80211s_hdr mesh_hdr __maybe_unused;
1815 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
1816 const u8 *encaps_data;
1817 int encaps_len, skip_header_bytes;
1818 int nh_pos, h_pos;
1819 struct sta_info *sta = NULL;
1820 bool wme_sta = false, authorized = false, tdls_auth = false;
1821 bool tdls_peer = false, tdls_setup_frame = false;
1822 bool multicast;
1823 u16 info_id = 0;
1824 struct ieee80211_chanctx_conf *chanctx_conf;
1825 struct ieee80211_sub_if_data *ap_sdata;
1826 enum ieee80211_band band;
1827 int ret;
1828
1829 /* convert Ethernet header to proper 802.11 header (based on
1830 * operation mode) */
1831 ethertype = (skb->data[12] << 8) | skb->data[13];
1832 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
1833
1834 switch (sdata->vif.type) {
1835 case NL80211_IFTYPE_AP_VLAN:
1836 sta = rcu_dereference(sdata->u.vlan.sta);
1837 if (sta) {
1838 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1839 /* RA TA DA SA */
1840 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
1841 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1842 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1843 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1844 hdrlen = 30;
1845 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1846 wme_sta = sta->sta.wme;
1847 }
1848 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1849 u.ap);
1850 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
1851 if (!chanctx_conf) {
1852 ret = -ENOTCONN;
1853 goto free;
1854 }
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 ret = -ENOTCONN;
1864 goto free;
1865 }
1866 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
1867 /* DA BSSID SA */
1868 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1869 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1870 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1871 hdrlen = 24;
1872 band = chanctx_conf->def.chan->band;
1873 break;
1874 case NL80211_IFTYPE_WDS:
1875 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1876 /* RA TA DA SA */
1877 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1878 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1879 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1880 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1881 hdrlen = 30;
1882 /*
1883 * This is the exception! WDS style interfaces are prohibited
1884 * when channel contexts are in used so this must be valid
1885 */
1886 band = local->hw.conf.chandef.chan->band;
1887 break;
1888 #ifdef CONFIG_MAC80211_MESH
1889 case NL80211_IFTYPE_MESH_POINT:
1890 if (!is_multicast_ether_addr(skb->data)) {
1891 struct sta_info *next_hop;
1892 bool mpp_lookup = true;
1893
1894 mpath = mesh_path_lookup(sdata, skb->data);
1895 if (mpath) {
1896 mpp_lookup = false;
1897 next_hop = rcu_dereference(mpath->next_hop);
1898 if (!next_hop ||
1899 !(mpath->flags & (MESH_PATH_ACTIVE |
1900 MESH_PATH_RESOLVING)))
1901 mpp_lookup = true;
1902 }
1903
1904 if (mpp_lookup)
1905 mppath = mpp_path_lookup(sdata, skb->data);
1906
1907 if (mppath && mpath)
1908 mesh_path_del(mpath->sdata, mpath->dst);
1909 }
1910
1911 /*
1912 * Use address extension if it is a packet from
1913 * another interface or if we know the destination
1914 * is being proxied by a portal (i.e. portal address
1915 * differs from proxied address)
1916 */
1917 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
1918 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
1919 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1920 skb->data, skb->data + ETH_ALEN);
1921 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
1922 NULL, NULL);
1923 } else {
1924 /* DS -> MBSS (802.11-2012 13.11.3.3).
1925 * For unicast with unknown forwarding information,
1926 * destination might be in the MBSS or if that fails
1927 * forwarded to another mesh gate. In either case
1928 * resolution will be handled in ieee80211_xmit(), so
1929 * leave the original DA. This also works for mcast */
1930 const u8 *mesh_da = skb->data;
1931
1932 if (mppath)
1933 mesh_da = mppath->mpp;
1934 else if (mpath)
1935 mesh_da = mpath->dst;
1936
1937 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1938 mesh_da, sdata->vif.addr);
1939 if (is_multicast_ether_addr(mesh_da))
1940 /* DA TA mSA AE:SA */
1941 meshhdrlen = ieee80211_new_mesh_header(
1942 sdata, &mesh_hdr,
1943 skb->data + ETH_ALEN, NULL);
1944 else
1945 /* RA TA mDA mSA AE:DA SA */
1946 meshhdrlen = ieee80211_new_mesh_header(
1947 sdata, &mesh_hdr, skb->data,
1948 skb->data + ETH_ALEN);
1949
1950 }
1951 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1952 if (!chanctx_conf) {
1953 ret = -ENOTCONN;
1954 goto free;
1955 }
1956 band = chanctx_conf->def.chan->band;
1957 break;
1958 #endif
1959 case NL80211_IFTYPE_STATION:
1960 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1961 sta = sta_info_get(sdata, skb->data);
1962 if (sta) {
1963 authorized = test_sta_flag(sta,
1964 WLAN_STA_AUTHORIZED);
1965 wme_sta = sta->sta.wme;
1966 tdls_peer = test_sta_flag(sta,
1967 WLAN_STA_TDLS_PEER);
1968 tdls_auth = test_sta_flag(sta,
1969 WLAN_STA_TDLS_PEER_AUTH);
1970 }
1971
1972 if (tdls_peer)
1973 tdls_setup_frame =
1974 ethertype == ETH_P_TDLS &&
1975 skb->len > 14 &&
1976 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
1977 }
1978
1979 /*
1980 * TDLS link during setup - throw out frames to peer. We allow
1981 * TDLS-setup frames to unauthorized peers for the special case
1982 * of a link teardown after a TDLS sta is removed due to being
1983 * unreachable.
1984 */
1985 if (tdls_peer && !tdls_auth && !tdls_setup_frame) {
1986 ret = -EINVAL;
1987 goto free;
1988 }
1989
1990 /* send direct packets to authorized TDLS peers */
1991 if (tdls_peer && tdls_auth) {
1992 /* DA SA BSSID */
1993 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1994 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1995 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
1996 hdrlen = 24;
1997 } else if (sdata->u.mgd.use_4addr &&
1998 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
1999 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2000 IEEE80211_FCTL_TODS);
2001 /* RA TA DA SA */
2002 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2003 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2004 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2005 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2006 hdrlen = 30;
2007 } else {
2008 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2009 /* BSSID SA DA */
2010 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2011 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2012 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2013 hdrlen = 24;
2014 }
2015 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2016 if (!chanctx_conf) {
2017 ret = -ENOTCONN;
2018 goto free;
2019 }
2020 band = chanctx_conf->def.chan->band;
2021 break;
2022 case NL80211_IFTYPE_OCB:
2023 /* DA SA BSSID */
2024 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2025 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2026 eth_broadcast_addr(hdr.addr3);
2027 hdrlen = 24;
2028 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2029 if (!chanctx_conf) {
2030 ret = -ENOTCONN;
2031 goto free;
2032 }
2033 band = chanctx_conf->def.chan->band;
2034 break;
2035 case NL80211_IFTYPE_ADHOC:
2036 /* DA SA BSSID */
2037 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2038 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2039 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2040 hdrlen = 24;
2041 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2042 if (!chanctx_conf) {
2043 ret = -ENOTCONN;
2044 goto free;
2045 }
2046 band = chanctx_conf->def.chan->band;
2047 break;
2048 default:
2049 ret = -EINVAL;
2050 goto free;
2051 }
2052
2053 /*
2054 * There's no need to try to look up the destination
2055 * if it is a multicast address (which can only happen
2056 * in AP mode)
2057 */
2058 multicast = is_multicast_ether_addr(hdr.addr1);
2059 if (!multicast) {
2060 sta = sta_info_get(sdata, hdr.addr1);
2061 if (sta) {
2062 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2063 wme_sta = sta->sta.wme;
2064 }
2065 }
2066
2067 /* For mesh, the use of the QoS header is mandatory */
2068 if (ieee80211_vif_is_mesh(&sdata->vif))
2069 wme_sta = true;
2070
2071 /* receiver and we are QoS enabled, use a QoS type frame */
2072 if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) {
2073 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2074 hdrlen += 2;
2075 }
2076
2077 /*
2078 * Drop unicast frames to unauthorised stations unless they are
2079 * EAPOL frames from the local station.
2080 */
2081 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2082 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2083 !multicast && !authorized &&
2084 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2085 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2086 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2087 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2088 sdata->name, hdr.addr1);
2089 #endif
2090
2091 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2092
2093 ret = -EPERM;
2094 goto free;
2095 }
2096
2097 if (unlikely(!multicast && skb->sk &&
2098 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2099 struct sk_buff *ack_skb = skb_clone_sk(skb);
2100
2101 if (ack_skb) {
2102 unsigned long flags;
2103 int id;
2104
2105 spin_lock_irqsave(&local->ack_status_lock, flags);
2106 id = idr_alloc(&local->ack_status_frames, ack_skb,
2107 1, 0x10000, GFP_ATOMIC);
2108 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2109
2110 if (id >= 0) {
2111 info_id = id;
2112 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2113 } else {
2114 kfree_skb(ack_skb);
2115 }
2116 }
2117 }
2118
2119 /*
2120 * If the skb is shared we need to obtain our own copy.
2121 */
2122 if (skb_shared(skb)) {
2123 struct sk_buff *tmp_skb = skb;
2124
2125 /* can't happen -- skb is a clone if info_id != 0 */
2126 WARN_ON(info_id);
2127
2128 skb = skb_clone(skb, GFP_ATOMIC);
2129 kfree_skb(tmp_skb);
2130
2131 if (!skb) {
2132 ret = -ENOMEM;
2133 goto free;
2134 }
2135 }
2136
2137 hdr.frame_control = fc;
2138 hdr.duration_id = 0;
2139 hdr.seq_ctrl = 0;
2140
2141 skip_header_bytes = ETH_HLEN;
2142 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2143 encaps_data = bridge_tunnel_header;
2144 encaps_len = sizeof(bridge_tunnel_header);
2145 skip_header_bytes -= 2;
2146 } else if (ethertype >= ETH_P_802_3_MIN) {
2147 encaps_data = rfc1042_header;
2148 encaps_len = sizeof(rfc1042_header);
2149 skip_header_bytes -= 2;
2150 } else {
2151 encaps_data = NULL;
2152 encaps_len = 0;
2153 }
2154
2155 nh_pos = skb_network_header(skb) - skb->data;
2156 h_pos = skb_transport_header(skb) - skb->data;
2157
2158 skb_pull(skb, skip_header_bytes);
2159 nh_pos -= skip_header_bytes;
2160 h_pos -= skip_header_bytes;
2161
2162 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2163
2164 /*
2165 * So we need to modify the skb header and hence need a copy of
2166 * that. The head_need variable above doesn't, so far, include
2167 * the needed header space that we don't need right away. If we
2168 * can, then we don't reallocate right now but only after the
2169 * frame arrives at the master device (if it does...)
2170 *
2171 * If we cannot, however, then we will reallocate to include all
2172 * the ever needed space. Also, if we need to reallocate it anyway,
2173 * make it big enough for everything we may ever need.
2174 */
2175
2176 if (head_need > 0 || skb_cloned(skb)) {
2177 head_need += sdata->encrypt_headroom;
2178 head_need += local->tx_headroom;
2179 head_need = max_t(int, 0, head_need);
2180 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2181 ieee80211_free_txskb(&local->hw, skb);
2182 skb = NULL;
2183 return ERR_PTR(-ENOMEM);
2184 }
2185 }
2186
2187 if (encaps_data) {
2188 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2189 nh_pos += encaps_len;
2190 h_pos += encaps_len;
2191 }
2192
2193 #ifdef CONFIG_MAC80211_MESH
2194 if (meshhdrlen > 0) {
2195 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2196 nh_pos += meshhdrlen;
2197 h_pos += meshhdrlen;
2198 }
2199 #endif
2200
2201 if (ieee80211_is_data_qos(fc)) {
2202 __le16 *qos_control;
2203
2204 qos_control = (__le16 *) skb_push(skb, 2);
2205 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2206 /*
2207 * Maybe we could actually set some fields here, for now just
2208 * initialise to zero to indicate no special operation.
2209 */
2210 *qos_control = 0;
2211 } else
2212 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2213
2214 nh_pos += hdrlen;
2215 h_pos += hdrlen;
2216
2217 /* Update skb pointers to various headers since this modified frame
2218 * is going to go through Linux networking code that may potentially
2219 * need things like pointer to IP header. */
2220 skb_set_mac_header(skb, 0);
2221 skb_set_network_header(skb, nh_pos);
2222 skb_set_transport_header(skb, h_pos);
2223
2224 info = IEEE80211_SKB_CB(skb);
2225 memset(info, 0, sizeof(*info));
2226
2227 info->flags = info_flags;
2228 info->ack_frame_id = info_id;
2229 info->band = band;
2230
2231 return skb;
2232 free:
2233 kfree_skb(skb);
2234 return ERR_PTR(ret);
2235 }
2236
2237 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
2238 struct net_device *dev,
2239 u32 info_flags)
2240 {
2241 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2242 struct ieee80211_local *local = sdata->local;
2243
2244 if (unlikely(skb->len < ETH_HLEN)) {
2245 kfree_skb(skb);
2246 return;
2247 }
2248
2249 rcu_read_lock();
2250
2251 /* Measure frame arrival for Tx latency statistics calculation */
2252 ieee80211_tx_latency_start_msrmnt(local, skb);
2253
2254 skb = ieee80211_build_hdr(sdata, skb, info_flags);
2255 if (IS_ERR(skb))
2256 goto out;
2257
2258 dev->stats.tx_packets++;
2259 dev->stats.tx_bytes += skb->len;
2260 dev->trans_start = jiffies;
2261
2262 ieee80211_xmit(sdata, skb);
2263 out:
2264 rcu_read_unlock();
2265 }
2266
2267 /**
2268 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
2269 * @skb: packet to be sent
2270 * @dev: incoming interface
2271 *
2272 * On failure skb will be freed.
2273 */
2274 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
2275 struct net_device *dev)
2276 {
2277 __ieee80211_subif_start_xmit(skb, dev, 0);
2278 return NETDEV_TX_OK;
2279 }
2280
2281 struct sk_buff *
2282 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
2283 struct sk_buff *skb, u32 info_flags)
2284 {
2285 struct ieee80211_hdr *hdr;
2286 struct ieee80211_tx_data tx = {
2287 .local = sdata->local,
2288 .sdata = sdata,
2289 };
2290
2291 rcu_read_lock();
2292
2293 skb = ieee80211_build_hdr(sdata, skb, info_flags);
2294 if (IS_ERR(skb))
2295 goto out;
2296
2297 hdr = (void *)skb->data;
2298 tx.sta = sta_info_get(sdata, hdr->addr1);
2299 tx.skb = skb;
2300
2301 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
2302 rcu_read_unlock();
2303 kfree_skb(skb);
2304 return ERR_PTR(-EINVAL);
2305 }
2306
2307 out:
2308 rcu_read_unlock();
2309 return skb;
2310 }
2311
2312 /*
2313 * ieee80211_clear_tx_pending may not be called in a context where
2314 * it is possible that it packets could come in again.
2315 */
2316 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
2317 {
2318 struct sk_buff *skb;
2319 int i;
2320
2321 for (i = 0; i < local->hw.queues; i++) {
2322 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
2323 ieee80211_free_txskb(&local->hw, skb);
2324 }
2325 }
2326
2327 /*
2328 * Returns false if the frame couldn't be transmitted but was queued instead,
2329 * which in this case means re-queued -- take as an indication to stop sending
2330 * more pending frames.
2331 */
2332 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2333 struct sk_buff *skb)
2334 {
2335 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2336 struct ieee80211_sub_if_data *sdata;
2337 struct sta_info *sta;
2338 struct ieee80211_hdr *hdr;
2339 bool result;
2340 struct ieee80211_chanctx_conf *chanctx_conf;
2341
2342 sdata = vif_to_sdata(info->control.vif);
2343
2344 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
2345 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2346 if (unlikely(!chanctx_conf)) {
2347 dev_kfree_skb(skb);
2348 return true;
2349 }
2350 info->band = chanctx_conf->def.chan->band;
2351 result = ieee80211_tx(sdata, skb, true);
2352 } else {
2353 struct sk_buff_head skbs;
2354
2355 __skb_queue_head_init(&skbs);
2356 __skb_queue_tail(&skbs, skb);
2357
2358 hdr = (struct ieee80211_hdr *)skb->data;
2359 sta = sta_info_get(sdata, hdr->addr1);
2360
2361 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
2362 }
2363
2364 return result;
2365 }
2366
2367 /*
2368 * Transmit all pending packets. Called from tasklet.
2369 */
2370 void ieee80211_tx_pending(unsigned long data)
2371 {
2372 struct ieee80211_local *local = (struct ieee80211_local *)data;
2373 unsigned long flags;
2374 int i;
2375 bool txok;
2376
2377 rcu_read_lock();
2378
2379 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2380 for (i = 0; i < local->hw.queues; i++) {
2381 /*
2382 * If queue is stopped by something other than due to pending
2383 * frames, or we have no pending frames, proceed to next queue.
2384 */
2385 if (local->queue_stop_reasons[i] ||
2386 skb_queue_empty(&local->pending[i]))
2387 continue;
2388
2389 while (!skb_queue_empty(&local->pending[i])) {
2390 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
2391 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2392
2393 if (WARN_ON(!info->control.vif)) {
2394 ieee80211_free_txskb(&local->hw, skb);
2395 continue;
2396 }
2397
2398 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2399 flags);
2400
2401 txok = ieee80211_tx_pending_skb(local, skb);
2402 spin_lock_irqsave(&local->queue_stop_reason_lock,
2403 flags);
2404 if (!txok)
2405 break;
2406 }
2407
2408 if (skb_queue_empty(&local->pending[i]))
2409 ieee80211_propagate_queue_wake(local, i);
2410 }
2411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2412
2413 rcu_read_unlock();
2414 }
2415
2416 /* functions for drivers to get certain frames */
2417
2418 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2419 struct ps_data *ps, struct sk_buff *skb,
2420 bool is_template)
2421 {
2422 u8 *pos, *tim;
2423 int aid0 = 0;
2424 int i, have_bits = 0, n1, n2;
2425
2426 /* Generate bitmap for TIM only if there are any STAs in power save
2427 * mode. */
2428 if (atomic_read(&ps->num_sta_ps) > 0)
2429 /* in the hope that this is faster than
2430 * checking byte-for-byte */
2431 have_bits = !bitmap_empty((unsigned long *)ps->tim,
2432 IEEE80211_MAX_AID+1);
2433 if (!is_template) {
2434 if (ps->dtim_count == 0)
2435 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
2436 else
2437 ps->dtim_count--;
2438 }
2439
2440 tim = pos = (u8 *) skb_put(skb, 6);
2441 *pos++ = WLAN_EID_TIM;
2442 *pos++ = 4;
2443 *pos++ = ps->dtim_count;
2444 *pos++ = sdata->vif.bss_conf.dtim_period;
2445
2446 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
2447 aid0 = 1;
2448
2449 ps->dtim_bc_mc = aid0 == 1;
2450
2451 if (have_bits) {
2452 /* Find largest even number N1 so that bits numbered 1 through
2453 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2454 * (N2 + 1) x 8 through 2007 are 0. */
2455 n1 = 0;
2456 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2457 if (ps->tim[i]) {
2458 n1 = i & 0xfe;
2459 break;
2460 }
2461 }
2462 n2 = n1;
2463 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2464 if (ps->tim[i]) {
2465 n2 = i;
2466 break;
2467 }
2468 }
2469
2470 /* Bitmap control */
2471 *pos++ = n1 | aid0;
2472 /* Part Virt Bitmap */
2473 skb_put(skb, n2 - n1);
2474 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
2475
2476 tim[1] = n2 - n1 + 4;
2477 } else {
2478 *pos++ = aid0; /* Bitmap control */
2479 *pos++ = 0; /* Part Virt Bitmap */
2480 }
2481 }
2482
2483 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2484 struct ps_data *ps, struct sk_buff *skb,
2485 bool is_template)
2486 {
2487 struct ieee80211_local *local = sdata->local;
2488
2489 /*
2490 * Not very nice, but we want to allow the driver to call
2491 * ieee80211_beacon_get() as a response to the set_tim()
2492 * callback. That, however, is already invoked under the
2493 * sta_lock to guarantee consistent and race-free update
2494 * of the tim bitmap in mac80211 and the driver.
2495 */
2496 if (local->tim_in_locked_section) {
2497 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2498 } else {
2499 spin_lock_bh(&local->tim_lock);
2500 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2501 spin_unlock_bh(&local->tim_lock);
2502 }
2503
2504 return 0;
2505 }
2506
2507 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
2508 struct beacon_data *beacon)
2509 {
2510 struct probe_resp *resp;
2511 u8 *beacon_data;
2512 size_t beacon_data_len;
2513 int i;
2514 u8 count = beacon->csa_current_counter;
2515
2516 switch (sdata->vif.type) {
2517 case NL80211_IFTYPE_AP:
2518 beacon_data = beacon->tail;
2519 beacon_data_len = beacon->tail_len;
2520 break;
2521 case NL80211_IFTYPE_ADHOC:
2522 beacon_data = beacon->head;
2523 beacon_data_len = beacon->head_len;
2524 break;
2525 case NL80211_IFTYPE_MESH_POINT:
2526 beacon_data = beacon->head;
2527 beacon_data_len = beacon->head_len;
2528 break;
2529 default:
2530 return;
2531 }
2532
2533 rcu_read_lock();
2534 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
2535 resp = rcu_dereference(sdata->u.ap.probe_resp);
2536
2537 if (beacon->csa_counter_offsets[i]) {
2538 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
2539 beacon_data_len)) {
2540 rcu_read_unlock();
2541 return;
2542 }
2543
2544 beacon_data[beacon->csa_counter_offsets[i]] = count;
2545 }
2546
2547 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
2548 resp->data[resp->csa_counter_offsets[i]] = count;
2549 }
2550 rcu_read_unlock();
2551 }
2552
2553 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
2554 {
2555 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2556 struct beacon_data *beacon = NULL;
2557 u8 count = 0;
2558
2559 rcu_read_lock();
2560
2561 if (sdata->vif.type == NL80211_IFTYPE_AP)
2562 beacon = rcu_dereference(sdata->u.ap.beacon);
2563 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2564 beacon = rcu_dereference(sdata->u.ibss.presp);
2565 else if (ieee80211_vif_is_mesh(&sdata->vif))
2566 beacon = rcu_dereference(sdata->u.mesh.beacon);
2567
2568 if (!beacon)
2569 goto unlock;
2570
2571 beacon->csa_current_counter--;
2572
2573 /* the counter should never reach 0 */
2574 WARN_ON_ONCE(!beacon->csa_current_counter);
2575 count = beacon->csa_current_counter;
2576
2577 unlock:
2578 rcu_read_unlock();
2579 return count;
2580 }
2581 EXPORT_SYMBOL(ieee80211_csa_update_counter);
2582
2583 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2584 {
2585 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2586 struct beacon_data *beacon = NULL;
2587 u8 *beacon_data;
2588 size_t beacon_data_len;
2589 int ret = false;
2590
2591 if (!ieee80211_sdata_running(sdata))
2592 return false;
2593
2594 rcu_read_lock();
2595 if (vif->type == NL80211_IFTYPE_AP) {
2596 struct ieee80211_if_ap *ap = &sdata->u.ap;
2597
2598 beacon = rcu_dereference(ap->beacon);
2599 if (WARN_ON(!beacon || !beacon->tail))
2600 goto out;
2601 beacon_data = beacon->tail;
2602 beacon_data_len = beacon->tail_len;
2603 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2604 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2605
2606 beacon = rcu_dereference(ifibss->presp);
2607 if (!beacon)
2608 goto out;
2609
2610 beacon_data = beacon->head;
2611 beacon_data_len = beacon->head_len;
2612 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2613 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2614
2615 beacon = rcu_dereference(ifmsh->beacon);
2616 if (!beacon)
2617 goto out;
2618
2619 beacon_data = beacon->head;
2620 beacon_data_len = beacon->head_len;
2621 } else {
2622 WARN_ON(1);
2623 goto out;
2624 }
2625
2626 if (!beacon->csa_counter_offsets[0])
2627 goto out;
2628
2629 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
2630 goto out;
2631
2632 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
2633 ret = true;
2634 out:
2635 rcu_read_unlock();
2636
2637 return ret;
2638 }
2639 EXPORT_SYMBOL(ieee80211_csa_is_complete);
2640
2641 static struct sk_buff *
2642 __ieee80211_beacon_get(struct ieee80211_hw *hw,
2643 struct ieee80211_vif *vif,
2644 struct ieee80211_mutable_offsets *offs,
2645 bool is_template)
2646 {
2647 struct ieee80211_local *local = hw_to_local(hw);
2648 struct beacon_data *beacon = NULL;
2649 struct sk_buff *skb = NULL;
2650 struct ieee80211_tx_info *info;
2651 struct ieee80211_sub_if_data *sdata = NULL;
2652 enum ieee80211_band band;
2653 struct ieee80211_tx_rate_control txrc;
2654 struct ieee80211_chanctx_conf *chanctx_conf;
2655 int csa_off_base = 0;
2656
2657 rcu_read_lock();
2658
2659 sdata = vif_to_sdata(vif);
2660 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2661
2662 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
2663 goto out;
2664
2665 if (offs)
2666 memset(offs, 0, sizeof(*offs));
2667
2668 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2669 struct ieee80211_if_ap *ap = &sdata->u.ap;
2670
2671 beacon = rcu_dereference(ap->beacon);
2672 if (beacon) {
2673 if (beacon->csa_counter_offsets[0]) {
2674 if (!is_template)
2675 ieee80211_csa_update_counter(vif);
2676
2677 ieee80211_set_csa(sdata, beacon);
2678 }
2679
2680 /*
2681 * headroom, head length,
2682 * tail length and maximum TIM length
2683 */
2684 skb = dev_alloc_skb(local->tx_headroom +
2685 beacon->head_len +
2686 beacon->tail_len + 256 +
2687 local->hw.extra_beacon_tailroom);
2688 if (!skb)
2689 goto out;
2690
2691 skb_reserve(skb, local->tx_headroom);
2692 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2693 beacon->head_len);
2694
2695 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
2696 is_template);
2697
2698 if (offs) {
2699 offs->tim_offset = beacon->head_len;
2700 offs->tim_length = skb->len - beacon->head_len;
2701
2702 /* for AP the csa offsets are from tail */
2703 csa_off_base = skb->len;
2704 }
2705
2706 if (beacon->tail)
2707 memcpy(skb_put(skb, beacon->tail_len),
2708 beacon->tail, beacon->tail_len);
2709 } else
2710 goto out;
2711 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2712 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2713 struct ieee80211_hdr *hdr;
2714
2715 beacon = rcu_dereference(ifibss->presp);
2716 if (!beacon)
2717 goto out;
2718
2719 if (beacon->csa_counter_offsets[0]) {
2720 if (!is_template)
2721 ieee80211_csa_update_counter(vif);
2722
2723 ieee80211_set_csa(sdata, beacon);
2724 }
2725
2726 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
2727 local->hw.extra_beacon_tailroom);
2728 if (!skb)
2729 goto out;
2730 skb_reserve(skb, local->tx_headroom);
2731 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2732 beacon->head_len);
2733
2734 hdr = (struct ieee80211_hdr *) skb->data;
2735 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2736 IEEE80211_STYPE_BEACON);
2737 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2738 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2739
2740 beacon = rcu_dereference(ifmsh->beacon);
2741 if (!beacon)
2742 goto out;
2743
2744 if (beacon->csa_counter_offsets[0]) {
2745 if (!is_template)
2746 /* TODO: For mesh csa_counter is in TU, so
2747 * decrementing it by one isn't correct, but
2748 * for now we leave it consistent with overall
2749 * mac80211's behavior.
2750 */
2751 ieee80211_csa_update_counter(vif);
2752
2753 ieee80211_set_csa(sdata, beacon);
2754 }
2755
2756 if (ifmsh->sync_ops)
2757 ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
2758
2759 skb = dev_alloc_skb(local->tx_headroom +
2760 beacon->head_len +
2761 256 + /* TIM IE */
2762 beacon->tail_len +
2763 local->hw.extra_beacon_tailroom);
2764 if (!skb)
2765 goto out;
2766 skb_reserve(skb, local->tx_headroom);
2767 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2768 beacon->head_len);
2769 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
2770
2771 if (offs) {
2772 offs->tim_offset = beacon->head_len;
2773 offs->tim_length = skb->len - beacon->head_len;
2774 }
2775
2776 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
2777 beacon->tail_len);
2778 } else {
2779 WARN_ON(1);
2780 goto out;
2781 }
2782
2783 /* CSA offsets */
2784 if (offs && beacon) {
2785 int i;
2786
2787 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
2788 u16 csa_off = beacon->csa_counter_offsets[i];
2789
2790 if (!csa_off)
2791 continue;
2792
2793 offs->csa_counter_offs[i] = csa_off_base + csa_off;
2794 }
2795 }
2796
2797 band = chanctx_conf->def.chan->band;
2798
2799 info = IEEE80211_SKB_CB(skb);
2800
2801 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2802 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2803 info->band = band;
2804
2805 memset(&txrc, 0, sizeof(txrc));
2806 txrc.hw = hw;
2807 txrc.sband = local->hw.wiphy->bands[band];
2808 txrc.bss_conf = &sdata->vif.bss_conf;
2809 txrc.skb = skb;
2810 txrc.reported_rate.idx = -1;
2811 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2812 if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
2813 txrc.max_rate_idx = -1;
2814 else
2815 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
2816 txrc.bss = true;
2817 rate_control_get_rate(sdata, NULL, &txrc);
2818
2819 info->control.vif = vif;
2820
2821 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
2822 IEEE80211_TX_CTL_ASSIGN_SEQ |
2823 IEEE80211_TX_CTL_FIRST_FRAGMENT;
2824 out:
2825 rcu_read_unlock();
2826 return skb;
2827
2828 }
2829
2830 struct sk_buff *
2831 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
2832 struct ieee80211_vif *vif,
2833 struct ieee80211_mutable_offsets *offs)
2834 {
2835 return __ieee80211_beacon_get(hw, vif, offs, true);
2836 }
2837 EXPORT_SYMBOL(ieee80211_beacon_get_template);
2838
2839 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2840 struct ieee80211_vif *vif,
2841 u16 *tim_offset, u16 *tim_length)
2842 {
2843 struct ieee80211_mutable_offsets offs = {};
2844 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
2845
2846 if (tim_offset)
2847 *tim_offset = offs.tim_offset;
2848
2849 if (tim_length)
2850 *tim_length = offs.tim_length;
2851
2852 return bcn;
2853 }
2854 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
2855
2856 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
2857 struct ieee80211_vif *vif)
2858 {
2859 struct ieee80211_if_ap *ap = NULL;
2860 struct sk_buff *skb = NULL;
2861 struct probe_resp *presp = NULL;
2862 struct ieee80211_hdr *hdr;
2863 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2864
2865 if (sdata->vif.type != NL80211_IFTYPE_AP)
2866 return NULL;
2867
2868 rcu_read_lock();
2869
2870 ap = &sdata->u.ap;
2871 presp = rcu_dereference(ap->probe_resp);
2872 if (!presp)
2873 goto out;
2874
2875 skb = dev_alloc_skb(presp->len);
2876 if (!skb)
2877 goto out;
2878
2879 memcpy(skb_put(skb, presp->len), presp->data, presp->len);
2880
2881 hdr = (struct ieee80211_hdr *) skb->data;
2882 memset(hdr->addr1, 0, sizeof(hdr->addr1));
2883
2884 out:
2885 rcu_read_unlock();
2886 return skb;
2887 }
2888 EXPORT_SYMBOL(ieee80211_proberesp_get);
2889
2890 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2891 struct ieee80211_vif *vif)
2892 {
2893 struct ieee80211_sub_if_data *sdata;
2894 struct ieee80211_if_managed *ifmgd;
2895 struct ieee80211_pspoll *pspoll;
2896 struct ieee80211_local *local;
2897 struct sk_buff *skb;
2898
2899 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2900 return NULL;
2901
2902 sdata = vif_to_sdata(vif);
2903 ifmgd = &sdata->u.mgd;
2904 local = sdata->local;
2905
2906 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2907 if (!skb)
2908 return NULL;
2909
2910 skb_reserve(skb, local->hw.extra_tx_headroom);
2911
2912 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2913 memset(pspoll, 0, sizeof(*pspoll));
2914 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2915 IEEE80211_STYPE_PSPOLL);
2916 pspoll->aid = cpu_to_le16(ifmgd->aid);
2917
2918 /* aid in PS-Poll has its two MSBs each set to 1 */
2919 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2920
2921 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2922 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2923
2924 return skb;
2925 }
2926 EXPORT_SYMBOL(ieee80211_pspoll_get);
2927
2928 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2929 struct ieee80211_vif *vif)
2930 {
2931 struct ieee80211_hdr_3addr *nullfunc;
2932 struct ieee80211_sub_if_data *sdata;
2933 struct ieee80211_if_managed *ifmgd;
2934 struct ieee80211_local *local;
2935 struct sk_buff *skb;
2936
2937 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2938 return NULL;
2939
2940 sdata = vif_to_sdata(vif);
2941 ifmgd = &sdata->u.mgd;
2942 local = sdata->local;
2943
2944 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2945 if (!skb)
2946 return NULL;
2947
2948 skb_reserve(skb, local->hw.extra_tx_headroom);
2949
2950 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2951 sizeof(*nullfunc));
2952 memset(nullfunc, 0, sizeof(*nullfunc));
2953 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2954 IEEE80211_STYPE_NULLFUNC |
2955 IEEE80211_FCTL_TODS);
2956 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2957 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2958 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2959
2960 return skb;
2961 }
2962 EXPORT_SYMBOL(ieee80211_nullfunc_get);
2963
2964 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2965 const u8 *src_addr,
2966 const u8 *ssid, size_t ssid_len,
2967 size_t tailroom)
2968 {
2969 struct ieee80211_local *local = hw_to_local(hw);
2970 struct ieee80211_hdr_3addr *hdr;
2971 struct sk_buff *skb;
2972 size_t ie_ssid_len;
2973 u8 *pos;
2974
2975 ie_ssid_len = 2 + ssid_len;
2976
2977 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2978 ie_ssid_len + tailroom);
2979 if (!skb)
2980 return NULL;
2981
2982 skb_reserve(skb, local->hw.extra_tx_headroom);
2983
2984 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2985 memset(hdr, 0, sizeof(*hdr));
2986 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2987 IEEE80211_STYPE_PROBE_REQ);
2988 eth_broadcast_addr(hdr->addr1);
2989 memcpy(hdr->addr2, src_addr, ETH_ALEN);
2990 eth_broadcast_addr(hdr->addr3);
2991
2992 pos = skb_put(skb, ie_ssid_len);
2993 *pos++ = WLAN_EID_SSID;
2994 *pos++ = ssid_len;
2995 if (ssid_len)
2996 memcpy(pos, ssid, ssid_len);
2997 pos += ssid_len;
2998
2999 return skb;
3000 }
3001 EXPORT_SYMBOL(ieee80211_probereq_get);
3002
3003 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3004 const void *frame, size_t frame_len,
3005 const struct ieee80211_tx_info *frame_txctl,
3006 struct ieee80211_rts *rts)
3007 {
3008 const struct ieee80211_hdr *hdr = frame;
3009
3010 rts->frame_control =
3011 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
3012 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
3013 frame_txctl);
3014 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
3015 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
3016 }
3017 EXPORT_SYMBOL(ieee80211_rts_get);
3018
3019 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3020 const void *frame, size_t frame_len,
3021 const struct ieee80211_tx_info *frame_txctl,
3022 struct ieee80211_cts *cts)
3023 {
3024 const struct ieee80211_hdr *hdr = frame;
3025
3026 cts->frame_control =
3027 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
3028 cts->duration = ieee80211_ctstoself_duration(hw, vif,
3029 frame_len, frame_txctl);
3030 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
3031 }
3032 EXPORT_SYMBOL(ieee80211_ctstoself_get);
3033
3034 struct sk_buff *
3035 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
3036 struct ieee80211_vif *vif)
3037 {
3038 struct ieee80211_local *local = hw_to_local(hw);
3039 struct sk_buff *skb = NULL;
3040 struct ieee80211_tx_data tx;
3041 struct ieee80211_sub_if_data *sdata;
3042 struct ps_data *ps;
3043 struct ieee80211_tx_info *info;
3044 struct ieee80211_chanctx_conf *chanctx_conf;
3045
3046 sdata = vif_to_sdata(vif);
3047
3048 rcu_read_lock();
3049 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3050
3051 if (!chanctx_conf)
3052 goto out;
3053
3054 if (sdata->vif.type == NL80211_IFTYPE_AP) {
3055 struct beacon_data *beacon =
3056 rcu_dereference(sdata->u.ap.beacon);
3057
3058 if (!beacon || !beacon->head)
3059 goto out;
3060
3061 ps = &sdata->u.ap.ps;
3062 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3063 ps = &sdata->u.mesh.ps;
3064 } else {
3065 goto out;
3066 }
3067
3068 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
3069 goto out; /* send buffered bc/mc only after DTIM beacon */
3070
3071 while (1) {
3072 skb = skb_dequeue(&ps->bc_buf);
3073 if (!skb)
3074 goto out;
3075 local->total_ps_buffered--;
3076
3077 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
3078 struct ieee80211_hdr *hdr =
3079 (struct ieee80211_hdr *) skb->data;
3080 /* more buffered multicast/broadcast frames ==> set
3081 * MoreData flag in IEEE 802.11 header to inform PS
3082 * STAs */
3083 hdr->frame_control |=
3084 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
3085 }
3086
3087 if (sdata->vif.type == NL80211_IFTYPE_AP)
3088 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
3089 if (!ieee80211_tx_prepare(sdata, &tx, skb))
3090 break;
3091 dev_kfree_skb_any(skb);
3092 }
3093
3094 info = IEEE80211_SKB_CB(skb);
3095
3096 tx.flags |= IEEE80211_TX_PS_BUFFERED;
3097 info->band = chanctx_conf->def.chan->band;
3098
3099 if (invoke_tx_handlers(&tx))
3100 skb = NULL;
3101 out:
3102 rcu_read_unlock();
3103
3104 return skb;
3105 }
3106 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
3107
3108 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
3109 {
3110 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3111 struct ieee80211_sub_if_data *sdata = sta->sdata;
3112 struct ieee80211_local *local = sdata->local;
3113 int ret;
3114 u32 queues;
3115
3116 lockdep_assert_held(&local->sta_mtx);
3117
3118 /* only some cases are supported right now */
3119 switch (sdata->vif.type) {
3120 case NL80211_IFTYPE_STATION:
3121 case NL80211_IFTYPE_AP:
3122 case NL80211_IFTYPE_AP_VLAN:
3123 break;
3124 default:
3125 WARN_ON(1);
3126 return -EINVAL;
3127 }
3128
3129 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
3130 return -EINVAL;
3131
3132 if (sta->reserved_tid == tid) {
3133 ret = 0;
3134 goto out;
3135 }
3136
3137 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
3138 sdata_err(sdata, "TID reservation already active\n");
3139 ret = -EALREADY;
3140 goto out;
3141 }
3142
3143 ieee80211_stop_vif_queues(sdata->local, sdata,
3144 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
3145
3146 synchronize_net();
3147
3148 /* Tear down BA sessions so we stop aggregating on this TID */
3149 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) {
3150 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
3151 __ieee80211_stop_tx_ba_session(sta, tid,
3152 AGG_STOP_LOCAL_REQUEST);
3153 }
3154
3155 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
3156 __ieee80211_flush_queues(local, sdata, queues);
3157
3158 sta->reserved_tid = tid;
3159
3160 ieee80211_wake_vif_queues(local, sdata,
3161 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
3162
3163 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)
3164 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
3165
3166 ret = 0;
3167 out:
3168 return ret;
3169 }
3170 EXPORT_SYMBOL(ieee80211_reserve_tid);
3171
3172 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
3173 {
3174 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3175 struct ieee80211_sub_if_data *sdata = sta->sdata;
3176
3177 lockdep_assert_held(&sdata->local->sta_mtx);
3178
3179 /* only some cases are supported right now */
3180 switch (sdata->vif.type) {
3181 case NL80211_IFTYPE_STATION:
3182 case NL80211_IFTYPE_AP:
3183 case NL80211_IFTYPE_AP_VLAN:
3184 break;
3185 default:
3186 WARN_ON(1);
3187 return;
3188 }
3189
3190 if (tid != sta->reserved_tid) {
3191 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
3192 return;
3193 }
3194
3195 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
3196 }
3197 EXPORT_SYMBOL(ieee80211_unreserve_tid);
3198
3199 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
3200 struct sk_buff *skb, int tid,
3201 enum ieee80211_band band)
3202 {
3203 int ac = ieee802_1d_to_ac[tid & 7];
3204
3205 skb_set_mac_header(skb, 0);
3206 skb_set_network_header(skb, 0);
3207 skb_set_transport_header(skb, 0);
3208
3209 skb_set_queue_mapping(skb, ac);
3210 skb->priority = tid;
3211
3212 skb->dev = sdata->dev;
3213
3214 /*
3215 * The other path calling ieee80211_xmit is from the tasklet,
3216 * and while we can handle concurrent transmissions locking
3217 * requirements are that we do not come into tx with bhs on.
3218 */
3219 local_bh_disable();
3220 IEEE80211_SKB_CB(skb)->band = band;
3221 ieee80211_xmit(sdata, skb);
3222 local_bh_enable();
3223 }