ieee80211softmac_assoc.c 11 KB

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