ARM: debug: qcom: add UART addresses to Kconfig help for APQ8084
[pandora-kernel.git] / drivers / staging / rtl8187se / ieee80211 / ieee80211_tx.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8
9   This program is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12   more details.
13
14   You should have received a copy of the GNU General Public License along with
15   this program; if not, write to the Free Software Foundation, Inc., 59
16   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18   The full GNU General Public License is included in this distribution in the
19   file called LICENSE.
20
21   Contact Information:
22   James P. Ketrenos <ipw2100-admin@linux.intel.com>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25 ******************************************************************************
26
27   Few modifications for Realtek's Wi-Fi drivers by
28   Andrea Merello <andrea.merello@gmail.com>
29
30   A special thanks goes to Realtek for their support !
31
32 ******************************************************************************/
33
34 #include <linux/compiler.h>
35 #include <linux/errno.h>
36 #include <linux/if_arp.h>
37 #include <linux/in6.h>
38 #include <linux/in.h>
39 #include <linux/ip.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/netdevice.h>
43 #include <linux/pci.h>
44 #include <linux/proc_fs.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <linux/tcp.h>
48 #include <linux/types.h>
49 #include <linux/wireless.h>
50 #include <linux/etherdevice.h>
51 #include <asm/uaccess.h>
52 #include <linux/if_vlan.h>
53
54 #include "ieee80211.h"
55
56
57 /*
58
59
60 802.11 Data Frame
61
62
63 802.11 frame_contorl for data frames - 2 bytes
64      ,-----------------------------------------------------------------------------------------.
65 bits | 0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  a  |  b  |  c  |  d  |  e   |
66      |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
67 val  | 0  |  0  |  0  |  1  |  x  |  0  |  0  |  0  |  1  |  0  |  x  |  x  |  x  |  x  |  x   |
68      |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
69 desc | ^-ver-^  |  ^type-^  |  ^-----subtype-----^  | to  |from |more |retry| pwr |more |wep   |
70      |          |           | x=0 data,x=1 data+ack | DS  | DS  |frag |     | mgm |data |      |
71      '-----------------------------------------------------------------------------------------'
72                                                     /\
73                                                     |
74 802.11 Data Frame                                   |
75            ,--------- 'ctrl' expands to >-----------'
76           |
77       ,--'---,-------------------------------------------------------------.
78 Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
79       |------|------|---------|---------|---------|------|---------|------|
80 Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
81       |      | tion | (BSSID) |         |         | ence |  data   |      |
82       `--------------------------------------------------|         |------'
83 Total: 28 non-data bytes                                 `----.----'
84                                                               |
85        .- 'Frame data' expands to <---------------------------'
86        |
87        V
88       ,---------------------------------------------------.
89 Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
90       |------|------|---------|----------|------|---------|
91 Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
92       | DSAP | SSAP |         |          |      | Packet  |
93       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
94       `-----------------------------------------|         |
95 Total: 8 non-data bytes                         `----.----'
96                                                      |
97        .- 'IP Packet' expands, if WEP enabled, to <--'
98        |
99        V
100       ,-----------------------.
101 Bytes |  4  |   0-2296  |  4  |
102       |-----|-----------|-----|
103 Desc. | IV  | Encrypted | ICV |
104       |     | IP Packet |     |
105       `-----------------------'
106 Total: 8 non-data bytes
107
108
109 802.3 Ethernet Data Frame
110
111       ,-----------------------------------------.
112 Bytes |   6   |   6   |  2   |  Variable |   4  |
113       |-------|-------|------|-----------|------|
114 Desc. | Dest. | Source| Type | IP Packet |  fcs |
115       |  MAC  |  MAC  |      |           |      |
116       `-----------------------------------------'
117 Total: 18 non-data bytes
118
119 In the event that fragmentation is required, the incoming payload is split into
120 N parts of size ieee->fts.  The first fragment contains the SNAP header and the
121 remaining packets are just data.
122
123 If encryption is enabled, each fragment payload size is reduced by enough space
124 to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
125 So if you have 1500 bytes of payload with ieee->fts set to 500 without
126 encryption it will take 3 frames.  With WEP it will take 4 frames as the
127 payload of each frame is reduced to 492 bytes.
128
129 * SKB visualization
130 *
131 *  ,- skb->data
132 * |
133 * |    ETHERNET HEADER        ,-<-- PAYLOAD
134 * |                           |     14 bytes from skb->data
135 * |  2 bytes for Type --> ,T. |     (sizeof ethhdr)
136 * |                       | | |
137 * |,-Dest.--. ,--Src.---. | | |
138 * |  6 bytes| | 6 bytes | | | |
139 * v         | |         | | | |
140 * 0         | v       1 | v | v           2
141 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
142 *     ^     | ^         | ^ |
143 *     |     | |         | | |
144 *     |     | |         | `T' <---- 2 bytes for Type
145 *     |     | |         |
146 *     |     | '---SNAP--' <-------- 6 bytes for SNAP
147 *     |     |
148 *     `-IV--' <-------------------- 4 bytes for IV (WEP)
149 *
150 *      SNAP HEADER
151 *
152 */
153
154 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
155 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
156
157 static inline int ieee80211_put_snap(u8 *data, u16 h_proto)
158 {
159         struct ieee80211_snap_hdr *snap;
160         u8 *oui;
161
162         snap = (struct ieee80211_snap_hdr *)data;
163         snap->dsap = 0xaa;
164         snap->ssap = 0xaa;
165         snap->ctrl = 0x03;
166
167         if (h_proto == 0x8137 || h_proto == 0x80f3)
168                 oui = P802_1H_OUI;
169         else
170                 oui = RFC1042_OUI;
171         snap->oui[0] = oui[0];
172         snap->oui[1] = oui[1];
173         snap->oui[2] = oui[2];
174
175         *(u16 *)(data + SNAP_SIZE) = htons(h_proto);
176
177         return SNAP_SIZE + sizeof(u16);
178 }
179
180 int ieee80211_encrypt_fragment(struct ieee80211_device *ieee,
181                                struct sk_buff *frag, int hdr_len)
182 {
183         struct ieee80211_crypt_data* crypt = ieee->crypt[ieee->tx_keyidx];
184         int res;
185
186         /*
187          * added to care about null crypt condition, to solve that system hangs
188          * when shared keys error
189          */
190         if (!crypt || !crypt->ops)
191                 return -1;
192
193 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
194         struct ieee80211_hdr_4addr *header;
195
196         if (ieee->tkip_countermeasures &&
197             crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
198                 header = (struct ieee80211_hdr_4addr *)frag->data;
199                 if (net_ratelimit()) {
200                         netdev_dbg(ieee->dev, "TKIP countermeasures: dropped "
201                                "TX packet to %pM\n", header->addr1);
202                 }
203                 return -1;
204         }
205 #endif
206         /*
207          * To encrypt, frame format is:
208          * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes)
209          *
210          * PR: FIXME: Copied from hostap. Check fragmentation/MSDU/MPDU
211          * encryption.
212          *
213          * Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
214          * call both MSDU and MPDU encryption functions from here.
215          */
216         atomic_inc(&crypt->refcnt);
217         res = 0;
218         if (crypt->ops->encrypt_msdu)
219                 res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
220         if (res == 0 && crypt->ops->encrypt_mpdu)
221                 res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
222
223         atomic_dec(&crypt->refcnt);
224         if (res < 0) {
225                 netdev_info(ieee->dev, "Encryption failed: len=%d.\n", frag->len);
226                 ieee->ieee_stats.tx_discards++;
227                 return -1;
228         }
229
230         return 0;
231 }
232
233
234 void ieee80211_txb_free(struct ieee80211_txb *txb)
235 {
236         int i;
237         if (unlikely(!txb))
238                 return;
239         for (i = 0; i < txb->nr_frags; i++)
240                 if (txb->fragments[i])
241                         dev_kfree_skb_any(txb->fragments[i]);
242         kfree(txb);
243 }
244
245 static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
246                                                  gfp_t gfp_mask)
247 {
248         struct ieee80211_txb *txb;
249         int i;
250         txb = kmalloc(
251                 sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
252                 gfp_mask);
253         if (!txb)
254                 return NULL;
255
256         memset(txb, 0, sizeof(struct ieee80211_txb));
257         txb->nr_frags = nr_frags;
258         txb->frag_size = txb_size;
259
260         for (i = 0; i < nr_frags; i++) {
261                 txb->fragments[i] = dev_alloc_skb(txb_size);
262                 if (unlikely(!txb->fragments[i])) {
263                         i--;
264                         break;
265                 }
266         }
267         if (unlikely(i != nr_frags)) {
268                 while (i >= 0)
269                         dev_kfree_skb_any(txb->fragments[i--]);
270                 kfree(txb);
271                 return NULL;
272         }
273         return txb;
274 }
275
276 /*
277  * Classify the to-be send data packet
278  * Need to acquire the sent queue index.
279  */
280 static int ieee80211_classify(struct sk_buff *skb,
281                               struct ieee80211_network *network)
282 {
283         struct ether_header *eh = (struct ether_header *)skb->data;
284         unsigned int wme_UP = 0;
285
286         if (!network->QoS_Enable) {
287                 skb->priority = 0;
288                 return(wme_UP);
289         }
290
291         if (eh->ether_type == __constant_htons(ETHERTYPE_IP)) {
292                 const struct iphdr *ih = (struct iphdr *)(skb->data +
293                     sizeof(struct ether_header));
294                 wme_UP = (ih->tos >> 5)&0x07;
295         } else if (vlan_tx_tag_present(skb)) {/* vtag packet */
296 #ifndef VLAN_PRI_SHIFT
297 #define VLAN_PRI_SHIFT  13              /* Shift to find VLAN user priority */
298 #define VLAN_PRI_MASK   7               /* Mask for user priority bits in VLAN */
299 #endif
300                 u32 tag = vlan_tx_tag_get(skb);
301                 wme_UP = (tag >> VLAN_PRI_SHIFT) & VLAN_PRI_MASK;
302         } else if (ETH_P_PAE ==  ntohs(((struct ethhdr *)skb->data)->h_proto)) {
303                 wme_UP = 7;
304         }
305
306         skb->priority = wme_UP;
307         return(wme_UP);
308 }
309
310 /* SKBs are added to the ieee->tx_queue. */
311 int ieee80211_rtl_xmit(struct sk_buff *skb, struct net_device *dev)
312 {
313         struct ieee80211_device *ieee = netdev_priv(dev);
314         struct ieee80211_txb *txb = NULL;
315         struct ieee80211_hdr_3addrqos *frag_hdr;
316         int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
317         unsigned long flags;
318         struct net_device_stats *stats = &ieee->stats;
319         int ether_type, encrypt;
320         int bytes, fc, qos_ctl, hdr_len;
321         struct sk_buff *skb_frag;
322         struct ieee80211_hdr_3addrqos header = { /* Ensure zero initialized */
323                 .duration_id = 0,
324                 .seq_ctl = 0,
325                 .qos_ctl = 0
326         };
327         u8 dest[ETH_ALEN], src[ETH_ALEN];
328
329         struct ieee80211_crypt_data* crypt;
330
331         spin_lock_irqsave(&ieee->lock, flags);
332
333         /*
334          * If there is no driver handler to take the TXB, don't bother
335          * creating it...
336          */
337         if ((!ieee->hard_start_xmit &&
338              !(ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)) ||
339             ((!ieee->softmac_data_hard_start_xmit &&
340               (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
341                 netdev_warn(ieee->dev, "No xmit handler.\n");
342                 goto success;
343         }
344
345         ieee80211_classify(skb,&ieee->current_network);
346         if (likely(ieee->raw_tx == 0)){
347
348                 if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
349                         netdev_warn(ieee->dev, "skb too small (%d).\n", skb->len);
350                         goto success;
351                 }
352
353                 ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
354
355                 crypt = ieee->crypt[ieee->tx_keyidx];
356
357                 encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
358                         ieee->host_encrypt && crypt && crypt->ops;
359
360                 if (!encrypt && ieee->ieee802_1x &&
361                 ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
362                         stats->tx_dropped++;
363                         goto success;
364                 }
365
366         #ifdef CONFIG_IEEE80211_DEBUG
367                 if (crypt && !encrypt && ether_type == ETH_P_PAE) {
368                         struct eapol *eap = (struct eapol *)(skb->data +
369                                 sizeof(struct ethhdr) - SNAP_SIZE - sizeof(u16));
370                         IEEE80211_DEBUG_EAP("TX: IEEE 802.11 EAPOL frame: %s\n",
371                                 eap_get_type(eap->type));
372                 }
373         #endif
374
375                 /* Save source and destination addresses */
376                 memcpy(&dest, skb->data, ETH_ALEN);
377                 memcpy(&src, skb->data+ETH_ALEN, ETH_ALEN);
378
379                 /* Advance the SKB to the start of the payload */
380                 skb_pull(skb, sizeof(struct ethhdr));
381
382                 /* Determine total amount of storage required for TXB packets */
383                 bytes = skb->len + SNAP_SIZE + sizeof(u16);
384
385                 if (ieee->current_network.QoS_Enable) {
386                         if (encrypt)
387                                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA |
388                                         IEEE80211_FCTL_WEP;
389                         else
390                                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA;
391
392                 } else {
393                         if (encrypt)
394                                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
395                                         IEEE80211_FCTL_WEP;
396                         else
397                                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
398                 }
399
400                 if (ieee->iw_mode == IW_MODE_INFRA) {
401                         fc |= IEEE80211_FCTL_TODS;
402                         /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
403                         memcpy(&header.addr1, ieee->current_network.bssid, ETH_ALEN);
404                         memcpy(&header.addr2, &src, ETH_ALEN);
405                         memcpy(&header.addr3, &dest, ETH_ALEN);
406                 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
407                         /*
408                          * not From/To DS: Addr1 = DA, Addr2 = SA,
409                          * Addr3 = BSSID
410                          */
411                         memcpy(&header.addr1, dest, ETH_ALEN);
412                         memcpy(&header.addr2, src, ETH_ALEN);
413                         memcpy(&header.addr3, ieee->current_network.bssid, ETH_ALEN);
414                 }
415                 header.frame_ctl = cpu_to_le16(fc);
416
417                 /*
418                  * Determine fragmentation size based on destination (multicast
419                  * and broadcast are not fragmented)
420                  */
421                 if (is_multicast_ether_addr(header.addr1)) {
422                         frag_size = MAX_FRAG_THRESHOLD;
423                         qos_ctl = QOS_CTL_NOTCONTAIN_ACK;
424                 } else {
425                         /* default:392 */
426                         frag_size = ieee->fts;
427                         qos_ctl = 0;
428                 }
429
430                 if (ieee->current_network.QoS_Enable)   {
431                         hdr_len = IEEE80211_3ADDR_LEN + 2;
432                         /* skb->priority is set in the ieee80211_classify() */
433                         qos_ctl |= skb->priority;
434                         header.qos_ctl = cpu_to_le16(qos_ctl);
435                 } else {
436                         hdr_len = IEEE80211_3ADDR_LEN;
437                 }
438
439                 /*
440                  * Determine amount of payload per fragment.  Regardless of if
441                  * this stack is providing the full 802.11 header, one will
442                  * eventually be affixed to this fragment -- so we must account
443                  * for it when determining the amount of payload space.
444                  */
445                 bytes_per_frag = frag_size - hdr_len;
446                 if (ieee->config &
447                 (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
448                         bytes_per_frag -= IEEE80211_FCS_LEN;
449
450                 /* Each fragment may need to have room for encryption pre/postfix */
451                 if (encrypt)
452                         bytes_per_frag -= crypt->ops->extra_prefix_len +
453                                 crypt->ops->extra_postfix_len;
454
455                 /*
456                  * Number of fragments is the total bytes_per_frag /
457                  * payload_per_fragment
458                  */
459                 nr_frags = bytes / bytes_per_frag;
460                 bytes_last_frag = bytes % bytes_per_frag;
461                 if (bytes_last_frag)
462                         nr_frags++;
463                 else
464                         bytes_last_frag = bytes_per_frag;
465
466                 /*
467                  * When we allocate the TXB we allocate enough space for the 
468                  * reserve and full fragment bytes (bytes_per_frag doesn't
469                  * include prefix, postfix, header, FCS, etc.)
470                  */
471                 txb = ieee80211_alloc_txb(nr_frags, frag_size, GFP_ATOMIC);
472                 if (unlikely(!txb)) {
473                         netdev_warn(ieee->dev, "Could not allocate TXB\n");
474                         goto failed;
475                 }
476                 txb->encrypted = encrypt;
477                 txb->payload_size = bytes;
478
479                 for (i = 0; i < nr_frags; i++) {
480                         skb_frag = txb->fragments[i];
481                         skb_frag->priority = UP2AC(skb->priority);
482                         if (encrypt)
483                                 skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
484
485                         frag_hdr = (struct ieee80211_hdr_3addrqos *)skb_put(
486                                 skb_frag, hdr_len);
487                         memcpy(frag_hdr, &header, hdr_len);
488
489                         /*
490                          * If this is not the last fragment, then add the MOREFRAGS
491                          * bit to the frame control
492                          */
493                         if (i != nr_frags - 1) {
494                                 frag_hdr->frame_ctl = cpu_to_le16(
495                                         fc | IEEE80211_FCTL_MOREFRAGS);
496                                 bytes = bytes_per_frag;
497
498                         } else {
499                                 /* The last fragment takes the remaining length */
500                                 bytes = bytes_last_frag;
501                         }
502                         if (ieee->current_network.QoS_Enable) {
503                                 /*
504                                  * add 1 only indicate to corresponding seq
505                                  * number control 2006/7/12
506                                  */
507                                 frag_hdr->seq_ctl = cpu_to_le16(
508                                         ieee->seq_ctrl[UP2AC(skb->priority)+1]<<4 | i);
509                         } else {
510                                 frag_hdr->seq_ctl = cpu_to_le16(
511                                         ieee->seq_ctrl[0]<<4 | i);
512                         }
513
514                         /* Put a SNAP header on the first fragment */
515                         if (i == 0) {
516                                 ieee80211_put_snap(
517                                         skb_put(skb_frag, SNAP_SIZE + sizeof(u16)),
518                                         ether_type);
519                                 bytes -= SNAP_SIZE + sizeof(u16);
520                         }
521
522                         memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
523
524                         /* Advance the SKB... */
525                         skb_pull(skb, bytes);
526
527                         /*
528                          * Encryption routine will move the header forward in
529                          * order to insert the IV between the header and the
530                          * payload
531                          */
532                         if (encrypt)
533                                 ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
534                         if (ieee->config &
535                         (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
536                                 skb_put(skb_frag, 4);
537                 }
538                 /* Advance sequence number in data frame. */
539                 if (ieee->current_network.QoS_Enable) {
540                         if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
541                                 ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
542                         else
543                                 ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
544                 } else {
545                         if (ieee->seq_ctrl[0] == 0xFFF)
546                                 ieee->seq_ctrl[0] = 0;
547                         else
548                                 ieee->seq_ctrl[0]++;
549                 }
550         } else {
551                 if (unlikely(skb->len < sizeof(struct ieee80211_hdr_3addr))) {
552                         netdev_warn(ieee->dev, "skb too small (%d).\n", skb->len);
553                         goto success;
554                 }
555
556                 txb = ieee80211_alloc_txb(1, skb->len, GFP_ATOMIC);
557                 if (!txb) {
558                         netdev_warn(ieee->dev, "Could not allocate TXB\n");
559                         goto failed;
560                 }
561
562                 txb->encrypted = 0;
563                 txb->payload_size = skb->len;
564                 memcpy(skb_put(txb->fragments[0], skb->len), skb->data, skb->len);
565         }
566
567  success:
568         spin_unlock_irqrestore(&ieee->lock, flags);
569                 dev_kfree_skb_any(skb);
570         if (txb) {
571                 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE) {
572                         ieee80211_softmac_xmit(txb, ieee);
573                 } else {
574                         if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
575                                 stats->tx_packets++;
576                                 stats->tx_bytes += txb->payload_size;
577                                 return NETDEV_TX_OK;
578                         }
579                         ieee80211_txb_free(txb);
580                 }
581         }
582
583         return NETDEV_TX_OK;
584
585  failed:
586         spin_unlock_irqrestore(&ieee->lock, flags);
587         netif_stop_queue(dev);
588         stats->tx_errors++;
589         return NETDEV_TX_BUSY;
590
591 }