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