ieee80211softmac_auth.c 11 KB

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