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