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