ieee80211softmac_assoc.c 16 KB

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