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