[PATCH] softmac: some comment stuff
[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
1dc09776
JB
167 /* Search the ieee80211 networks for this network if we didn't find it by bssid,
168 * but only if we've scanned at least once (to get a better list of networks to
169 * select from). If we have not scanned before, the !found logic below will be
170 * invoked and will scan. */
171 if (!found && (mac->associnfo.scan_retry < IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT))
370121e5 172 {
78e4f36e
JB
173 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
174 because it cannot follow the best pointer logic. */
370121e5
JB
175 spin_lock_irqsave(&mac->ieee->lock, flags);
176 list_for_each_entry(net, &mac->ieee->network_list, list) {
177 /* we're supposed to find the network with
178 * the best signal here, as we're asked to join
179 * any network with a specific ESSID, and many
180 * different ones could have that.
181 *
78e4f36e 182 * I'll for now just go with the reported rssi.
370121e5
JB
183 *
184 * We also should take into account the rateset
185 * here to find the best BSSID to try.
186 */
187 if (network_matches_request(mac, net)) {
188 if (!best) {
189 best = net;
78e4f36e 190 rssi = best->stats.rssi;
370121e5
JB
191 continue;
192 }
193 /* we already had a matching network, so
194 * compare their properties to get the
195 * better of the two ... (see above)
196 */
78e4f36e
JB
197 if (rssi < net->stats.rssi) {
198 best = net;
199 rssi = best->stats.rssi;
200 }
370121e5
JB
201 }
202 }
203 /* if we unlock here, we might get interrupted and the `best'
204 * pointer could go stale */
205 if (best) {
206 found = ieee80211softmac_create_network(mac, best);
207 /* if found is still NULL, then we got -ENOMEM somewhere */
208 if (found)
209 ieee80211softmac_add_network(mac, found);
210 }
211 spin_unlock_irqrestore(&mac->ieee->lock, flags);
212 }
213
214 if (!found) {
215 if (mac->associnfo.scan_retry > 0) {
216 spin_lock_irqsave(&mac->lock, flags);
217 mac->associnfo.scan_retry--;
218 spin_unlock_irqrestore(&mac->lock, flags);
219
220 /* We know of no such network. Let's scan.
221 * NB: this also happens if we had no memory to copy the network info...
222 * Maybe we can hope to have more memory after scanning finishes ;)
223 */
1dc09776 224 dprintk(KERN_INFO PFX "Associate: Scanning for networks first.\n");
370121e5
JB
225 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL);
226 if (ieee80211softmac_start_scan(mac))
1dc09776 227 dprintk(KERN_INFO PFX "Associate: failed to initiate scan. Is device up?\n");
370121e5
JB
228 return;
229 }
230 else {
231 spin_lock_irqsave(&mac->lock, flags);
232 mac->associnfo.associating = 0;
233 mac->associated = 0;
234 spin_unlock_irqrestore(&mac->lock, flags);
235
1dc09776 236 dprintk(KERN_INFO PFX "Unable to find matching network after scan!\n");
370121e5
JB
237 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
238 return;
239 }
240 }
241
242 mac->associnfo.bssvalid = 1;
243 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
244 /* copy the ESSID for displaying it */
245 mac->associnfo.associate_essid.len = found->essid.len;
246 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
247
248 /* we found a network! authenticate (if necessary) and associate to it. */
249 if (!found->authenticated) {
250 /* This relies on the fact that _auth_req only queues the work,
251 * otherwise adding the notification would be racy. */
252 if (!ieee80211softmac_auth_req(mac, found)) {
253 dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n");
254 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL);
255 } else {
256 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
257 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
258 }
259 return;
260 }
261 /* finally! now we can start associating */
262 ieee80211softmac_assoc(mac, found);
263}
264
265/* call this to do whatever is necessary when we're associated */
266static void
267ieee80211softmac_associated(struct ieee80211softmac_device *mac,
268 struct ieee80211_assoc_response * resp,
269 struct ieee80211softmac_network *net)
270{
271 mac->associnfo.associating = 0;
272 mac->associated = 1;
273 if (mac->set_bssid_filter)
274 mac->set_bssid_filter(mac->dev, net->bssid);
275 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
2dd50801 276 netif_carrier_on(mac->dev);
370121e5
JB
277
278 mac->association_id = le16_to_cpup(&resp->aid);
279}
280
281/* received frame handling functions */
282int
283ieee80211softmac_handle_assoc_response(struct net_device * dev,
284 struct ieee80211_assoc_response * resp,
285 struct ieee80211_network * _ieee80211_network_do_not_use)
286{
287 /* NOTE: the network parameter has to be ignored by
288 * this code because it is the ieee80211's pointer
289 * to the struct, not ours (we made a copy)
290 */
291 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
292 u16 status = le16_to_cpup(&resp->status);
293 struct ieee80211softmac_network *network = NULL;
294 unsigned long flags;
295
296 spin_lock_irqsave(&mac->lock, flags);
297
298 if (!mac->associnfo.associating) {
299 /* we race against the timeout function, so make sure
300 * only one of us can do work */
301 spin_unlock_irqrestore(&mac->lock, flags);
302 return 0;
303 }
304 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
305
306 /* someone sending us things without us knowing him? Ignore. */
307 if (!network) {
308 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
309 spin_unlock_irqrestore(&mac->lock, flags);
310 return 0;
311 }
312
313 /* now that we know it was for us, we can cancel the timeout */
314 cancel_delayed_work(&mac->associnfo.timeout);
315
316 switch (status) {
317 case 0:
318 dprintk(KERN_INFO PFX "associated!\n");
319 ieee80211softmac_associated(mac, resp, network);
320 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
321 break;
322 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
323 if (!network->auth_desynced_once) {
324 /* there seem to be a few rare cases where our view of
325 * the world is obscured, or buggy APs that don't DEAUTH
326 * us properly. So we handle that, but allow it only once.
327 */
328 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
329 network->authenticated = 0;
330 /* we don't want to do this more than once ... */
331 network->auth_desynced_once = 1;
5c4df6da 332 schedule_work(&mac->associnfo.work);
370121e5
JB
333 break;
334 }
335 default:
336 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
337 mac->associnfo.associating = 0;
338 mac->associnfo.bssvalid = 0;
339 mac->associated = 0;
340 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
341 }
342
343 spin_unlock_irqrestore(&mac->lock, flags);
344 return 0;
345}
346
347int
348ieee80211softmac_handle_disassoc(struct net_device * dev,
349 struct ieee80211_disassoc *disassoc)
350{
351 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
352 unsigned long flags;
48b2e4ce
JB
353 if (memcmp(disassoc->header.addr2, mac->associnfo.bssid, ETH_ALEN))
354 return 0;
355 if (memcmp(disassoc->header.addr1, mac->dev->dev_addr, ETH_ALEN))
356 return 0;
370121e5 357 dprintk(KERN_INFO PFX "got disassoc frame\n");
2dd50801 358 netif_carrier_off(dev);
370121e5
JB
359 spin_lock_irqsave(&mac->lock, flags);
360 mac->associnfo.bssvalid = 0;
361 mac->associated = 0;
d1469cf2 362 schedule_work(&mac->associnfo.work);
370121e5
JB
363 spin_unlock_irqrestore(&mac->lock, flags);
364
365 return 0;
366}
This page took 0.037539 seconds and 5 git commands to generate.