Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / net / ieee80211 / softmac / ieee80211softmac_io.c
1 /* 
2  * Some parts based on code from net80211
3  * Copyright (c) 2001 Atsushi Onoe
4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include "ieee80211softmac_priv.h"
36
37 /* Helper functions for inserting data into the frames */
38
39 /* 
40  * Adds an ESSID element to the frame
41  *
42  */
43 static u8 *
44 ieee80211softmac_add_essid(u8 *dst, struct ieee80211softmac_essid *essid)
45 {
46         if (essid) {
47                 *dst++ = MFIE_TYPE_SSID;
48                 *dst++ = essid->len;
49                 memcpy(dst, essid->data, essid->len);
50                 return dst+essid->len;
51         } else {
52                 *dst++ = MFIE_TYPE_SSID;
53                 *dst++ = 0;
54                 return dst;
55         }
56 }     
57
58 /* Adds Supported Rates and if required Extended Rates Information Element
59  * to the frame, ASSUMES WE HAVE A SORTED LIST OF RATES */
60 static u8 *
61 ieee80211softmac_frame_add_rates(u8 *dst, const struct ieee80211softmac_ratesinfo *r)
62 {
63         int cck_len, ofdm_len;
64         *dst++ = MFIE_TYPE_RATES;
65
66         for(cck_len=0; ieee80211_is_cck_rate(r->rates[cck_len]) && (cck_len < r->count);cck_len++);
67
68         if(cck_len > IEEE80211SOFTMAC_MAX_RATES_LEN)
69                 cck_len = IEEE80211SOFTMAC_MAX_RATES_LEN;
70         *dst++ = cck_len;
71         memcpy(dst, r->rates, cck_len);
72         dst += cck_len;
73
74         if(cck_len < r->count){
75                 for (ofdm_len=0; ieee80211_is_ofdm_rate(r->rates[ofdm_len + cck_len]) && (ofdm_len + cck_len < r->count); ofdm_len++);
76                 if (ofdm_len > 0) {
77                         if (ofdm_len > IEEE80211SOFTMAC_MAX_EX_RATES_LEN)
78                                 ofdm_len = IEEE80211SOFTMAC_MAX_EX_RATES_LEN;
79                         *dst++ = MFIE_TYPE_RATES_EX;
80                         *dst++ = ofdm_len;
81                         memcpy(dst, r->rates + cck_len, ofdm_len);
82                         dst += ofdm_len;
83                 }
84         }       
85         return dst;
86 }
87
88 /* Allocate a management frame */
89 static u8 * 
90 ieee80211softmac_alloc_mgt(u32 size)
91 {
92         u8 * data;
93         
94         /* Add the header and FCS to the size */
95         size = size + IEEE80211_3ADDR_LEN;      
96         if(size > IEEE80211_DATA_LEN)
97                 return NULL;
98         /* Allocate the frame */
99         data = kmalloc(size, GFP_ATOMIC);
100         memset(data, 0, size);
101         return data;
102 }
103
104 /*
105  * Add a 2 Address Header
106  */
107 static void 
108 ieee80211softmac_hdr_2addr(struct ieee80211softmac_device *mac,
109         struct ieee80211_hdr_2addr *header, u32 type, u8 *dest)
110 {
111         /* Fill in the frame control flags */
112         header->frame_ctl = cpu_to_le16(type);
113         /* Control packets always have WEP turned off */        
114         if(type > IEEE80211_STYPE_CFENDACK && type < IEEE80211_STYPE_PSPOLL)
115                 header->frame_ctl |= mac->ieee->sec.level ? cpu_to_le16(IEEE80211_FCTL_PROTECTED) : 0;
116
117         /* Fill in the duration */
118         header->duration_id = 0;
119         /* FIXME: How do I find this?
120          * calculate. But most drivers just fill in 0 (except if it's a station id of course) */
121
122         /* Fill in the Destination Address */
123         if(dest == NULL)
124                 memset(header->addr1, 0xFF, ETH_ALEN);
125         else
126                 memcpy(header->addr1, dest, ETH_ALEN);
127         /* Fill in the Source Address */
128         memcpy(header->addr2, mac->ieee->dev->dev_addr, ETH_ALEN);
129
130 }
131
132
133 /* Add a 3 Address Header */
134 static void 
135 ieee80211softmac_hdr_3addr(struct ieee80211softmac_device *mac,
136         struct ieee80211_hdr_3addr *header, u32 type, u8 *dest, u8 *bssid)
137 {
138         /* This is common with 2addr, so use that instead */
139         ieee80211softmac_hdr_2addr(mac, (struct ieee80211_hdr_2addr *)header, type, dest);      
140         
141         /* Fill in the BSS ID */
142         if(bssid == NULL)
143                 memset(header->addr3, 0xFF, ETH_ALEN);
144         else
145                 memcpy(header->addr3, bssid, ETH_ALEN);
146
147         /* Fill in the sequence # */
148         /* FIXME: I need to add this to the softmac struct
149          * shouldn't the sequence number be in ieee80211? */
150 }
151
152
153 /*****************************************************************************
154  * Create Management packets
155  *****************************************************************************/ 
156
157 /* Creates an association request packet */
158 static u32
159 ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt, 
160         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
161 {
162         u8 *data;
163         (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
164                 2 +             /* Capability Info */
165                 2 +             /* Listen Interval */
166                 /* SSID IE */
167                 1 + 1 + IW_ESSID_MAX_SIZE +
168                 /* Rates IE */
169                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
170                 /* Extended Rates IE */
171                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
172                 /* WPA IE if present */
173                 mac->wpa.IElen
174                 /* Other IE's?  Optional?
175                  * Yeah, probably need an extra IE parameter -- lots of vendors like to
176                  * fill in their own IEs */
177         );
178         if (unlikely((*pkt) == NULL))
179                 return 0;
180         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
181
182         /* Fill in capability Info */
183         switch (mac->ieee->iw_mode) {
184         case IW_MODE_INFRA:
185                 (*pkt)->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
186                 break;
187         case IW_MODE_ADHOC:
188                 (*pkt)->capability = cpu_to_le16(WLAN_CAPABILITY_IBSS);
189                 break;
190         case IW_MODE_AUTO:
191                 (*pkt)->capability = net->capabilities & (WLAN_CAPABILITY_ESS|WLAN_CAPABILITY_IBSS);
192                 break;
193         default:
194                 /* bleh. we don't ever go to these modes */
195                 printk(KERN_ERR PFX "invalid iw_mode!\n");
196                 break;
197         }
198         /* Need to add this
199         (*pkt)->capability |= mac->ieee->short_slot ? 
200                         cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME) : 0;
201          */
202         (*pkt)->capability |= mac->ieee->sec.level ? cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
203         /* Fill in Listen Interval (?) */
204         (*pkt)->listen_interval = cpu_to_le16(10);
205         
206         data = (u8 *)(*pkt)->info_element;
207         /* Add SSID */
208         data = ieee80211softmac_add_essid(data, &net->essid);
209         /* Add Rates */
210         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
211         /* Add WPA IE */
212         if (mac->wpa.IElen && mac->wpa.IE) {
213                 memcpy(data, mac->wpa.IE, mac->wpa.IElen);
214                 data += mac->wpa.IElen;
215         }
216         /* Return the number of used bytes */
217         return (data - (u8*)(*pkt));
218 }
219
220 /* Create a reassociation request packet */
221 static u32
222 ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt, 
223         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
224 {
225         u8 *data;
226         (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
227                 2 +             /* Capability Info */
228                 2 +             /* Listen Interval */
229                 ETH_ALEN +      /* AP MAC */
230                 /* SSID IE */
231                 1 + 1 + IW_ESSID_MAX_SIZE +
232                 /* Rates IE */
233                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
234                 /* Extended Rates IE */
235                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN 
236                 /* Other IE's? */
237         );                              
238         if (unlikely((*pkt) == NULL))
239                 return 0;
240         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
241
242         /* Fill in capability Info */
243         (*pkt)->capability = mac->ieee->iw_mode == IW_MODE_MASTER ? 
244                                 cpu_to_le16(WLAN_CAPABILITY_ESS) :
245                                 cpu_to_le16(WLAN_CAPABILITY_IBSS);
246         /*
247         (*pkt)->capability |= mac->ieee->short_slot ? 
248                         cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME) : 0;
249          */
250         (*pkt)->capability |= mac->ieee->sec.level ?
251                         cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
252                 
253         /* Fill in Listen Interval (?) */
254         (*pkt)->listen_interval = cpu_to_le16(10);
255         /* Fill in the current AP MAC */
256         memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
257         
258         data = (u8 *)(*pkt)->info_element;
259         /* Add SSID */
260         data = ieee80211softmac_add_essid(data, &net->essid); 
261         /* Add Rates */
262         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
263         /* Return packet size */
264         return (data - (u8 *)(*pkt));
265 }
266
267 /* Create an authentication packet */
268 static u32
269 ieee80211softmac_auth(struct ieee80211_auth **pkt, 
270         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
271         u16 transaction, u16 status)
272 {
273         u8 *data;
274         /* Allocate Packet */
275         (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
276                 2 +             /* Auth Algorithm */
277                 2 +             /* Auth Transaction Seq */
278                 2 +             /* Status Code */
279                  /* Challenge Text IE */
280                 mac->ieee->open_wep ? 0 : 
281                 1 + 1 + WLAN_AUTH_CHALLENGE_LEN
282         );      
283         if (unlikely((*pkt) == NULL))
284                 return 0;
285         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
286                 
287         /* Algorithm */
288         (*pkt)->algorithm = mac->ieee->open_wep ? 
289                 cpu_to_le16(WLAN_AUTH_OPEN) :
290                 cpu_to_le16(WLAN_AUTH_SHARED_KEY);
291         /* Transaction */
292         (*pkt)->transaction = cpu_to_le16(transaction);
293         /* Status */
294         (*pkt)->status = cpu_to_le16(status);
295         
296         data = (u8 *)(*pkt)->info_element;
297         /* Challenge Text */
298         if(!mac->ieee->open_wep){
299                 *data = MFIE_TYPE_CHALLENGE;
300                 data++;
301                 
302                 /* Copy the challenge in */
303                 // *data = challenge length
304                 // data += sizeof(u16);
305                 // memcpy(data, challenge, challenge length);
306                 // data += challenge length;
307                 
308                 /* Add the full size to the packet length */
309         }
310
311         /* Return the packet size */
312         return (data - (u8 *)(*pkt));
313 }
314
315 /* Create a disassocation or deauthentication packet */
316 static u32
317 ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
318         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
319         u16 type, u16 reason)
320 {
321         /* Allocate Packet */
322         (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
323         if (unlikely((*pkt) == NULL))
324                 return 0;
325         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
326         /* Reason */
327         (*pkt)->reason = cpu_to_le16(reason);
328         /* Return the packet size */
329         return (2 + IEEE80211_3ADDR_LEN);
330 }
331
332 /* Create a probe request packet */
333 static u32
334 ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
335         struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
336 {
337         u8 *data;       
338         /* Allocate Packet */
339         (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
340                 /* SSID of requested network */
341                 1 + 1 + IW_ESSID_MAX_SIZE +
342                 /* Rates IE */
343                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
344                 /* Extended Rates IE */
345                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN 
346         );
347         if (unlikely((*pkt) == NULL))
348                 return 0;
349         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
350                 
351         data = (u8 *)(*pkt)->info_element;
352         /* Add ESSID (can be NULL) */
353         data = ieee80211softmac_add_essid(data, essid);
354         /* Add Rates */
355         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
356         /* Return packet size */
357         return (data - (u8 *)(*pkt));
358 }
359
360 /* Create a probe response packet */
361 /* FIXME: Not complete */
362 static u32
363 ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
364         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
365 {
366         u8 *data;
367         /* Allocate Packet */
368         (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
369                 8 +             /* Timestamp */
370                 2 +             /* Beacon Interval */
371                 2 +             /* Capability Info */
372                                 /* SSID IE */
373                 1 + 1 + IW_ESSID_MAX_SIZE +
374                 7 +             /* FH Parameter Set */
375                 2 +             /* DS Parameter Set */
376                 8 +             /* CF Parameter Set */
377                 4               /* IBSS Parameter Set */
378         );      
379         if (unlikely((*pkt) == NULL))
380                 return 0;
381         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
382         data = (u8 *)(*pkt)->info_element;
383
384         /* Return the packet size */
385         return (data - (u8 *)(*pkt));
386 }
387
388
389 /* Sends a manangement packet
390  * FIXME: document the use of the arg parameter
391  * for _AUTH: (transaction #) | (status << 16)
392  */
393 int
394 ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
395         void *ptrarg, u32 type, u32 arg)
396 {
397         void *pkt = NULL;
398         u32 pkt_size = 0;
399
400         switch(type) {
401         case IEEE80211_STYPE_ASSOC_REQ:
402                 pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
403                 break;
404         case IEEE80211_STYPE_REASSOC_REQ:
405                 pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
406                 break;
407         case IEEE80211_STYPE_AUTH:
408                 pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16));
409                 break;
410         case IEEE80211_STYPE_DISASSOC:
411         case IEEE80211_STYPE_DEAUTH:
412                 pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
413                 break;
414         case IEEE80211_STYPE_PROBE_REQ:
415                 pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
416                 break;
417         case IEEE80211_STYPE_PROBE_RESP:
418                 pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
419                 break;
420         default:
421                 printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
422                 return -EINVAL;
423         };
424
425         if(pkt_size == 0 || pkt == NULL) {
426                 printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
427                 return -ENOMEM;
428         }
429         
430         /* Send the packet to the ieee80211 layer for tx */
431         /* we defined softmac->mgmt_xmit for this. Should we keep it
432          * as it is (that means we'd need to wrap this into a txb),
433          * modify the prototype (so it matches this function),
434          * or get rid of it alltogether?
435          * Does this work for you now?
436          */
437         ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt, pkt_size);
438
439         kfree(pkt);
440         return 0;
441 }
442
443
444 /* Create an rts/cts frame */
445 static u32
446 ieee80211softmac_rts_cts(struct ieee80211_hdr_2addr **pkt,
447         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net, 
448         u32 type)
449 {
450         /* Allocate Packet */
451         (*pkt) = kmalloc(IEEE80211_2ADDR_LEN, GFP_ATOMIC);      
452         memset(*pkt, 0, IEEE80211_2ADDR_LEN);
453         if((*pkt) == NULL)
454                 return 0;
455         ieee80211softmac_hdr_2addr(mac, (*pkt), type, net->bssid);
456         return IEEE80211_2ADDR_LEN;
457 }
458
459
460 /* Sends a control packet */
461 static int
462 ieee80211softmac_send_ctl_frame(struct ieee80211softmac_device *mac,
463         struct ieee80211softmac_network *net, u32 type, u32 arg)
464 {
465         void *pkt = NULL;
466         u32 pkt_size = 0;
467         
468         switch(type) {
469         case IEEE80211_STYPE_RTS:
470         case IEEE80211_STYPE_CTS:
471                 pkt_size = ieee80211softmac_rts_cts((struct ieee80211_hdr_2addr **)(&pkt), mac, net, type);
472                 break;
473         default:
474                 printkl(KERN_DEBUG PFX "Unsupported Control Frame type: %i\n", type);
475                 return -EINVAL;
476         }
477
478         if(pkt_size == 0)
479                 return -ENOMEM;
480         
481         /* Send the packet to the ieee80211 layer for tx */
482         ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *) pkt, pkt_size);
483
484         kfree(pkt);
485         return 0;
486 }