[PATCH] Fix softmac scan
[powerpc.git] / net / ieee80211 / softmac / ieee80211softmac_auth.c
1 /*
2  * This file contains the softmac's authentication logic.
3  *
4  * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
5  *                          Joseph Jezak <josejx@gentoo.org>
6  *                          Larry Finger <Larry.Finger@lwfinger.net>
7  *                          Danny van Dyk <kugelfang@gentoo.org>
8  *                          Michael Buesch <mbuesch@freenet.de>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  *
23  * The full GNU General Public License is included in this distribution in the
24  * file called COPYING.
25  */
26
27 #include "ieee80211softmac_priv.h"
28
29 static void ieee80211softmac_auth_queue(void *data);
30
31 /* Queues an auth request to the desired AP */
32 int
33 ieee80211softmac_auth_req(struct ieee80211softmac_device *mac, 
34         struct ieee80211softmac_network *net)
35 {
36         struct ieee80211softmac_auth_queue_item *auth;
37         unsigned long flags;
38         
39         function_enter();
40         
41         if (net->authenticating)
42                 return 0;
43
44         /* Add the network if it's not already added */
45         ieee80211softmac_add_network(mac, net);
46
47         dprintk(KERN_NOTICE PFX "Queueing Authentication Request to "MAC_FMT"\n", MAC_ARG(net->bssid));
48         /* Queue the auth request */
49         auth = (struct ieee80211softmac_auth_queue_item *)
50                 kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
51         if(auth == NULL)
52                 return -ENOMEM;
53
54         auth->net = net;
55         auth->mac = mac;
56         auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
57         auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
58         INIT_WORK(&auth->work, &ieee80211softmac_auth_queue, (void *)auth);
59         
60         /* Lock (for list) */
61         spin_lock_irqsave(&mac->lock, flags);
62
63         /* add to list */
64         list_add_tail(&auth->list, &mac->auth_queue);
65         schedule_work(&auth->work);
66         spin_unlock_irqrestore(&mac->lock, flags);
67         
68         return 0;
69 }
70
71
72 /* Sends an auth request to the desired AP and handles timeouts */
73 static void
74 ieee80211softmac_auth_queue(void *data)
75 {
76         struct ieee80211softmac_device *mac;
77         struct ieee80211softmac_auth_queue_item *auth;
78         struct ieee80211softmac_network *net;
79         unsigned long flags;
80
81         function_enter();
82         
83         auth = (struct ieee80211softmac_auth_queue_item *)data;
84         net = auth->net;
85         mac = auth->mac;
86
87         if(auth->retry > 0) {
88                 /* Switch to correct channel for this network */
89                 mac->set_channel(mac->dev, net->channel);
90                 
91                 /* Lock and set flags */
92                 spin_lock_irqsave(&mac->lock, flags);
93                 net->authenticated = 0;
94                 net->authenticating = 1;
95                 /* add a timeout call so we eventually give up waiting for an auth reply */
96                 schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
97                 auth->retry--;
98                 spin_unlock_irqrestore(&mac->lock, flags);
99                 if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
100                         dprintk(KERN_NOTICE PFX "Sending Authentication Request to "MAC_FMT" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net->bssid));
101                 else
102                         dprintk(KERN_NOTICE PFX "Sent Authentication Request to "MAC_FMT".\n", MAC_ARG(net->bssid));
103                 return;
104         }
105
106         printkl(KERN_WARNING PFX "Authentication timed out with "MAC_FMT"\n", MAC_ARG(net->bssid));
107         /* Remove this item from the queue */
108         spin_lock_irqsave(&mac->lock, flags);
109         ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
110         cancel_delayed_work(&auth->work); /* just to make sure... */
111         list_del(&auth->list);
112         spin_unlock_irqrestore(&mac->lock, flags);
113         /* Free it */
114         kfree(auth);
115 }
116
117 /* Handle the auth response from the AP
118  * This should be registered with ieee80211 as handle_auth 
119  */
120 int 
121 ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
122 {       
123
124         struct list_head *list_ptr;
125         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
126         struct ieee80211softmac_auth_queue_item *aq = NULL;
127         struct ieee80211softmac_network *net = NULL;
128         unsigned long flags;
129         u8 * data;
130         
131         function_enter();
132         
133         /* Find correct auth queue item */
134         spin_lock_irqsave(&mac->lock, flags);
135         list_for_each(list_ptr, &mac->auth_queue) {
136                 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
137                 net = aq->net;
138                 if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
139                         break;
140                 else
141                         aq = NULL;
142         }
143         spin_unlock_irqrestore(&mac->lock, flags);
144         
145         /* Make sure that we've got an auth queue item for this request */
146         if(aq == NULL)
147         {
148                 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but no queue item exists.\n", MAC_ARG(auth->header.addr2));
149                 /* Error #? */
150                 return -1;
151         }                       
152         
153         /* Check for out of order authentication */
154         if(!net->authenticating)
155         {
156                 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but did not request authentication.\n",MAC_ARG(auth->header.addr2));
157                 return -1;
158         }
159
160         /* Parse the auth packet */
161         switch(auth->algorithm) {
162         case WLAN_AUTH_OPEN:
163                 /* Check the status code of the response */
164
165                 switch(auth->status) {
166                 case WLAN_STATUS_SUCCESS:
167                         /* Update the status to Authenticated */
168                         spin_lock_irqsave(&mac->lock, flags);
169                         net->authenticating = 0;
170                         net->authenticated = 1;
171                         spin_unlock_irqrestore(&mac->lock, flags);
172                         
173                         /* Send event */
174                         printkl(KERN_NOTICE PFX "Open Authentication completed with "MAC_FMT"\n", MAC_ARG(net->bssid));
175                         ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
176                         break;
177                 default:
178                         /* Lock and reset flags */
179                         spin_lock_irqsave(&mac->lock, flags);
180                         net->authenticated = 0;
181                         net->authenticating = 0;
182                         spin_unlock_irqrestore(&mac->lock, flags);
183                         
184                         printkl(KERN_NOTICE PFX "Open Authentication with "MAC_FMT" failed, error code: %i\n", 
185                                 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
186                         /* Count the error? */
187                         break;
188                 }
189                 goto free_aq;
190                 break;
191         case WLAN_AUTH_SHARED_KEY:
192                 /* Figure out where we are in the process */
193                 switch(auth->transaction) {
194                 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
195                         /* Check to make sure we have a challenge IE */
196                         data = (u8 *)auth->info_element;
197                         if(*data++ != MFIE_TYPE_CHALLENGE){
198                                 printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
199                                 break;  
200                         }
201                         /* Save the challenge */
202                         spin_lock_irqsave(&mac->lock, flags);
203                         net->challenge_len = *data++;   
204                         if(net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
205                                 net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
206                         if(net->challenge != NULL)
207                                 kfree(net->challenge);
208                         net->challenge = kmalloc(net->challenge_len, GFP_ATOMIC);
209                         memcpy(net->challenge, data, net->challenge_len);
210                         aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE; 
211                         spin_unlock_irqrestore(&mac->lock, flags);
212
213                         /* Switch to correct channel for this network */
214                         mac->set_channel(mac->dev, net->channel);
215                         
216                         /* Send our response (How to encrypt?) */
217                         ieee80211softmac_send_mgt_frame(mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
218                         break;
219                 case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
220                         /* Check the status code of the response */
221                         switch(auth->status) {
222                         case WLAN_STATUS_SUCCESS:
223                                 /* Update the status to Authenticated */        
224                                 spin_lock_irqsave(&mac->lock, flags);
225                                 net->authenticating = 0;
226                                 net->authenticated = 1;
227                                 spin_unlock_irqrestore(&mac->lock, flags);
228                                 printkl(KERN_NOTICE PFX "Shared Key Authentication completed with "MAC_FMT"\n", 
229                                         MAC_ARG(net->bssid));
230                                 break;
231                         default:
232                                 printkl(KERN_NOTICE PFX "Shared Key Authentication with "MAC_FMT" failed, error code: %i\n", 
233                                         MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
234                                 /* Lock and reset flags */
235                                 spin_lock_irqsave(&mac->lock, flags);
236                                 net->authenticating = 0;
237                                 net->authenticated = 0;
238                                 spin_unlock_irqrestore(&mac->lock, flags);
239                                 /* Count the error? */
240                                 break;
241                         }
242                         goto free_aq;
243                         break;
244                 default:
245                         printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
246                         break;
247                 }
248                 goto free_aq;
249                 break;
250         default:
251                 /* ERROR */     
252                 goto free_aq;
253                 break;
254         }
255         return 0;
256 free_aq:
257         /* Cancel the timeout */
258         spin_lock_irqsave(&mac->lock, flags);
259         cancel_delayed_work(&aq->work);
260         /* Remove this item from the queue */
261         list_del(&aq->list);
262         spin_unlock_irqrestore(&mac->lock, flags);
263
264         /* Free it */
265         kfree(aq);
266         return 0;
267 }
268
269 /*
270  * Handle deauthorization
271  */
272 static void
273 ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
274         struct ieee80211softmac_network *net)
275 {
276         struct ieee80211softmac_auth_queue_item *aq = NULL;
277         struct list_head *list_ptr;
278         unsigned long flags;
279
280         function_enter();
281         
282         /* Lock and reset status flags */
283         spin_lock_irqsave(&mac->lock, flags);
284         net->authenticating = 0;
285         net->authenticated = 0;
286         
287         /* Find correct auth queue item, if it exists */
288         list_for_each(list_ptr, &mac->auth_queue) {
289                 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
290                 if (!memcmp(net->bssid, aq->net->bssid, ETH_ALEN))
291                         break;
292                 else
293                         aq = NULL;
294         }
295         
296         /* Cancel pending work */
297         if(aq != NULL)
298                 /* Not entirely safe?  What about running work? */
299                 cancel_delayed_work(&aq->work);
300
301         /* Free our network ref */
302         ieee80211softmac_del_network_locked(mac, net);
303         if(net->challenge != NULL)
304                 kfree(net->challenge);
305         kfree(net);
306         
307         /* can't transmit data right now... */
308         netif_carrier_off(mac->dev);
309         /* let's try to re-associate */
310         schedule_work(&mac->associnfo.work);
311         spin_unlock_irqrestore(&mac->lock, flags);
312 }
313
314 /* 
315  * Sends a deauth request to the desired AP
316  */
317 int 
318 ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac, 
319         struct ieee80211softmac_network *net, int reason)
320 {
321         int ret;
322         
323         function_enter();
324
325         /* Make sure the network is authenticated */
326         if (!net->authenticated)
327         {
328                 printkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
329                 /* Error okay? */
330                 return -EPERM;
331         }
332         
333         /* Send the de-auth packet */
334         if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
335                 return ret;
336         
337         ieee80211softmac_deauth_from_net(mac, net);
338         return 0;
339 }
340  
341 /*
342  * This should be registered with ieee80211 as handle_deauth
343  */
344 int 
345 ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
346 {
347         
348         struct ieee80211softmac_network *net = NULL;
349         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
350         
351         function_enter();
352         
353         if (!deauth) {
354                 dprintk("deauth without deauth packet. eek!\n");
355                 return 0;
356         }
357
358         net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
359         
360         if (net == NULL) {
361                 printkl(KERN_DEBUG PFX "Received deauthentication packet from "MAC_FMT", but that network is unknown.\n",
362                         MAC_ARG(deauth->header.addr2));
363                 return 0;
364         }
365
366         /* Make sure the network is authenticated */
367         if(!net->authenticated)
368         {
369                 printkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
370                 /* Error okay? */
371                 return -EPERM;
372         }
373
374         ieee80211softmac_deauth_from_net(mac, net);
375         return 0;
376 }