[PATCH] SoftMAC: Prevent multiple authentication attempts on the same network
[linux-2.6] / 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 static u16
153 ieee80211softmac_capabilities(struct ieee80211softmac_device *mac,
154         struct ieee80211softmac_network *net)
155 {
156         u16 capability = 0;
157
158         /* ESS and IBSS bits are set according to the current mode */
159         switch (mac->ieee->iw_mode) {
160         case IW_MODE_INFRA:
161                 capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
162                 break;
163         case IW_MODE_ADHOC:
164                 capability = cpu_to_le16(WLAN_CAPABILITY_IBSS);
165                 break;
166         case IW_MODE_AUTO:
167                 capability = net->capabilities &
168                         (WLAN_CAPABILITY_ESS|WLAN_CAPABILITY_IBSS);
169                 break;
170         default:
171                 /* bleh. we don't ever go to these modes */
172                 printk(KERN_ERR PFX "invalid iw_mode!\n");
173                 break;
174         }
175
176         /* CF Pollable / CF Poll Request */
177         /* Needs to be implemented, for now, the 0's == not supported */
178
179         /* Privacy Bit */
180         capability |= mac->ieee->sec.level ?
181                 cpu_to_le16(WLAN_CAPABILITY_PRIVACY) : 0;
182
183         /* Short Preamble */
184         /* Always supported: we probably won't ever be powering devices which
185          * dont support this... */
186         capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
187
188         /* PBCC */
189         /* Not widely used */
190
191         /* Channel Agility */
192         /* Not widely used */
193
194         /* Short Slot */
195         /* Will be implemented later */
196
197         /* DSSS-OFDM */
198         /* Not widely used */
199
200         return capability;
201 }
202
203 /*****************************************************************************
204  * Create Management packets
205  *****************************************************************************/ 
206
207 /* Creates an association request packet */
208 static u32
209 ieee80211softmac_assoc_req(struct ieee80211_assoc_request **pkt, 
210         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
211 {
212         u8 *data;
213         (*pkt) = (struct ieee80211_assoc_request *)ieee80211softmac_alloc_mgt(
214                 2 +             /* Capability Info */
215                 2 +             /* Listen Interval */
216                 /* SSID IE */
217                 1 + 1 + IW_ESSID_MAX_SIZE +
218                 /* Rates IE */
219                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
220                 /* Extended Rates IE */
221                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN +
222                 /* WPA IE if present */
223                 mac->wpa.IElen
224                 /* Other IE's?  Optional?
225                  * Yeah, probably need an extra IE parameter -- lots of vendors like to
226                  * fill in their own IEs */
227         );
228         if (unlikely((*pkt) == NULL))
229                 return 0;
230         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_ASSOC_REQ, net->bssid, net->bssid);
231
232         /* Fill in the capabilities */
233         (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
234
235         /* Fill in Listen Interval (?) */
236         (*pkt)->listen_interval = cpu_to_le16(10);
237         
238         data = (u8 *)(*pkt)->info_element;
239         /* Add SSID */
240         data = ieee80211softmac_add_essid(data, &net->essid);
241         /* Add Rates */
242         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
243         /* Add WPA IE */
244         if (mac->wpa.IElen && mac->wpa.IE) {
245                 memcpy(data, mac->wpa.IE, mac->wpa.IElen);
246                 data += mac->wpa.IElen;
247         }
248         /* Return the number of used bytes */
249         return (data - (u8*)(*pkt));
250 }
251
252 /* Create a reassociation request packet */
253 static u32
254 ieee80211softmac_reassoc_req(struct ieee80211_reassoc_request **pkt, 
255         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
256 {
257         u8 *data;
258         (*pkt) = (struct ieee80211_reassoc_request *)ieee80211softmac_alloc_mgt(
259                 2 +             /* Capability Info */
260                 2 +             /* Listen Interval */
261                 ETH_ALEN +      /* AP MAC */
262                 /* SSID IE */
263                 1 + 1 + IW_ESSID_MAX_SIZE +
264                 /* Rates IE */
265                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
266                 /* Extended Rates IE */
267                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN 
268                 /* Other IE's? */
269         );                              
270         if (unlikely((*pkt) == NULL))
271                 return 0;
272         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_REASSOC_REQ, net->bssid, net->bssid);
273
274         /* Fill in the capabilities */
275         (*pkt)->capability = ieee80211softmac_capabilities(mac, net);
276
277         /* Fill in Listen Interval (?) */
278         (*pkt)->listen_interval = cpu_to_le16(10);
279         /* Fill in the current AP MAC */
280         memcpy((*pkt)->current_ap, mac->ieee->bssid, ETH_ALEN);
281         
282         data = (u8 *)(*pkt)->info_element;
283         /* Add SSID */
284         data = ieee80211softmac_add_essid(data, &net->essid); 
285         /* Add Rates */
286         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
287         /* Return packet size */
288         return (data - (u8 *)(*pkt));
289 }
290
291 /* Create an authentication packet */
292 static u32
293 ieee80211softmac_auth(struct ieee80211_auth **pkt, 
294         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
295         u16 transaction, u16 status, int *encrypt_mpdu)
296 {
297         u8 *data;
298         int auth_mode = mac->ieee->sec.auth_mode;
299         int is_shared_response = (auth_mode == WLAN_AUTH_SHARED_KEY
300                 && transaction == IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE);
301
302         /* Allocate Packet */
303         (*pkt) = (struct ieee80211_auth *)ieee80211softmac_alloc_mgt(
304                 2 +             /* Auth Algorithm */
305                 2 +             /* Auth Transaction Seq */
306                 2 +             /* Status Code */
307                  /* Challenge Text IE */
308                 is_shared_response ? 0 : 1 + 1 + net->challenge_len
309         );
310         if (unlikely((*pkt) == NULL))
311                 return 0;
312         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_AUTH, net->bssid, net->bssid);
313                 
314         /* Algorithm */
315         (*pkt)->algorithm = cpu_to_le16(auth_mode);
316         /* Transaction */
317         (*pkt)->transaction = cpu_to_le16(transaction);
318         /* Status */
319         (*pkt)->status = cpu_to_le16(status);
320         
321         data = (u8 *)(*pkt)->info_element;
322         /* Challenge Text */
323         if (is_shared_response) {
324                 *data = MFIE_TYPE_CHALLENGE;
325                 data++;
326                 
327                 /* Copy the challenge in */
328                 *data = net->challenge_len;
329                 data++;
330                 memcpy(data, net->challenge, net->challenge_len);
331                 data += net->challenge_len;
332
333                 /* Make sure this frame gets encrypted with the shared key */
334                 *encrypt_mpdu = 1;
335         } else
336                 *encrypt_mpdu = 0;
337
338         /* Return the packet size */
339         return (data - (u8 *)(*pkt));
340 }
341
342 /* Create a disassocation or deauthentication packet */
343 static u32
344 ieee80211softmac_disassoc_deauth(struct ieee80211_disassoc **pkt,
345         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net,
346         u16 type, u16 reason)
347 {
348         /* Allocate Packet */
349         (*pkt) = (struct ieee80211_disassoc *)ieee80211softmac_alloc_mgt(2);
350         if (unlikely((*pkt) == NULL))
351                 return 0;
352         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), type, net->bssid, net->bssid);
353         /* Reason */
354         (*pkt)->reason = cpu_to_le16(reason);
355         /* Return the packet size */
356         return (2 + IEEE80211_3ADDR_LEN);
357 }
358
359 /* Create a probe request packet */
360 static u32
361 ieee80211softmac_probe_req(struct ieee80211_probe_request **pkt,
362         struct ieee80211softmac_device *mac, struct ieee80211softmac_essid *essid)
363 {
364         u8 *data;       
365         /* Allocate Packet */
366         (*pkt) = (struct ieee80211_probe_request *)ieee80211softmac_alloc_mgt(
367                 /* SSID of requested network */
368                 1 + 1 + IW_ESSID_MAX_SIZE +
369                 /* Rates IE */
370                 1 + 1 + IEEE80211SOFTMAC_MAX_RATES_LEN +
371                 /* Extended Rates IE */
372                 1 + 1 + IEEE80211SOFTMAC_MAX_EX_RATES_LEN 
373         );
374         if (unlikely((*pkt) == NULL))
375                 return 0;
376         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_REQ, NULL, NULL);
377                 
378         data = (u8 *)(*pkt)->info_element;
379         /* Add ESSID (can be NULL) */
380         data = ieee80211softmac_add_essid(data, essid);
381         /* Add Rates */
382         data = ieee80211softmac_frame_add_rates(data, &mac->ratesinfo);
383         /* Return packet size */
384         return (data - (u8 *)(*pkt));
385 }
386
387 /* Create a probe response packet */
388 /* FIXME: Not complete */
389 static u32
390 ieee80211softmac_probe_resp(struct ieee80211_probe_response **pkt,
391         struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
392 {
393         u8 *data;
394         /* Allocate Packet */
395         (*pkt) = (struct ieee80211_probe_response *)ieee80211softmac_alloc_mgt(
396                 8 +             /* Timestamp */
397                 2 +             /* Beacon Interval */
398                 2 +             /* Capability Info */
399                                 /* SSID IE */
400                 1 + 1 + IW_ESSID_MAX_SIZE +
401                 7 +             /* FH Parameter Set */
402                 2 +             /* DS Parameter Set */
403                 8 +             /* CF Parameter Set */
404                 4               /* IBSS Parameter Set */
405         );      
406         if (unlikely((*pkt) == NULL))
407                 return 0;
408         ieee80211softmac_hdr_3addr(mac, &((*pkt)->header), IEEE80211_STYPE_PROBE_RESP, net->bssid, net->bssid);
409         data = (u8 *)(*pkt)->info_element;
410
411         /* Return the packet size */
412         return (data - (u8 *)(*pkt));
413 }
414
415
416 /* Sends a manangement packet
417  * FIXME: document the use of the arg parameter
418  * for _AUTH: (transaction #) | (status << 16)
419  */
420 int
421 ieee80211softmac_send_mgt_frame(struct ieee80211softmac_device *mac,
422         void *ptrarg, u32 type, u32 arg)
423 {
424         void *pkt = NULL;
425         u32 pkt_size = 0;
426         int encrypt_mpdu = 0;
427
428         switch(type) {
429         case IEEE80211_STYPE_ASSOC_REQ:
430                 pkt_size = ieee80211softmac_assoc_req((struct ieee80211_assoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
431                 break;
432         case IEEE80211_STYPE_REASSOC_REQ:
433                 pkt_size = ieee80211softmac_reassoc_req((struct ieee80211_reassoc_request **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
434                 break;
435         case IEEE80211_STYPE_AUTH:
436                 pkt_size = ieee80211softmac_auth((struct ieee80211_auth **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, (u16)(arg & 0xFFFF), (u16) (arg >> 16), &encrypt_mpdu);
437                 break;
438         case IEEE80211_STYPE_DISASSOC:
439         case IEEE80211_STYPE_DEAUTH:
440                 pkt_size = ieee80211softmac_disassoc_deauth((struct ieee80211_disassoc **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg, type, (u16)(arg & 0xFFFF));
441                 break;
442         case IEEE80211_STYPE_PROBE_REQ:
443                 pkt_size = ieee80211softmac_probe_req((struct ieee80211_probe_request **)(&pkt), mac, (struct ieee80211softmac_essid *)ptrarg);
444                 break;
445         case IEEE80211_STYPE_PROBE_RESP:
446                 pkt_size = ieee80211softmac_probe_resp((struct ieee80211_probe_response **)(&pkt), mac, (struct ieee80211softmac_network *)ptrarg);
447                 break;
448         default:
449                 printkl(KERN_DEBUG PFX "Unsupported Management Frame type: %i\n", type);
450                 return -EINVAL;
451         };
452
453         if(pkt_size == 0 || pkt == NULL) {
454                 printkl(KERN_DEBUG PFX "Error, packet is nonexistant or 0 length\n");
455                 return -ENOMEM;
456         }
457         
458         /* Send the packet to the ieee80211 layer for tx */
459         /* we defined softmac->mgmt_xmit for this. Should we keep it
460          * as it is (that means we'd need to wrap this into a txb),
461          * modify the prototype (so it matches this function),
462          * or get rid of it alltogether?
463          * Does this work for you now?
464          */
465         ieee80211_tx_frame(mac->ieee, (struct ieee80211_hdr *)pkt,
466                 IEEE80211_3ADDR_LEN, pkt_size, encrypt_mpdu);
467
468         kfree(pkt);
469         return 0;
470 }