ieee80211softmac_auth.c 12 KB

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