[PATCH] softmac: make non-operational after being stopped
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ieee80211 / softmac / ieee80211softmac_assoc.c
blob57ea9f6f465c50679ff62af18dec5f0dc9f8b42f
1 /*
2 * This file contains the softmac's association logic.
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>
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.
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.
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
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
27 #include "ieee80211softmac_priv.h"
30 * Overview
32 * Before you can associate, you have to authenticate.
36 /* Sends out an association request to the desired AP */
37 static void
38 ieee80211softmac_assoc(struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
40 unsigned long flags;
42 /* Switch to correct channel for this network */
43 mac->set_channel(mac->dev, net->channel);
45 /* Send association request */
46 ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_ASSOC_REQ, 0);
48 dprintk(KERN_INFO PFX "sent association request!\n");
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 */
55 /* Set a timer for timeout */
56 /* FIXME: make timeout configurable */
57 if (likely(mac->running))
58 schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ);
59 spin_unlock_irqrestore(&mac->lock, flags);
62 void
63 ieee80211softmac_assoc_timeout(void *d)
65 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
66 unsigned long flags;
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;
75 mac->associnfo.associating = 0;
76 mac->associnfo.bssvalid = 0;
77 mac->associated = 0;
78 spin_unlock_irqrestore(&mac->lock, flags);
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);
85 /* Sends out a disassociation request to the desired AP */
86 static void
87 ieee80211softmac_disassoc(struct ieee80211softmac_device *mac, u16 reason)
89 unsigned long flags;
90 struct ieee80211softmac_network *found;
92 if (mac->associnfo.bssvalid && mac->associated) {
93 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
94 if (found)
95 ieee80211softmac_send_mgt_frame(mac, found, IEEE80211_STYPE_DISASSOC, reason);
96 } else if (mac->associnfo.associating) {
97 cancel_delayed_work(&mac->associnfo.timeout);
100 /* Change our state */
101 spin_lock_irqsave(&mac->lock, flags);
102 /* Do NOT clear bssvalid as that will break ieee80211softmac_assoc_work! */
103 mac->associated = 0;
104 mac->associnfo.associating = 0;
105 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_DISASSOCIATED, NULL);
106 spin_unlock_irqrestore(&mac->lock, flags);
109 static inline int
110 we_support_all_basic_rates(struct ieee80211softmac_device *mac, u8 *from, u8 from_len)
112 int idx, search, found;
113 u8 rate, search_rate;
115 for (idx = 0; idx < (from_len); idx++) {
116 rate = (from)[idx];
117 if (!(rate & IEEE80211_BASIC_RATE_MASK))
118 continue;
119 found = 0;
120 rate &= ~IEEE80211_BASIC_RATE_MASK;
121 for (search = 0; search < mac->ratesinfo.count; search++) {
122 search_rate = mac->ratesinfo.rates[search];
123 search_rate &= ~IEEE80211_BASIC_RATE_MASK;
124 if (rate == search_rate) {
125 found = 1;
126 break;
129 if (!found)
130 return 0;
132 return 1;
135 static int
136 network_matches_request(struct ieee80211softmac_device *mac, struct ieee80211_network *net)
138 /* we cannot associate to networks whose name we don't know */
139 if (ieee80211_is_empty_essid(net->ssid, net->ssid_len))
140 return 0;
141 /* do not associate to a network whose BSSBasicRateSet we cannot support */
142 if (!we_support_all_basic_rates(mac, net->rates, net->rates_len))
143 return 0;
144 /* do we really need to check the ex rates? */
145 if (!we_support_all_basic_rates(mac, net->rates_ex, net->rates_ex_len))
146 return 0;
148 /* assume that users know what they're doing ...
149 * (note we don't let them select a net we're incompatible with) */
150 if (mac->associnfo.bssfixed) {
151 return !memcmp(mac->associnfo.bssid, net->bssid, ETH_ALEN);
154 /* if 'ANY' network requested, take any that doesn't have privacy enabled */
155 if (mac->associnfo.req_essid.len == 0
156 && !(net->capability & WLAN_CAPABILITY_PRIVACY))
157 return 1;
158 if (net->ssid_len != mac->associnfo.req_essid.len)
159 return 0;
160 if (!memcmp(net->ssid, mac->associnfo.req_essid.data, mac->associnfo.req_essid.len))
161 return 1;
162 return 0;
165 static void
166 ieee80211softmac_assoc_notify(struct net_device *dev, void *context)
168 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
169 ieee80211softmac_assoc_work((void*)mac);
172 /* This function is called to handle userspace requests (asynchronously) */
173 void
174 ieee80211softmac_assoc_work(void *d)
176 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
177 struct ieee80211softmac_network *found = NULL;
178 struct ieee80211_network *net = NULL, *best = NULL;
179 unsigned long flags;
181 /* meh */
182 if (mac->associated)
183 ieee80211softmac_disassoc(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
185 /* try to find the requested network in our list, if we found one already */
186 if (mac->associnfo.bssvalid || mac->associnfo.bssfixed)
187 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
189 /* Search the ieee80211 networks for this network if we didn't find it by bssid,
190 * but only if we've scanned at least once (to get a better list of networks to
191 * select from). If we have not scanned before, the !found logic below will be
192 * invoked and will scan. */
193 if (!found && (mac->associnfo.scan_retry < IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT))
195 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
196 because it cannot follow the best pointer logic. */
197 spin_lock_irqsave(&mac->ieee->lock, flags);
198 list_for_each_entry(net, &mac->ieee->network_list, list) {
199 /* we're supposed to find the network with
200 * the best signal here, as we're asked to join
201 * any network with a specific ESSID, and many
202 * different ones could have that.
204 * I'll for now just go with the reported rssi.
206 * We also should take into account the rateset
207 * here to find the best BSSID to try.
209 if (network_matches_request(mac, net)) {
210 if (!best) {
211 best = net;
212 rssi = best->stats.rssi;
213 continue;
215 /* we already had a matching network, so
216 * compare their properties to get the
217 * better of the two ... (see above)
219 if (rssi < net->stats.rssi) {
220 best = net;
221 rssi = best->stats.rssi;
225 /* if we unlock here, we might get interrupted and the `best'
226 * pointer could go stale */
227 if (best) {
228 found = ieee80211softmac_create_network(mac, best);
229 /* if found is still NULL, then we got -ENOMEM somewhere */
230 if (found)
231 ieee80211softmac_add_network(mac, found);
233 spin_unlock_irqrestore(&mac->ieee->lock, flags);
236 if (!found) {
237 if (mac->associnfo.scan_retry > 0) {
238 spin_lock_irqsave(&mac->lock, flags);
239 mac->associnfo.scan_retry--;
240 spin_unlock_irqrestore(&mac->lock, flags);
242 /* We know of no such network. Let's scan.
243 * NB: this also happens if we had no memory to copy the network info...
244 * Maybe we can hope to have more memory after scanning finishes ;)
246 dprintk(KERN_INFO PFX "Associate: Scanning for networks first.\n");
247 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL);
248 if (ieee80211softmac_start_scan(mac))
249 dprintk(KERN_INFO PFX "Associate: failed to initiate scan. Is device up?\n");
250 return;
251 } else {
252 spin_lock_irqsave(&mac->lock, flags);
253 mac->associnfo.associating = 0;
254 mac->associated = 0;
255 spin_unlock_irqrestore(&mac->lock, flags);
257 dprintk(KERN_INFO PFX "Unable to find matching network after scan!\n");
258 /* reset the retry counter for the next user request since we
259 * break out and don't reschedule ourselves after this point. */
260 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
261 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
262 return;
266 /* reset the retry counter for the next user request since we
267 * now found a net and will try to associate to it, but not
268 * schedule this function again. */
269 mac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;
270 mac->associnfo.bssvalid = 1;
271 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
272 /* copy the ESSID for displaying it */
273 mac->associnfo.associate_essid.len = found->essid.len;
274 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
276 /* we found a network! authenticate (if necessary) and associate to it. */
277 if (!found->authenticated) {
278 /* This relies on the fact that _auth_req only queues the work,
279 * otherwise adding the notification would be racy. */
280 if (!ieee80211softmac_auth_req(mac, found)) {
281 dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n");
282 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL);
283 } else {
284 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
285 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
287 return;
289 /* finally! now we can start associating */
290 ieee80211softmac_assoc(mac, found);
293 /* call this to do whatever is necessary when we're associated */
294 static void
295 ieee80211softmac_associated(struct ieee80211softmac_device *mac,
296 struct ieee80211_assoc_response * resp,
297 struct ieee80211softmac_network *net)
299 mac->associnfo.associating = 0;
300 mac->associated = 1;
301 if (mac->set_bssid_filter)
302 mac->set_bssid_filter(mac->dev, net->bssid);
303 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
304 netif_carrier_on(mac->dev);
306 mac->association_id = le16_to_cpup(&resp->aid);
309 /* received frame handling functions */
311 ieee80211softmac_handle_assoc_response(struct net_device * dev,
312 struct ieee80211_assoc_response * resp,
313 struct ieee80211_network * _ieee80211_network_do_not_use)
315 /* NOTE: the network parameter has to be ignored by
316 * this code because it is the ieee80211's pointer
317 * to the struct, not ours (we made a copy)
319 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
320 u16 status = le16_to_cpup(&resp->status);
321 struct ieee80211softmac_network *network = NULL;
322 unsigned long flags;
324 if (unlikely(!mac->running))
325 return -ENODEV;
327 spin_lock_irqsave(&mac->lock, flags);
329 if (!mac->associnfo.associating) {
330 /* we race against the timeout function, so make sure
331 * only one of us can do work */
332 spin_unlock_irqrestore(&mac->lock, flags);
333 return 0;
335 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
337 /* someone sending us things without us knowing him? Ignore. */
338 if (!network) {
339 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
340 spin_unlock_irqrestore(&mac->lock, flags);
341 return 0;
344 /* now that we know it was for us, we can cancel the timeout */
345 cancel_delayed_work(&mac->associnfo.timeout);
347 switch (status) {
348 case 0:
349 dprintk(KERN_INFO PFX "associated!\n");
350 ieee80211softmac_associated(mac, resp, network);
351 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
352 break;
353 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
354 if (!network->auth_desynced_once) {
355 /* there seem to be a few rare cases where our view of
356 * the world is obscured, or buggy APs that don't DEAUTH
357 * us properly. So we handle that, but allow it only once.
359 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
360 network->authenticated = 0;
361 /* we don't want to do this more than once ... */
362 network->auth_desynced_once = 1;
363 schedule_work(&mac->associnfo.work);
364 break;
366 default:
367 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
368 mac->associnfo.associating = 0;
369 mac->associnfo.bssvalid = 0;
370 mac->associated = 0;
371 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
374 spin_unlock_irqrestore(&mac->lock, flags);
375 return 0;
379 ieee80211softmac_handle_disassoc(struct net_device * dev,
380 struct ieee80211_disassoc *disassoc)
382 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
383 unsigned long flags;
385 if (unlikely(!mac->running))
386 return -ENODEV;
388 if (memcmp(disassoc->header.addr2, mac->associnfo.bssid, ETH_ALEN))
389 return 0;
391 if (memcmp(disassoc->header.addr1, mac->dev->dev_addr, ETH_ALEN))
392 return 0;
394 dprintk(KERN_INFO PFX "got disassoc frame\n");
395 netif_carrier_off(dev);
396 spin_lock_irqsave(&mac->lock, flags);
397 mac->associnfo.bssvalid = 0;
398 mac->associated = 0;
399 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_DISASSOCIATED, NULL);
400 schedule_work(&mac->associnfo.work);
401 spin_unlock_irqrestore(&mac->lock, flags);
403 return 0;
407 ieee80211softmac_handle_reassoc_req(struct net_device * dev,
408 struct ieee80211_reassoc_request * resp)
410 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
411 struct ieee80211softmac_network *network;
413 if (unlikely(!mac->running))
414 return -ENODEV;
416 network = ieee80211softmac_get_network_by_bssid(mac, resp->header.addr3);
417 if (!network) {
418 dprintkl(KERN_INFO PFX "reassoc request from unknown network\n");
419 return 0;
421 schedule_work(&mac->associnfo.work);
423 return 0;