ieee80211softmac_auth.c 12 KB

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