ieee80211softmac_auth.c 12.3 KB
Newer Older
1
2
3
/*
 * This file contains the softmac's authentication logic.
 *
4
5
6
7
8
 * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
 *                          Joseph Jezak <josejx@gentoo.org>
 *                          Larry Finger <Larry.Finger@lwfinger.net>
 *                          Danny van Dyk <kugelfang@gentoo.org>
 *                          Michael Buesch <mbuesch@freenet.de>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called COPYING.
 */

27
28
#include "ieee80211softmac_priv.h"

David Howells's avatar
David Howells committed
29
static void ieee80211softmac_auth_queue(struct work_struct *work);
30
31
32

/* Queues an auth request to the desired AP */
int
33
ieee80211softmac_auth_req(struct ieee80211softmac_device *mac,
34
35
36
37
	struct ieee80211softmac_network *net)
{
	struct ieee80211softmac_auth_queue_item *auth;
	unsigned long flags;
38
	DECLARE_MAC_BUF(mac2);
39

40
	if (net->authenticating || net->authenticated)
41
		return 0;
42
	net->authenticating = 1;
43
44
45
46

	/* Add the network if it's not already added */
	ieee80211softmac_add_network(mac, net);

47
	dprintk(KERN_NOTICE PFX "Queueing Authentication Request to %s\n", print_mac(mac2, net->bssid));
48
49
50
51
52
53
54
55
56
57
	/* Queue the auth request */
	auth = (struct ieee80211softmac_auth_queue_item *)
		kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
	if(auth == NULL)
		return -ENOMEM;

	auth->net = net;
	auth->mac = mac;
	auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
	auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
David Howells's avatar
David Howells committed
58
	INIT_DELAYED_WORK(&auth->work, ieee80211softmac_auth_queue);
59

60
61
62
63
64
	/* Lock (for list) */
	spin_lock_irqsave(&mac->lock, flags);

	/* add to list */
	list_add_tail(&auth->list, &mac->auth_queue);
65
	queue_delayed_work(mac->wq, &auth->work, 0);
66
	spin_unlock_irqrestore(&mac->lock, flags);
67

68
69
70
71
72
73
	return 0;
}


/* Sends an auth request to the desired AP and handles timeouts */
static void
David Howells's avatar
David Howells committed
74
ieee80211softmac_auth_queue(struct work_struct *work)
75
76
77
78
79
{
	struct ieee80211softmac_device *mac;
	struct ieee80211softmac_auth_queue_item *auth;
	struct ieee80211softmac_network *net;
	unsigned long flags;
80
	DECLARE_MAC_BUF(mac2);
81

David Howells's avatar
David Howells committed
82
83
	auth = container_of(work, struct ieee80211softmac_auth_queue_item,
			    work.work);
84
85
86
87
88
89
	net = auth->net;
	mac = auth->mac;

	if(auth->retry > 0) {
		/* Switch to correct channel for this network */
		mac->set_channel(mac->dev, net->channel);
90

91
92
		/* Lock and set flags */
		spin_lock_irqsave(&mac->lock, flags);
93
94
95
96
97
		if (unlikely(!mac->running)) {
			/* Prevent reschedule on workqueue flush */
			spin_unlock_irqrestore(&mac->lock, flags);
			return;
		}
98
99
		net->authenticated = 0;
		/* add a timeout call so we eventually give up waiting for an auth reply */
100
		queue_delayed_work(mac->wq, &auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
101
102
103
		auth->retry--;
		spin_unlock_irqrestore(&mac->lock, flags);
		if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
104
105
			dprintk(KERN_NOTICE PFX "Sending Authentication Request to %s failed (this shouldn't happen, wait for the timeout).\n",
				print_mac(mac2, net->bssid));
106
		else
107
			dprintk(KERN_NOTICE PFX "Sent Authentication Request to %s.\n", print_mac(mac2, net->bssid));
108
109
110
		return;
	}

111
	printkl(KERN_WARNING PFX "Authentication timed out with %s\n", print_mac(mac2, net->bssid));
112
113
	/* Remove this item from the queue */
	spin_lock_irqsave(&mac->lock, flags);
114
	net->authenticating = 0;
115
116
117
118
119
120
121
122
	ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
	cancel_delayed_work(&auth->work); /* just to make sure... */
	list_del(&auth->list);
	spin_unlock_irqrestore(&mac->lock, flags);
	/* Free it */
	kfree(auth);
}

123
124
/* Sends a response to an auth challenge (for shared key auth). */
static void
David Howells's avatar
David Howells committed
125
ieee80211softmac_auth_challenge_response(struct work_struct *work)
126
{
David Howells's avatar
David Howells committed
127
128
129
	struct ieee80211softmac_auth_queue_item *aq =
		container_of(work, struct ieee80211softmac_auth_queue_item,
			     work.work);
130
131
132
133
134

	/* Send our response */
	ieee80211softmac_send_mgt_frame(aq->mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
}

135
/* Handle the auth response from the AP
136
 * This should be registered with ieee80211 as handle_auth
137
 */
138
int
139
ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
140
{
141
142
143
144
145
146
147

	struct list_head *list_ptr;
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);
	struct ieee80211softmac_auth_queue_item *aq = NULL;
	struct ieee80211softmac_network *net = NULL;
	unsigned long flags;
	u8 * data;
148
	DECLARE_MAC_BUF(mac2);
149

150
151
152
	if (unlikely(!mac->running))
		return -ENODEV;

153
154
155
156
157
158
159
160
161
162
163
	/* Find correct auth queue item */
	spin_lock_irqsave(&mac->lock, flags);
	list_for_each(list_ptr, &mac->auth_queue) {
		aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
		net = aq->net;
		if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
			break;
		else
			aq = NULL;
	}
	spin_unlock_irqrestore(&mac->lock, flags);
164

165
166
167
	/* Make sure that we've got an auth queue item for this request */
	if(aq == NULL)
	{
168
		dprintkl(KERN_DEBUG PFX "Authentication response received from %s but no queue item exists.\n", print_mac(mac2, auth->header.addr2));
169
170
		/* Error #? */
		return -1;
171
172
	}

173
174
175
	/* Check for out of order authentication */
	if(!net->authenticating)
	{
176
		dprintkl(KERN_DEBUG PFX "Authentication response received from %s but did not request authentication.\n",print_mac(mac2, auth->header.addr2));
177
178
179
180
		return -1;
	}

	/* Parse the auth packet */
Al Viro's avatar
Al Viro committed
181
	switch(le16_to_cpu(auth->algorithm)) {
182
183
184
	case WLAN_AUTH_OPEN:
		/* Check the status code of the response */

Al Viro's avatar
Al Viro committed
185
		switch(le16_to_cpu(auth->status)) {
186
187
188
		case WLAN_STATUS_SUCCESS:
			/* Update the status to Authenticated */
			spin_lock_irqsave(&mac->lock, flags);
189
			net->authenticating = 0;
190
191
			net->authenticated = 1;
			spin_unlock_irqrestore(&mac->lock, flags);
192

193
			/* Send event */
194
			printkl(KERN_NOTICE PFX "Open Authentication completed with %s\n", print_mac(mac2, net->bssid));
195
196
197
198
199
200
201
202
			ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
			break;
		default:
			/* Lock and reset flags */
			spin_lock_irqsave(&mac->lock, flags);
			net->authenticated = 0;
			net->authenticating = 0;
			spin_unlock_irqrestore(&mac->lock, flags);
203

204
205
			printkl(KERN_NOTICE PFX "Open Authentication with %s failed, error code: %i\n",
				print_mac(mac2, net->bssid), le16_to_cpup(&auth->status));
206
207
208
209
210
211
212
			/* Count the error? */
			break;
		}
		goto free_aq;
		break;
	case WLAN_AUTH_SHARED_KEY:
		/* Figure out where we are in the process */
Al Viro's avatar
Al Viro committed
213
		switch(le16_to_cpu(auth->transaction)) {
214
215
216
		case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
			/* Check to make sure we have a challenge IE */
			data = (u8 *)auth->info_element;
217
			if (*data++ != MFIE_TYPE_CHALLENGE) {
218
				printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
219
				break;
220
221
222
			}
			/* Save the challenge */
			spin_lock_irqsave(&mac->lock, flags);
223
			net->challenge_len = *data++;
224
			if (net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
225
				net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
226
227
228
229
230
231
232
233
234
235
			kfree(net->challenge);
			net->challenge = kmemdup(data, net->challenge_len,
						 GFP_ATOMIC);
			if (net->challenge == NULL) {
				printkl(KERN_NOTICE PFX "Shared Key "
					"Authentication failed due to "
					"memory shortage.\n");
				spin_unlock_irqrestore(&mac->lock, flags);
				break;
			}
236
			aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
237

238
239
240
241
242
243
			/* We reuse the work struct from the auth request here.
			 * It is safe to do so as each one is per-request, and
			 * at this point (dealing with authentication response)
			 * we have obviously already sent the initial auth
			 * request. */
			cancel_delayed_work(&aq->work);
David Howells's avatar
David Howells committed
244
			INIT_DELAYED_WORK(&aq->work, &ieee80211softmac_auth_challenge_response);
245
			queue_delayed_work(mac->wq, &aq->work, 0);
246
			spin_unlock_irqrestore(&mac->lock, flags);
247
			return 0;
248
		case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
249
250
251
			kfree(net->challenge);
			net->challenge = NULL;
			net->challenge_len = 0;
252
253
254
			/* Check the status code of the response */
			switch(auth->status) {
			case WLAN_STATUS_SUCCESS:
255
				/* Update the status to Authenticated */
256
257
258
259
				spin_lock_irqsave(&mac->lock, flags);
				net->authenticating = 0;
				net->authenticated = 1;
				spin_unlock_irqrestore(&mac->lock, flags);
260
261
				printkl(KERN_NOTICE PFX "Shared Key Authentication completed with %s\n",
					print_mac(mac2, net->bssid));
262
				ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
263
264
				break;
			default:
265
266
				printkl(KERN_NOTICE PFX "Shared Key Authentication with %s failed, error code: %i\n",
					print_mac(mac2, net->bssid), le16_to_cpup(&auth->status));
267
268
				/* Lock and reset flags */
				spin_lock_irqsave(&mac->lock, flags);
269
270
				net->authenticating = 0;
				net->authenticated = 0;
271
272
273
274
275
276
277
278
279
280
281
282
283
				spin_unlock_irqrestore(&mac->lock, flags);
				/* Count the error? */
				break;
			}
			goto free_aq;
			break;
		default:
			printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
			break;
		}
		goto free_aq;
		break;
	default:
284
		/* ERROR */
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
		goto free_aq;
		break;
	}
	return 0;
free_aq:
	/* Cancel the timeout */
	spin_lock_irqsave(&mac->lock, flags);
	cancel_delayed_work(&aq->work);
	/* Remove this item from the queue */
	list_del(&aq->list);
	spin_unlock_irqrestore(&mac->lock, flags);

	/* Free it */
	kfree(aq);
	return 0;
}

/*
 * Handle deauthorization
 */
305
static void
306
307
308
309
310
311
312
ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_network *net)
{
	struct ieee80211softmac_auth_queue_item *aq = NULL;
	struct list_head *list_ptr;
	unsigned long flags;

313
314
315
	/* deauthentication implies disassociation */
	ieee80211softmac_disassoc(mac);

316
317
318
319
	/* Lock and reset status flags */
	spin_lock_irqsave(&mac->lock, flags);
	net->authenticating = 0;
	net->authenticated = 0;
320

321
322
323
324
325
326
327
328
	/* Find correct auth queue item, if it exists */
	list_for_each(list_ptr, &mac->auth_queue) {
		aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
		if (!memcmp(net->bssid, aq->net->bssid, ETH_ALEN))
			break;
		else
			aq = NULL;
	}
329

330
331
332
333
334
335
336
337
338
339
	/* Cancel pending work */
	if(aq != NULL)
		/* Not entirely safe?  What about running work? */
		cancel_delayed_work(&aq->work);

	/* Free our network ref */
	ieee80211softmac_del_network_locked(mac, net);
	if(net->challenge != NULL)
		kfree(net->challenge);
	kfree(net);
340

341
342
	/* can't transmit data right now... */
	netif_carrier_off(mac->dev);
343
	spin_unlock_irqrestore(&mac->lock, flags);
344
345

	ieee80211softmac_try_reassoc(mac);
346
347
}

348
/*
349
350
 * Sends a deauth request to the desired AP
 */
351
352
int
ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
353
354
355
	struct ieee80211softmac_network *net, int reason)
{
	int ret;
356

357
358
359
	/* Make sure the network is authenticated */
	if (!net->authenticated)
	{
360
		dprintkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
361
362
363
		/* Error okay? */
		return -EPERM;
	}
364

365
366
367
	/* Send the de-auth packet */
	if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
		return ret;
368

369
370
371
	ieee80211softmac_deauth_from_net(mac, net);
	return 0;
}
372

373
374
375
/*
 * This should be registered with ieee80211 as handle_deauth
 */
376
int
377
ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
378
{
379

380
381
	struct ieee80211softmac_network *net = NULL;
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);
382
	DECLARE_MAC_BUF(mac2);
383

384
385
386
	if (unlikely(!mac->running))
		return -ENODEV;

387
	if (!deauth) {
388
389
390
391
		dprintk("deauth without deauth packet. eek!\n");
		return 0;
	}

392
	net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
393

394
	if (net == NULL) {
395
396
		dprintkl(KERN_DEBUG PFX "Received deauthentication packet from %s, but that network is unknown.\n",
			print_mac(mac2, deauth->header.addr2));
397
398
399
400
401
402
		return 0;
	}

	/* Make sure the network is authenticated */
	if(!net->authenticated)
	{
403
		dprintkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
404
405
406
407
408
		/* Error okay? */
		return -EPERM;
	}

	ieee80211softmac_deauth_from_net(mac, net);
409
410

	/* let's try to re-associate */
411
	queue_delayed_work(mac->wq, &mac->associnfo.work, 0);
412
413
	return 0;
}