WorkStruct: make allyesconfig
[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
370121e5 50 spin_lock_irqsave(&mac->lock, flags);
7c28ad2d 51 mac->associnfo.associated = 0; /* just to make sure */
370121e5
JB
52
53 /* Set a timer for timeout */
54 /* FIXME: make timeout configurable */
d57336e3
DD
55 if (likely(mac->running))
56 schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ);
57 spin_unlock_irqrestore(&mac->lock, flags);
370121e5
JB
58}
59
60void
c4028958 61ieee80211softmac_assoc_timeout(struct work_struct *work)
370121e5 62{
c4028958
DH
63 struct ieee80211softmac_device *mac =
64 container_of(work, struct ieee80211softmac_device,
65 associnfo.timeout.work);
9f101fc5 66 struct ieee80211softmac_network *n;
370121e5 67
7c28ad2d 68 mutex_lock(&mac->associnfo.mutex);
370121e5
JB
69 /* we might race against ieee80211softmac_handle_assoc_response,
70 * so make sure only one of us does something */
7c28ad2d
MB
71 if (!mac->associnfo.associating)
72 goto out;
370121e5
JB
73 mac->associnfo.associating = 0;
74 mac->associnfo.bssvalid = 0;
7c28ad2d 75 mac->associnfo.associated = 0;
9f101fc5
JJ
76
77 n = ieee80211softmac_get_network_by_bssid_locked(mac, mac->associnfo.bssid);
370121e5
JB
78
79 dprintk(KERN_INFO PFX "assoc request timed out!\n");
9f101fc5 80 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_TIMEOUT, n);
7c28ad2d
MB
81out:
82 mutex_unlock(&mac->associnfo.mutex);
370121e5
JB
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
7c28ad2d 96 mac->associnfo.associated = 0;
6d92f83f
DD
97 mac->associnfo.bssvalid = 0;
98 mac->associnfo.associating = 0;
5acd0c41 99 ieee80211softmac_init_bss(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 109
7c28ad2d 110 if (mac->associnfo.bssvalid && mac->associnfo.associated) {
370121e5
JB
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
6ae15df1 167ieee80211softmac_assoc_notify_scan(struct net_device *dev, int event_type, void *context)
370121e5
JB
168{
169 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
170 ieee80211softmac_assoc_work((void*)mac);
171}
172
6ae15df1
DD
173static void
174ieee80211softmac_assoc_notify_auth(struct net_device *dev, int event_type, void *context)
175{
176 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
177
178 switch (event_type) {
179 case IEEE80211SOFTMAC_EVENT_AUTHENTICATED:
180 ieee80211softmac_assoc_work((void*)mac);
181 break;
182 case IEEE80211SOFTMAC_EVENT_AUTH_FAILED:
183 case IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT:
184 ieee80211softmac_disassoc(mac);
185 break;
186 }
187}
188
370121e5
JB
189/* This function is called to handle userspace requests (asynchronously) */
190void
c4028958 191ieee80211softmac_assoc_work(struct work_struct *work)
370121e5 192{
c4028958
DH
193 struct ieee80211softmac_device *mac =
194 container_of(work, struct ieee80211softmac_device,
195 associnfo.work.work);
370121e5
JB
196 struct ieee80211softmac_network *found = NULL;
197 struct ieee80211_network *net = NULL, *best = NULL;
6d92f83f 198 int bssvalid;
370121e5 199 unsigned long flags;
6d92f83f 200
7c28ad2d
MB
201 mutex_lock(&mac->associnfo.mutex);
202
203 if (!mac->associnfo.associating)
204 goto out;
205
6d92f83f
DD
206 /* ieee80211_disassoc might clear this */
207 bssvalid = mac->associnfo.bssvalid;
208
370121e5 209 /* meh */
7c28ad2d 210 if (mac->associnfo.associated)
6d92f83f 211 ieee80211softmac_send_disassoc_req(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
370121e5
JB
212
213 /* try to find the requested network in our list, if we found one already */
6d92f83f 214 if (bssvalid || mac->associnfo.bssfixed)
370121e5
JB
215 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
216
1dc09776
JB
217 /* Search the ieee80211 networks for this network if we didn't find it by bssid,
218 * but only if we've scanned at least once (to get a better list of networks to
219 * select from). If we have not scanned before, the !found logic below will be
220 * invoked and will scan. */
221 if (!found && (mac->associnfo.scan_retry < IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT))
370121e5 222 {
78e4f36e
JB
223 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
224 because it cannot follow the best pointer logic. */
370121e5
JB
225 spin_lock_irqsave(&mac->ieee->lock, flags);
226 list_for_each_entry(net, &mac->ieee->network_list, list) {
227 /* we're supposed to find the network with
228 * the best signal here, as we're asked to join
229 * any network with a specific ESSID, and many
230 * different ones could have that.
231 *
78e4f36e 232 * I'll for now just go with the reported rssi.
370121e5
JB
233 *
234 * We also should take into account the rateset
235 * here to find the best BSSID to try.
236 */
237 if (network_matches_request(mac, net)) {
238 if (!best) {
239 best = net;
78e4f36e 240 rssi = best->stats.rssi;
370121e5
JB
241 continue;
242 }
243 /* we already had a matching network, so
244 * compare their properties to get the
245 * better of the two ... (see above)
246 */
78e4f36e
JB
247 if (rssi < net->stats.rssi) {
248 best = net;
249 rssi = best->stats.rssi;
250 }
370121e5
JB
251 }
252 }
253 /* if we unlock here, we might get interrupted and the `best'
254 * pointer could go stale */
255 if (best) {
256 found = ieee80211softmac_create_network(mac, best);
257 /* if found is still NULL, then we got -ENOMEM somewhere */
258 if (found)
259 ieee80211softmac_add_network(mac, found);
260 }
261 spin_unlock_irqrestore(&mac->ieee->lock, flags);
262 }
263
264 if (!found) {
265 if (mac->associnfo.scan_retry > 0) {
370121e5 266 mac->associnfo.scan_retry--;
7c28ad2d 267
370121e5
JB
268 /* We know of no such network. Let's scan.
269 * NB: this also happens if we had no memory to copy the network info...
270 * Maybe we can hope to have more memory after scanning finishes ;)
271 */
1dc09776 272 dprintk(KERN_INFO PFX "Associate: Scanning for networks first.\n");
6ae15df1 273 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify_scan, NULL);
370121e5 274 if (ieee80211softmac_start_scan(mac))
1dc09776 275 dprintk(KERN_INFO PFX "Associate: failed to initiate scan. Is device up?\n");
7c28ad2d 276 goto out;
818667f7 277 } else {
370121e5 278 mac->associnfo.associating = 0;
7c28ad2d 279 mac->associnfo.associated = 0;
370121e5 280
1dc09776 281 dprintk(KERN_INFO PFX "Unable to find matching network after scan!\n");
818667f7
JB
282 /* reset the retry counter for the next user request since we
283 * break out and don't reschedule ourselves after this point. */
284 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
370121e5 285 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
7c28ad2d 286 goto out;
370121e5
JB
287 }
288 }
818667f7
JB
289
290 /* reset the retry counter for the next user request since we
291 * now found a net and will try to associate to it, but not
292 * schedule this function again. */
293 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
370121e5
JB
294 mac->associnfo.bssvalid = 1;
295 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
296 /* copy the ESSID for displaying it */
297 mac->associnfo.associate_essid.len = found->essid.len;
298 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
7c28ad2d 299
370121e5 300 /* we found a network! authenticate (if necessary) and associate to it. */
cb74c432
JJ
301 if (found->authenticating) {
302 dprintk(KERN_INFO PFX "Already requested authentication, waiting...\n");
303 if(!mac->associnfo.assoc_wait) {
304 mac->associnfo.assoc_wait = 1;
4a232e72 305 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify_auth, NULL, GFP_KERNEL);
cb74c432 306 }
7c28ad2d 307 goto out;
cb74c432
JJ
308 }
309 if (!found->authenticated && !found->authenticating) {
370121e5
JB
310 /* This relies on the fact that _auth_req only queues the work,
311 * otherwise adding the notification would be racy. */
312 if (!ieee80211softmac_auth_req(mac, found)) {
cb74c432
JJ
313 if(!mac->associnfo.assoc_wait) {
314 dprintk(KERN_INFO PFX "Cannot associate without being authenticated, requested authentication\n");
315 mac->associnfo.assoc_wait = 1;
4a232e72 316 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify_auth, NULL, GFP_KERNEL);
cb74c432 317 }
370121e5
JB
318 } else {
319 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
cb74c432 320 mac->associnfo.assoc_wait = 0;
370121e5
JB
321 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
322 }
7c28ad2d 323 goto out;
370121e5
JB
324 }
325 /* finally! now we can start associating */
cb74c432 326 mac->associnfo.assoc_wait = 0;
370121e5 327 ieee80211softmac_assoc(mac, found);
7c28ad2d
MB
328
329out:
330 mutex_unlock(&mac->associnfo.mutex);
370121e5
JB
331}
332
333/* call this to do whatever is necessary when we're associated */
334static void
335ieee80211softmac_associated(struct ieee80211softmac_device *mac,
336 struct ieee80211_assoc_response * resp,
337 struct ieee80211softmac_network *net)
338{
5acd0c41
DD
339 u16 cap = le16_to_cpu(resp->capability);
340 u8 erp_value = net->erp_value;
341
370121e5 342 mac->associnfo.associating = 0;
5acd0c41 343 mac->bssinfo.supported_rates = net->supported_rates;
8462fe3c
DD
344 ieee80211softmac_recalc_txrates(mac);
345
7c28ad2d 346 mac->associnfo.associated = 1;
5acd0c41
DD
347
348 mac->associnfo.short_preamble_available =
349 (cap & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0;
350 ieee80211softmac_process_erp(mac, erp_value);
351
370121e5
JB
352 if (mac->set_bssid_filter)
353 mac->set_bssid_filter(mac->dev, net->bssid);
354 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
2dd50801 355 netif_carrier_on(mac->dev);
370121e5
JB
356
357 mac->association_id = le16_to_cpup(&resp->aid);
358}
359
360/* received frame handling functions */
361int
362ieee80211softmac_handle_assoc_response(struct net_device * dev,
363 struct ieee80211_assoc_response * resp,
5acd0c41 364 struct ieee80211_network * _ieee80211_network)
370121e5 365{
5acd0c41 366 /* NOTE: the network parameter has to be mostly ignored by
370121e5
JB
367 * this code because it is the ieee80211's pointer
368 * to the struct, not ours (we made a copy)
369 */
370 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
371 u16 status = le16_to_cpup(&resp->status);
372 struct ieee80211softmac_network *network = NULL;
373 unsigned long flags;
d57336e3
DD
374
375 if (unlikely(!mac->running))
376 return -ENODEV;
370121e5
JB
377
378 spin_lock_irqsave(&mac->lock, flags);
379
380 if (!mac->associnfo.associating) {
381 /* we race against the timeout function, so make sure
382 * only one of us can do work */
383 spin_unlock_irqrestore(&mac->lock, flags);
384 return 0;
385 }
386 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
387
388 /* someone sending us things without us knowing him? Ignore. */
389 if (!network) {
390 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
391 spin_unlock_irqrestore(&mac->lock, flags);
392 return 0;
393 }
394
395 /* now that we know it was for us, we can cancel the timeout */
396 cancel_delayed_work(&mac->associnfo.timeout);
397
5acd0c41
DD
398 /* if the association response included an ERP IE, update our saved
399 * copy */
400 if (_ieee80211_network->flags & NETWORK_HAS_ERP_VALUE)
401 network->erp_value = _ieee80211_network->erp_value;
402
370121e5
JB
403 switch (status) {
404 case 0:
405 dprintk(KERN_INFO PFX "associated!\n");
406 ieee80211softmac_associated(mac, resp, network);
407 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
408 break;
409 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
410 if (!network->auth_desynced_once) {
411 /* there seem to be a few rare cases where our view of
412 * the world is obscured, or buggy APs that don't DEAUTH
413 * us properly. So we handle that, but allow it only once.
414 */
415 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
416 network->authenticated = 0;
417 /* we don't want to do this more than once ... */
418 network->auth_desynced_once = 1;
c4028958 419 schedule_delayed_work(&mac->associnfo.work, 0);
370121e5
JB
420 break;
421 }
422 default:
423 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
424 mac->associnfo.associating = 0;
425 mac->associnfo.bssvalid = 0;
7c28ad2d 426 mac->associnfo.associated = 0;
370121e5
JB
427 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
428 }
429
430 spin_unlock_irqrestore(&mac->lock, flags);
431 return 0;
432}
433
434int
435ieee80211softmac_handle_disassoc(struct net_device * dev,
436 struct ieee80211_disassoc *disassoc)
437{
438 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
d57336e3
DD
439
440 if (unlikely(!mac->running))
441 return -ENODEV;
442
48b2e4ce
JB
443 if (memcmp(disassoc->header.addr2, mac->associnfo.bssid, ETH_ALEN))
444 return 0;
d57336e3 445
48b2e4ce
JB
446 if (memcmp(disassoc->header.addr1, mac->dev->dev_addr, ETH_ALEN))
447 return 0;
d57336e3 448
370121e5 449 dprintk(KERN_INFO PFX "got disassoc frame\n");
6d92f83f
DD
450 ieee80211softmac_disassoc(mac);
451
452 /* try to reassociate */
c4028958 453 schedule_delayed_work(&mac->associnfo.work, 0);
6d92f83f 454
370121e5
JB
455 return 0;
456}
b6c7658e
JB
457
458int
459ieee80211softmac_handle_reassoc_req(struct net_device * dev,
460 struct ieee80211_reassoc_request * resp)
461{
462 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
463 struct ieee80211softmac_network *network;
464
d57336e3
DD
465 if (unlikely(!mac->running))
466 return -ENODEV;
467
b6c7658e
JB
468 network = ieee80211softmac_get_network_by_bssid(mac, resp->header.addr3);
469 if (!network) {
470 dprintkl(KERN_INFO PFX "reassoc request from unknown network\n");
471 return 0;
472 }
c4028958 473 schedule_delayed_work(&mac->associnfo.work, 0);
9b0b4d8a 474
b6c7658e
JB
475 return 0;
476}
This page took 0.180839 seconds and 5 git commands to generate.