ieee80211softmac_assoc.c 13 KB

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