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