blob: 06e332624665e8d456c505a376788993451d4ccb [file] [log] [blame]
Johannes Berg4855d252006-01-12 21:12:59 +01001/*
2 * This file contains the softmac's authentication logic.
3 *
Johannes Berg79859052006-01-31 19:31:41 +01004 * 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>
Johannes Berg4855d252006-01-12 21:12:59 +01009 *
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
Johannes Berg370121e2006-01-04 16:32:16 +010027#include "ieee80211softmac_priv.h"
28
29static void ieee80211softmac_auth_queue(void *data);
30
31/* Queues an auth request to the desired AP */
32int
33ieee80211softmac_auth_req(struct ieee80211softmac_device *mac,
34 struct ieee80211softmac_network *net)
35{
36 struct ieee80211softmac_auth_queue_item *auth;
37 unsigned long flags;
38
Johannes Berg370121e2006-01-04 16:32:16 +010039 if (net->authenticating)
40 return 0;
41
42 /* Add the network if it's not already added */
43 ieee80211softmac_add_network(mac, net);
44
45 dprintk(KERN_NOTICE PFX "Queueing Authentication Request to "MAC_FMT"\n", MAC_ARG(net->bssid));
46 /* Queue the auth request */
47 auth = (struct ieee80211softmac_auth_queue_item *)
48 kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
49 if(auth == NULL)
50 return -ENOMEM;
51
52 auth->net = net;
53 auth->mac = mac;
54 auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
55 auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
56 INIT_WORK(&auth->work, &ieee80211softmac_auth_queue, (void *)auth);
57
58 /* Lock (for list) */
59 spin_lock_irqsave(&mac->lock, flags);
60
61 /* add to list */
62 list_add_tail(&auth->list, &mac->auth_queue);
Johannes Berg5c4df6d2006-01-06 01:43:45 +010063 schedule_work(&auth->work);
Johannes Berg370121e2006-01-04 16:32:16 +010064 spin_unlock_irqrestore(&mac->lock, flags);
65
66 return 0;
67}
68
69
70/* Sends an auth request to the desired AP and handles timeouts */
71static void
72ieee80211softmac_auth_queue(void *data)
73{
74 struct ieee80211softmac_device *mac;
75 struct ieee80211softmac_auth_queue_item *auth;
76 struct ieee80211softmac_network *net;
77 unsigned long flags;
78
Johannes Berg370121e2006-01-04 16:32:16 +010079 auth = (struct ieee80211softmac_auth_queue_item *)data;
80 net = auth->net;
81 mac = auth->mac;
82
83 if(auth->retry > 0) {
84 /* Switch to correct channel for this network */
85 mac->set_channel(mac->dev, net->channel);
86
87 /* Lock and set flags */
88 spin_lock_irqsave(&mac->lock, flags);
Daniel Draked57336e2006-04-30 22:09:07 +010089 if (unlikely(!mac->running)) {
90 /* Prevent reschedule on workqueue flush */
91 spin_unlock_irqrestore(&mac->lock, flags);
92 return;
93 }
Johannes Berg370121e2006-01-04 16:32:16 +010094 net->authenticated = 0;
95 net->authenticating = 1;
96 /* add a timeout call so we eventually give up waiting for an auth reply */
Johannes Berg5c4df6d2006-01-06 01:43:45 +010097 schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
Johannes Berg370121e2006-01-04 16:32:16 +010098 auth->retry--;
99 spin_unlock_irqrestore(&mac->lock, flags);
100 if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
101 dprintk(KERN_NOTICE PFX "Sending Authentication Request to "MAC_FMT" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net->bssid));
102 else
103 dprintk(KERN_NOTICE PFX "Sent Authentication Request to "MAC_FMT".\n", MAC_ARG(net->bssid));
104 return;
105 }
106
107 printkl(KERN_WARNING PFX "Authentication timed out with "MAC_FMT"\n", MAC_ARG(net->bssid));
108 /* Remove this item from the queue */
109 spin_lock_irqsave(&mac->lock, flags);
110 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
111 cancel_delayed_work(&auth->work); /* just to make sure... */
112 list_del(&auth->list);
113 spin_unlock_irqrestore(&mac->lock, flags);
114 /* Free it */
115 kfree(auth);
116}
117
118/* Handle the auth response from the AP
119 * This should be registered with ieee80211 as handle_auth
120 */
121int
122ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
123{
124
125 struct list_head *list_ptr;
126 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
127 struct ieee80211softmac_auth_queue_item *aq = NULL;
128 struct ieee80211softmac_network *net = NULL;
129 unsigned long flags;
130 u8 * data;
131
Daniel Draked57336e2006-04-30 22:09:07 +0100132 if (unlikely(!mac->running))
133 return -ENODEV;
134
Johannes Berg370121e2006-01-04 16:32:16 +0100135 /* Find correct auth queue item */
136 spin_lock_irqsave(&mac->lock, flags);
137 list_for_each(list_ptr, &mac->auth_queue) {
138 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
139 net = aq->net;
140 if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
141 break;
142 else
143 aq = NULL;
144 }
145 spin_unlock_irqrestore(&mac->lock, flags);
146
147 /* Make sure that we've got an auth queue item for this request */
148 if(aq == NULL)
149 {
150 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but no queue item exists.\n", MAC_ARG(auth->header.addr2));
151 /* Error #? */
152 return -1;
153 }
154
155 /* Check for out of order authentication */
156 if(!net->authenticating)
157 {
158 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but did not request authentication.\n",MAC_ARG(auth->header.addr2));
159 return -1;
160 }
161
162 /* Parse the auth packet */
163 switch(auth->algorithm) {
164 case WLAN_AUTH_OPEN:
165 /* Check the status code of the response */
166
167 switch(auth->status) {
168 case WLAN_STATUS_SUCCESS:
169 /* Update the status to Authenticated */
170 spin_lock_irqsave(&mac->lock, flags);
171 net->authenticating = 0;
172 net->authenticated = 1;
173 spin_unlock_irqrestore(&mac->lock, flags);
174
175 /* Send event */
176 printkl(KERN_NOTICE PFX "Open Authentication completed with "MAC_FMT"\n", MAC_ARG(net->bssid));
177 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
178 break;
179 default:
180 /* Lock and reset flags */
181 spin_lock_irqsave(&mac->lock, flags);
182 net->authenticated = 0;
183 net->authenticating = 0;
184 spin_unlock_irqrestore(&mac->lock, flags);
185
186 printkl(KERN_NOTICE PFX "Open Authentication with "MAC_FMT" failed, error code: %i\n",
187 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
188 /* Count the error? */
189 break;
190 }
191 goto free_aq;
192 break;
193 case WLAN_AUTH_SHARED_KEY:
194 /* Figure out where we are in the process */
195 switch(auth->transaction) {
196 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
197 /* Check to make sure we have a challenge IE */
198 data = (u8 *)auth->info_element;
199 if(*data++ != MFIE_TYPE_CHALLENGE){
200 printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
201 break;
202 }
203 /* Save the challenge */
204 spin_lock_irqsave(&mac->lock, flags);
205 net->challenge_len = *data++;
206 if(net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
207 net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
208 if(net->challenge != NULL)
209 kfree(net->challenge);
210 net->challenge = kmalloc(net->challenge_len, GFP_ATOMIC);
211 memcpy(net->challenge, data, net->challenge_len);
212 aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
213 spin_unlock_irqrestore(&mac->lock, flags);
214
215 /* Switch to correct channel for this network */
216 mac->set_channel(mac->dev, net->channel);
217
218 /* Send our response (How to encrypt?) */
219 ieee80211softmac_send_mgt_frame(mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
220 break;
221 case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
222 /* Check the status code of the response */
223 switch(auth->status) {
224 case WLAN_STATUS_SUCCESS:
225 /* Update the status to Authenticated */
226 spin_lock_irqsave(&mac->lock, flags);
227 net->authenticating = 0;
228 net->authenticated = 1;
229 spin_unlock_irqrestore(&mac->lock, flags);
230 printkl(KERN_NOTICE PFX "Shared Key Authentication completed with "MAC_FMT"\n",
231 MAC_ARG(net->bssid));
232 break;
233 default:
234 printkl(KERN_NOTICE PFX "Shared Key Authentication with "MAC_FMT" failed, error code: %i\n",
235 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
236 /* Lock and reset flags */
237 spin_lock_irqsave(&mac->lock, flags);
238 net->authenticating = 0;
239 net->authenticated = 0;
240 spin_unlock_irqrestore(&mac->lock, flags);
241 /* Count the error? */
242 break;
243 }
244 goto free_aq;
245 break;
246 default:
247 printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
248 break;
249 }
250 goto free_aq;
251 break;
252 default:
253 /* ERROR */
254 goto free_aq;
255 break;
256 }
257 return 0;
258free_aq:
259 /* Cancel the timeout */
260 spin_lock_irqsave(&mac->lock, flags);
261 cancel_delayed_work(&aq->work);
262 /* Remove this item from the queue */
263 list_del(&aq->list);
264 spin_unlock_irqrestore(&mac->lock, flags);
265
266 /* Free it */
267 kfree(aq);
268 return 0;
269}
270
271/*
272 * Handle deauthorization
273 */
Johannes Berg714e1a52006-01-04 21:06:28 +0100274static void
Johannes Berg370121e2006-01-04 16:32:16 +0100275ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
276 struct ieee80211softmac_network *net)
277{
278 struct ieee80211softmac_auth_queue_item *aq = NULL;
279 struct list_head *list_ptr;
280 unsigned long flags;
281
Johannes Berg370121e2006-01-04 16:32:16 +0100282 /* 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
Johannes Berg2dd50802006-01-06 18:11:23 +0100307 /* can't transmit data right now... */
308 netif_carrier_off(mac->dev);
Johannes Berg370121e2006-01-04 16:32:16 +0100309 spin_unlock_irqrestore(&mac->lock, flags);
310}
311
312/*
313 * Sends a deauth request to the desired AP
314 */
315int
316ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
317 struct ieee80211softmac_network *net, int reason)
318{
319 int ret;
320
Johannes Berg370121e2006-01-04 16:32:16 +0100321 /* Make sure the network is authenticated */
322 if (!net->authenticated)
323 {
324 printkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
325 /* Error okay? */
326 return -EPERM;
327 }
328
329 /* Send the de-auth packet */
330 if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
331 return ret;
332
333 ieee80211softmac_deauth_from_net(mac, net);
334 return 0;
335}
336
337/*
338 * This should be registered with ieee80211 as handle_deauth
339 */
340int
Johannes Bergb10c9912006-01-31 19:48:06 +0100341ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
Johannes Berg370121e2006-01-04 16:32:16 +0100342{
343
344 struct ieee80211softmac_network *net = NULL;
345 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
346
Daniel Draked57336e2006-04-30 22:09:07 +0100347 if (unlikely(!mac->running))
348 return -ENODEV;
349
Johannes Bergb10c9912006-01-31 19:48:06 +0100350 if (!deauth) {
Johannes Berg370121e2006-01-04 16:32:16 +0100351 dprintk("deauth without deauth packet. eek!\n");
352 return 0;
353 }
354
Johannes Bergb10c9912006-01-31 19:48:06 +0100355 net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
Johannes Berg370121e2006-01-04 16:32:16 +0100356
357 if (net == NULL) {
Johannes Bergf484d582006-01-31 19:35:14 +0100358 printkl(KERN_DEBUG PFX "Received deauthentication packet from "MAC_FMT", but that network is unknown.\n",
Johannes Bergb10c9912006-01-31 19:48:06 +0100359 MAC_ARG(deauth->header.addr2));
Johannes Berg370121e2006-01-04 16:32:16 +0100360 return 0;
361 }
362
363 /* Make sure the network is authenticated */
364 if(!net->authenticated)
365 {
366 printkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
367 /* Error okay? */
368 return -EPERM;
369 }
370
371 ieee80211softmac_deauth_from_net(mac, net);
Daniel Drake995c9922006-04-30 19:49:30 +0100372
373 /* let's try to re-associate */
374 schedule_work(&mac->associnfo.work);
Johannes Berg370121e2006-01-04 16:32:16 +0100375 return 0;
376}