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