[PATCH] softmac: select "best" network based on rssi
[deliverable/linux.git] / net / ieee80211 / softmac / ieee80211softmac_assoc.c
CommitLineData
370121e5
JB
1#include "ieee80211softmac_priv.h"
2
3/*
4 * Overview
5 *
6 * Before you can associate, you have to authenticate.
7 *
8 */
9
10/* Sends out an association request to the desired AP */
11static void
12ieee80211softmac_assoc(struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
13{
14 unsigned long flags;
15 function_enter();
16 /* Switch to correct channel for this network */
17 mac->set_channel(mac->dev, net->channel);
18
19 /* Send association request */
20 ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_ASSOC_REQ, 0);
21
22 dprintk(KERN_INFO PFX "sent association request!\n");
23
24 /* Change the state to associating */
25 spin_lock_irqsave(&mac->lock, flags);
26 mac->associnfo.associating = 1;
27 mac->associated = 0; /* just to make sure */
28 spin_unlock_irqrestore(&mac->lock, flags);
29
30 /* Set a timer for timeout */
31 /* FIXME: make timeout configurable */
5c4df6da 32 schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ);
370121e5
JB
33}
34
35void
36ieee80211softmac_assoc_timeout(void *d)
37{
38 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
39 unsigned long flags;
40
41 function_enter();
42
43 spin_lock_irqsave(&mac->lock, flags);
44 /* we might race against ieee80211softmac_handle_assoc_response,
45 * so make sure only one of us does something */
46 if (!mac->associnfo.associating) {
47 spin_unlock_irqrestore(&mac->lock, flags);
48 return;
49 }
50 mac->associnfo.associating = 0;
51 mac->associnfo.bssvalid = 0;
52 mac->associated = 0;
53 spin_unlock_irqrestore(&mac->lock, flags);
54
55 dprintk(KERN_INFO PFX "assoc request timed out!\n");
56 /* FIXME: we need to know the network here. that requires a bit of restructuring */
57 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_TIMEOUT, NULL);
58}
59
60static void
61ieee80211softmac_reassoc(struct ieee80211softmac_device *mac)
62{
63 function_enter();
64}
65
66
67/* Sends out a disassociation request to the desired AP */
68static void
69ieee80211softmac_disassoc(struct ieee80211softmac_device *mac, u16 reason)
70{
71 unsigned long flags;
72 struct ieee80211softmac_network *found;
73 function_enter();
74
75 if (mac->associnfo.bssvalid && mac->associated) {
76 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
77 if (found)
78 ieee80211softmac_send_mgt_frame(mac, found, IEEE80211_STYPE_DISASSOC, reason);
79 } else if (mac->associnfo.associating) {
80 cancel_delayed_work(&mac->associnfo.timeout);
81 }
82
83 /* Change our state */
84 spin_lock_irqsave(&mac->lock, flags);
85 /* Do NOT clear bssvalid as that will break ieee80211softmac_assoc_work! */
86 mac->associated = 0;
87 mac->associnfo.associating = 0;
88 spin_unlock_irqrestore(&mac->lock, flags);
89}
90
91static inline int
92we_support_all_basic_rates(struct ieee80211softmac_device *mac, u8 *from, u8 from_len)
93{
94 int idx, search, found;
95 u8 rate, search_rate;
96
97 for (idx = 0; idx < (from_len); idx++) {
98 rate = (from)[idx];
99 if (!(rate & IEEE80211_BASIC_RATE_MASK))
100 continue;
101 found = 0;
102 rate &= ~IEEE80211_BASIC_RATE_MASK;
103 for (search = 0; search < mac->ratesinfo.count; search++) {
104 search_rate = mac->ratesinfo.rates[search];
105 search_rate &= ~IEEE80211_BASIC_RATE_MASK;
106 if (rate == search_rate) {
107 found = 1;
108 break;
109 }
110 }
111 if (!found)
112 return 0;
113 }
114 return 1;
115}
116
117static int
118network_matches_request(struct ieee80211softmac_device *mac, struct ieee80211_network *net)
119{
120 /* we cannot associate to networks whose name we don't know */
121 if (ieee80211_is_empty_essid(net->ssid, net->ssid_len))
122 return 0;
123 /* do not associate to a network whose BSSBasicRateSet we cannot support */
124 if (!we_support_all_basic_rates(mac, net->rates, net->rates_len))
125 return 0;
126 /* do we really need to check the ex rates? */
127 if (!we_support_all_basic_rates(mac, net->rates_ex, net->rates_ex_len))
128 return 0;
129
130 /* if 'ANY' network requested, take any that doesn't have privacy enabled */
131 if (mac->associnfo.req_essid.len == 0
132 && !(net->capability & WLAN_CAPABILITY_PRIVACY))
133 return 1;
134 if (net->ssid_len != mac->associnfo.req_essid.len)
135 return 0;
136 if (!memcmp(net->ssid, mac->associnfo.req_essid.data, mac->associnfo.req_essid.len))
137 return 1;
138 return 0;
139}
140
141static void
142ieee80211softmac_assoc_notify(struct net_device *dev, void *context)
143{
144 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
145 ieee80211softmac_assoc_work((void*)mac);
146}
147
148/* This function is called to handle userspace requests (asynchronously) */
149void
150ieee80211softmac_assoc_work(void *d)
151{
152 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
153 struct ieee80211softmac_network *found = NULL;
154 struct ieee80211_network *net = NULL, *best = NULL;
155 unsigned long flags;
156
157 function_enter();
158
159 /* meh */
160 if (mac->associated)
161 ieee80211softmac_disassoc(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
162
163 /* try to find the requested network in our list, if we found one already */
164 if (mac->associnfo.bssvalid)
165 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
166
167 /* Search the ieee80211 networks for this network if we didn't find it */
168 if (!found)
169 {
78e4f36e
JB
170 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
171 because it cannot follow the best pointer logic. */
370121e5
JB
172 spin_lock_irqsave(&mac->ieee->lock, flags);
173 list_for_each_entry(net, &mac->ieee->network_list, list) {
174 /* we're supposed to find the network with
175 * the best signal here, as we're asked to join
176 * any network with a specific ESSID, and many
177 * different ones could have that.
178 *
78e4f36e 179 * I'll for now just go with the reported rssi.
370121e5
JB
180 *
181 * We also should take into account the rateset
182 * here to find the best BSSID to try.
183 */
184 if (network_matches_request(mac, net)) {
185 if (!best) {
186 best = net;
78e4f36e 187 rssi = best->stats.rssi;
370121e5
JB
188 continue;
189 }
190 /* we already had a matching network, so
191 * compare their properties to get the
192 * better of the two ... (see above)
193 */
78e4f36e
JB
194 if (rssi < net->stats.rssi) {
195 best = net;
196 rssi = best->stats.rssi;
197 }
370121e5
JB
198 }
199 }
200 /* if we unlock here, we might get interrupted and the `best'
201 * pointer could go stale */
202 if (best) {
203 found = ieee80211softmac_create_network(mac, best);
204 /* if found is still NULL, then we got -ENOMEM somewhere */
205 if (found)
206 ieee80211softmac_add_network(mac, found);
207 }
208 spin_unlock_irqrestore(&mac->ieee->lock, flags);
209 }
210
211 if (!found) {
212 if (mac->associnfo.scan_retry > 0) {
213 spin_lock_irqsave(&mac->lock, flags);
214 mac->associnfo.scan_retry--;
215 spin_unlock_irqrestore(&mac->lock, flags);
216
217 /* We know of no such network. Let's scan.
218 * NB: this also happens if we had no memory to copy the network info...
219 * Maybe we can hope to have more memory after scanning finishes ;)
220 */
221 dprintk(KERN_INFO PFX "Associate: Network not known, trying to initiate scan: ");
222 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL);
223 if (ieee80211softmac_start_scan(mac))
224 dprintk("failed.\n");
225 else
226 dprintk("ok.\n");
227 return;
228 }
229 else {
230 spin_lock_irqsave(&mac->lock, flags);
231 mac->associnfo.associating = 0;
232 mac->associated = 0;
233 spin_unlock_irqrestore(&mac->lock, flags);
234
235 dprintk(KERN_INFO PFX "Unable to find network after scan!\n");
236 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
237 return;
238 }
239 }
240
241 mac->associnfo.bssvalid = 1;
242 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
243 /* copy the ESSID for displaying it */
244 mac->associnfo.associate_essid.len = found->essid.len;
245 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
246
247 /* we found a network! authenticate (if necessary) and associate to it. */
248 if (!found->authenticated) {
249 /* This relies on the fact that _auth_req only queues the work,
250 * otherwise adding the notification would be racy. */
251 if (!ieee80211softmac_auth_req(mac, found)) {
252 dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n");
253 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL);
254 } else {
255 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
256 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
257 }
258 return;
259 }
260 /* finally! now we can start associating */
261 ieee80211softmac_assoc(mac, found);
262}
263
264/* call this to do whatever is necessary when we're associated */
265static void
266ieee80211softmac_associated(struct ieee80211softmac_device *mac,
267 struct ieee80211_assoc_response * resp,
268 struct ieee80211softmac_network *net)
269{
270 mac->associnfo.associating = 0;
271 mac->associated = 1;
272 if (mac->set_bssid_filter)
273 mac->set_bssid_filter(mac->dev, net->bssid);
274 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
2dd50801 275 netif_carrier_on(mac->dev);
370121e5
JB
276
277 mac->association_id = le16_to_cpup(&resp->aid);
278}
279
280/* received frame handling functions */
281int
282ieee80211softmac_handle_assoc_response(struct net_device * dev,
283 struct ieee80211_assoc_response * resp,
284 struct ieee80211_network * _ieee80211_network_do_not_use)
285{
286 /* NOTE: the network parameter has to be ignored by
287 * this code because it is the ieee80211's pointer
288 * to the struct, not ours (we made a copy)
289 */
290 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
291 u16 status = le16_to_cpup(&resp->status);
292 struct ieee80211softmac_network *network = NULL;
293 unsigned long flags;
294
295 spin_lock_irqsave(&mac->lock, flags);
296
297 if (!mac->associnfo.associating) {
298 /* we race against the timeout function, so make sure
299 * only one of us can do work */
300 spin_unlock_irqrestore(&mac->lock, flags);
301 return 0;
302 }
303 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
304
305 /* someone sending us things without us knowing him? Ignore. */
306 if (!network) {
307 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
308 spin_unlock_irqrestore(&mac->lock, flags);
309 return 0;
310 }
311
312 /* now that we know it was for us, we can cancel the timeout */
313 cancel_delayed_work(&mac->associnfo.timeout);
314
315 switch (status) {
316 case 0:
317 dprintk(KERN_INFO PFX "associated!\n");
318 ieee80211softmac_associated(mac, resp, network);
319 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
320 break;
321 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
322 if (!network->auth_desynced_once) {
323 /* there seem to be a few rare cases where our view of
324 * the world is obscured, or buggy APs that don't DEAUTH
325 * us properly. So we handle that, but allow it only once.
326 */
327 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
328 network->authenticated = 0;
329 /* we don't want to do this more than once ... */
330 network->auth_desynced_once = 1;
5c4df6da 331 schedule_work(&mac->associnfo.work);
370121e5
JB
332 break;
333 }
334 default:
335 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
336 mac->associnfo.associating = 0;
337 mac->associnfo.bssvalid = 0;
338 mac->associated = 0;
339 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
340 }
341
342 spin_unlock_irqrestore(&mac->lock, flags);
343 return 0;
344}
345
346int
347ieee80211softmac_handle_disassoc(struct net_device * dev,
348 struct ieee80211_disassoc *disassoc)
349{
350 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
351 unsigned long flags;
51da28a8 352 /* FIXME: check that this frame is from the right AP!! */
370121e5 353 dprintk(KERN_INFO PFX "got disassoc frame\n");
2dd50801 354 netif_carrier_off(dev);
370121e5
JB
355 spin_lock_irqsave(&mac->lock, flags);
356 mac->associnfo.bssvalid = 0;
357 mac->associated = 0;
d1469cf2 358 schedule_work(&mac->associnfo.work);
370121e5
JB
359 spin_unlock_irqrestore(&mac->lock, flags);
360
361 return 0;
362}
This page took 0.038842 seconds and 5 git commands to generate.