ieee80211softmac_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * This file contains the softmac's authentication 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. static void ieee80211softmac_auth_queue(void *data);
  28. /* Queues an auth request to the desired AP */
  29. int
  30. ieee80211softmac_auth_req(struct ieee80211softmac_device *mac,
  31. struct ieee80211softmac_network *net)
  32. {
  33. struct ieee80211softmac_auth_queue_item *auth;
  34. unsigned long flags;
  35. if (net->authenticating || net->authenticated)
  36. return 0;
  37. net->authenticating = 1;
  38. /* Add the network if it's not already added */
  39. ieee80211softmac_add_network(mac, net);
  40. dprintk(KERN_NOTICE PFX "Queueing Authentication Request to "MAC_FMT"\n", MAC_ARG(net->bssid));
  41. /* Queue the auth request */
  42. auth = (struct ieee80211softmac_auth_queue_item *)
  43. kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
  44. if(auth == NULL)
  45. return -ENOMEM;
  46. auth->net = net;
  47. auth->mac = mac;
  48. auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
  49. auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
  50. INIT_WORK(&auth->work, &ieee80211softmac_auth_queue, (void *)auth);
  51. /* Lock (for list) */
  52. spin_lock_irqsave(&mac->lock, flags);
  53. /* add to list */
  54. list_add_tail(&auth->list, &mac->auth_queue);
  55. schedule_work(&auth->work);
  56. spin_unlock_irqrestore(&mac->lock, flags);
  57. return 0;
  58. }
  59. /* Sends an auth request to the desired AP and handles timeouts */
  60. static void
  61. ieee80211softmac_auth_queue(void *data)
  62. {
  63. struct ieee80211softmac_device *mac;
  64. struct ieee80211softmac_auth_queue_item *auth;
  65. struct ieee80211softmac_network *net;
  66. unsigned long flags;
  67. auth = (struct ieee80211softmac_auth_queue_item *)data;
  68. net = auth->net;
  69. mac = auth->mac;
  70. if(auth->retry > 0) {
  71. /* Switch to correct channel for this network */
  72. mac->set_channel(mac->dev, net->channel);
  73. /* Lock and set flags */
  74. spin_lock_irqsave(&mac->lock, flags);
  75. if (unlikely(!mac->running)) {
  76. /* Prevent reschedule on workqueue flush */
  77. spin_unlock_irqrestore(&mac->lock, flags);
  78. return;
  79. }
  80. net->authenticated = 0;
  81. /* add a timeout call so we eventually give up waiting for an auth reply */
  82. schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
  83. auth->retry--;
  84. spin_unlock_irqrestore(&mac->lock, flags);
  85. if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
  86. dprintk(KERN_NOTICE PFX "Sending Authentication Request to "MAC_FMT" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net->bssid));
  87. else
  88. dprintk(KERN_NOTICE PFX "Sent Authentication Request to "MAC_FMT".\n", MAC_ARG(net->bssid));
  89. return;
  90. }
  91. printkl(KERN_WARNING PFX "Authentication timed out with "MAC_FMT"\n", MAC_ARG(net->bssid));
  92. /* Remove this item from the queue */
  93. spin_lock_irqsave(&mac->lock, flags);
  94. net->authenticating = 0;
  95. ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
  96. cancel_delayed_work(&auth->work); /* just to make sure... */
  97. list_del(&auth->list);
  98. spin_unlock_irqrestore(&mac->lock, flags);
  99. /* Free it */
  100. kfree(auth);
  101. }
  102. /* Sends a response to an auth challenge (for shared key auth). */
  103. static void
  104. ieee80211softmac_auth_challenge_response(void *_aq)
  105. {
  106. struct ieee80211softmac_auth_queue_item *aq = _aq;
  107. /* Send our response */
  108. ieee80211softmac_send_mgt_frame(aq->mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
  109. }
  110. /* Handle the auth response from the AP
  111. * This should be registered with ieee80211 as handle_auth
  112. */
  113. int
  114. ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
  115. {
  116. struct list_head *list_ptr;
  117. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  118. struct ieee80211softmac_auth_queue_item *aq = NULL;
  119. struct ieee80211softmac_network *net = NULL;
  120. unsigned long flags;
  121. u8 * data;
  122. if (unlikely(!mac->running))
  123. return -ENODEV;
  124. /* Find correct auth queue item */
  125. spin_lock_irqsave(&mac->lock, flags);
  126. list_for_each(list_ptr, &mac->auth_queue) {
  127. aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
  128. net = aq->net;
  129. if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
  130. break;
  131. else
  132. aq = NULL;
  133. }
  134. spin_unlock_irqrestore(&mac->lock, flags);
  135. /* Make sure that we've got an auth queue item for this request */
  136. if(aq == NULL)
  137. {
  138. printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but no queue item exists.\n", MAC_ARG(auth->header.addr2));
  139. /* Error #? */
  140. return -1;
  141. }
  142. /* Check for out of order authentication */
  143. if(!net->authenticating)
  144. {
  145. printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but did not request authentication.\n",MAC_ARG(auth->header.addr2));
  146. return -1;
  147. }
  148. /* Parse the auth packet */
  149. switch(auth->algorithm) {
  150. case WLAN_AUTH_OPEN:
  151. /* Check the status code of the response */
  152. switch(auth->status) {
  153. case WLAN_STATUS_SUCCESS:
  154. /* Update the status to Authenticated */
  155. spin_lock_irqsave(&mac->lock, flags);
  156. net->authenticating = 0;
  157. net->authenticated = 1;
  158. spin_unlock_irqrestore(&mac->lock, flags);
  159. /* Send event */
  160. printkl(KERN_NOTICE PFX "Open Authentication completed with "MAC_FMT"\n", MAC_ARG(net->bssid));
  161. ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
  162. break;
  163. default:
  164. /* Lock and reset flags */
  165. spin_lock_irqsave(&mac->lock, flags);
  166. net->authenticated = 0;
  167. net->authenticating = 0;
  168. spin_unlock_irqrestore(&mac->lock, flags);
  169. printkl(KERN_NOTICE PFX "Open Authentication with "MAC_FMT" failed, error code: %i\n",
  170. MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
  171. /* Count the error? */
  172. break;
  173. }
  174. goto free_aq;
  175. break;
  176. case WLAN_AUTH_SHARED_KEY:
  177. /* Figure out where we are in the process */
  178. switch(auth->transaction) {
  179. case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
  180. /* Check to make sure we have a challenge IE */
  181. data = (u8 *)auth->info_element;
  182. if (*data++ != MFIE_TYPE_CHALLENGE) {
  183. printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
  184. break;
  185. }
  186. /* Save the challenge */
  187. spin_lock_irqsave(&mac->lock, flags);
  188. net->challenge_len = *data++;
  189. if (net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
  190. net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
  191. if (net->challenge != NULL)
  192. kfree(net->challenge);
  193. net->challenge = kmalloc(net->challenge_len, GFP_ATOMIC);
  194. memcpy(net->challenge, data, net->challenge_len);
  195. aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
  196. /* We reuse the work struct from the auth request here.
  197. * It is safe to do so as each one is per-request, and
  198. * at this point (dealing with authentication response)
  199. * we have obviously already sent the initial auth
  200. * request. */
  201. cancel_delayed_work(&aq->work);
  202. INIT_WORK(&aq->work, &ieee80211softmac_auth_challenge_response, (void *)aq);
  203. schedule_work(&aq->work);
  204. spin_unlock_irqrestore(&mac->lock, flags);
  205. return 0;
  206. case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
  207. kfree(net->challenge);
  208. net->challenge = NULL;
  209. net->challenge_len = 0;
  210. /* Check the status code of the response */
  211. switch(auth->status) {
  212. case WLAN_STATUS_SUCCESS:
  213. /* Update the status to Authenticated */
  214. spin_lock_irqsave(&mac->lock, flags);
  215. net->authenticating = 0;
  216. net->authenticated = 1;
  217. spin_unlock_irqrestore(&mac->lock, flags);
  218. printkl(KERN_NOTICE PFX "Shared Key Authentication completed with "MAC_FMT"\n",
  219. MAC_ARG(net->bssid));
  220. ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
  221. break;
  222. default:
  223. printkl(KERN_NOTICE PFX "Shared Key Authentication with "MAC_FMT" failed, error code: %i\n",
  224. MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
  225. /* Lock and reset flags */
  226. spin_lock_irqsave(&mac->lock, flags);
  227. net->authenticating = 0;
  228. net->authenticated = 0;
  229. spin_unlock_irqrestore(&mac->lock, flags);
  230. /* Count the error? */
  231. break;
  232. }
  233. goto free_aq;
  234. break;
  235. default:
  236. printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
  237. break;
  238. }
  239. goto free_aq;
  240. break;
  241. default:
  242. /* ERROR */
  243. goto free_aq;
  244. break;
  245. }
  246. return 0;
  247. free_aq:
  248. /* Cancel the timeout */
  249. spin_lock_irqsave(&mac->lock, flags);
  250. cancel_delayed_work(&aq->work);
  251. /* Remove this item from the queue */
  252. list_del(&aq->list);
  253. spin_unlock_irqrestore(&mac->lock, flags);
  254. /* Free it */
  255. kfree(aq);
  256. return 0;
  257. }
  258. /*
  259. * Handle deauthorization
  260. */
  261. static void
  262. ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
  263. struct ieee80211softmac_network *net)
  264. {
  265. struct ieee80211softmac_auth_queue_item *aq = NULL;
  266. struct list_head *list_ptr;
  267. unsigned long flags;
  268. /* deauthentication implies disassociation */
  269. ieee80211softmac_disassoc(mac);
  270. /* Lock and reset status flags */
  271. spin_lock_irqsave(&mac->lock, flags);
  272. net->authenticating = 0;
  273. net->authenticated = 0;
  274. /* Find correct auth queue item, if it exists */
  275. list_for_each(list_ptr, &mac->auth_queue) {
  276. aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
  277. if (!memcmp(net->bssid, aq->net->bssid, ETH_ALEN))
  278. break;
  279. else
  280. aq = NULL;
  281. }
  282. /* Cancel pending work */
  283. if(aq != NULL)
  284. /* Not entirely safe? What about running work? */
  285. cancel_delayed_work(&aq->work);
  286. /* Free our network ref */
  287. ieee80211softmac_del_network_locked(mac, net);
  288. if(net->challenge != NULL)
  289. kfree(net->challenge);
  290. kfree(net);
  291. /* can't transmit data right now... */
  292. netif_carrier_off(mac->dev);
  293. spin_unlock_irqrestore(&mac->lock, flags);
  294. }
  295. /*
  296. * Sends a deauth request to the desired AP
  297. */
  298. int
  299. ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
  300. struct ieee80211softmac_network *net, int reason)
  301. {
  302. int ret;
  303. /* Make sure the network is authenticated */
  304. if (!net->authenticated)
  305. {
  306. printkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
  307. /* Error okay? */
  308. return -EPERM;
  309. }
  310. /* Send the de-auth packet */
  311. if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
  312. return ret;
  313. ieee80211softmac_deauth_from_net(mac, net);
  314. return 0;
  315. }
  316. /*
  317. * This should be registered with ieee80211 as handle_deauth
  318. */
  319. int
  320. ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
  321. {
  322. struct ieee80211softmac_network *net = NULL;
  323. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  324. if (unlikely(!mac->running))
  325. return -ENODEV;
  326. if (!deauth) {
  327. dprintk("deauth without deauth packet. eek!\n");
  328. return 0;
  329. }
  330. net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
  331. if (net == NULL) {
  332. printkl(KERN_DEBUG PFX "Received deauthentication packet from "MAC_FMT", but that network is unknown.\n",
  333. MAC_ARG(deauth->header.addr2));
  334. return 0;
  335. }
  336. /* Make sure the network is authenticated */
  337. if(!net->authenticated)
  338. {
  339. printkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
  340. /* Error okay? */
  341. return -EPERM;
  342. }
  343. ieee80211softmac_deauth_from_net(mac, net);
  344. /* let's try to re-associate */
  345. schedule_work(&mac->associnfo.work);
  346. return 0;
  347. }