merge in changes from HEAD (merge of Fedora Core 2 Updates kernel-2.6.10-1.771_FC2)
[linux-2.6.git] / drivers / net / wireless / 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 #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/pci.h>
37 #include <linux/proc_fs.h>
38 #include <linux/skbuff.h>
39 #include <linux/slab.h>
40 #include <linux/tcp.h>
41 #include <linux/types.h>
42 #include <linux/version.h>
43 #include <linux/wireless.h>
44 #include <linux/etherdevice.h>
45 #include <asm/uaccess.h>
46
47 #include "ieee80211.h"
48
49
50 /*
51
52
53 802.11 Data Frame 
54
55       ,-------------------------------------------------------------------.
56 Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
57       |------|------|---------|---------|---------|------|---------|------|
58 Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
59       |      | tion | (BSSID) |         |         | ence |  data   |      |
60       `--------------------------------------------------|         |------'
61 Total: 28 non-data bytes                                 `----.----'  
62                                                               |
63        .- 'Frame data' expands to <---------------------------'
64        |
65        V
66       ,---------------------------------------------------.
67 Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
68       |------|------|---------|----------|------|---------|
69 Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
70       | DSAP | SSAP |         |          |      | Packet  |
71       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
72       `-----------------------------------------|         |
73 Total: 8 non-data bytes                         `----.----'
74                                                      |
75        .- 'IP Packet' expands, if WEP enabled, to <--'
76        |
77        V
78       ,-----------------------.
79 Bytes |  4  |   0-2296  |  4  |
80       |-----|-----------|-----|
81 Desc. | IV  | Encrypted | ICV |
82       |     | IP Packet |     |
83       `-----------------------'
84 Total: 8 non-data bytes
85
86
87 802.3 Ethernet Data Frame 
88
89       ,-----------------------------------------.
90 Bytes |   6   |   6   |  2   |  Variable |   4  |
91       |-------|-------|------|-----------|------|
92 Desc. | Dest. | Source| Type | IP Packet |  fcs |
93       |  MAC  |  MAC  |      |           |      |
94       `-----------------------------------------'
95 Total: 18 non-data bytes
96
97 In the event that fragmentation is required, the incoming payload is split into
98 N parts of size ieee->fts.  The first fragment contains the SNAP header and the
99 remaining packets are just data.
100
101 If encryption is enabled, each fragment payload size is reduced by enough space
102 to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
103 So if you have 1500 bytes of payload with ieee->fts set to 500 without 
104 encryption it will take 3 frames.  With WEP it will take 4 frames as the 
105 payload of each frame is reduced to 492 bytes.
106
107 * SKB visualization
108
109 *  ,- skb->data 
110 * |
111 * |    ETHERNET HEADER        ,-<-- PAYLOAD
112 * |                           |     14 bytes from skb->data 
113 * |  2 bytes for Type --> ,T. |     (sizeof ethhdr)
114 * |                       | | |    
115 * |,-Dest.--. ,--Src.---. | | |     
116 * |  6 bytes| | 6 bytes | | | |
117 * v         | |         | | | |
118 * 0         | v       1 | v | v           2       
119 * 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
120 *     ^     | ^         | ^ | 
121 *     |     | |         | | |  
122 *     |     | |         | `T' <---- 2 bytes for Type
123 *     |     | |         |
124 *     |     | '---SNAP--' <-------- 6 bytes for SNAP
125 *     |     |  
126 *     `-IV--' <-------------------- 4 bytes for IV (WEP)
127 *
128 *      SNAP HEADER
129 *
130 */
131
132 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
133 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
134
135 static inline int ieee80211_put_snap(u8 *data, u16 h_proto)
136 {
137         struct ieee80211_snap_hdr *snap;
138         u8 *oui;
139
140         snap = (struct ieee80211_snap_hdr *)data;
141         snap->dsap = 0xaa;
142         snap->ssap = 0xaa;
143         snap->ctrl = 0x03;
144
145         if (h_proto == 0x8137 || h_proto == 0x80f3)
146                 oui = P802_1H_OUI;
147         else
148                 oui = RFC1042_OUI;
149         snap->oui[0] = oui[0];
150         snap->oui[1] = oui[1];
151         snap->oui[2] = oui[2];
152
153         *(u16 *)(data + SNAP_SIZE) = htons(h_proto);
154
155         return SNAP_SIZE + sizeof(u16);
156 }
157
158 #ifdef CONFIG_IEEE80211_CRYPT
159 static inline int ieee80211_encrypt_fragment(
160         struct ieee80211_device *ieee, 
161         struct sk_buff *frag,
162         int hdr_len)
163 {
164         struct ieee80211_crypt_data* crypt = ieee->crypt[ieee->tx_keyidx];
165         int res;
166 #ifdef CONFIG_IEEE80211_WPA
167         struct ieee80211_hdr *header;
168         
169         if (ieee->tkip_countermeasures &&
170             crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
171                 header = (struct ieee80211_hdr *) frag->data;
172                 if (net_ratelimit()) {
173                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
174                                "TX packet to " MAC_FMT "\n",
175                                ieee->dev->name, MAC_ARG(header->addr1));
176                 }
177                 return -1;
178         }
179 #endif
180         /* To encrypt, frame format is:
181          * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
182
183         // PR: FIXME: Copied from hostap. Check fragmentation/MSDU/MPDU encryption.
184         /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
185          * call both MSDU and MPDU encryption functions from here. */
186         atomic_inc(&crypt->refcnt);
187         res = 0;
188         if (crypt->ops->encrypt_msdu)
189                 res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
190         if (res == 0 && crypt->ops->encrypt_mpdu)
191                 res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
192         
193         atomic_dec(&crypt->refcnt);
194         if (res < 0) {
195                 printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
196                        ieee->dev->name, frag->len);
197                 ieee->ieee_stats.tx_discards++;
198                 return -1;
199         }
200
201         return 0;
202 }
203 #endif
204
205
206 void ieee80211_txb_free(struct ieee80211_txb *txb) {
207         int i;
208         if (unlikely(!txb))
209                 return;
210         for (i = 0; i < txb->nr_frags; i++) 
211                 if (txb->fragments[i])
212                         dev_kfree_skb_any(txb->fragments[i]);
213         kfree(txb);
214 }
215
216 struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
217                                           int gfp_mask) {
218         struct ieee80211_txb *txb;
219         int i;
220         txb = kmalloc(
221                 sizeof(struct ieee80211_txb) + (sizeof(u8*) * nr_frags), 
222                 gfp_mask);
223         if (!txb)
224                 return NULL;
225
226         memset(txb, 0, sizeof(struct ieee80211_txb));
227         txb->nr_frags = nr_frags;
228         txb->frag_size = txb_size;
229
230         for (i = 0; i < nr_frags; i++) {
231                 txb->fragments[i] = dev_alloc_skb(txb_size);
232                 if (unlikely(!txb->fragments[i])) {
233                         i--;
234                         break;
235                 }
236         }
237         if (unlikely(i != nr_frags)) {
238                 while (i >= 0)
239                         dev_kfree_skb_any(txb->fragments[i--]);
240                 kfree(txb);
241                 return NULL;
242         }
243         return txb;
244 }
245
246 /* SKBs are added to the ieee->tx_queue. */
247 struct ieee80211_txb *ieee80211_skb_to_txb(struct ieee80211_device *ieee, 
248                                            struct sk_buff *skb)
249 {
250         struct ieee80211_txb *txb;
251         int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
252         unsigned long flags;
253         struct net_device_stats *stats = &ieee->stats;
254         int ether_type, encrypt;
255         int bytes, fc, hdr_len;
256         struct sk_buff *skb_frag;
257         struct ieee80211_hdr header;
258         u8 dest[ETH_ALEN], src[ETH_ALEN];
259
260 #ifdef CONFIG_IEEE80211_CRYPT
261         struct ieee80211_crypt_data* crypt;
262 #endif
263
264         spin_lock_irqsave(&ieee->lock, flags);
265
266         if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
267                 printk(KERN_WARNING "%s: skb too small (%d).\n",
268                        ieee->dev->name, skb->len);
269                 goto failed;
270         }
271
272         ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
273
274 #ifndef CONFIG_IEEE80211_CRYPT
275         encrypt = 0;
276 #else   /* CONFIG_IEEE80211_CRYPT */
277         crypt = ieee->crypt[ieee->tx_keyidx];
278
279 #ifndef CONFIG_IEEE80211_WPA
280         encrypt = (ether_type != ETH_P_PAE) && 
281                 ieee->host_encrypt && crypt && crypt->ops;
282         
283 #else /* CONFIG_IEEE80211_WPA */
284         encrypt = !(ether_type == ETH_P_PAE && ieee->ieee_802_1x) && 
285                 ieee->host_encrypt && crypt && crypt->ops;
286
287         if (!encrypt && ieee->ieee_802_1x &&
288             ieee->drop_unencrypted && ether_type != ETH_P_PAE){
289                 stats->tx_dropped++;
290                 /* FIXME: Allocate an empty txb and return it; this 
291                  * isn't the best code path since an alloc/free is
292                  * required for no real reason except to return a
293                  * special case success code... */
294                 txb = ieee80211_alloc_txb(0, ieee->fts, GFP_ATOMIC);
295                 if (unlikely(!txb)) {
296                         printk(KERN_WARNING 
297                                "%s: Could not allocate TXB\n",
298                                ieee->dev->name);
299                         goto failed;
300                 }
301
302                 if (net_ratelimit()) {
303                         printk(KERN_DEBUG "%s: dropped unencrypted TX data "
304                                "frame (drop_unencrypted=1)\n",
305                                ieee->dev->name);
306                 }
307
308                 goto success;
309         }
310
311 #endif /* CONFIG_IEEE80211_WPA */
312         if (crypt && !encrypt && ether_type == ETH_P_PAE) 
313                 IEEE80211_DEBUG_EAP("TX: IEEE 802.11 - sending EAPOL frame\n");
314 #endif  /* CONFIG_IEEE80211_CRYPT */
315
316         if (encrypt) {
317                 /* Save source and destination addresses */
318                 memcpy(&dest, skb->data, ETH_ALEN);
319                 memcpy(&src, skb->data+ETH_ALEN, ETH_ALEN);
320         }
321
322         /* Advance the SKB to the start of the payload */
323         skb_pull(skb, sizeof(struct ethhdr));
324
325         /* Determine total amount of storage required for TXB packets */
326         bytes = skb->len + SNAP_SIZE + sizeof(u16);
327
328         if (!ieee->tx_payload_only) {
329                 if (encrypt) 
330                         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
331                                 IEEE80211_FCTL_WEP;
332                 else
333                         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
334                 
335                 if (ieee->iw_mode == IW_MODE_INFRA) {
336                         fc |= IEEE80211_FCTL_TODS;
337                         hdr_len = 24;
338                         /* To DS: Addr1 = BSSID, Addr2 = SA, 
339                            Addr3 = DA */
340                         memcpy(&header.addr1, ieee->bssid, ETH_ALEN);
341                         memcpy(&header.addr2, &src, ETH_ALEN);
342                         memcpy(&header.addr3, &dest, ETH_ALEN);
343                 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
344                         /* not From/To DS: Addr1 = DA, Addr2 = SA, 
345                            Addr3 = BSSID */
346                         memcpy(&header.addr1, dest, ETH_ALEN);
347                         memcpy(&header.addr2, src, ETH_ALEN);
348                         memcpy(&header.addr3, ieee->bssid, ETH_ALEN);
349                 }               
350                 header.frame_ctl = cpu_to_le16(fc);
351                 hdr_len = IEEE80211_3ADDR_SIZE;
352         } else 
353                 hdr_len = 0;
354         
355         /* Determine amount of payload per fragment.  Regardless of if
356          * this stack is providing the full 802.11 header, one will 
357          * eventually be affixed to this fragment -- so we must account for
358          * it when determining the amount of payload space. */
359         if (is_multicast_ether_addr(dest) ||
360             is_broadcast_ether_addr(dest))
361                 frag_size = MAX_FRAG_THRESHOLD - IEEE80211_3ADDR_SIZE;
362         else
363                 frag_size = ieee->fts - IEEE80211_3ADDR_SIZE;
364
365         bytes_per_frag = frag_size;
366
367 #ifdef CONFIG_IEEE80211_CRYPT
368         /* Each fragment may need to have room for encryptiong pre/postfix */
369         if (encrypt) 
370                 bytes_per_frag -= crypt->ops->extra_prefix_len +
371                         crypt->ops->extra_postfix_len;
372 #endif
373
374         /* Number of fragments is the total bytes_per_frag / 
375          * payload_per_fragment */
376         nr_frags = bytes / bytes_per_frag;
377         bytes_last_frag = bytes % bytes_per_frag;
378         if (bytes_last_frag)
379                 nr_frags++;
380         else 
381                 bytes_last_frag = bytes_per_frag;
382
383         /* When we allocate the TXB we allocate enough space for the reserve
384          * and full fragment bytes (bytes_per_frag doesn't include prefix and 
385          * postfix) */
386         txb = ieee80211_alloc_txb(nr_frags, frag_size, GFP_ATOMIC);
387         if (unlikely(!txb)) {
388                 printk(KERN_WARNING "%s: Could not allocate TXB\n",
389                        ieee->dev->name);
390                 goto failed;
391         }
392         txb->encrypted = encrypt;
393         txb->payload_size = bytes;
394
395         for (i = 0; i < nr_frags; i++) {
396                 skb_frag = txb->fragments[i];
397
398 #ifdef CONFIG_IEEE80211_CRYPT
399                 if (encrypt) 
400                         skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
401 #endif
402
403                 if (hdr_len)
404                         memcpy(skb_put(skb_frag, hdr_len), &header, hdr_len);
405                 
406                 bytes = (i == nr_frags - 1) ? bytes_last_frag : bytes_per_frag;
407
408                 /* Put a SNAP header on the first fragment */
409                 if (i == 0) {
410                         ieee80211_put_snap(
411                                 skb_put(skb_frag, SNAP_SIZE + sizeof(u16)), 
412                                 ether_type);
413                         bytes -= SNAP_SIZE + sizeof(u16);
414                 }
415
416                 memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
417
418                 /* Advance the SKB... */
419                 skb_pull(skb, bytes);
420
421 #ifdef CONFIG_IEEE80211_CRYPT
422                 /* Encryption routine will move the header forward in order
423                  * to insert the IV between the header and the payload */
424                 if (encrypt) {
425                         ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
426                         skb_pull(skb_frag, hdr_len);
427                 }
428 #endif
429         }
430
431         stats->tx_packets++;
432         stats->tx_bytes += txb->payload_size;
433
434 #ifdef CONFIG_IEEE80211_WPA
435  success:
436 #endif
437         /* We are now done with the SKB provided to us */
438         dev_kfree_skb_any(skb);
439         
440         spin_unlock_irqrestore(&ieee->lock, flags);
441
442         return txb;
443
444  failed:
445         stats->tx_errors++;
446
447         return NULL;
448
449 }
450
451 EXPORT_SYMBOL(ieee80211_skb_to_txb);
452 EXPORT_SYMBOL(ieee80211_txb_free);