2 * This file contains the softmac's association logic.
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>
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.
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
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
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
27 #include "ieee80211softmac_priv.h"
32 * Before you can associate, you have to authenticate.
36 /* Sends out an association request to the desired AP */
38 ieee80211softmac_assoc(struct ieee80211softmac_device
*mac
, struct ieee80211softmac_network
*net
)
42 /* Switch to correct channel for this network */
43 mac
->set_channel(mac
->dev
, net
->channel
);
45 /* Send association request */
46 ieee80211softmac_send_mgt_frame(mac
, net
, IEEE80211_STYPE_ASSOC_REQ
, 0);
48 dprintk(KERN_INFO PFX
"sent association request!\n");
50 spin_lock_irqsave(&mac
->lock
, flags
);
51 mac
->associnfo
.associated
= 0; /* just to make sure */
53 /* Set a timer for timeout */
54 /* FIXME: make timeout configurable */
55 if (likely(mac
->running
))
56 schedule_delayed_work(&mac
->associnfo
.timeout
, 5 * HZ
);
57 spin_unlock_irqrestore(&mac
->lock
, flags
);
61 ieee80211softmac_assoc_timeout(struct work_struct
*work
)
63 struct ieee80211softmac_device
*mac
=
64 container_of(work
, struct ieee80211softmac_device
,
65 associnfo
.timeout
.work
);
66 struct ieee80211softmac_network
*n
;
68 mutex_lock(&mac
->associnfo
.mutex
);
69 /* we might race against ieee80211softmac_handle_assoc_response,
70 * so make sure only one of us does something */
71 if (!mac
->associnfo
.associating
)
73 mac
->associnfo
.associating
= 0;
74 mac
->associnfo
.bssvalid
= 0;
75 mac
->associnfo
.associated
= 0;
77 n
= ieee80211softmac_get_network_by_bssid_locked(mac
, mac
->associnfo
.bssid
);
79 dprintk(KERN_INFO PFX
"assoc request timed out!\n");
80 ieee80211softmac_call_events(mac
, IEEE80211SOFTMAC_EVENT_ASSOCIATE_TIMEOUT
, n
);
82 mutex_unlock(&mac
->associnfo
.mutex
);
86 ieee80211softmac_disassoc(struct ieee80211softmac_device
*mac
)
90 spin_lock_irqsave(&mac
->lock
, flags
);
91 if (mac
->associnfo
.associating
)
92 cancel_delayed_work(&mac
->associnfo
.timeout
);
94 netif_carrier_off(mac
->dev
);
96 mac
->associnfo
.associated
= 0;
97 mac
->associnfo
.bssvalid
= 0;
98 mac
->associnfo
.associating
= 0;
99 ieee80211softmac_init_bss(mac
);
100 ieee80211softmac_call_events_locked(mac
, IEEE80211SOFTMAC_EVENT_DISASSOCIATED
, NULL
);
101 spin_unlock_irqrestore(&mac
->lock
, flags
);
104 /* Sends out a disassociation request to the desired AP */
106 ieee80211softmac_send_disassoc_req(struct ieee80211softmac_device
*mac
, u16 reason
)
108 struct ieee80211softmac_network
*found
;
110 if (mac
->associnfo
.bssvalid
&& mac
->associnfo
.associated
) {
111 found
= ieee80211softmac_get_network_by_bssid(mac
, mac
->associnfo
.bssid
);
113 ieee80211softmac_send_mgt_frame(mac
, found
, IEEE80211_STYPE_DISASSOC
, reason
);
116 ieee80211softmac_disassoc(mac
);
120 we_support_all_basic_rates(struct ieee80211softmac_device
*mac
, u8
*from
, u8 from_len
)
125 for (idx
= 0; idx
< (from_len
); idx
++) {
127 if (!(rate
& IEEE80211_BASIC_RATE_MASK
))
129 rate
&= ~IEEE80211_BASIC_RATE_MASK
;
130 if (!ieee80211softmac_ratesinfo_rate_supported(&mac
->ratesinfo
, rate
))
137 network_matches_request(struct ieee80211softmac_device
*mac
, struct ieee80211_network
*net
)
139 /* we cannot associate to networks whose name we don't know */
140 if (ieee80211_is_empty_essid(net
->ssid
, net
->ssid_len
))
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
))
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
))
149 /* assume that users know what they're doing ...
150 * (note we don't let them select a net we're incompatible with) */
151 if (mac
->associnfo
.bssfixed
) {
152 return !memcmp(mac
->associnfo
.bssid
, net
->bssid
, ETH_ALEN
);
155 /* if 'ANY' network requested, take any that doesn't have privacy enabled */
156 if (mac
->associnfo
.req_essid
.len
== 0
157 && !(net
->capability
& WLAN_CAPABILITY_PRIVACY
))
159 if (net
->ssid_len
!= mac
->associnfo
.req_essid
.len
)
161 if (!memcmp(net
->ssid
, mac
->associnfo
.req_essid
.data
, mac
->associnfo
.req_essid
.len
))
167 ieee80211softmac_assoc_notify_scan(struct net_device
*dev
, int event_type
, void *context
)
169 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
170 ieee80211softmac_assoc_work(&mac
->associnfo
.work
.work
);
174 ieee80211softmac_assoc_notify_auth(struct net_device
*dev
, int event_type
, void *context
)
176 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
178 switch (event_type
) {
179 case IEEE80211SOFTMAC_EVENT_AUTHENTICATED
:
180 ieee80211softmac_assoc_work(&mac
->associnfo
.work
.work
);
182 case IEEE80211SOFTMAC_EVENT_AUTH_FAILED
:
183 case IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT
:
184 ieee80211softmac_disassoc(mac
);
189 /* This function is called to handle userspace requests (asynchronously) */
191 ieee80211softmac_assoc_work(struct work_struct
*work
)
193 struct ieee80211softmac_device
*mac
=
194 container_of(work
, struct ieee80211softmac_device
,
195 associnfo
.work
.work
);
196 struct ieee80211softmac_network
*found
= NULL
;
197 struct ieee80211_network
*net
= NULL
, *best
= NULL
;
201 mutex_lock(&mac
->associnfo
.mutex
);
203 if (!mac
->associnfo
.associating
)
206 /* ieee80211_disassoc might clear this */
207 bssvalid
= mac
->associnfo
.bssvalid
;
210 if (mac
->associnfo
.associated
)
211 ieee80211softmac_send_disassoc_req(mac
, WLAN_REASON_DISASSOC_STA_HAS_LEFT
);
213 /* try to find the requested network in our list, if we found one already */
214 if (bssvalid
|| mac
->associnfo
.bssfixed
)
215 found
= ieee80211softmac_get_network_by_bssid(mac
, mac
->associnfo
.bssid
);
217 /* Search the ieee80211 networks for this network if we didn't find it by bssid,
218 * but only if we've scanned at least once (to get a better list of networks to
219 * select from). If we have not scanned before, the !found logic below will be
220 * invoked and will scan. */
221 if (!found
&& (mac
->associnfo
.scan_retry
< IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT
))
223 s8 rssi
= -128; /* if I don't initialise, gcc emits an invalid warning
224 because it cannot follow the best pointer logic. */
225 spin_lock_irqsave(&mac
->ieee
->lock
, flags
);
226 list_for_each_entry(net
, &mac
->ieee
->network_list
, list
) {
227 /* we're supposed to find the network with
228 * the best signal here, as we're asked to join
229 * any network with a specific ESSID, and many
230 * different ones could have that.
232 * I'll for now just go with the reported rssi.
234 * We also should take into account the rateset
235 * here to find the best BSSID to try.
237 if (network_matches_request(mac
, net
)) {
240 rssi
= best
->stats
.rssi
;
243 /* we already had a matching network, so
244 * compare their properties to get the
245 * better of the two ... (see above)
247 if (rssi
< net
->stats
.rssi
) {
249 rssi
= best
->stats
.rssi
;
253 /* if we unlock here, we might get interrupted and the `best'
254 * pointer could go stale */
256 found
= ieee80211softmac_create_network(mac
, best
);
257 /* if found is still NULL, then we got -ENOMEM somewhere */
259 ieee80211softmac_add_network(mac
, found
);
261 spin_unlock_irqrestore(&mac
->ieee
->lock
, flags
);
265 if (mac
->associnfo
.scan_retry
> 0) {
266 mac
->associnfo
.scan_retry
--;
268 /* We know of no such network. Let's scan.
269 * NB: this also happens if we had no memory to copy the network info...
270 * Maybe we can hope to have more memory after scanning finishes ;)
272 dprintk(KERN_INFO PFX
"Associate: Scanning for networks first.\n");
273 ieee80211softmac_notify(mac
->dev
, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED
, ieee80211softmac_assoc_notify_scan
, NULL
);
274 if (ieee80211softmac_start_scan(mac
)) {
275 dprintk(KERN_INFO PFX
"Associate: failed to initiate scan. Is device up?\n");
276 mac
->associnfo
.associating
= 0;
277 mac
->associnfo
.associated
= 0;
281 mac
->associnfo
.associating
= 0;
282 mac
->associnfo
.associated
= 0;
284 dprintk(KERN_INFO PFX
"Unable to find matching network after scan!\n");
285 /* reset the retry counter for the next user request since we
286 * break out and don't reschedule ourselves after this point. */
287 mac
->associnfo
.scan_retry
= IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT
;
288 ieee80211softmac_call_events(mac
, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND
, NULL
);
293 /* reset the retry counter for the next user request since we
294 * now found a net and will try to associate to it, but not
295 * schedule this function again. */
296 mac
->associnfo
.scan_retry
= IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT
;
297 mac
->associnfo
.bssvalid
= 1;
298 memcpy(mac
->associnfo
.bssid
, found
->bssid
, ETH_ALEN
);
299 /* copy the ESSID for displaying it */
300 mac
->associnfo
.associate_essid
.len
= found
->essid
.len
;
301 memcpy(mac
->associnfo
.associate_essid
.data
, found
->essid
.data
, IW_ESSID_MAX_SIZE
+ 1);
303 /* we found a network! authenticate (if necessary) and associate to it. */
304 if (found
->authenticating
) {
305 dprintk(KERN_INFO PFX
"Already requested authentication, waiting...\n");
306 if(!mac
->associnfo
.assoc_wait
) {
307 mac
->associnfo
.assoc_wait
= 1;
308 ieee80211softmac_notify_internal(mac
, IEEE80211SOFTMAC_EVENT_ANY
, found
, ieee80211softmac_assoc_notify_auth
, NULL
, GFP_KERNEL
);
312 if (!found
->authenticated
&& !found
->authenticating
) {
313 /* This relies on the fact that _auth_req only queues the work,
314 * otherwise adding the notification would be racy. */
315 if (!ieee80211softmac_auth_req(mac
, found
)) {
316 if(!mac
->associnfo
.assoc_wait
) {
317 dprintk(KERN_INFO PFX
"Cannot associate without being authenticated, requested authentication\n");
318 mac
->associnfo
.assoc_wait
= 1;
319 ieee80211softmac_notify_internal(mac
, IEEE80211SOFTMAC_EVENT_ANY
, found
, ieee80211softmac_assoc_notify_auth
, NULL
, GFP_KERNEL
);
322 printkl(KERN_WARNING PFX
"Not authenticated, but requesting authentication failed. Giving up to associate\n");
323 mac
->associnfo
.assoc_wait
= 0;
324 ieee80211softmac_call_events(mac
, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED
, found
);
328 /* finally! now we can start associating */
329 mac
->associnfo
.assoc_wait
= 0;
330 ieee80211softmac_assoc(mac
, found
);
333 mutex_unlock(&mac
->associnfo
.mutex
);
336 /* call this to do whatever is necessary when we're associated */
338 ieee80211softmac_associated(struct ieee80211softmac_device
*mac
,
339 struct ieee80211_assoc_response
* resp
,
340 struct ieee80211softmac_network
*net
)
342 u16 cap
= le16_to_cpu(resp
->capability
);
343 u8 erp_value
= net
->erp_value
;
345 mac
->associnfo
.associating
= 0;
346 mac
->bssinfo
.supported_rates
= net
->supported_rates
;
347 ieee80211softmac_recalc_txrates(mac
);
349 mac
->associnfo
.associated
= 1;
351 mac
->associnfo
.short_preamble_available
=
352 (cap
& WLAN_CAPABILITY_SHORT_PREAMBLE
) != 0;
353 ieee80211softmac_process_erp(mac
, erp_value
);
355 if (mac
->set_bssid_filter
)
356 mac
->set_bssid_filter(mac
->dev
, net
->bssid
);
357 memcpy(mac
->ieee
->bssid
, net
->bssid
, ETH_ALEN
);
358 netif_carrier_on(mac
->dev
);
360 mac
->association_id
= le16_to_cpup(&resp
->aid
);
363 /* received frame handling functions */
365 ieee80211softmac_handle_assoc_response(struct net_device
* dev
,
366 struct ieee80211_assoc_response
* resp
,
367 struct ieee80211_network
* _ieee80211_network
)
369 /* NOTE: the network parameter has to be mostly ignored by
370 * this code because it is the ieee80211's pointer
371 * to the struct, not ours (we made a copy)
373 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
374 u16 status
= le16_to_cpup(&resp
->status
);
375 struct ieee80211softmac_network
*network
= NULL
;
378 if (unlikely(!mac
->running
))
381 spin_lock_irqsave(&mac
->lock
, flags
);
383 if (!mac
->associnfo
.associating
) {
384 /* we race against the timeout function, so make sure
385 * only one of us can do work */
386 spin_unlock_irqrestore(&mac
->lock
, flags
);
389 network
= ieee80211softmac_get_network_by_bssid_locked(mac
, resp
->header
.addr3
);
391 /* someone sending us things without us knowing him? Ignore. */
393 dprintk(KERN_INFO PFX
"Received unrequested assocation response from " MAC_FMT
"\n", MAC_ARG(resp
->header
.addr3
));
394 spin_unlock_irqrestore(&mac
->lock
, flags
);
398 /* now that we know it was for us, we can cancel the timeout */
399 cancel_delayed_work(&mac
->associnfo
.timeout
);
401 /* if the association response included an ERP IE, update our saved
403 if (_ieee80211_network
->flags
& NETWORK_HAS_ERP_VALUE
)
404 network
->erp_value
= _ieee80211_network
->erp_value
;
408 dprintk(KERN_INFO PFX
"associated!\n");
409 ieee80211softmac_associated(mac
, resp
, network
);
410 ieee80211softmac_call_events_locked(mac
, IEEE80211SOFTMAC_EVENT_ASSOCIATED
, network
);
412 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH
:
413 if (!network
->auth_desynced_once
) {
414 /* there seem to be a few rare cases where our view of
415 * the world is obscured, or buggy APs that don't DEAUTH
416 * us properly. So we handle that, but allow it only once.
418 printkl(KERN_INFO PFX
"We were not authenticated during association, retrying...\n");
419 network
->authenticated
= 0;
420 /* we don't want to do this more than once ... */
421 network
->auth_desynced_once
= 1;
422 schedule_delayed_work(&mac
->associnfo
.work
, 0);
426 dprintk(KERN_INFO PFX
"associating failed (reason: 0x%x)!\n", status
);
427 mac
->associnfo
.associating
= 0;
428 mac
->associnfo
.bssvalid
= 0;
429 mac
->associnfo
.associated
= 0;
430 ieee80211softmac_call_events_locked(mac
, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED
, network
);
433 spin_unlock_irqrestore(&mac
->lock
, flags
);
438 ieee80211softmac_try_reassoc(struct ieee80211softmac_device
*mac
)
442 spin_lock_irqsave(&mac
->lock
, flags
);
443 mac
->associnfo
.associating
= 1;
444 schedule_delayed_work(&mac
->associnfo
.work
, 0);
445 spin_unlock_irqrestore(&mac
->lock
, flags
);
449 ieee80211softmac_handle_disassoc(struct net_device
* dev
,
450 struct ieee80211_disassoc
*disassoc
)
452 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
454 if (unlikely(!mac
->running
))
457 if (memcmp(disassoc
->header
.addr2
, mac
->associnfo
.bssid
, ETH_ALEN
))
460 if (memcmp(disassoc
->header
.addr1
, mac
->dev
->dev_addr
, ETH_ALEN
))
463 dprintk(KERN_INFO PFX
"got disassoc frame\n");
464 ieee80211softmac_disassoc(mac
);
466 ieee80211softmac_try_reassoc(mac
);
472 ieee80211softmac_handle_reassoc_req(struct net_device
* dev
,
473 struct ieee80211_reassoc_request
* resp
)
475 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
476 struct ieee80211softmac_network
*network
;
478 if (unlikely(!mac
->running
))
481 network
= ieee80211softmac_get_network_by_bssid(mac
, resp
->header
.addr3
);
483 dprintkl(KERN_INFO PFX
"reassoc request from unknown network\n");
486 schedule_delayed_work(&mac
->associnfo
.work
, 0);