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