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(void *data);
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;
41 if (net->authenticating)
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 "MAC_FMT"\n", MAC_ARG(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_WORK(&auth->work, &ieee80211softmac_auth_queue, (void *)auth);
61 spin_lock_irqsave(&mac->lock, flags);
64 list_add_tail(&auth->list, &mac->auth_queue);
65 schedule_work(&auth->work);
66 spin_unlock_irqrestore(&mac->lock, flags);
72 /* Sends an auth request to the desired AP and handles timeouts */
74 ieee80211softmac_auth_queue(void *data)
76 struct ieee80211softmac_device *mac;
77 struct ieee80211softmac_auth_queue_item *auth;
78 struct ieee80211softmac_network *net;
83 auth = (struct ieee80211softmac_auth_queue_item *)data;
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 net->authenticated = 0;
94 net->authenticating = 1;
95 /* add a timeout call so we eventually give up waiting for an auth reply */
96 schedule_delayed_work(&auth->work, IEEE80211SOFTMAC_AUTH_TIMEOUT);
98 spin_unlock_irqrestore(&mac->lock, flags);
99 if (ieee80211softmac_send_mgt_frame(mac, auth->net, IEEE80211_STYPE_AUTH, auth->state))
100 dprintk(KERN_NOTICE PFX "Sending Authentication Request to "MAC_FMT" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net->bssid));
102 dprintk(KERN_NOTICE PFX "Sent Authentication Request to "MAC_FMT".\n", MAC_ARG(net->bssid));
106 printkl(KERN_WARNING PFX "Authentication timed out with "MAC_FMT"\n", MAC_ARG(net->bssid));
107 /* Remove this item from the queue */
108 spin_lock_irqsave(&mac->lock, flags);
109 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT, net);
110 cancel_delayed_work(&auth->work); /* just to make sure... */
111 list_del(&auth->list);
112 spin_unlock_irqrestore(&mac->lock, flags);
117 /* Handle the auth response from the AP
118 * This should be registered with ieee80211 as handle_auth
121 ieee80211softmac_auth_resp(struct net_device *dev, struct ieee80211_auth *auth)
124 struct list_head *list_ptr;
125 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
126 struct ieee80211softmac_auth_queue_item *aq = NULL;
127 struct ieee80211softmac_network *net = NULL;
133 /* Find correct auth queue item */
134 spin_lock_irqsave(&mac->lock, flags);
135 list_for_each(list_ptr, &mac->auth_queue) {
136 aq = list_entry(list_ptr, struct ieee80211softmac_auth_queue_item, list);
138 if (!memcmp(net->bssid, auth->header.addr2, ETH_ALEN))
143 spin_unlock_irqrestore(&mac->lock, flags);
145 /* Make sure that we've got an auth queue item for this request */
148 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but no queue item exists.\n", MAC_ARG(auth->header.addr2));
153 /* Check for out of order authentication */
154 if(!net->authenticating)
156 printkl(KERN_DEBUG PFX "Authentication response received from "MAC_FMT" but did not request authentication.\n",MAC_ARG(auth->header.addr2));
160 /* Parse the auth packet */
161 switch(auth->algorithm) {
163 /* Check the status code of the response */
165 switch(auth->status) {
166 case WLAN_STATUS_SUCCESS:
167 /* Update the status to Authenticated */
168 spin_lock_irqsave(&mac->lock, flags);
169 net->authenticating = 0;
170 net->authenticated = 1;
171 spin_unlock_irqrestore(&mac->lock, flags);
174 printkl(KERN_NOTICE PFX "Open Authentication completed with "MAC_FMT"\n", MAC_ARG(net->bssid));
175 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_AUTHENTICATED, net);
178 /* Lock and reset flags */
179 spin_lock_irqsave(&mac->lock, flags);
180 net->authenticated = 0;
181 net->authenticating = 0;
182 spin_unlock_irqrestore(&mac->lock, flags);
184 printkl(KERN_NOTICE PFX "Open Authentication with "MAC_FMT" failed, error code: %i\n",
185 MAC_ARG(net->bssid), le16_to_cpup(&auth->status));
186 /* Count the error? */
191 case WLAN_AUTH_SHARED_KEY:
192 /* Figure out where we are in the process */
193 switch(auth->transaction) {
194 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE:
195 /* Check to make sure we have a challenge IE */
196 data = (u8 *)auth->info_element;
197 if(*data++ != MFIE_TYPE_CHALLENGE){
198 printkl(KERN_NOTICE PFX "Shared Key Authentication failed due to a missing challenge.\n");
201 /* Save the challenge */
202 spin_lock_irqsave(&mac->lock, flags);
203 net->challenge_len = *data++;
204 if(net->challenge_len > WLAN_AUTH_CHALLENGE_LEN)
205 net->challenge_len = WLAN_AUTH_CHALLENGE_LEN;
206 if(net->challenge != NULL)
207 kfree(net->challenge);
208 net->challenge = kmalloc(net->challenge_len, GFP_ATOMIC);
209 memcpy(net->challenge, data, net->challenge_len);
210 aq->state = IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE;
211 spin_unlock_irqrestore(&mac->lock, flags);
213 /* Switch to correct channel for this network */
214 mac->set_channel(mac->dev, net->channel);
216 /* Send our response (How to encrypt?) */
217 ieee80211softmac_send_mgt_frame(mac, aq->net, IEEE80211_STYPE_AUTH, aq->state);
219 case IEEE80211SOFTMAC_AUTH_SHARED_PASS:
220 /* Check the status code of the response */
221 switch(auth->status) {
222 case WLAN_STATUS_SUCCESS:
223 /* Update the status to Authenticated */
224 spin_lock_irqsave(&mac->lock, flags);
225 net->authenticating = 0;
226 net->authenticated = 1;
227 spin_unlock_irqrestore(&mac->lock, flags);
228 printkl(KERN_NOTICE PFX "Shared Key Authentication completed with "MAC_FMT"\n",
229 MAC_ARG(net->bssid));
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? */
245 printkl(KERN_WARNING PFX "Unhandled Authentication Step: %i\n", auth->transaction);
257 /* Cancel the timeout */
258 spin_lock_irqsave(&mac->lock, flags);
259 cancel_delayed_work(&aq->work);
260 /* Remove this item from the queue */
262 spin_unlock_irqrestore(&mac->lock, flags);
270 * Handle deauthorization
273 ieee80211softmac_deauth_from_net(struct ieee80211softmac_device *mac,
274 struct ieee80211softmac_network *net)
276 struct ieee80211softmac_auth_queue_item *aq = NULL;
277 struct list_head *list_ptr;
282 /* Lock and reset status flags */
283 spin_lock_irqsave(&mac->lock, flags);
284 net->authenticating = 0;
285 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))
296 /* Cancel pending work */
298 /* Not entirely safe? What about running work? */
299 cancel_delayed_work(&aq->work);
301 /* Free our network ref */
302 ieee80211softmac_del_network_locked(mac, net);
303 if(net->challenge != NULL)
304 kfree(net->challenge);
307 /* can't transmit data right now... */
308 netif_carrier_off(mac->dev);
309 /* let's try to re-associate */
310 schedule_work(&mac->associnfo.work);
311 spin_unlock_irqrestore(&mac->lock, flags);
315 * Sends a deauth request to the desired AP
318 ieee80211softmac_deauth_req(struct ieee80211softmac_device *mac,
319 struct ieee80211softmac_network *net, int reason)
325 /* Make sure the network is authenticated */
326 if (!net->authenticated)
328 printkl(KERN_DEBUG PFX "Can't send deauthentication packet, network is not authenticated.\n");
333 /* Send the de-auth packet */
334 if((ret = ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_DEAUTH, reason)))
337 ieee80211softmac_deauth_from_net(mac, net);
342 * This should be registered with ieee80211 as handle_deauth
345 ieee80211softmac_deauth_resp(struct net_device *dev, struct ieee80211_deauth *deauth)
348 struct ieee80211softmac_network *net = NULL;
349 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
354 dprintk("deauth without deauth packet. eek!\n");
358 net = ieee80211softmac_get_network_by_bssid(mac, deauth->header.addr2);
361 printkl(KERN_DEBUG PFX "Received deauthentication packet from "MAC_FMT", but that network is unknown.\n",
362 MAC_ARG(deauth->header.addr2));
366 /* Make sure the network is authenticated */
367 if(!net->authenticated)
369 printkl(KERN_DEBUG PFX "Can't perform deauthentication, network is not authenticated.\n");
374 ieee80211softmac_deauth_from_net(mac, net);