ieee80211softmac_assoc.c 14 KB

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