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