2 * This file contains the softmac's authentication logic.
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>
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.
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
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
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
27 #include "ieee80211softmac_priv.h"
29 static void ieee80211softmac_auth_queue(struct work_struct *work);
31 /* Queues an auth request to the desired AP */
33 ieee80211softmac_auth_req(struct ieee80211softmac_device *mac,
34 struct ieee80211softmac_network *net)
36 struct ieee80211softmac_auth_queue_item *auth;
38 DECLARE_MAC_BUF(mac2);
40 if (net->authenticating || net->authenticated)
42 net->authenticating = 1;
44 /* Add the network if it's not already added */
45 ieee80211softmac_add_network(mac, net);
47 dprintk(KERN_NOTICE PFX "Queueing Authentication Request to %s\n", print_mac(mac2, net->bssid));
48 /* Queue the auth request */
49 auth = (struct ieee80211softmac_auth_queue_item *)
50 kmalloc(sizeof(struct ieee80211softmac_auth_queue_item), GFP_KERNEL);
56 auth->retry = IEEE80211SOFTMAC_AUTH_RETRY_LIMIT;
57 auth->state = IEEE80211SOFTMAC_AUTH_OPEN_REQUEST;
58 INIT_DELAYED_WORK(&auth->work, ieee80211softmac_auth_queue);
61 spin_lock_irqsave(&mac->lock, flags);
64 list_add_tail(&auth->list, &mac->auth_queue);
65 schedule_delayed_work(&auth->work, 0);
66 spin_unlock_irqrestore(&mac->lock, flags);
72 /* Sends an auth request to the desired AP and handles timeouts */
74 ieee80211softmac_auth_queue(struct work_struct *work)
76 struct ieee80211softmac_device *mac;
77 struct ieee80211softmac_auth_queue_item *auth;
78 struct ieee80211softmac_network *net;
80 DECLARE_MAC_BUF(mac2);
82 auth = container_of(work, struct ieee80211softmac_auth_queue_item,
88 /* Switch to correct channel for this network */
89 mac->set_channel(mac->dev, net->channel);
91 /* Lock and set flags */
92 spin_lock_irqsave(&mac->lock, flags);
93 if (unlikely(!mac->running)) {
94 /* Prevent reschedule on workqueue flush */
95 spin_unlock_irqrestore(&mac->lock, flags);
98 net->authenticated = 0;
99 /* add a timeout call so we eventually give up waiting for an auth reply */
100 schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
102 spin_unlock_irqrestore(&mac->lock, flags);
103 if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
104 dprintk(KERN_NOTICE PFX "Sending Authentication Request to %s failed (this shouldn't happen, wait for the timeout).\n",
105 print_mac(mac2, net->bssid));
107 dprintk(KERN_NOTICE PFX "Sent Authentication Request to %s.\n", print_mac(mac2, net->bssid));
111 printkl(KERN_WARNING PFX "Authentication timed out with %s\n", print_mac(mac2, net->bssid));
112 /* Remove this item from the queue */
113 spin_lock_irqsave(&mac->lock, flags);
114 net->authenticating = 0;
115 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
116 cancel_delayed_work(&auth->work); /* just to make sure... */
117 list_del(&auth->list);
118 spin_unlock_irqrestore(&mac->lock, flags);
123 /* Sends a response to an auth challenge (for shared key auth). */
125 ieee80211softmac_auth_challenge_response(struct work_struct *work)
127 struct ieee80211softmac_auth_queue_item *aq =
128 container_of(work, struct ieee80211softmac_auth_queue_item,
131 /* Send our response */
132 ieee80211softmac_send_mgt_frame(aq->mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
135 /* Handle the auth response from the AP
136 * This should be registered with ieee80211 as handle_auth
139 ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
142 struct list_head *list_ptr;
143 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
144 struct ieee80211softmac_auth_queue_item *aq = NULL;
145 struct ieee80211softmac_network *net = NULL;
148 DECLARE_MAC_BUF(mac2);
150 if (unlikely(!mac->running))
153 /* Find correct auth queue item */
154 spin_lock_irqsave(&mac->lock, flags);
155 list_for_each(list_ptr, &mac->auth_queue) {
156 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
158 if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
163 spin_unlock_irqrestore(&mac->lock, flags);
165 /* Make sure that we've got an auth queue item for this request */
168 dprintkl(KERN_DEBUG PFX "Authentication response received from %s but no queue item exists.\n", print_mac(mac2, auth->header.addr2));
173 /* Check for out of order authentication */
174 if(!net->authenticating)
176 dprintkl(KERN_DEBUG PFX "Authentication response received from %s but did not request authentication.\n",print_mac(mac2, auth->header.addr2));
180 /* Parse the auth packet */
181 switch(auth->algorithm) {
183 /* Check the status code of the response */
185 switch(auth->status) {
186 case WLAN_STATUS_SUCCESS:
187 /* Update the status to Authenticated */
188 spin_lock_irqsave(&mac->lock, flags);
189 net->authenticating = 0;
190 net->authenticated = 1;
191 spin_unlock_irqrestore(&mac->lock, flags);
194 printkl(KERN_NOTICE PFX "Open Authentication completed with %s\n", print_mac(mac2, net->bssid));
195 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
198 /* Lock and reset flags */
199 spin_lock_irqsave(&mac->lock, flags);
200 net->authenticated = 0;
201 net->authenticating = 0;
202 spin_unlock_irqrestore(&mac->lock, flags);
204 printkl(KERN_NOTICE PFX "Open Authentication with %s failed, error code: %i\n",
205 print_mac(mac2, net->bssid), le16_to_cpup(&auth->status));
206 /* Count the error? */
211 case WLAN_AUTH_SHARED_KEY:
212 /* Figure out where we are in the process */
213 switch(auth->transaction) {
214 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
215 /* Check to make sure we have a challenge IE */
216 data = (u8 *)auth->info_element;
217 if (*data++ != MFIE_TYPE_CHALLENGE) {
218 printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
221 /* Save the challenge */
222 spin_lock_irqsave(&mac->lock, flags);
223 net->challenge_len = *data++;
224 if (net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
225 net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
226 kfree(net->challenge);
227 net->challenge = kmemdup(data, net->challenge_len,
229 if (net->challenge == NULL) {
230 printkl(KERN_NOTICE PFX "Shared Key "
231 "Authentication failed due to "
232 "memory shortage.\n");
233 spin_unlock_irqrestore(&mac->lock, flags);
236 aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
238 /* We reuse the work struct from the auth request here.
239 * It is safe to do so as each one is per-request, and
240 * at this point (dealing with authentication response)
241 * we have obviously already sent the initial auth
243 cancel_delayed_work(&aq->work);
244 INIT_DELAYED_WORK(&aq->work, &ieee80211softmac_auth_challenge_response);
245 schedule_delayed_work(&aq->work, 0);
246 spin_unlock_irqrestore(&mac->lock, flags);
248 case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
249 kfree(net->challenge);
250 net->challenge = NULL;
251 net->challenge_len = 0;
252 /* Check the status code of the response */
253 switch(auth->status) {
254 case WLAN_STATUS_SUCCESS:
255 /* Update the status to Authenticated */
256 spin_lock_irqsave(&mac->lock, flags);
257 net->authenticating = 0;
258 net->authenticated = 1;
259 spin_unlock_irqrestore(&mac->lock, flags);
260 printkl(KERN_NOTICE PFX "Shared Key Authentication completed with %s\n",
261 print_mac(mac2, net->bssid));
262 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
265 printkl(KERN_NOTICE PFX "Shared Key Authentication with %s failed, error code: %i\n",
266 print_mac(mac2, net->bssid), le16_to_cpup(&auth->status));
267 /* Lock and reset flags */
268 spin_lock_irqsave(&mac->lock, flags);
269 net->authenticating = 0;
270 net->authenticated = 0;
271 spin_unlock_irqrestore(&mac->lock, flags);
272 /* Count the error? */
278 printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
290 /* Cancel the timeout */
291 spin_lock_irqsave(&mac->lock, flags);
292 cancel_delayed_work(&aq->work);
293 /* Remove this item from the queue */
295 spin_unlock_irqrestore(&mac->lock, flags);
303 * Handle deauthorization
306 ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
307 struct ieee80211softmac_network *net)
309 struct ieee80211softmac_auth_queue_item *aq = NULL;
310 struct list_head *list_ptr;
313 /* deauthentication implies disassociation */
314 ieee80211softmac_disassoc(mac);
316 /* Lock and reset status flags */
317 spin_lock_irqsave(&mac->lock, flags);
318 net->authenticating = 0;
319 net->authenticated = 0;
321 /* Find correct auth queue item, if it exists */
322 list_for_each(list_ptr, &mac->auth_queue) {
323 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
324 if (!memcmp(net->bssid, aq->net->bssid, ETH_ALEN))
330 /* Cancel pending work */
332 /* Not entirely safe? What about running work? */
333 cancel_delayed_work(&aq->work);
335 /* Free our network ref */
336 ieee80211softmac_del_network_locked(mac, net);
337 if(net->challenge != NULL)
338 kfree(net->challenge);
341 /* can't transmit data right now... */
342 netif_carrier_off(mac->dev);
343 spin_unlock_irqrestore(&mac->lock, flags);
345 ieee80211softmac_try_reassoc(mac);
349 * Sends a deauth request to the desired AP
352 ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
353 struct ieee80211softmac_network *net, int reason)
357 /* Make sure the network is authenticated */
358 if (!net->authenticated)
360 dprintkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
365 /* Send the de-auth packet */
366 if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
369 ieee80211softmac_deauth_from_net(mac, net);
374 * This should be registered with ieee80211 as handle_deauth
377 ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
380 struct ieee80211softmac_network *net = NULL;
381 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
382 DECLARE_MAC_BUF(mac2);
384 if (unlikely(!mac->running))
388 dprintk("deauth without deauth packet. eek!\n");
392 net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
395 dprintkl(KERN_DEBUG PFX "Received deauthentication packet from %s, but that network is unknown.\n",
396 print_mac(mac2, deauth->header.addr2));
400 /* Make sure the network is authenticated */
401 if(!net->authenticated)
403 dprintkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
408 ieee80211softmac_deauth_from_net(mac, net);
410 /* let's try to re-associate */
411 schedule_delayed_work(&mac->associnfo.work, 0);