blob: 2ae1833b657ab658e1c3f2a1074dad0c625020a9 [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
David Howellsc4028952006-11-22 14:57:56 +000029static void ieee80211softmac_auth_queue(struct work_struct *work);
Johannes Berg370121e2006-01-04 16:32:16 +010030
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
Joseph Jezakcb74c432006-06-11 12:00:37 -040039 if (net->authenticating || net->authenticated)
Johannes Berg370121e2006-01-04 16:32:16 +010040 return 0;
Joseph Jezakcb74c432006-06-11 12:00:37 -040041 net->authenticating = 1;
Johannes Berg370121e2006-01-04 16:32:16 +010042
43 /* Add the network if it's not already added */
44 ieee80211softmac_add_network(mac, net);
45
46 dprintk(KERN_NOTICE PFX "Queueing Authentication Request to "MAC_FMT"\n", MAC_ARG(net->bssid));
47 /* Queue the auth request */
48 auth = (struct ieee80211softmac_auth_queue_item *)
49 kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
50 if(auth == NULL)
51 return -ENOMEM;
52
53 auth->net = net;
54 auth->mac = mac;
55 auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
56 auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
David Howellsc4028952006-11-22 14:57:56 +000057 INIT_DELAYED_WORK(&auth->work, ieee80211softmac_auth_queue);
Johannes Berg370121e2006-01-04 16:32:16 +010058
59 /* Lock (for list) */
60 spin_lock_irqsave(&mac->lock, flags);
61
62 /* add to list */
63 list_add_tail(&auth->list, &mac->auth_queue);
David Howellsc4028952006-11-22 14:57:56 +000064 schedule_delayed_work(&auth->work, 0);
Johannes Berg370121e2006-01-04 16:32:16 +010065 spin_unlock_irqrestore(&mac->lock, flags);
66
67 return 0;
68}
69
70
71/* Sends an auth request to the desired AP and handles timeouts */
72static void
David Howellsc4028952006-11-22 14:57:56 +000073ieee80211softmac_auth_queue(struct work_struct *work)
Johannes Berg370121e2006-01-04 16:32:16 +010074{
75 struct ieee80211softmac_device *mac;
76 struct ieee80211softmac_auth_queue_item *auth;
77 struct ieee80211softmac_network *net;
78 unsigned long flags;
79
David Howellsc4028952006-11-22 14:57:56 +000080 auth = container_of(work, struct ieee80211softmac_auth_queue_item,
81 work.work);
Johannes Berg370121e2006-01-04 16:32:16 +010082 net = auth->net;
83 mac = auth->mac;
84
85 if(auth->retry > 0) {
86 /* Switch to correct channel for this network */
87 mac->set_channel(mac->dev, net->channel);
88
89 /* Lock and set flags */
90 spin_lock_irqsave(&mac->lock, flags);
Daniel Draked57336e2006-04-30 22:09:07 +010091 if (unlikely(!mac->running)) {
92 /* Prevent reschedule on workqueue flush */
93 spin_unlock_irqrestore(&mac->lock, flags);
94 return;
95 }
Johannes Berg370121e2006-01-04 16:32:16 +010096 net->authenticated = 0;
Johannes Berg370121e2006-01-04 16:32:16 +010097 /* add a timeout call so we eventually give up waiting for an auth reply */
Johannes Berg5c4df6d2006-01-06 01:43:45 +010098 schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
Johannes Berg370121e2006-01-04 16:32:16 +010099 auth->retry--;
100 spin_unlock_irqrestore(&mac->lock, flags);
101 if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
102 dprintk(KERN_NOTICE PFX "Sending Authentication Request to "MAC_FMT" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net->bssid));
103 else
104 dprintk(KERN_NOTICE PFX "Sent Authentication Request to "MAC_FMT".\n", MAC_ARG(net->bssid));
105 return;
106 }
107
108 printkl(KERN_WARNING PFX "Authentication timed out with "MAC_FMT"\n", MAC_ARG(net->bssid));
109 /* Remove this item from the queue */
110 spin_lock_irqsave(&mac->lock, flags);
Daniel Drake76ea4c72006-06-01 15:34:26 +0100111 net->authenticating = 0;
Johannes Berg370121e2006-01-04 16:32:16 +0100112 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
113 cancel_delayed_work(&auth->work); /* just to make sure... */
114 list_del(&auth->list);
115 spin_unlock_irqrestore(&mac->lock, flags);
116 /* Free it */
117 kfree(auth);
118}
119
Daniel Drake345f6b82006-07-11 23:16:34 +0100120/* Sends a response to an auth challenge (for shared key auth). */
121static void
David Howellsc4028952006-11-22 14:57:56 +0000122ieee80211softmac_auth_challenge_response(struct work_struct *work)
Daniel Drake345f6b82006-07-11 23:16:34 +0100123{
David Howellsc4028952006-11-22 14:57:56 +0000124 struct ieee80211softmac_auth_queue_item *aq =
125 container_of(work, struct ieee80211softmac_auth_queue_item,
126 work.work);
Daniel Drake345f6b82006-07-11 23:16:34 +0100127
128 /* Send our response */
129 ieee80211softmac_send_mgt_frame(aq->mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
130}
131
Johannes Berg370121e2006-01-04 16:32:16 +0100132/* Handle the auth response from the AP
133 * This should be registered with ieee80211 as handle_auth
134 */
135int
136ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
137{
138
139 struct list_head *list_ptr;
140 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
141 struct ieee80211softmac_auth_queue_item *aq = NULL;
142 struct ieee80211softmac_network *net = NULL;
143 unsigned long flags;
144 u8 * data;
145
Daniel Draked57336e2006-04-30 22:09:07 +0100146 if (unlikely(!mac->running))
147 return -ENODEV;
148
Johannes Berg370121e2006-01-04 16:32:16 +0100149 /* Find correct auth queue item */
150 spin_lock_irqsave(&mac->lock, flags);
151 list_for_each(list_ptr, &mac->auth_queue) {
152 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
153 net = aq->net;
154 if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
155 break;
156 else
157 aq = NULL;
158 }
159 spin_unlock_irqrestore(&mac->lock, flags);
160
161 /* Make sure that we've got an auth queue item for this request */
162 if(aq == NULL)
163 {
164 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but no queue item exists.\n", MAC_ARG(auth->header.addr2));
165 /* Error #? */
166 return -1;
167 }
168
169 /* Check for out of order authentication */
170 if(!net->authenticating)
171 {
172 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but did not request authentication.\n",MAC_ARG(auth->header.addr2));
173 return -1;
174 }
175
176 /* Parse the auth packet */
177 switch(auth->algorithm) {
178 case WLAN_AUTH_OPEN:
179 /* Check the status code of the response */
180
181 switch(auth->status) {
182 case WLAN_STATUS_SUCCESS:
183 /* Update the status to Authenticated */
184 spin_lock_irqsave(&mac->lock, flags);
185 net->authenticating = 0;
186 net->authenticated = 1;
187 spin_unlock_irqrestore(&mac->lock, flags);
188
189 /* Send event */
190 printkl(KERN_NOTICE PFX "Open Authentication completed with "MAC_FMT"\n", MAC_ARG(net->bssid));
191 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
192 break;
193 default:
194 /* Lock and reset flags */
195 spin_lock_irqsave(&mac->lock, flags);
196 net->authenticated = 0;
197 net->authenticating = 0;
198 spin_unlock_irqrestore(&mac->lock, flags);
199
200 printkl(KERN_NOTICE PFX "Open Authentication with "MAC_FMT" failed, error code: %i\n",
201 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
202 /* Count the error? */
203 break;
204 }
205 goto free_aq;
206 break;
207 case WLAN_AUTH_SHARED_KEY:
208 /* Figure out where we are in the process */
209 switch(auth->transaction) {
210 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
211 /* Check to make sure we have a challenge IE */
212 data = (u8 *)auth->info_element;
Daniel Drake345f6b82006-07-11 23:16:34 +0100213 if (*data++ != MFIE_TYPE_CHALLENGE) {
Johannes Berg370121e2006-01-04 16:32:16 +0100214 printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
215 break;
216 }
217 /* Save the challenge */
218 spin_lock_irqsave(&mac->lock, flags);
219 net->challenge_len = *data++;
Daniel Drake345f6b82006-07-11 23:16:34 +0100220 if (net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
Johannes Berg370121e2006-01-04 16:32:16 +0100221 net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
Daniel Drake345f6b82006-07-11 23:16:34 +0100222 if (net->challenge != NULL)
Johannes Berg370121e2006-01-04 16:32:16 +0100223 kfree(net->challenge);
224 net->challenge = kmalloc(net->challenge_len, GFP_ATOMIC);
225 memcpy(net->challenge, data, net->challenge_len);
226 aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
Johannes Berg370121e2006-01-04 16:32:16 +0100227
Daniel Drake345f6b82006-07-11 23:16:34 +0100228 /* We reuse the work struct from the auth request here.
229 * It is safe to do so as each one is per-request, and
230 * at this point (dealing with authentication response)
231 * we have obviously already sent the initial auth
232 * request. */
233 cancel_delayed_work(&aq->work);
David Howellsc4028952006-11-22 14:57:56 +0000234 INIT_DELAYED_WORK(&aq->work, &ieee80211softmac_auth_challenge_response);
235 schedule_delayed_work(&aq->work, 0);
Daniel Drake345f6b82006-07-11 23:16:34 +0100236 spin_unlock_irqrestore(&mac->lock, flags);
Daniel Drake76ea4c72006-06-01 15:34:26 +0100237 return 0;
Johannes Berg370121e2006-01-04 16:32:16 +0100238 case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
Daniel Drake76ea4c72006-06-01 15:34:26 +0100239 kfree(net->challenge);
240 net->challenge = NULL;
241 net->challenge_len = 0;
Johannes Berg370121e2006-01-04 16:32:16 +0100242 /* Check the status code of the response */
243 switch(auth->status) {
244 case WLAN_STATUS_SUCCESS:
245 /* Update the status to Authenticated */
246 spin_lock_irqsave(&mac->lock, flags);
247 net->authenticating = 0;
248 net->authenticated = 1;
249 spin_unlock_irqrestore(&mac->lock, flags);
250 printkl(KERN_NOTICE PFX "Shared Key Authentication completed with "MAC_FMT"\n",
251 MAC_ARG(net->bssid));
Daniel Drake76ea4c72006-06-01 15:34:26 +0100252 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
Johannes Berg370121e2006-01-04 16:32:16 +0100253 break;
254 default:
255 printkl(KERN_NOTICE PFX "Shared Key Authentication with "MAC_FMT" failed, error code: %i\n",
256 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
257 /* Lock and reset flags */
258 spin_lock_irqsave(&mac->lock, flags);
259 net->authenticating = 0;
260 net->authenticated = 0;
261 spin_unlock_irqrestore(&mac->lock, flags);
262 /* Count the error? */
263 break;
264 }
265 goto free_aq;
266 break;
267 default:
268 printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
269 break;
270 }
271 goto free_aq;
272 break;
273 default:
274 /* ERROR */
275 goto free_aq;
276 break;
277 }
278 return 0;
279free_aq:
280 /* Cancel the timeout */
281 spin_lock_irqsave(&mac->lock, flags);
282 cancel_delayed_work(&aq->work);
283 /* Remove this item from the queue */
284 list_del(&aq->list);
285 spin_unlock_irqrestore(&mac->lock, flags);
286
287 /* Free it */
288 kfree(aq);
289 return 0;
290}
291
292/*
293 * Handle deauthorization
294 */
Johannes Berg714e1a52006-01-04 21:06:28 +0100295static void
Johannes Berg370121e2006-01-04 16:32:16 +0100296ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
297 struct ieee80211softmac_network *net)
298{
299 struct ieee80211softmac_auth_queue_item *aq = NULL;
300 struct list_head *list_ptr;
301 unsigned long flags;
302
Daniel Drake6d92f832006-05-01 22:23:27 +0100303 /* deauthentication implies disassociation */
304 ieee80211softmac_disassoc(mac);
305
Johannes Berg370121e2006-01-04 16:32:16 +0100306 /* Lock and reset status flags */
307 spin_lock_irqsave(&mac->lock, flags);
308 net->authenticating = 0;
309 net->authenticated = 0;
310
311 /* Find correct auth queue item, if it exists */
312 list_for_each(list_ptr, &mac->auth_queue) {
313 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
314 if (!memcmp(net->bssid, aq->net->bssid, ETH_ALEN))
315 break;
316 else
317 aq = NULL;
318 }
319
320 /* Cancel pending work */
321 if(aq != NULL)
322 /* Not entirely safe? What about running work? */
323 cancel_delayed_work(&aq->work);
324
325 /* Free our network ref */
326 ieee80211softmac_del_network_locked(mac, net);
327 if(net->challenge != NULL)
328 kfree(net->challenge);
329 kfree(net);
330
Johannes Berg2dd50802006-01-06 18:11:23 +0100331 /* can't transmit data right now... */
332 netif_carrier_off(mac->dev);
Johannes Berg370121e2006-01-04 16:32:16 +0100333 spin_unlock_irqrestore(&mac->lock, flags);
334}
335
336/*
337 * Sends a deauth request to the desired AP
338 */
339int
340ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
341 struct ieee80211softmac_network *net, int reason)
342{
343 int ret;
344
Johannes Berg370121e2006-01-04 16:32:16 +0100345 /* Make sure the network is authenticated */
346 if (!net->authenticated)
347 {
348 printkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
349 /* Error okay? */
350 return -EPERM;
351 }
352
353 /* Send the de-auth packet */
354 if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
355 return ret;
356
357 ieee80211softmac_deauth_from_net(mac, net);
358 return 0;
359}
360
361/*
362 * This should be registered with ieee80211 as handle_deauth
363 */
364int
Johannes Bergb10c9912006-01-31 19:48:06 +0100365ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
Johannes Berg370121e2006-01-04 16:32:16 +0100366{
367
368 struct ieee80211softmac_network *net = NULL;
369 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
370
Daniel Draked57336e2006-04-30 22:09:07 +0100371 if (unlikely(!mac->running))
372 return -ENODEV;
373
Johannes Bergb10c9912006-01-31 19:48:06 +0100374 if (!deauth) {
Johannes Berg370121e2006-01-04 16:32:16 +0100375 dprintk("deauth without deauth packet. eek!\n");
376 return 0;
377 }
378
Johannes Bergb10c9912006-01-31 19:48:06 +0100379 net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
Johannes Berg370121e2006-01-04 16:32:16 +0100380
381 if (net == NULL) {
Johannes Bergf484d582006-01-31 19:35:14 +0100382 printkl(KERN_DEBUG PFX "Received deauthentication packet from "MAC_FMT", but that network is unknown.\n",
Johannes Bergb10c9912006-01-31 19:48:06 +0100383 MAC_ARG(deauth->header.addr2));
Johannes Berg370121e2006-01-04 16:32:16 +0100384 return 0;
385 }
386
387 /* Make sure the network is authenticated */
388 if(!net->authenticated)
389 {
390 printkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
391 /* Error okay? */
392 return -EPERM;
393 }
394
395 ieee80211softmac_deauth_from_net(mac, net);
Daniel Drake995c9922006-04-30 19:49:30 +0100396
397 /* let's try to re-associate */
David Howellsc4028952006-11-22 14:57:56 +0000398 schedule_delayed_work(&mac->associnfo.work, 0);
Johannes Berg370121e2006-01-04 16:32:16 +0100399 return 0;
400}