233d527c69533c275dcc25363a750358ed8c5b7d
[pandora-kernel.git] / net / ieee80211 / ieee80211_tx.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2005 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 #include <linux/compiler.h>
27 #include <linux/config.h>
28 #include <linux/errno.h>
29 #include <linux/if_arp.h>
30 #include <linux/in6.h>
31 #include <linux/in.h>
32 #include <linux/ip.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/netdevice.h>
36 #include <linux/proc_fs.h>
37 #include <linux/skbuff.h>
38 #include <linux/slab.h>
39 #include <linux/tcp.h>
40 #include <linux/types.h>
41 #include <linux/wireless.h>
42 #include <linux/etherdevice.h>
43 #include <asm/uaccess.h>
44
45 #include <net/ieee80211.h>
46
47 /*
48
49 802.11 Data Frame
50
51       ,-------------------------------------------------------------------.
52 Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
53       |------|------|---------|---------|---------|------|---------|------|
54 Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
55       |      | tion | (BSSID) |         |         | ence |  data   |      |
56       `--------------------------------------------------|         |------'
57 Total: 28 non-data bytes                                 `----.----'
58                                                               |
59        .- 'Frame data' expands, if WEP enabled, to <----------'
60        |
61        V
62       ,-----------------------.
63 Bytes |  4  |   0-2296  |  4  |
64       |-----|-----------|-----|
65 Desc. | IV  | Encrypted | ICV |
66       |     | Packet    |     |
67       `-----|           |-----'
68             `-----.-----'
69                   |
70        .- 'Encrypted Packet' expands to
71        |
72        V
73       ,---------------------------------------------------.
74 Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
75       |------|------|---------|----------|------|---------|
76 Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
77       | DSAP | SSAP |         |          |      | Packet  |
78       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
79       `----------------------------------------------------
80 Total: 8 non-data bytes
81
82 802.3 Ethernet Data Frame
83
84       ,-----------------------------------------.
85 Bytes |   6   |   6   |  2   |  Variable |   4  |
86       |-------|-------|------|-----------|------|
87 Desc. | Dest. | Source| Type | IP Packet |  fcs |
88       |  MAC  |  MAC  |      |           |      |
89       `-----------------------------------------'
90 Total: 18 non-data bytes
91
92 In the event that fragmentation is required, the incoming payload is split into
93 N parts of size ieee->fts.  The first fragment contains the SNAP header and the
94 remaining packets are just data.
95
96 If encryption is enabled, each fragment payload size is reduced by enough space
97 to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
98 So if you have 1500 bytes of payload with ieee->fts set to 500 without
99 encryption it will take 3 frames.  With WEP it will take 4 frames as the
100 payload of each frame is reduced to 492 bytes.
101
102 * SKB visualization
103 *
104 *  ,- skb->data
105 * |
106 * |    ETHERNET HEADER        ,-<-- PAYLOAD
107 * |                           |     14 bytes from skb->data
108 * |  2 bytes for Type --> ,T. |     (sizeof ethhdr)
109 * |                       | | |
110 * |,-Dest.--. ,--Src.---. | | |
111 * |  6 bytes| | 6 bytes | | | |
112 * v         | |         | | | |
113 * 0         | v       1 | v | v           2
114 * 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
115 *     ^     | ^         | ^ |
116 *     |     | |         | | |
117 *     |     | |         | `T' <---- 2 bytes for Type
118 *     |     | |         |
119 *     |     | '---SNAP--' <-------- 6 bytes for SNAP
120 *     |     |
121 *     `-IV--' <-------------------- 4 bytes for IV (WEP)
122 *
123 *      SNAP HEADER
124 *
125 */
126
127 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
128 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
129
130 static int ieee80211_copy_snap(u8 * data, u16 h_proto)
131 {
132         struct ieee80211_snap_hdr *snap;
133         u8 *oui;
134
135         snap = (struct ieee80211_snap_hdr *)data;
136         snap->dsap = 0xaa;
137         snap->ssap = 0xaa;
138         snap->ctrl = 0x03;
139
140         if (h_proto == 0x8137 || h_proto == 0x80f3)
141                 oui = P802_1H_OUI;
142         else
143                 oui = RFC1042_OUI;
144         snap->oui[0] = oui[0];
145         snap->oui[1] = oui[1];
146         snap->oui[2] = oui[2];
147
148         *(u16 *) (data + SNAP_SIZE) = htons(h_proto);
149
150         return SNAP_SIZE + sizeof(u16);
151 }
152
153 static int ieee80211_encrypt_fragment(struct ieee80211_device *ieee,
154                                              struct sk_buff *frag, int hdr_len)
155 {
156         struct ieee80211_crypt_data *crypt = ieee->crypt[ieee->tx_keyidx];
157         int res;
158
159         if (crypt == NULL)
160                 return -1;
161
162         /* To encrypt, frame format is:
163          * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
164         atomic_inc(&crypt->refcnt);
165         res = 0;
166         if (crypt->ops && crypt->ops->encrypt_mpdu)
167                 res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
168
169         atomic_dec(&crypt->refcnt);
170         if (res < 0) {
171                 printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
172                        ieee->dev->name, frag->len);
173                 ieee->ieee_stats.tx_discards++;
174                 return -1;
175         }
176
177         return 0;
178 }
179
180 void ieee80211_txb_free(struct ieee80211_txb *txb)
181 {
182         int i;
183         if (unlikely(!txb))
184                 return;
185         for (i = 0; i < txb->nr_frags; i++)
186                 if (txb->fragments[i])
187                         dev_kfree_skb_any(txb->fragments[i]);
188         kfree(txb);
189 }
190
191 static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
192                                                  int headroom, gfp_t gfp_mask)
193 {
194         struct ieee80211_txb *txb;
195         int i;
196         txb = kmalloc(sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
197                       gfp_mask);
198         if (!txb)
199                 return NULL;
200
201         memset(txb, 0, sizeof(struct ieee80211_txb));
202         txb->nr_frags = nr_frags;
203         txb->frag_size = txb_size;
204
205         for (i = 0; i < nr_frags; i++) {
206                 txb->fragments[i] = __dev_alloc_skb(txb_size + headroom,
207                                                     gfp_mask);
208                 if (unlikely(!txb->fragments[i])) {
209                         i--;
210                         break;
211                 }
212                 skb_reserve(txb->fragments[i], headroom);
213         }
214         if (unlikely(i != nr_frags)) {
215                 while (i >= 0)
216                         dev_kfree_skb_any(txb->fragments[i--]);
217                 kfree(txb);
218                 return NULL;
219         }
220         return txb;
221 }
222
223 static int ieee80211_classify(struct sk_buff *skb)
224 {
225         struct ethhdr *eth;
226         struct iphdr *ip;
227
228         eth = (struct ethhdr *)skb->data;
229         if (eth->h_proto != __constant_htons(ETH_P_IP))
230                 return 0;
231
232         ip = skb->nh.iph;
233         switch (ip->tos & 0xfc) {
234         case 0x20:
235                 return 2;
236         case 0x40:
237                 return 1;
238         case 0x60:
239                 return 3;
240         case 0x80:
241                 return 4;
242         case 0xa0:
243                 return 5;
244         case 0xc0:
245                 return 6;
246         case 0xe0:
247                 return 7;
248         default:
249                 return 0;
250         }
251 }
252
253 /* Incoming skb is converted to a txb which consists of
254  * a block of 802.11 fragment packets (stored as skbs) */
255 int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
256 {
257         struct ieee80211_device *ieee = netdev_priv(dev);
258         struct ieee80211_txb *txb = NULL;
259         struct ieee80211_hdr_3addrqos *frag_hdr;
260         int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size,
261             rts_required;
262         unsigned long flags;
263         struct net_device_stats *stats = &ieee->stats;
264         int ether_type, encrypt, host_encrypt, host_encrypt_msdu, host_build_iv;
265         int bytes, fc, hdr_len;
266         struct sk_buff *skb_frag;
267         struct ieee80211_hdr_3addrqos header = {/* Ensure zero initialized */
268                 .duration_id = 0,
269                 .seq_ctl = 0,
270                 .qos_ctl = 0
271         };
272         u8 dest[ETH_ALEN], src[ETH_ALEN];
273         struct ieee80211_crypt_data *crypt;
274         int priority = skb->priority;
275         int snapped = 0;
276
277         if (ieee->is_queue_full && (*ieee->is_queue_full) (dev, priority))
278                 return NETDEV_TX_BUSY;
279
280         spin_lock_irqsave(&ieee->lock, flags);
281
282         /* If there is no driver handler to take the TXB, dont' bother
283          * creating it... */
284         if (!ieee->hard_start_xmit) {
285                 printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
286                 goto success;
287         }
288
289         if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
290                 printk(KERN_WARNING "%s: skb too small (%d).\n",
291                        ieee->dev->name, skb->len);
292                 goto success;
293         }
294
295         ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
296
297         crypt = ieee->crypt[ieee->tx_keyidx];
298
299         encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
300             ieee->sec.encrypt;
301
302         host_encrypt = ieee->host_encrypt && encrypt && crypt;
303         host_encrypt_msdu = ieee->host_encrypt_msdu && encrypt && crypt;
304         host_build_iv = ieee->host_build_iv && encrypt && crypt;
305
306         if (!encrypt && ieee->ieee802_1x &&
307             ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
308                 stats->tx_dropped++;
309                 goto success;
310         }
311
312         /* Save source and destination addresses */
313         memcpy(dest, skb->data, ETH_ALEN);
314         memcpy(src, skb->data + ETH_ALEN, ETH_ALEN);
315
316         if (host_encrypt || host_build_iv)
317                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
318                     IEEE80211_FCTL_PROTECTED;
319         else
320                 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
321
322         if (ieee->iw_mode == IW_MODE_INFRA) {
323                 fc |= IEEE80211_FCTL_TODS;
324                 /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
325                 memcpy(header.addr1, ieee->bssid, ETH_ALEN);
326                 memcpy(header.addr2, src, ETH_ALEN);
327                 memcpy(header.addr3, dest, ETH_ALEN);
328         } else if (ieee->iw_mode == IW_MODE_ADHOC) {
329                 /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
330                 memcpy(header.addr1, dest, ETH_ALEN);
331                 memcpy(header.addr2, src, ETH_ALEN);
332                 memcpy(header.addr3, ieee->bssid, ETH_ALEN);
333         }
334         hdr_len = IEEE80211_3ADDR_LEN;
335
336         if (ieee->is_qos_active && ieee->is_qos_active(dev, skb)) {
337                 fc |= IEEE80211_STYPE_QOS_DATA;
338                 hdr_len += 2;
339
340                 skb->priority = ieee80211_classify(skb);
341                 header.qos_ctl |= skb->priority & IEEE80211_QCTL_TID;
342         }
343         header.frame_ctl = cpu_to_le16(fc);
344
345         /* Advance the SKB to the start of the payload */
346         skb_pull(skb, sizeof(struct ethhdr));
347
348         /* Determine total amount of storage required for TXB packets */
349         bytes = skb->len + SNAP_SIZE + sizeof(u16);
350
351         /* Encrypt msdu first on the whole data packet. */
352         if ((host_encrypt || host_encrypt_msdu) &&
353             crypt && crypt->ops && crypt->ops->encrypt_msdu) {
354                 int res = 0;
355                 int len = bytes + hdr_len + crypt->ops->extra_msdu_prefix_len +
356                     crypt->ops->extra_msdu_postfix_len;
357                 struct sk_buff *skb_new = dev_alloc_skb(len);
358
359                 if (unlikely(!skb_new))
360                         goto failed;
361
362                 skb_reserve(skb_new, crypt->ops->extra_msdu_prefix_len);
363                 memcpy(skb_put(skb_new, hdr_len), &header, hdr_len);
364                 snapped = 1;
365                 ieee80211_copy_snap(skb_put(skb_new, SNAP_SIZE + sizeof(u16)),
366                                     ether_type);
367                 memcpy(skb_put(skb_new, skb->len), skb->data, skb->len);
368                 res = crypt->ops->encrypt_msdu(skb_new, hdr_len, crypt->priv);
369                 if (res < 0) {
370                         IEEE80211_ERROR("msdu encryption failed\n");
371                         dev_kfree_skb_any(skb_new);
372                         goto failed;
373                 }
374                 dev_kfree_skb_any(skb);
375                 skb = skb_new;
376                 bytes += crypt->ops->extra_msdu_prefix_len +
377                     crypt->ops->extra_msdu_postfix_len;
378                 skb_pull(skb, hdr_len);
379         }
380
381         if (host_encrypt || ieee->host_open_frag) {
382                 /* Determine fragmentation size based on destination (multicast
383                  * and broadcast are not fragmented) */
384                 if (is_multicast_ether_addr(dest) ||
385                     is_broadcast_ether_addr(dest))
386                         frag_size = MAX_FRAG_THRESHOLD;
387                 else
388                         frag_size = ieee->fts;
389
390                 /* Determine amount of payload per fragment.  Regardless of if
391                  * this stack is providing the full 802.11 header, one will
392                  * eventually be affixed to this fragment -- so we must account
393                  * for it when determining the amount of payload space. */
394                 bytes_per_frag = frag_size - IEEE80211_3ADDR_LEN;
395                 if (ieee->config &
396                     (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
397                         bytes_per_frag -= IEEE80211_FCS_LEN;
398
399                 /* Each fragment may need to have room for encryptiong
400                  * pre/postfix */
401                 if (host_encrypt)
402                         bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
403                             crypt->ops->extra_mpdu_postfix_len;
404
405                 /* Number of fragments is the total
406                  * bytes_per_frag / payload_per_fragment */
407                 nr_frags = bytes / bytes_per_frag;
408                 bytes_last_frag = bytes % bytes_per_frag;
409                 if (bytes_last_frag)
410                         nr_frags++;
411                 else
412                         bytes_last_frag = bytes_per_frag;
413         } else {
414                 nr_frags = 1;
415                 bytes_per_frag = bytes_last_frag = bytes;
416                 frag_size = bytes + IEEE80211_3ADDR_LEN;
417         }
418
419         rts_required = (frag_size > ieee->rts
420                         && ieee->config & CFG_IEEE80211_RTS);
421         if (rts_required)
422                 nr_frags++;
423
424         /* When we allocate the TXB we allocate enough space for the reserve
425          * and full fragment bytes (bytes_per_frag doesn't include prefix,
426          * postfix, header, FCS, etc.) */
427         txb = ieee80211_alloc_txb(nr_frags, frag_size,
428                                   ieee->tx_headroom, GFP_ATOMIC);
429         if (unlikely(!txb)) {
430                 printk(KERN_WARNING "%s: Could not allocate TXB\n",
431                        ieee->dev->name);
432                 goto failed;
433         }
434         txb->encrypted = encrypt;
435         if (host_encrypt)
436                 txb->payload_size = frag_size * (nr_frags - 1) +
437                     bytes_last_frag;
438         else
439                 txb->payload_size = bytes;
440
441         if (rts_required) {
442                 skb_frag = txb->fragments[0];
443                 frag_hdr =
444                     (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
445
446                 /*
447                  * Set header frame_ctl to the RTS.
448                  */
449                 header.frame_ctl =
450                     cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
451                 memcpy(frag_hdr, &header, hdr_len);
452
453                 /*
454                  * Restore header frame_ctl to the original data setting.
455                  */
456                 header.frame_ctl = cpu_to_le16(fc);
457
458                 if (ieee->config &
459                     (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
460                         skb_put(skb_frag, 4);
461
462                 txb->rts_included = 1;
463                 i = 1;
464         } else
465                 i = 0;
466
467         for (; i < nr_frags; i++) {
468                 skb_frag = txb->fragments[i];
469
470                 if (host_encrypt || host_build_iv)
471                         skb_reserve(skb_frag,
472                                     crypt->ops->extra_mpdu_prefix_len);
473
474                 frag_hdr =
475                     (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
476                 memcpy(frag_hdr, &header, hdr_len);
477
478                 /* If this is not the last fragment, then add the MOREFRAGS
479                  * bit to the frame control */
480                 if (i != nr_frags - 1) {
481                         frag_hdr->frame_ctl =
482                             cpu_to_le16(fc | IEEE80211_FCTL_MOREFRAGS);
483                         bytes = bytes_per_frag;
484                 } else {
485                         /* The last fragment takes the remaining length */
486                         bytes = bytes_last_frag;
487                 }
488
489                 if (i == 0 && !snapped) {
490                         ieee80211_copy_snap(skb_put
491                                             (skb_frag, SNAP_SIZE + sizeof(u16)),
492                                             ether_type);
493                         bytes -= SNAP_SIZE + sizeof(u16);
494                 }
495
496                 memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
497
498                 /* Advance the SKB... */
499                 skb_pull(skb, bytes);
500
501                 /* Encryption routine will move the header forward in order
502                  * to insert the IV between the header and the payload */
503                 if (host_encrypt)
504                         ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
505                 else if (host_build_iv) {
506                         struct ieee80211_crypt_data *crypt;
507
508                         crypt = ieee->crypt[ieee->tx_keyidx];
509                         atomic_inc(&crypt->refcnt);
510                         if (crypt->ops->build_iv)
511                                 crypt->ops->build_iv(skb_frag, hdr_len,
512                                       ieee->sec.keys[ieee->sec.active_key],
513                                       ieee->sec.key_sizes[ieee->sec.active_key],
514                                       crypt->priv);
515                         atomic_dec(&crypt->refcnt);
516                 }
517
518                 if (ieee->config &
519                     (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
520                         skb_put(skb_frag, 4);
521         }
522
523       success:
524         spin_unlock_irqrestore(&ieee->lock, flags);
525
526         dev_kfree_skb_any(skb);
527
528         if (txb) {
529                 int ret = (*ieee->hard_start_xmit) (txb, dev, priority);
530                 if (ret == 0) {
531                         stats->tx_packets++;
532                         stats->tx_bytes += txb->payload_size;
533                         return 0;
534                 }
535
536                 if (ret == NETDEV_TX_BUSY) {
537                         printk(KERN_ERR "%s: NETDEV_TX_BUSY returned; "
538                                "driver should report queue full via "
539                                "ieee_device->is_queue_full.\n",
540                                ieee->dev->name);
541                 }
542
543                 ieee80211_txb_free(txb);
544         }
545
546         return 0;
547
548       failed:
549         spin_unlock_irqrestore(&ieee->lock, flags);
550         netif_stop_queue(dev);
551         stats->tx_errors++;
552         return 1;
553 }
554
555 /* Incoming 802.11 strucure is converted to a TXB
556  * a block of 802.11 fragment packets (stored as skbs) */
557 int ieee80211_tx_frame(struct ieee80211_device *ieee,
558                        struct ieee80211_hdr *frame, int len)
559 {
560         struct ieee80211_txb *txb = NULL;
561         unsigned long flags;
562         struct net_device_stats *stats = &ieee->stats;
563         struct sk_buff *skb_frag;
564         int priority = -1;
565
566         spin_lock_irqsave(&ieee->lock, flags);
567
568         /* If there is no driver handler to take the TXB, dont' bother
569          * creating it... */
570         if (!ieee->hard_start_xmit) {
571                 printk(KERN_WARNING "%s: No xmit handler.\n", ieee->dev->name);
572                 goto success;
573         }
574
575         if (unlikely(len < 24)) {
576                 printk(KERN_WARNING "%s: skb too small (%d).\n",
577                        ieee->dev->name, len);
578                 goto success;
579         }
580
581         /* When we allocate the TXB we allocate enough space for the reserve
582          * and full fragment bytes (bytes_per_frag doesn't include prefix,
583          * postfix, header, FCS, etc.) */
584         txb = ieee80211_alloc_txb(1, len, ieee->tx_headroom, GFP_ATOMIC);
585         if (unlikely(!txb)) {
586                 printk(KERN_WARNING "%s: Could not allocate TXB\n",
587                        ieee->dev->name);
588                 goto failed;
589         }
590         txb->encrypted = 0;
591         txb->payload_size = len;
592
593         skb_frag = txb->fragments[0];
594
595         memcpy(skb_put(skb_frag, len), frame, len);
596
597         if (ieee->config &
598             (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
599                 skb_put(skb_frag, 4);
600
601       success:
602         spin_unlock_irqrestore(&ieee->lock, flags);
603
604         if (txb) {
605                 if ((*ieee->hard_start_xmit) (txb, ieee->dev, priority) == 0) {
606                         stats->tx_packets++;
607                         stats->tx_bytes += txb->payload_size;
608                         return 0;
609                 }
610                 ieee80211_txb_free(txb);
611         }
612         return 0;
613
614       failed:
615         spin_unlock_irqrestore(&ieee->lock, flags);
616         stats->tx_errors++;
617         return 1;
618 }
619
620 EXPORT_SYMBOL(ieee80211_tx_frame);
621 EXPORT_SYMBOL(ieee80211_txb_free);