mac80211: remove "dynamic" RX/TX handlers
[linux-2.6] / 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 <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <asm/unaligned.h>
26
27 #include "ieee80211_i.h"
28 #include "ieee80211_led.h"
29 #include "wep.h"
30 #include "wpa.h"
31 #include "wme.h"
32 #include "ieee80211_rate.h"
33
34 #define IEEE80211_TX_OK         0
35 #define IEEE80211_TX_AGAIN      1
36 #define IEEE80211_TX_FRAG_AGAIN 2
37
38 /* misc utils */
39
40 static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
41                                               struct ieee80211_hdr *hdr)
42 {
43         /* Set the sequence number for this frame. */
44         hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
45
46         /* Increase the sequence number. */
47         sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
48 }
49
50 #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
51 static void ieee80211_dump_frame(const char *ifname, const char *title,
52                                  const struct sk_buff *skb)
53 {
54         const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
55         u16 fc;
56         int hdrlen;
57         DECLARE_MAC_BUF(mac);
58
59         printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
60         if (skb->len < 4) {
61                 printk("\n");
62                 return;
63         }
64
65         fc = le16_to_cpu(hdr->frame_control);
66         hdrlen = ieee80211_get_hdrlen(fc);
67         if (hdrlen > skb->len)
68                 hdrlen = skb->len;
69         if (hdrlen >= 4)
70                 printk(" FC=0x%04x DUR=0x%04x",
71                        fc, le16_to_cpu(hdr->duration_id));
72         if (hdrlen >= 10)
73                 printk(" A1=%s", print_mac(mac, hdr->addr1));
74         if (hdrlen >= 16)
75                 printk(" A2=%s", print_mac(mac, hdr->addr2));
76         if (hdrlen >= 24)
77                 printk(" A3=%s", print_mac(mac, hdr->addr3));
78         if (hdrlen >= 30)
79                 printk(" A4=%s", print_mac(mac, hdr->addr4));
80         printk("\n");
81 }
82 #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
83 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
84                                         struct sk_buff *skb)
85 {
86 }
87 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
88
89 static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
90                               int next_frag_len)
91 {
92         int rate, mrate, erp, dur, i;
93         struct ieee80211_rate *txrate = tx->u.tx.rate;
94         struct ieee80211_local *local = tx->local;
95         struct ieee80211_supported_band *sband;
96
97         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
98
99         erp = 0;
100         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
101                 erp = txrate->flags & IEEE80211_RATE_ERP_G;
102
103         /*
104          * data and mgmt (except PS Poll):
105          * - during CFP: 32768
106          * - during contention period:
107          *   if addr1 is group address: 0
108          *   if more fragments = 0 and addr1 is individual address: time to
109          *      transmit one ACK plus SIFS
110          *   if more fragments = 1 and addr1 is individual address: time to
111          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
112          *
113          * IEEE 802.11, 9.6:
114          * - control response frame (CTS or ACK) shall be transmitted using the
115          *   same rate as the immediately previous frame in the frame exchange
116          *   sequence, if this rate belongs to the PHY mandatory rates, or else
117          *   at the highest possible rate belonging to the PHY rates in the
118          *   BSSBasicRateSet
119          */
120
121         if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
122                 /* TODO: These control frames are not currently sent by
123                  * 80211.o, but should they be implemented, this function
124                  * needs to be updated to support duration field calculation.
125                  *
126                  * RTS: time needed to transmit pending data/mgmt frame plus
127                  *    one CTS frame plus one ACK frame plus 3 x SIFS
128                  * CTS: duration of immediately previous RTS minus time
129                  *    required to transmit CTS and its SIFS
130                  * ACK: 0 if immediately previous directed data/mgmt had
131                  *    more=0, with more=1 duration in ACK frame is duration
132                  *    from previous frame minus time needed to transmit ACK
133                  *    and its SIFS
134                  * PS Poll: BIT(15) | BIT(14) | aid
135                  */
136                 return 0;
137         }
138
139         /* data/mgmt */
140         if (0 /* FIX: data/mgmt during CFP */)
141                 return 32768;
142
143         if (group_addr) /* Group address as the destination - no ACK */
144                 return 0;
145
146         /* Individual destination address:
147          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
148          * CTS and ACK frames shall be transmitted using the highest rate in
149          * basic rate set that is less than or equal to the rate of the
150          * immediately previous frame and that is using the same modulation
151          * (CCK or OFDM). If no basic rate set matches with these requirements,
152          * the highest mandatory rate of the PHY that is less than or equal to
153          * the rate of the previous frame is used.
154          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
155          */
156         rate = -1;
157         /* use lowest available if everything fails */
158         mrate = sband->bitrates[0].bitrate;
159         for (i = 0; i < sband->n_bitrates; i++) {
160                 struct ieee80211_rate *r = &sband->bitrates[i];
161
162                 if (r->bitrate > txrate->bitrate)
163                         break;
164
165                 if (tx->sdata->basic_rates & BIT(i))
166                         rate = r->bitrate;
167
168                 switch (sband->band) {
169                 case IEEE80211_BAND_2GHZ: {
170                         u32 flag;
171                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
172                                 flag = IEEE80211_RATE_MANDATORY_G;
173                         else
174                                 flag = IEEE80211_RATE_MANDATORY_B;
175                         if (r->flags & flag)
176                                 mrate = r->bitrate;
177                         break;
178                 }
179                 case IEEE80211_BAND_5GHZ:
180                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
181                                 mrate = r->bitrate;
182                         break;
183                 case IEEE80211_NUM_BANDS:
184                         WARN_ON(1);
185                         break;
186                 }
187         }
188         if (rate == -1) {
189                 /* No matching basic rate found; use highest suitable mandatory
190                  * PHY rate */
191                 rate = mrate;
192         }
193
194         /* Time needed to transmit ACK
195          * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
196          * to closest integer */
197
198         dur = ieee80211_frame_duration(local, 10, rate, erp,
199                                 tx->sdata->bss_conf.use_short_preamble);
200
201         if (next_frag_len) {
202                 /* Frame is fragmented: duration increases with time needed to
203                  * transmit next fragment plus ACK and 2 x SIFS. */
204                 dur *= 2; /* ACK + SIFS */
205                 /* next fragment */
206                 dur += ieee80211_frame_duration(local, next_frag_len,
207                                 txrate->bitrate, erp,
208                                 tx->sdata->bss_conf.use_short_preamble);
209         }
210
211         return dur;
212 }
213
214 static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
215                                             int queue)
216 {
217         return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
218 }
219
220 static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
221                                             int queue)
222 {
223         return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
224 }
225
226 static int inline is_ieee80211_device(struct net_device *dev,
227                                       struct net_device *master)
228 {
229         return (wdev_priv(dev->ieee80211_ptr) ==
230                 wdev_priv(master->ieee80211_ptr));
231 }
232
233 /* tx handlers */
234
235 static ieee80211_tx_result
236 ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
237 {
238 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
239         struct sk_buff *skb = tx->skb;
240         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
241 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
242         u32 sta_flags;
243
244         if (unlikely(tx->flags & IEEE80211_TXRXD_TX_INJECTED))
245                 return TX_CONTINUE;
246
247         if (unlikely(tx->local->sta_sw_scanning) &&
248             ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
249              (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
250                 return TX_DROP;
251
252         if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
253                 return TX_CONTINUE;
254
255         sta_flags = tx->sta ? tx->sta->flags : 0;
256
257         if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
258                 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
259                              tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
260                              (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
261 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
262                         DECLARE_MAC_BUF(mac);
263                         printk(KERN_DEBUG "%s: dropped data frame to not "
264                                "associated station %s\n",
265                                tx->dev->name, print_mac(mac, hdr->addr1));
266 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
267                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
268                         return TX_DROP;
269                 }
270         } else {
271                 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
272                              tx->local->num_sta == 0 &&
273                              tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
274                         /*
275                          * No associated STAs - no need to send multicast
276                          * frames.
277                          */
278                         return TX_DROP;
279                 }
280                 return TX_CONTINUE;
281         }
282
283         return TX_CONTINUE;
284 }
285
286 static ieee80211_tx_result
287 ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
288 {
289         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
290
291         if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
292                 ieee80211_include_sequence(tx->sdata, hdr);
293
294         return TX_CONTINUE;
295 }
296
297 /* This function is called whenever the AP is about to exceed the maximum limit
298  * of buffered frames for power saving STAs. This situation should not really
299  * happen often during normal operation, so dropping the oldest buffered packet
300  * from each queue should be OK to make some room for new frames. */
301 static void purge_old_ps_buffers(struct ieee80211_local *local)
302 {
303         int total = 0, purged = 0;
304         struct sk_buff *skb;
305         struct ieee80211_sub_if_data *sdata;
306         struct sta_info *sta;
307
308         /*
309          * virtual interfaces are protected by RCU
310          */
311         rcu_read_lock();
312
313         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
314                 struct ieee80211_if_ap *ap;
315                 if (sdata->dev == local->mdev ||
316                     sdata->vif.type != IEEE80211_IF_TYPE_AP)
317                         continue;
318                 ap = &sdata->u.ap;
319                 skb = skb_dequeue(&ap->ps_bc_buf);
320                 if (skb) {
321                         purged++;
322                         dev_kfree_skb(skb);
323                 }
324                 total += skb_queue_len(&ap->ps_bc_buf);
325         }
326         rcu_read_unlock();
327
328         read_lock_bh(&local->sta_lock);
329         list_for_each_entry(sta, &local->sta_list, list) {
330                 skb = skb_dequeue(&sta->ps_tx_buf);
331                 if (skb) {
332                         purged++;
333                         dev_kfree_skb(skb);
334                 }
335                 total += skb_queue_len(&sta->ps_tx_buf);
336         }
337         read_unlock_bh(&local->sta_lock);
338
339         local->total_ps_buffered = total;
340         printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
341                wiphy_name(local->hw.wiphy), purged);
342 }
343
344 static ieee80211_tx_result
345 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
346 {
347         /*
348          * broadcast/multicast frame
349          *
350          * If any of the associated stations is in power save mode,
351          * the frame is buffered to be sent after DTIM beacon frame.
352          * This is done either by the hardware or us.
353          */
354
355         /* not AP/IBSS or ordered frame */
356         if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
357                 return TX_CONTINUE;
358
359         /* no stations in PS mode */
360         if (!atomic_read(&tx->sdata->bss->num_sta_ps))
361                 return TX_CONTINUE;
362
363         /* buffered in mac80211 */
364         if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
365                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
366                         purge_old_ps_buffers(tx->local);
367                 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
368                     AP_MAX_BC_BUFFER) {
369                         if (net_ratelimit()) {
370                                 printk(KERN_DEBUG "%s: BC TX buffer full - "
371                                        "dropping the oldest frame\n",
372                                        tx->dev->name);
373                         }
374                         dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
375                 } else
376                         tx->local->total_ps_buffered++;
377                 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
378                 return TX_QUEUED;
379         }
380
381         /* buffered in hardware */
382         tx->u.tx.control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
383
384         return TX_CONTINUE;
385 }
386
387 static ieee80211_tx_result
388 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
389 {
390         struct sta_info *sta = tx->sta;
391         DECLARE_MAC_BUF(mac);
392
393         if (unlikely(!sta ||
394                      ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
395                       (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
396                 return TX_CONTINUE;
397
398         if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
399                 struct ieee80211_tx_packet_data *pkt_data;
400 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
401                 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
402                        "before %d)\n",
403                        print_mac(mac, sta->addr), sta->aid,
404                        skb_queue_len(&sta->ps_tx_buf));
405 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
406                 sta->flags |= WLAN_STA_TIM;
407                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
408                         purge_old_ps_buffers(tx->local);
409                 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
410                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
411                         if (net_ratelimit()) {
412                                 printk(KERN_DEBUG "%s: STA %s TX "
413                                        "buffer full - dropping oldest frame\n",
414                                        tx->dev->name, print_mac(mac, sta->addr));
415                         }
416                         dev_kfree_skb(old);
417                 } else
418                         tx->local->total_ps_buffered++;
419                 /* Queue frame to be sent after STA sends an PS Poll frame */
420                 if (skb_queue_empty(&sta->ps_tx_buf)) {
421                         if (tx->local->ops->set_tim)
422                                 tx->local->ops->set_tim(local_to_hw(tx->local),
423                                                        sta->aid, 1);
424                         if (tx->sdata->bss)
425                                 bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
426                 }
427                 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
428                 pkt_data->jiffies = jiffies;
429                 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
430                 return TX_QUEUED;
431         }
432 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
433         else if (unlikely(sta->flags & WLAN_STA_PS)) {
434                 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
435                        "set -> send frame\n", tx->dev->name,
436                        print_mac(mac, sta->addr));
437         }
438 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
439         sta->pspoll = 0;
440
441         return TX_CONTINUE;
442 }
443
444 static ieee80211_tx_result
445 ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
446 {
447         if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
448                 return TX_CONTINUE;
449
450         if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
451                 return ieee80211_tx_h_unicast_ps_buf(tx);
452         else
453                 return ieee80211_tx_h_multicast_ps_buf(tx);
454 }
455
456 static ieee80211_tx_result
457 ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
458 {
459         struct ieee80211_key *key;
460         u16 fc = tx->fc;
461
462         if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
463                 tx->key = NULL;
464         else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
465                 tx->key = key;
466         else if ((key = rcu_dereference(tx->sdata->default_key)))
467                 tx->key = key;
468         else if (tx->sdata->drop_unencrypted &&
469                  !(tx->u.tx.control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
470                  !(tx->flags & IEEE80211_TXRXD_TX_INJECTED)) {
471                 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
472                 return TX_DROP;
473         } else
474                 tx->key = NULL;
475
476         if (tx->key) {
477                 u16 ftype, stype;
478
479                 tx->key->tx_rx_count++;
480                 /* TODO: add threshold stuff again */
481
482                 switch (tx->key->conf.alg) {
483                 case ALG_WEP:
484                         ftype = fc & IEEE80211_FCTL_FTYPE;
485                         stype = fc & IEEE80211_FCTL_STYPE;
486
487                         if (ftype == IEEE80211_FTYPE_MGMT &&
488                             stype == IEEE80211_STYPE_AUTH)
489                                 break;
490                 case ALG_TKIP:
491                 case ALG_CCMP:
492                         if (!WLAN_FC_DATA_PRESENT(fc))
493                                 tx->key = NULL;
494                         break;
495                 }
496         }
497
498         if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
499                 tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
500
501         return TX_CONTINUE;
502 }
503
504 static ieee80211_tx_result
505 ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
506 {
507         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
508         size_t hdrlen, per_fragm, num_fragm, payload_len, left;
509         struct sk_buff **frags, *first, *frag;
510         int i;
511         u16 seq;
512         u8 *pos;
513         int frag_threshold = tx->local->fragmentation_threshold;
514
515         if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
516                 return TX_CONTINUE;
517
518         first = tx->skb;
519
520         hdrlen = ieee80211_get_hdrlen(tx->fc);
521         payload_len = first->len - hdrlen;
522         per_fragm = frag_threshold - hdrlen - FCS_LEN;
523         num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
524
525         frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
526         if (!frags)
527                 goto fail;
528
529         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
530         seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
531         pos = first->data + hdrlen + per_fragm;
532         left = payload_len - per_fragm;
533         for (i = 0; i < num_fragm - 1; i++) {
534                 struct ieee80211_hdr *fhdr;
535                 size_t copylen;
536
537                 if (left <= 0)
538                         goto fail;
539
540                 /* reserve enough extra head and tail room for possible
541                  * encryption */
542                 frag = frags[i] =
543                         dev_alloc_skb(tx->local->tx_headroom +
544                                       frag_threshold +
545                                       IEEE80211_ENCRYPT_HEADROOM +
546                                       IEEE80211_ENCRYPT_TAILROOM);
547                 if (!frag)
548                         goto fail;
549                 /* Make sure that all fragments use the same priority so
550                  * that they end up using the same TX queue */
551                 frag->priority = first->priority;
552                 skb_reserve(frag, tx->local->tx_headroom +
553                                   IEEE80211_ENCRYPT_HEADROOM);
554                 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
555                 memcpy(fhdr, first->data, hdrlen);
556                 if (i == num_fragm - 2)
557                         fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
558                 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
559                 copylen = left > per_fragm ? per_fragm : left;
560                 memcpy(skb_put(frag, copylen), pos, copylen);
561
562                 pos += copylen;
563                 left -= copylen;
564         }
565         skb_trim(first, hdrlen + per_fragm);
566
567         tx->u.tx.num_extra_frag = num_fragm - 1;
568         tx->u.tx.extra_frag = frags;
569
570         return TX_CONTINUE;
571
572  fail:
573         printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
574         if (frags) {
575                 for (i = 0; i < num_fragm - 1; i++)
576                         if (frags[i])
577                                 dev_kfree_skb(frags[i]);
578                 kfree(frags);
579         }
580         I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
581         return TX_DROP;
582 }
583
584 static ieee80211_tx_result
585 ieee80211_tx_h_encrypt(struct ieee80211_txrx_data *tx)
586 {
587         if (!tx->key)
588                 return TX_CONTINUE;
589
590         switch (tx->key->conf.alg) {
591         case ALG_WEP:
592                 return ieee80211_crypto_wep_encrypt(tx);
593         case ALG_TKIP:
594                 return ieee80211_crypto_tkip_encrypt(tx);
595         case ALG_CCMP:
596                 return ieee80211_crypto_ccmp_encrypt(tx);
597         }
598
599         /* not reached */
600         WARN_ON(1);
601         return TX_DROP;
602 }
603
604 static ieee80211_tx_result
605 ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
606 {
607         struct rate_selection rsel;
608         struct ieee80211_supported_band *sband;
609
610         sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
611
612         if (likely(!tx->u.tx.rate)) {
613                 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
614                 tx->u.tx.rate = rsel.rate;
615                 if (unlikely(rsel.probe)) {
616                         tx->u.tx.control->flags |=
617                                 IEEE80211_TXCTL_RATE_CTRL_PROBE;
618                         tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
619                         tx->u.tx.control->alt_retry_rate = tx->u.tx.rate;
620                         tx->u.tx.rate = rsel.probe;
621                 } else
622                         tx->u.tx.control->alt_retry_rate = NULL;
623
624                 if (!tx->u.tx.rate)
625                         return TX_DROP;
626         } else
627                 tx->u.tx.control->alt_retry_rate = NULL;
628
629         if (tx->sdata->bss_conf.use_cts_prot &&
630             (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && rsel.nonerp) {
631                 tx->u.tx.last_frag_rate = tx->u.tx.rate;
632                 if (rsel.probe)
633                         tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
634                 else
635                         tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
636                 tx->u.tx.rate = rsel.nonerp;
637                 tx->u.tx.control->tx_rate = rsel.nonerp;
638                 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
639         } else {
640                 tx->u.tx.last_frag_rate = tx->u.tx.rate;
641                 tx->u.tx.control->tx_rate = tx->u.tx.rate;
642         }
643         tx->u.tx.control->tx_rate = tx->u.tx.rate;
644
645         return TX_CONTINUE;
646 }
647
648 static ieee80211_tx_result
649 ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
650 {
651         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
652         u16 fc = le16_to_cpu(hdr->frame_control);
653         u16 dur;
654         struct ieee80211_tx_control *control = tx->u.tx.control;
655
656         if (!control->retry_limit) {
657                 if (!is_multicast_ether_addr(hdr->addr1)) {
658                         if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
659                             && tx->local->rts_threshold <
660                                         IEEE80211_MAX_RTS_THRESHOLD) {
661                                 control->flags |=
662                                         IEEE80211_TXCTL_USE_RTS_CTS;
663                                 control->flags |=
664                                         IEEE80211_TXCTL_LONG_RETRY_LIMIT;
665                                 control->retry_limit =
666                                         tx->local->long_retry_limit;
667                         } else {
668                                 control->retry_limit =
669                                         tx->local->short_retry_limit;
670                         }
671                 } else {
672                         control->retry_limit = 1;
673                 }
674         }
675
676         if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
677                 /* Do not use multiple retry rates when sending fragmented
678                  * frames.
679                  * TODO: The last fragment could still use multiple retry
680                  * rates. */
681                 control->alt_retry_rate = NULL;
682         }
683
684         /* Use CTS protection for unicast frames sent using extended rates if
685          * there are associated non-ERP stations and RTS/CTS is not configured
686          * for the frame. */
687         if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
688             (tx->u.tx.rate->flags & IEEE80211_RATE_ERP_G) &&
689             (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
690             tx->sdata->bss_conf.use_cts_prot &&
691             !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
692                 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
693
694         /* Transmit data frames using short preambles if the driver supports
695          * short preambles at the selected rate and short preambles are
696          * available on the network at the current point in time. */
697         if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
698             (tx->u.tx.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
699             tx->sdata->bss_conf.use_short_preamble &&
700             (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
701                 tx->u.tx.control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
702         }
703
704         /* Setup duration field for the first fragment of the frame. Duration
705          * for remaining fragments will be updated when they are being sent
706          * to low-level driver in ieee80211_tx(). */
707         dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
708                                  (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
709                                  tx->u.tx.extra_frag[0]->len : 0);
710         hdr->duration_id = cpu_to_le16(dur);
711
712         if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
713             (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
714                 struct ieee80211_supported_band *sband;
715                 struct ieee80211_rate *rate, *baserate;
716                 int idx;
717
718                 sband = tx->local->hw.wiphy->bands[
719                                 tx->local->hw.conf.channel->band];
720
721                 /* Do not use multiple retry rates when using RTS/CTS */
722                 control->alt_retry_rate = NULL;
723
724                 /* Use min(data rate, max base rate) as CTS/RTS rate */
725                 rate = tx->u.tx.rate;
726                 baserate = NULL;
727
728                 for (idx = 0; idx < sband->n_bitrates; idx++) {
729                         if (sband->bitrates[idx].bitrate > rate->bitrate)
730                                 continue;
731                         if (tx->sdata->basic_rates & BIT(idx) &&
732                             (!baserate ||
733                              (baserate->bitrate < sband->bitrates[idx].bitrate)))
734                                 baserate = &sband->bitrates[idx];
735                 }
736
737                 if (baserate)
738                         control->rts_cts_rate = baserate;
739                 else
740                         control->rts_cts_rate = &sband->bitrates[0];
741         }
742
743         if (tx->sta) {
744                 tx->sta->tx_packets++;
745                 tx->sta->tx_fragments++;
746                 tx->sta->tx_bytes += tx->skb->len;
747                 if (tx->u.tx.extra_frag) {
748                         int i;
749                         tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
750                         for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
751                                 tx->sta->tx_bytes +=
752                                         tx->u.tx.extra_frag[i]->len;
753                         }
754                 }
755         }
756
757         return TX_CONTINUE;
758 }
759
760 static ieee80211_tx_result
761 ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
762 {
763         struct ieee80211_local *local = tx->local;
764         struct sk_buff *skb = tx->skb;
765         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
766         u32 load = 0, hdrtime;
767         struct ieee80211_rate *rate = tx->u.tx.rate;
768
769         /* TODO: this could be part of tx_status handling, so that the number
770          * of retries would be known; TX rate should in that case be stored
771          * somewhere with the packet */
772
773         /* Estimate total channel use caused by this frame */
774
775         /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
776          * 1 usec = 1/8 * (1080 / 10) = 13.5 */
777
778         if (tx->u.tx.channel->band == IEEE80211_BAND_5GHZ ||
779             (tx->u.tx.channel->band == IEEE80211_BAND_2GHZ &&
780              rate->flags & IEEE80211_RATE_ERP_G))
781                 hdrtime = CHAN_UTIL_HDR_SHORT;
782         else
783                 hdrtime = CHAN_UTIL_HDR_LONG;
784
785         load = hdrtime;
786         if (!is_multicast_ether_addr(hdr->addr1))
787                 load += hdrtime;
788
789         if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
790                 load += 2 * hdrtime;
791         else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
792                 load += hdrtime;
793
794         /* TODO: optimise again */
795         load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
796
797         if (tx->u.tx.extra_frag) {
798                 int i;
799                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
800                         load += 2 * hdrtime;
801                         load += tx->u.tx.extra_frag[i]->len *
802                                 tx->u.tx.rate->bitrate;
803                 }
804         }
805
806         /* Divide channel_use by 8 to avoid wrapping around the counter */
807         load >>= CHAN_UTIL_SHIFT;
808         local->channel_use_raw += load;
809         if (tx->sta)
810                 tx->sta->channel_use_raw += load;
811         tx->sdata->channel_use_raw += load;
812
813         return TX_CONTINUE;
814 }
815
816
817 typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_txrx_data *);
818 static ieee80211_tx_handler ieee80211_tx_handlers[] =
819 {
820         ieee80211_tx_h_check_assoc,
821         ieee80211_tx_h_sequence,
822         ieee80211_tx_h_ps_buf,
823         ieee80211_tx_h_select_key,
824         ieee80211_tx_h_michael_mic_add,
825         ieee80211_tx_h_fragment,
826         ieee80211_tx_h_encrypt,
827         ieee80211_tx_h_rate_ctrl,
828         ieee80211_tx_h_misc,
829         ieee80211_tx_h_load_stats,
830         NULL
831 };
832
833 /* actual transmit path */
834
835 /*
836  * deal with packet injection down monitor interface
837  * with Radiotap Header -- only called for monitor mode interface
838  */
839 static ieee80211_tx_result
840 __ieee80211_parse_tx_radiotap(struct ieee80211_txrx_data *tx,
841                               struct sk_buff *skb)
842 {
843         /*
844          * this is the moment to interpret and discard the radiotap header that
845          * must be at the start of the packet injected in Monitor mode
846          *
847          * Need to take some care with endian-ness since radiotap
848          * args are little-endian
849          */
850
851         struct ieee80211_radiotap_iterator iterator;
852         struct ieee80211_radiotap_header *rthdr =
853                 (struct ieee80211_radiotap_header *) skb->data;
854         struct ieee80211_supported_band *sband;
855         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
856         struct ieee80211_tx_control *control = tx->u.tx.control;
857
858         sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
859
860         control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
861         tx->flags |= IEEE80211_TXRXD_TX_INJECTED;
862         tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
863
864         /*
865          * for every radiotap entry that is present
866          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
867          * entries present, or -EINVAL on error)
868          */
869
870         while (!ret) {
871                 int i, target_rate;
872
873                 ret = ieee80211_radiotap_iterator_next(&iterator);
874
875                 if (ret)
876                         continue;
877
878                 /* see if this argument is something we can use */
879                 switch (iterator.this_arg_index) {
880                 /*
881                  * You must take care when dereferencing iterator.this_arg
882                  * for multibyte types... the pointer is not aligned.  Use
883                  * get_unaligned((type *)iterator.this_arg) to dereference
884                  * iterator.this_arg for type "type" safely on all arches.
885                 */
886                 case IEEE80211_RADIOTAP_RATE:
887                         /*
888                          * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
889                          * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
890                          */
891                         target_rate = (*iterator.this_arg) * 5;
892                         for (i = 0; i < sband->n_bitrates; i++) {
893                                 struct ieee80211_rate *r;
894
895                                 r = &sband->bitrates[i];
896
897                                 if (r->bitrate == target_rate) {
898                                         tx->u.tx.rate = r;
899                                         break;
900                                 }
901                         }
902                         break;
903
904                 case IEEE80211_RADIOTAP_ANTENNA:
905                         /*
906                          * radiotap uses 0 for 1st ant, mac80211 is 1 for
907                          * 1st ant
908                          */
909                         control->antenna_sel_tx = (*iterator.this_arg) + 1;
910                         break;
911
912 #if 0
913                 case IEEE80211_RADIOTAP_DBM_TX_POWER:
914                         control->power_level = *iterator.this_arg;
915                         break;
916 #endif
917
918                 case IEEE80211_RADIOTAP_FLAGS:
919                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
920                                 /*
921                                  * this indicates that the skb we have been
922                                  * handed has the 32-bit FCS CRC at the end...
923                                  * we should react to that by snipping it off
924                                  * because it will be recomputed and added
925                                  * on transmission
926                                  */
927                                 if (skb->len < (iterator.max_length + FCS_LEN))
928                                         return TX_DROP;
929
930                                 skb_trim(skb, skb->len - FCS_LEN);
931                         }
932                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
933                                 control->flags &=
934                                         ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
935                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
936                                 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
937                         break;
938
939                 /*
940                  * Please update the file
941                  * Documentation/networking/mac80211-injection.txt
942                  * when parsing new fields here.
943                  */
944
945                 default:
946                         break;
947                 }
948         }
949
950         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
951                 return TX_DROP;
952
953         /*
954          * remove the radiotap header
955          * iterator->max_length was sanity-checked against
956          * skb->len by iterator init
957          */
958         skb_pull(skb, iterator.max_length);
959
960         return TX_CONTINUE;
961 }
962
963 /*
964  * initialises @tx
965  */
966 static ieee80211_tx_result
967 __ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
968                        struct sk_buff *skb,
969                        struct net_device *dev,
970                        struct ieee80211_tx_control *control)
971 {
972         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
973         struct ieee80211_hdr *hdr;
974         struct ieee80211_sub_if_data *sdata;
975
976         int hdrlen;
977
978         memset(tx, 0, sizeof(*tx));
979         tx->skb = skb;
980         tx->dev = dev; /* use original interface */
981         tx->local = local;
982         tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
983         tx->u.tx.control = control;
984         /*
985          * Set this flag (used below to indicate "automatic fragmentation"),
986          * it will be cleared/left by radiotap as desired.
987          */
988         tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
989
990         /* process and remove the injection radiotap header */
991         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
992         if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
993                 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
994                         return TX_DROP;
995
996                 /*
997                  * __ieee80211_parse_tx_radiotap has now removed
998                  * the radiotap header that was present and pre-filled
999                  * 'tx' with tx control information.
1000                  */
1001         }
1002
1003         hdr = (struct ieee80211_hdr *) skb->data;
1004
1005         tx->sta = sta_info_get(local, hdr->addr1);
1006         tx->fc = le16_to_cpu(hdr->frame_control);
1007
1008         if (is_multicast_ether_addr(hdr->addr1)) {
1009                 tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
1010                 control->flags |= IEEE80211_TXCTL_NO_ACK;
1011         } else {
1012                 tx->flags |= IEEE80211_TXRXD_TXUNICAST;
1013                 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
1014         }
1015
1016         if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
1017                 if ((tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
1018                     skb->len + FCS_LEN > local->fragmentation_threshold &&
1019                     !local->ops->set_frag_threshold)
1020                         tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
1021                 else
1022                         tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
1023         }
1024
1025         if (!tx->sta)
1026                 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1027         else if (tx->sta->clear_dst_mask) {
1028                 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1029                 tx->sta->clear_dst_mask = 0;
1030         }
1031
1032         hdrlen = ieee80211_get_hdrlen(tx->fc);
1033         if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1034                 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1035                 tx->ethertype = (pos[0] << 8) | pos[1];
1036         }
1037         control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1038
1039         return TX_CONTINUE;
1040 }
1041
1042 /*
1043  * NB: @tx is uninitialised when passed in here
1044  */
1045 static int ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1046                                 struct sk_buff *skb,
1047                                 struct net_device *mdev,
1048                                 struct ieee80211_tx_control *control)
1049 {
1050         struct ieee80211_tx_packet_data *pkt_data;
1051         struct net_device *dev;
1052
1053         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1054         dev = dev_get_by_index(&init_net, pkt_data->ifindex);
1055         if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1056                 dev_put(dev);
1057                 dev = NULL;
1058         }
1059         if (unlikely(!dev))
1060                 return -ENODEV;
1061         /* initialises tx with control */
1062         __ieee80211_tx_prepare(tx, skb, dev, control);
1063         dev_put(dev);
1064         return 0;
1065 }
1066
1067 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1068                           struct ieee80211_txrx_data *tx)
1069 {
1070         struct ieee80211_tx_control *control = tx->u.tx.control;
1071         int ret, i;
1072
1073         if (!ieee80211_qdisc_installed(local->mdev) &&
1074             __ieee80211_queue_stopped(local, 0)) {
1075                 netif_stop_queue(local->mdev);
1076                 return IEEE80211_TX_AGAIN;
1077         }
1078         if (skb) {
1079                 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1080                                      "TX to low-level driver", skb);
1081                 ret = local->ops->tx(local_to_hw(local), skb, control);
1082                 if (ret)
1083                         return IEEE80211_TX_AGAIN;
1084                 local->mdev->trans_start = jiffies;
1085                 ieee80211_led_tx(local, 1);
1086         }
1087         if (tx->u.tx.extra_frag) {
1088                 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1089                                     IEEE80211_TXCTL_USE_CTS_PROTECT |
1090                                     IEEE80211_TXCTL_CLEAR_DST_MASK |
1091                                     IEEE80211_TXCTL_FIRST_FRAGMENT);
1092                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1093                         if (!tx->u.tx.extra_frag[i])
1094                                 continue;
1095                         if (__ieee80211_queue_stopped(local, control->queue))
1096                                 return IEEE80211_TX_FRAG_AGAIN;
1097                         if (i == tx->u.tx.num_extra_frag) {
1098                                 control->tx_rate = tx->u.tx.last_frag_rate;
1099
1100                                 if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
1101                                         control->flags |=
1102                                                 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1103                                 else
1104                                         control->flags &=
1105                                                 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1106                         }
1107
1108                         ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1109                                              "TX to low-level driver",
1110                                              tx->u.tx.extra_frag[i]);
1111                         ret = local->ops->tx(local_to_hw(local),
1112                                             tx->u.tx.extra_frag[i],
1113                                             control);
1114                         if (ret)
1115                                 return IEEE80211_TX_FRAG_AGAIN;
1116                         local->mdev->trans_start = jiffies;
1117                         ieee80211_led_tx(local, 1);
1118                         tx->u.tx.extra_frag[i] = NULL;
1119                 }
1120                 kfree(tx->u.tx.extra_frag);
1121                 tx->u.tx.extra_frag = NULL;
1122         }
1123         return IEEE80211_TX_OK;
1124 }
1125
1126 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
1127                         struct ieee80211_tx_control *control)
1128 {
1129         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1130         struct sta_info *sta;
1131         ieee80211_tx_handler *handler;
1132         struct ieee80211_txrx_data tx;
1133         ieee80211_tx_result res = TX_DROP, res_prepare;
1134         int ret, i;
1135
1136         WARN_ON(__ieee80211_queue_pending(local, control->queue));
1137
1138         if (unlikely(skb->len < 10)) {
1139                 dev_kfree_skb(skb);
1140                 return 0;
1141         }
1142
1143         /* initialises tx */
1144         res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1145
1146         if (res_prepare == TX_DROP) {
1147                 dev_kfree_skb(skb);
1148                 return 0;
1149         }
1150
1151         /*
1152          * key references are protected using RCU and this requires that
1153          * we are in a read-site RCU section during receive processing
1154          */
1155         rcu_read_lock();
1156
1157         sta = tx.sta;
1158         tx.u.tx.channel = local->hw.conf.channel;
1159
1160         for (handler = ieee80211_tx_handlers; *handler != NULL;
1161              handler++) {
1162                 res = (*handler)(&tx);
1163                 if (res != TX_CONTINUE)
1164                         break;
1165         }
1166
1167         skb = tx.skb; /* handlers are allowed to change skb */
1168
1169         if (sta)
1170                 sta_info_put(sta);
1171
1172         if (unlikely(res == TX_DROP)) {
1173                 I802_DEBUG_INC(local->tx_handlers_drop);
1174                 goto drop;
1175         }
1176
1177         if (unlikely(res == TX_QUEUED)) {
1178                 I802_DEBUG_INC(local->tx_handlers_queued);
1179                 rcu_read_unlock();
1180                 return 0;
1181         }
1182
1183         if (tx.u.tx.extra_frag) {
1184                 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1185                         int next_len, dur;
1186                         struct ieee80211_hdr *hdr =
1187                                 (struct ieee80211_hdr *)
1188                                 tx.u.tx.extra_frag[i]->data;
1189
1190                         if (i + 1 < tx.u.tx.num_extra_frag) {
1191                                 next_len = tx.u.tx.extra_frag[i + 1]->len;
1192                         } else {
1193                                 next_len = 0;
1194                                 tx.u.tx.rate = tx.u.tx.last_frag_rate;
1195                         }
1196                         dur = ieee80211_duration(&tx, 0, next_len);
1197                         hdr->duration_id = cpu_to_le16(dur);
1198                 }
1199         }
1200
1201 retry:
1202         ret = __ieee80211_tx(local, skb, &tx);
1203         if (ret) {
1204                 struct ieee80211_tx_stored_packet *store =
1205                         &local->pending_packet[control->queue];
1206
1207                 if (ret == IEEE80211_TX_FRAG_AGAIN)
1208                         skb = NULL;
1209                 set_bit(IEEE80211_LINK_STATE_PENDING,
1210                         &local->state[control->queue]);
1211                 smp_mb();
1212                 /* When the driver gets out of buffers during sending of
1213                  * fragments and calls ieee80211_stop_queue, there is
1214                  * a small window between IEEE80211_LINK_STATE_XOFF and
1215                  * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1216                  * gets available in that window (i.e. driver calls
1217                  * ieee80211_wake_queue), we would end up with ieee80211_tx
1218                  * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1219                  * continuing transmitting here when that situation is
1220                  * possible to have happened. */
1221                 if (!__ieee80211_queue_stopped(local, control->queue)) {
1222                         clear_bit(IEEE80211_LINK_STATE_PENDING,
1223                                   &local->state[control->queue]);
1224                         goto retry;
1225                 }
1226                 memcpy(&store->control, control,
1227                        sizeof(struct ieee80211_tx_control));
1228                 store->skb = skb;
1229                 store->extra_frag = tx.u.tx.extra_frag;
1230                 store->num_extra_frag = tx.u.tx.num_extra_frag;
1231                 store->last_frag_rate = tx.u.tx.last_frag_rate;
1232                 store->last_frag_rate_ctrl_probe =
1233                         !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
1234         }
1235         rcu_read_unlock();
1236         return 0;
1237
1238  drop:
1239         if (skb)
1240                 dev_kfree_skb(skb);
1241         for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1242                 if (tx.u.tx.extra_frag[i])
1243                         dev_kfree_skb(tx.u.tx.extra_frag[i]);
1244         kfree(tx.u.tx.extra_frag);
1245         rcu_read_unlock();
1246         return 0;
1247 }
1248
1249 /* device xmit handlers */
1250
1251 int ieee80211_master_start_xmit(struct sk_buff *skb,
1252                                 struct net_device *dev)
1253 {
1254         struct ieee80211_tx_control control;
1255         struct ieee80211_tx_packet_data *pkt_data;
1256         struct net_device *odev = NULL;
1257         struct ieee80211_sub_if_data *osdata;
1258         int headroom;
1259         int ret;
1260
1261         /*
1262          * copy control out of the skb so other people can use skb->cb
1263          */
1264         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1265         memset(&control, 0, sizeof(struct ieee80211_tx_control));
1266
1267         if (pkt_data->ifindex)
1268                 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
1269         if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1270                 dev_put(odev);
1271                 odev = NULL;
1272         }
1273         if (unlikely(!odev)) {
1274 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1275                 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1276                        "originating device\n", dev->name);
1277 #endif
1278                 dev_kfree_skb(skb);
1279                 return 0;
1280         }
1281         osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1282
1283         headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1284         if (skb_headroom(skb) < headroom) {
1285                 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1286                         dev_kfree_skb(skb);
1287                         dev_put(odev);
1288                         return 0;
1289                 }
1290         }
1291
1292         control.vif = &osdata->vif;
1293         control.type = osdata->vif.type;
1294         if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
1295                 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
1296         if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
1297                 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1298         if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
1299                 control.flags |= IEEE80211_TXCTL_REQUEUE;
1300         if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1301                 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
1302         if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1303                 control.flags |= IEEE80211_TXCTL_AMPDU;
1304         control.queue = pkt_data->queue;
1305
1306         ret = ieee80211_tx(odev, skb, &control);
1307         dev_put(odev);
1308
1309         return ret;
1310 }
1311
1312 int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1313                                  struct net_device *dev)
1314 {
1315         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1316         struct ieee80211_tx_packet_data *pkt_data;
1317         struct ieee80211_radiotap_header *prthdr =
1318                 (struct ieee80211_radiotap_header *)skb->data;
1319         u16 len_rthdr;
1320
1321         /* check for not even having the fixed radiotap header part */
1322         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1323                 goto fail; /* too short to be possibly valid */
1324
1325         /* is it a header version we can trust to find length from? */
1326         if (unlikely(prthdr->it_version))
1327                 goto fail; /* only version 0 is supported */
1328
1329         /* then there must be a radiotap header with a length we can use */
1330         len_rthdr = ieee80211_get_radiotap_len(skb->data);
1331
1332         /* does the skb contain enough to deliver on the alleged length? */
1333         if (unlikely(skb->len < len_rthdr))
1334                 goto fail; /* skb too short for claimed rt header extent */
1335
1336         skb->dev = local->mdev;
1337
1338         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1339         memset(pkt_data, 0, sizeof(*pkt_data));
1340         /* needed because we set skb device to master */
1341         pkt_data->ifindex = dev->ifindex;
1342
1343         pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1344
1345         /*
1346          * fix up the pointers accounting for the radiotap
1347          * header still being in there.  We are being given
1348          * a precooked IEEE80211 header so no need for
1349          * normal processing
1350          */
1351         skb_set_mac_header(skb, len_rthdr);
1352         /*
1353          * these are just fixed to the end of the rt area since we
1354          * don't have any better information and at this point, nobody cares
1355          */
1356         skb_set_network_header(skb, len_rthdr);
1357         skb_set_transport_header(skb, len_rthdr);
1358
1359         /* pass the radiotap header up to the next stage intact */
1360         dev_queue_xmit(skb);
1361         return NETDEV_TX_OK;
1362
1363 fail:
1364         dev_kfree_skb(skb);
1365         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1366 }
1367
1368 /**
1369  * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1370  * subinterfaces (wlan#, WDS, and VLAN interfaces)
1371  * @skb: packet to be sent
1372  * @dev: incoming interface
1373  *
1374  * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1375  * not be freed, and caller is responsible for either retrying later or freeing
1376  * skb).
1377  *
1378  * This function takes in an Ethernet header and encapsulates it with suitable
1379  * IEEE 802.11 header based on which interface the packet is coming in. The
1380  * encapsulated packet will then be passed to master interface, wlan#.11, for
1381  * transmission (through low-level driver).
1382  */
1383 int ieee80211_subif_start_xmit(struct sk_buff *skb,
1384                                struct net_device *dev)
1385 {
1386         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1387         struct ieee80211_tx_packet_data *pkt_data;
1388         struct ieee80211_sub_if_data *sdata;
1389         int ret = 1, head_need;
1390         u16 ethertype, hdrlen, fc;
1391         struct ieee80211_hdr hdr;
1392         const u8 *encaps_data;
1393         int encaps_len, skip_header_bytes;
1394         int nh_pos, h_pos;
1395         struct sta_info *sta;
1396         u32 sta_flags = 0;
1397
1398         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1399         if (unlikely(skb->len < ETH_HLEN)) {
1400                 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1401                        dev->name, skb->len);
1402                 ret = 0;
1403                 goto fail;
1404         }
1405
1406         nh_pos = skb_network_header(skb) - skb->data;
1407         h_pos = skb_transport_header(skb) - skb->data;
1408
1409         /* convert Ethernet header to proper 802.11 header (based on
1410          * operation mode) */
1411         ethertype = (skb->data[12] << 8) | skb->data[13];
1412         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1413
1414         switch (sdata->vif.type) {
1415         case IEEE80211_IF_TYPE_AP:
1416         case IEEE80211_IF_TYPE_VLAN:
1417                 fc |= IEEE80211_FCTL_FROMDS;
1418                 /* DA BSSID SA */
1419                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1420                 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1421                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1422                 hdrlen = 24;
1423                 break;
1424         case IEEE80211_IF_TYPE_WDS:
1425                 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1426                 /* RA TA DA SA */
1427                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1428                 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1429                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1430                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1431                 hdrlen = 30;
1432                 break;
1433         case IEEE80211_IF_TYPE_STA:
1434                 fc |= IEEE80211_FCTL_TODS;
1435                 /* BSSID SA DA */
1436                 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1437                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1438                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1439                 hdrlen = 24;
1440                 break;
1441         case IEEE80211_IF_TYPE_IBSS:
1442                 /* DA SA BSSID */
1443                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1444                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1445                 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1446                 hdrlen = 24;
1447                 break;
1448         default:
1449                 ret = 0;
1450                 goto fail;
1451         }
1452
1453         /*
1454          * There's no need to try to look up the destination
1455          * if it is a multicast address (which can only happen
1456          * in AP mode)
1457          */
1458         if (!is_multicast_ether_addr(hdr.addr1)) {
1459                 sta = sta_info_get(local, hdr.addr1);
1460                 if (sta) {
1461                         sta_flags = sta->flags;
1462                         sta_info_put(sta);
1463                 }
1464         }
1465
1466         /* receiver is QoS enabled, use a QoS type frame */
1467         if (sta_flags & WLAN_STA_WME) {
1468                 fc |= IEEE80211_STYPE_QOS_DATA;
1469                 hdrlen += 2;
1470         }
1471
1472         /*
1473          * Drop unicast frames to unauthorised stations unless they are
1474          * EAPOL frames from the local station.
1475          */
1476         if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
1477                      !(sta_flags & WLAN_STA_AUTHORIZED) &&
1478                      !(ethertype == ETH_P_PAE &&
1479                        compare_ether_addr(dev->dev_addr,
1480                                           skb->data + ETH_ALEN) == 0))) {
1481 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1482                 DECLARE_MAC_BUF(mac);
1483
1484                 if (net_ratelimit())
1485                         printk(KERN_DEBUG "%s: dropped frame to %s"
1486                                " (unauthorized port)\n", dev->name,
1487                                print_mac(mac, hdr.addr1));
1488 #endif
1489
1490                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1491
1492                 ret = 0;
1493                 goto fail;
1494         }
1495
1496         hdr.frame_control = cpu_to_le16(fc);
1497         hdr.duration_id = 0;
1498         hdr.seq_ctrl = 0;
1499
1500         skip_header_bytes = ETH_HLEN;
1501         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1502                 encaps_data = bridge_tunnel_header;
1503                 encaps_len = sizeof(bridge_tunnel_header);
1504                 skip_header_bytes -= 2;
1505         } else if (ethertype >= 0x600) {
1506                 encaps_data = rfc1042_header;
1507                 encaps_len = sizeof(rfc1042_header);
1508                 skip_header_bytes -= 2;
1509         } else {
1510                 encaps_data = NULL;
1511                 encaps_len = 0;
1512         }
1513
1514         skb_pull(skb, skip_header_bytes);
1515         nh_pos -= skip_header_bytes;
1516         h_pos -= skip_header_bytes;
1517
1518         /* TODO: implement support for fragments so that there is no need to
1519          * reallocate and copy payload; it might be enough to support one
1520          * extra fragment that would be copied in the beginning of the frame
1521          * data.. anyway, it would be nice to include this into skb structure
1522          * somehow
1523          *
1524          * There are few options for this:
1525          * use skb->cb as an extra space for 802.11 header
1526          * allocate new buffer if not enough headroom
1527          * make sure that there is enough headroom in every skb by increasing
1528          * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1529          * alloc_skb() (net/core/skbuff.c)
1530          */
1531         head_need = hdrlen + encaps_len + local->tx_headroom;
1532         head_need -= skb_headroom(skb);
1533
1534         /* We are going to modify skb data, so make a copy of it if happens to
1535          * be cloned. This could happen, e.g., with Linux bridge code passing
1536          * us broadcast frames. */
1537
1538         if (head_need > 0 || skb_cloned(skb)) {
1539 #if 0
1540                 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1541                        "of headroom\n", dev->name, head_need);
1542 #endif
1543
1544                 if (skb_cloned(skb))
1545                         I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1546                 else
1547                         I802_DEBUG_INC(local->tx_expand_skb_head);
1548                 /* Since we have to reallocate the buffer, make sure that there
1549                  * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1550                  * before payload and 12 after). */
1551                 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1552                                      12, GFP_ATOMIC)) {
1553                         printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1554                                "\n", dev->name);
1555                         goto fail;
1556                 }
1557         }
1558
1559         if (encaps_data) {
1560                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1561                 nh_pos += encaps_len;
1562                 h_pos += encaps_len;
1563         }
1564
1565         if (fc & IEEE80211_STYPE_QOS_DATA) {
1566                 __le16 *qos_control;
1567
1568                 qos_control = (__le16*) skb_push(skb, 2);
1569                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1570                 /*
1571                  * Maybe we could actually set some fields here, for now just
1572                  * initialise to zero to indicate no special operation.
1573                  */
1574                 *qos_control = 0;
1575         } else
1576                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1577
1578         nh_pos += hdrlen;
1579         h_pos += hdrlen;
1580
1581         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1582         memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1583         pkt_data->ifindex = dev->ifindex;
1584         if (ethertype == ETH_P_PAE)
1585                 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
1586
1587         skb->dev = local->mdev;
1588         dev->stats.tx_packets++;
1589         dev->stats.tx_bytes += skb->len;
1590
1591         /* Update skb pointers to various headers since this modified frame
1592          * is going to go through Linux networking code that may potentially
1593          * need things like pointer to IP header. */
1594         skb_set_mac_header(skb, 0);
1595         skb_set_network_header(skb, nh_pos);
1596         skb_set_transport_header(skb, h_pos);
1597
1598         dev->trans_start = jiffies;
1599         dev_queue_xmit(skb);
1600
1601         return 0;
1602
1603  fail:
1604         if (!ret)
1605                 dev_kfree_skb(skb);
1606
1607         return ret;
1608 }
1609
1610 /* helper functions for pending packets for when queues are stopped */
1611
1612 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1613 {
1614         int i, j;
1615         struct ieee80211_tx_stored_packet *store;
1616
1617         for (i = 0; i < local->hw.queues; i++) {
1618                 if (!__ieee80211_queue_pending(local, i))
1619                         continue;
1620                 store = &local->pending_packet[i];
1621                 kfree_skb(store->skb);
1622                 for (j = 0; j < store->num_extra_frag; j++)
1623                         kfree_skb(store->extra_frag[j]);
1624                 kfree(store->extra_frag);
1625                 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1626         }
1627 }
1628
1629 void ieee80211_tx_pending(unsigned long data)
1630 {
1631         struct ieee80211_local *local = (struct ieee80211_local *)data;
1632         struct net_device *dev = local->mdev;
1633         struct ieee80211_tx_stored_packet *store;
1634         struct ieee80211_txrx_data tx;
1635         int i, ret, reschedule = 0;
1636
1637         netif_tx_lock_bh(dev);
1638         for (i = 0; i < local->hw.queues; i++) {
1639                 if (__ieee80211_queue_stopped(local, i))
1640                         continue;
1641                 if (!__ieee80211_queue_pending(local, i)) {
1642                         reschedule = 1;
1643                         continue;
1644                 }
1645                 store = &local->pending_packet[i];
1646                 tx.u.tx.control = &store->control;
1647                 tx.u.tx.extra_frag = store->extra_frag;
1648                 tx.u.tx.num_extra_frag = store->num_extra_frag;
1649                 tx.u.tx.last_frag_rate = store->last_frag_rate;
1650                 tx.flags = 0;
1651                 if (store->last_frag_rate_ctrl_probe)
1652                         tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
1653                 ret = __ieee80211_tx(local, store->skb, &tx);
1654                 if (ret) {
1655                         if (ret == IEEE80211_TX_FRAG_AGAIN)
1656                                 store->skb = NULL;
1657                 } else {
1658                         clear_bit(IEEE80211_LINK_STATE_PENDING,
1659                                   &local->state[i]);
1660                         reschedule = 1;
1661                 }
1662         }
1663         netif_tx_unlock_bh(dev);
1664         if (reschedule) {
1665                 if (!ieee80211_qdisc_installed(dev)) {
1666                         if (!__ieee80211_queue_stopped(local, 0))
1667                                 netif_wake_queue(dev);
1668                 } else
1669                         netif_schedule(dev);
1670         }
1671 }
1672
1673 /* functions for drivers to get certain frames */
1674
1675 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1676                                      struct ieee80211_if_ap *bss,
1677                                      struct sk_buff *skb,
1678                                      struct beacon_data *beacon)
1679 {
1680         u8 *pos, *tim;
1681         int aid0 = 0;
1682         int i, have_bits = 0, n1, n2;
1683
1684         /* Generate bitmap for TIM only if there are any STAs in power save
1685          * mode. */
1686         read_lock_bh(&local->sta_lock);
1687         if (atomic_read(&bss->num_sta_ps) > 0)
1688                 /* in the hope that this is faster than
1689                  * checking byte-for-byte */
1690                 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1691                                           IEEE80211_MAX_AID+1);
1692
1693         if (bss->dtim_count == 0)
1694                 bss->dtim_count = beacon->dtim_period - 1;
1695         else
1696                 bss->dtim_count--;
1697
1698         tim = pos = (u8 *) skb_put(skb, 6);
1699         *pos++ = WLAN_EID_TIM;
1700         *pos++ = 4;
1701         *pos++ = bss->dtim_count;
1702         *pos++ = beacon->dtim_period;
1703
1704         if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1705                 aid0 = 1;
1706
1707         if (have_bits) {
1708                 /* Find largest even number N1 so that bits numbered 1 through
1709                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1710                  * (N2 + 1) x 8 through 2007 are 0. */
1711                 n1 = 0;
1712                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1713                         if (bss->tim[i]) {
1714                                 n1 = i & 0xfe;
1715                                 break;
1716                         }
1717                 }
1718                 n2 = n1;
1719                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1720                         if (bss->tim[i]) {
1721                                 n2 = i;
1722                                 break;
1723                         }
1724                 }
1725
1726                 /* Bitmap control */
1727                 *pos++ = n1 | aid0;
1728                 /* Part Virt Bitmap */
1729                 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1730
1731                 tim[1] = n2 - n1 + 4;
1732                 skb_put(skb, n2 - n1);
1733         } else {
1734                 *pos++ = aid0; /* Bitmap control */
1735                 *pos++ = 0; /* Part Virt Bitmap */
1736         }
1737         read_unlock_bh(&local->sta_lock);
1738 }
1739
1740 struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1741                                      struct ieee80211_vif *vif,
1742                                      struct ieee80211_tx_control *control)
1743 {
1744         struct ieee80211_local *local = hw_to_local(hw);
1745         struct sk_buff *skb;
1746         struct net_device *bdev;
1747         struct ieee80211_sub_if_data *sdata = NULL;
1748         struct ieee80211_if_ap *ap = NULL;
1749         struct rate_selection rsel;
1750         struct beacon_data *beacon;
1751         struct ieee80211_supported_band *sband;
1752
1753         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1754
1755         rcu_read_lock();
1756
1757         sdata = vif_to_sdata(vif);
1758         bdev = sdata->dev;
1759         ap = &sdata->u.ap;
1760
1761         beacon = rcu_dereference(ap->beacon);
1762
1763         if (!ap || sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon) {
1764 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1765                 if (net_ratelimit())
1766                         printk(KERN_DEBUG "no beacon data avail for %s\n",
1767                                bdev->name);
1768 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
1769                 skb = NULL;
1770                 goto out;
1771         }
1772
1773         /* headroom, head length, tail length and maximum TIM length */
1774         skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
1775                             beacon->tail_len + 256);
1776         if (!skb)
1777                 goto out;
1778
1779         skb_reserve(skb, local->tx_headroom);
1780         memcpy(skb_put(skb, beacon->head_len), beacon->head,
1781                beacon->head_len);
1782
1783         ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
1784
1785         ieee80211_beacon_add_tim(local, ap, skb, beacon);
1786
1787         if (beacon->tail)
1788                 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
1789                        beacon->tail_len);
1790
1791         if (control) {
1792                 rate_control_get_rate(local->mdev, sband, skb, &rsel);
1793                 if (!rsel.rate) {
1794                         if (net_ratelimit()) {
1795                                 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1796                                        "no rate found\n",
1797                                        wiphy_name(local->hw.wiphy));
1798                         }
1799                         dev_kfree_skb(skb);
1800                         skb = NULL;
1801                         goto out;
1802                 }
1803
1804                 control->vif = vif;
1805                 control->tx_rate = rsel.rate;
1806                 if (sdata->bss_conf.use_short_preamble &&
1807                     rsel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
1808                         control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
1809                 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1810                 control->flags |= IEEE80211_TXCTL_NO_ACK;
1811                 control->retry_limit = 1;
1812                 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1813         }
1814
1815         ap->num_beacons++;
1816
1817  out:
1818         rcu_read_unlock();
1819         return skb;
1820 }
1821 EXPORT_SYMBOL(ieee80211_beacon_get);
1822
1823 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1824                        const void *frame, size_t frame_len,
1825                        const struct ieee80211_tx_control *frame_txctl,
1826                        struct ieee80211_rts *rts)
1827 {
1828         const struct ieee80211_hdr *hdr = frame;
1829         u16 fctl;
1830
1831         fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1832         rts->frame_control = cpu_to_le16(fctl);
1833         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1834                                                frame_txctl);
1835         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1836         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1837 }
1838 EXPORT_SYMBOL(ieee80211_rts_get);
1839
1840 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1841                              const void *frame, size_t frame_len,
1842                              const struct ieee80211_tx_control *frame_txctl,
1843                              struct ieee80211_cts *cts)
1844 {
1845         const struct ieee80211_hdr *hdr = frame;
1846         u16 fctl;
1847
1848         fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1849         cts->frame_control = cpu_to_le16(fctl);
1850         cts->duration = ieee80211_ctstoself_duration(hw, vif,
1851                                                      frame_len, frame_txctl);
1852         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1853 }
1854 EXPORT_SYMBOL(ieee80211_ctstoself_get);
1855
1856 struct sk_buff *
1857 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1858                           struct ieee80211_vif *vif,
1859                           struct ieee80211_tx_control *control)
1860 {
1861         struct ieee80211_local *local = hw_to_local(hw);
1862         struct sk_buff *skb;
1863         struct sta_info *sta;
1864         ieee80211_tx_handler *handler;
1865         struct ieee80211_txrx_data tx;
1866         ieee80211_tx_result res = TX_DROP;
1867         struct net_device *bdev;
1868         struct ieee80211_sub_if_data *sdata;
1869         struct ieee80211_if_ap *bss = NULL;
1870         struct beacon_data *beacon;
1871
1872         sdata = vif_to_sdata(vif);
1873         bdev = sdata->dev;
1874
1875
1876         if (!bss)
1877                 return NULL;
1878
1879         rcu_read_lock();
1880         beacon = rcu_dereference(bss->beacon);
1881
1882         if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1883             !beacon->head) {
1884                 rcu_read_unlock();
1885                 return NULL;
1886         }
1887         rcu_read_unlock();
1888
1889         if (bss->dtim_count != 0)
1890                 return NULL; /* send buffered bc/mc only after DTIM beacon */
1891         memset(control, 0, sizeof(*control));
1892         while (1) {
1893                 skb = skb_dequeue(&bss->ps_bc_buf);
1894                 if (!skb)
1895                         return NULL;
1896                 local->total_ps_buffered--;
1897
1898                 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1899                         struct ieee80211_hdr *hdr =
1900                                 (struct ieee80211_hdr *) skb->data;
1901                         /* more buffered multicast/broadcast frames ==> set
1902                          * MoreData flag in IEEE 802.11 header to inform PS
1903                          * STAs */
1904                         hdr->frame_control |=
1905                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1906                 }
1907
1908                 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
1909                         break;
1910                 dev_kfree_skb_any(skb);
1911         }
1912         sta = tx.sta;
1913         tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
1914         tx.u.tx.channel = local->hw.conf.channel;
1915
1916         for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
1917                 res = (*handler)(&tx);
1918                 if (res == TX_DROP || res == TX_QUEUED)
1919                         break;
1920         }
1921         skb = tx.skb; /* handlers are allowed to change skb */
1922
1923         if (res == TX_DROP) {
1924                 I802_DEBUG_INC(local->tx_handlers_drop);
1925                 dev_kfree_skb(skb);
1926                 skb = NULL;
1927         } else if (res == TX_QUEUED) {
1928                 I802_DEBUG_INC(local->tx_handlers_queued);
1929                 skb = NULL;
1930         }
1931
1932         if (sta)
1933                 sta_info_put(sta);
1934
1935         return skb;
1936 }
1937 EXPORT_SYMBOL(ieee80211_get_buffered_bc);