[PATCH] softmac: complete shared key authentication
[deliverable/linux.git] / net / ieee80211 / softmac / ieee80211softmac_assoc.c
CommitLineData
4855d25b
JB
1/*
2 * This file contains the softmac's association logic.
3 *
79859051
JB
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>
4855d25b
JB
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
370121e5
JB
27#include "ieee80211softmac_priv.h"
28
29/*
30 * Overview
31 *
32 * Before you can associate, you have to authenticate.
33 *
34 */
35
36/* Sends out an association request to the desired AP */
37static void
38ieee80211softmac_assoc(struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
39{
40 unsigned long flags;
9a107aa2 41
370121e5
JB
42 /* Switch to correct channel for this network */
43 mac->set_channel(mac->dev, net->channel);
44
45 /* Send association request */
46 ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_ASSOC_REQ, 0);
47
48 dprintk(KERN_INFO PFX "sent association request!\n");
49
50 /* Change the state to associating */
51 spin_lock_irqsave(&mac->lock, flags);
52 mac->associnfo.associating = 1;
53 mac->associated = 0; /* just to make sure */
370121e5
JB
54
55 /* Set a timer for timeout */
56 /* FIXME: make timeout configurable */
d57336e3
DD
57 if (likely(mac->running))
58 schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ);
59 spin_unlock_irqrestore(&mac->lock, flags);
370121e5
JB
60}
61
62void
63ieee80211softmac_assoc_timeout(void *d)
64{
65 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
66 unsigned long flags;
67
370121e5
JB
68 spin_lock_irqsave(&mac->lock, flags);
69 /* we might race against ieee80211softmac_handle_assoc_response,
70 * so make sure only one of us does something */
71 if (!mac->associnfo.associating) {
72 spin_unlock_irqrestore(&mac->lock, flags);
73 return;
74 }
75 mac->associnfo.associating = 0;
76 mac->associnfo.bssvalid = 0;
77 mac->associated = 0;
78 spin_unlock_irqrestore(&mac->lock, flags);
79
80 dprintk(KERN_INFO PFX "assoc request timed out!\n");
81 /* FIXME: we need to know the network here. that requires a bit of restructuring */
82 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_TIMEOUT, NULL);
83}
84
9a1771e8 85void
6d92f83f 86ieee80211softmac_disassoc(struct ieee80211softmac_device *mac)
370121e5
JB
87{
88 unsigned long flags;
6d92f83f
DD
89
90 spin_lock_irqsave(&mac->lock, flags);
91 if (mac->associnfo.associating)
92 cancel_delayed_work(&mac->associnfo.timeout);
93
94 netif_carrier_off(mac->dev);
95
96 mac->associated = 0;
97 mac->associnfo.bssvalid = 0;
98 mac->associnfo.associating = 0;
8462fe3c 99 ieee80211softmac_init_txrates(mac);
6d92f83f
DD
100 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_DISASSOCIATED, NULL);
101 spin_unlock_irqrestore(&mac->lock, flags);
102}
103
104/* Sends out a disassociation request to the desired AP */
105void
106ieee80211softmac_send_disassoc_req(struct ieee80211softmac_device *mac, u16 reason)
107{
370121e5 108 struct ieee80211softmac_network *found;
370121e5
JB
109
110 if (mac->associnfo.bssvalid && mac->associated) {
111 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
112 if (found)
113 ieee80211softmac_send_mgt_frame(mac, found, IEEE80211_STYPE_DISASSOC, reason);
370121e5
JB
114 }
115
6d92f83f 116 ieee80211softmac_disassoc(mac);
370121e5
JB
117}
118
119static inline int
120we_support_all_basic_rates(struct ieee80211softmac_device *mac, u8 *from, u8 from_len)
121{
8462fe3c
DD
122 int idx;
123 u8 rate;
370121e5
JB
124
125 for (idx = 0; idx < (from_len); idx++) {
126 rate = (from)[idx];
127 if (!(rate & IEEE80211_BASIC_RATE_MASK))
128 continue;
370121e5 129 rate &= ~IEEE80211_BASIC_RATE_MASK;
8462fe3c 130 if (!ieee80211softmac_ratesinfo_rate_supported(&mac->ratesinfo, rate))
370121e5
JB
131 return 0;
132 }
133 return 1;
134}
135
136static int
137network_matches_request(struct ieee80211softmac_device *mac, struct ieee80211_network *net)
138{
139 /* we cannot associate to networks whose name we don't know */
140 if (ieee80211_is_empty_essid(net->ssid, net->ssid_len))
141 return 0;
142 /* do not associate to a network whose BSSBasicRateSet we cannot support */
143 if (!we_support_all_basic_rates(mac, net->rates, net->rates_len))
144 return 0;
145 /* do we really need to check the ex rates? */
146 if (!we_support_all_basic_rates(mac, net->rates_ex, net->rates_ex_len))
147 return 0;
148
818667f7
JB
149 /* assume that users know what they're doing ...
150 * (note we don't let them select a net we're incompatible with) */
151 if (mac->associnfo.bssfixed) {
152 return !memcmp(mac->associnfo.bssid, net->bssid, ETH_ALEN);
153 }
154
370121e5
JB
155 /* if 'ANY' network requested, take any that doesn't have privacy enabled */
156 if (mac->associnfo.req_essid.len == 0
157 && !(net->capability & WLAN_CAPABILITY_PRIVACY))
158 return 1;
159 if (net->ssid_len != mac->associnfo.req_essid.len)
160 return 0;
161 if (!memcmp(net->ssid, mac->associnfo.req_essid.data, mac->associnfo.req_essid.len))
162 return 1;
163 return 0;
164}
165
166static void
167ieee80211softmac_assoc_notify(struct net_device *dev, void *context)
168{
169 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
170 ieee80211softmac_assoc_work((void*)mac);
171}
172
173/* This function is called to handle userspace requests (asynchronously) */
174void
175ieee80211softmac_assoc_work(void *d)
176{
177 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
178 struct ieee80211softmac_network *found = NULL;
179 struct ieee80211_network *net = NULL, *best = NULL;
6d92f83f 180 int bssvalid;
370121e5 181 unsigned long flags;
6d92f83f
DD
182
183 /* ieee80211_disassoc might clear this */
184 bssvalid = mac->associnfo.bssvalid;
185
370121e5
JB
186 /* meh */
187 if (mac->associated)
6d92f83f 188 ieee80211softmac_send_disassoc_req(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
370121e5
JB
189
190 /* try to find the requested network in our list, if we found one already */
6d92f83f 191 if (bssvalid || mac->associnfo.bssfixed)
370121e5
JB
192 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
193
1dc09776
JB
194 /* Search the ieee80211 networks for this network if we didn't find it by bssid,
195 * but only if we've scanned at least once (to get a better list of networks to
196 * select from). If we have not scanned before, the !found logic below will be
197 * invoked and will scan. */
198 if (!found && (mac->associnfo.scan_retry < IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT))
370121e5 199 {
78e4f36e
JB
200 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
201 because it cannot follow the best pointer logic. */
370121e5
JB
202 spin_lock_irqsave(&mac->ieee->lock, flags);
203 list_for_each_entry(net, &mac->ieee->network_list, list) {
204 /* we're supposed to find the network with
205 * the best signal here, as we're asked to join
206 * any network with a specific ESSID, and many
207 * different ones could have that.
208 *
78e4f36e 209 * I'll for now just go with the reported rssi.
370121e5
JB
210 *
211 * We also should take into account the rateset
212 * here to find the best BSSID to try.
213 */
214 if (network_matches_request(mac, net)) {
215 if (!best) {
216 best = net;
78e4f36e 217 rssi = best->stats.rssi;
370121e5
JB
218 continue;
219 }
220 /* we already had a matching network, so
221 * compare their properties to get the
222 * better of the two ... (see above)
223 */
78e4f36e
JB
224 if (rssi < net->stats.rssi) {
225 best = net;
226 rssi = best->stats.rssi;
227 }
370121e5
JB
228 }
229 }
230 /* if we unlock here, we might get interrupted and the `best'
231 * pointer could go stale */
232 if (best) {
233 found = ieee80211softmac_create_network(mac, best);
234 /* if found is still NULL, then we got -ENOMEM somewhere */
235 if (found)
236 ieee80211softmac_add_network(mac, found);
237 }
238 spin_unlock_irqrestore(&mac->ieee->lock, flags);
239 }
240
241 if (!found) {
242 if (mac->associnfo.scan_retry > 0) {
243 spin_lock_irqsave(&mac->lock, flags);
244 mac->associnfo.scan_retry--;
245 spin_unlock_irqrestore(&mac->lock, flags);
246
247 /* We know of no such network. Let's scan.
248 * NB: this also happens if we had no memory to copy the network info...
249 * Maybe we can hope to have more memory after scanning finishes ;)
250 */
1dc09776 251 dprintk(KERN_INFO PFX "Associate: Scanning for networks first.\n");
370121e5
JB
252 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL);
253 if (ieee80211softmac_start_scan(mac))
1dc09776 254 dprintk(KERN_INFO PFX "Associate: failed to initiate scan. Is device up?\n");
370121e5 255 return;
818667f7 256 } else {
370121e5
JB
257 spin_lock_irqsave(&mac->lock, flags);
258 mac->associnfo.associating = 0;
259 mac->associated = 0;
260 spin_unlock_irqrestore(&mac->lock, flags);
261
1dc09776 262 dprintk(KERN_INFO PFX "Unable to find matching network after scan!\n");
818667f7
JB
263 /* reset the retry counter for the next user request since we
264 * break out and don't reschedule ourselves after this point. */
265 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
370121e5
JB
266 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
267 return;
268 }
269 }
818667f7
JB
270
271 /* reset the retry counter for the next user request since we
272 * now found a net and will try to associate to it, but not
273 * schedule this function again. */
274 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
370121e5
JB
275 mac->associnfo.bssvalid = 1;
276 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
277 /* copy the ESSID for displaying it */
278 mac->associnfo.associate_essid.len = found->essid.len;
279 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
280
281 /* we found a network! authenticate (if necessary) and associate to it. */
282 if (!found->authenticated) {
283 /* This relies on the fact that _auth_req only queues the work,
284 * otherwise adding the notification would be racy. */
285 if (!ieee80211softmac_auth_req(mac, found)) {
286 dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n");
287 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL);
288 } else {
289 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
290 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
291 }
292 return;
293 }
294 /* finally! now we can start associating */
295 ieee80211softmac_assoc(mac, found);
296}
297
298/* call this to do whatever is necessary when we're associated */
299static void
300ieee80211softmac_associated(struct ieee80211softmac_device *mac,
301 struct ieee80211_assoc_response * resp,
302 struct ieee80211softmac_network *net)
303{
304 mac->associnfo.associating = 0;
8462fe3c
DD
305 mac->associnfo.supported_rates = net->supported_rates;
306 ieee80211softmac_recalc_txrates(mac);
307
370121e5
JB
308 mac->associated = 1;
309 if (mac->set_bssid_filter)
310 mac->set_bssid_filter(mac->dev, net->bssid);
311 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
2dd50801 312 netif_carrier_on(mac->dev);
370121e5
JB
313
314 mac->association_id = le16_to_cpup(&resp->aid);
315}
316
317/* received frame handling functions */
318int
319ieee80211softmac_handle_assoc_response(struct net_device * dev,
320 struct ieee80211_assoc_response * resp,
321 struct ieee80211_network * _ieee80211_network_do_not_use)
322{
323 /* NOTE: the network parameter has to be ignored by
324 * this code because it is the ieee80211's pointer
325 * to the struct, not ours (we made a copy)
326 */
327 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
328 u16 status = le16_to_cpup(&resp->status);
329 struct ieee80211softmac_network *network = NULL;
330 unsigned long flags;
d57336e3
DD
331
332 if (unlikely(!mac->running))
333 return -ENODEV;
370121e5
JB
334
335 spin_lock_irqsave(&mac->lock, flags);
336
337 if (!mac->associnfo.associating) {
338 /* we race against the timeout function, so make sure
339 * only one of us can do work */
340 spin_unlock_irqrestore(&mac->lock, flags);
341 return 0;
342 }
343 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
344
345 /* someone sending us things without us knowing him? Ignore. */
346 if (!network) {
347 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
348 spin_unlock_irqrestore(&mac->lock, flags);
349 return 0;
350 }
351
352 /* now that we know it was for us, we can cancel the timeout */
353 cancel_delayed_work(&mac->associnfo.timeout);
354
355 switch (status) {
356 case 0:
357 dprintk(KERN_INFO PFX "associated!\n");
358 ieee80211softmac_associated(mac, resp, network);
359 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
360 break;
361 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
362 if (!network->auth_desynced_once) {
363 /* there seem to be a few rare cases where our view of
364 * the world is obscured, or buggy APs that don't DEAUTH
365 * us properly. So we handle that, but allow it only once.
366 */
367 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
368 network->authenticated = 0;
369 /* we don't want to do this more than once ... */
370 network->auth_desynced_once = 1;
5c4df6da 371 schedule_work(&mac->associnfo.work);
370121e5
JB
372 break;
373 }
374 default:
375 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
376 mac->associnfo.associating = 0;
377 mac->associnfo.bssvalid = 0;
378 mac->associated = 0;
379 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
380 }
381
382 spin_unlock_irqrestore(&mac->lock, flags);
383 return 0;
384}
385
386int
387ieee80211softmac_handle_disassoc(struct net_device * dev,
388 struct ieee80211_disassoc *disassoc)
389{
390 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
d57336e3
DD
391
392 if (unlikely(!mac->running))
393 return -ENODEV;
394
48b2e4ce
JB
395 if (memcmp(disassoc->header.addr2, mac->associnfo.bssid, ETH_ALEN))
396 return 0;
d57336e3 397
48b2e4ce
JB
398 if (memcmp(disassoc->header.addr1, mac->dev->dev_addr, ETH_ALEN))
399 return 0;
d57336e3 400
370121e5 401 dprintk(KERN_INFO PFX "got disassoc frame\n");
6d92f83f
DD
402 ieee80211softmac_disassoc(mac);
403
404 /* try to reassociate */
d1469cf2 405 schedule_work(&mac->associnfo.work);
6d92f83f 406
370121e5
JB
407 return 0;
408}
b6c7658e
JB
409
410int
411ieee80211softmac_handle_reassoc_req(struct net_device * dev,
412 struct ieee80211_reassoc_request * resp)
413{
414 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
415 struct ieee80211softmac_network *network;
416
d57336e3
DD
417 if (unlikely(!mac->running))
418 return -ENODEV;
419
b6c7658e
JB
420 network = ieee80211softmac_get_network_by_bssid(mac, resp->header.addr3);
421 if (!network) {
422 dprintkl(KERN_INFO PFX "reassoc request from unknown network\n");
423 return 0;
424 }
9b0b4d8a
MB
425 schedule_work(&mac->associnfo.work);
426
b6c7658e
JB
427 return 0;
428}
This page took 0.063544 seconds and 5 git commands to generate.