ieee80211softmac_assoc.c 13 KB

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