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
;
39 if (net
->authenticating
|| net
->authenticated
)
41 net
->authenticating
= 1;
43 /* Add the network if it's not already added */
44 ieee80211softmac_add_network(mac
, net
);
46 dprintk(KERN_NOTICE PFX
"Queueing Authentication Request to "MAC_FMT
"\n", MAC_ARG(net
->bssid
));
47 /* Queue the auth request */
48 auth
= (struct ieee80211softmac_auth_queue_item
*)
49 kmalloc(sizeof(struct ieee80211softmac_auth_queue_item
), GFP_KERNEL
);
55 auth
->retry
= IEEE80211SOFTMAC_AUTH_RETRY_LIMIT
;
56 auth
->state
= IEEE80211SOFTMAC_AUTH_OPEN_REQUEST
;
57 INIT_WORK(&auth
->work
, &ieee80211softmac_auth_queue
, (void *)auth
);
60 spin_lock_irqsave(&mac
->lock
, flags
);
63 list_add_tail(&auth
->list
, &mac
->auth_queue
);
64 schedule_work(&auth
->work
);
65 spin_unlock_irqrestore(&mac
->lock
, flags
);
71 /* Sends an auth request to the desired AP and handles timeouts */
73 ieee80211softmac_auth_queue(void *data
)
75 struct ieee80211softmac_device
*mac
;
76 struct ieee80211softmac_auth_queue_item
*auth
;
77 struct ieee80211softmac_network
*net
;
80 auth
= (struct ieee80211softmac_auth_queue_item
*)data
;
85 /* Switch to correct channel for this network */
86 mac
->set_channel(mac
->dev
, net
->channel
);
88 /* Lock and set flags */
89 spin_lock_irqsave(&mac
->lock
, flags
);
90 if (unlikely(!mac
->running
)) {
91 /* Prevent reschedule on workqueue flush */
92 spin_unlock_irqrestore(&mac
->lock
, flags
);
95 net
->authenticated
= 0;
96 /* add a timeout call so we eventually give up waiting for an auth reply */
97 schedule_delayed_work(&auth
->work
, IEEE80211SOFTMAC_AUTH_TIMEOUT
);
99 spin_unlock_irqrestore(&mac
->lock
, flags
);
100 if (ieee80211softmac_send_mgt_frame(mac
, auth
->net
, IEEE80211_STYPE_AUTH
, auth
->state
))
101 dprintk(KERN_NOTICE PFX
"Sending Authentication Request to "MAC_FMT
" failed (this shouldn't happen, wait for the timeout).\n", MAC_ARG(net
->bssid
));
103 dprintk(KERN_NOTICE PFX
"Sent Authentication Request to "MAC_FMT
".\n", MAC_ARG(net
->bssid
));
107 printkl(KERN_WARNING PFX
"Authentication timed out with "MAC_FMT
"\n", MAC_ARG(net
->bssid
));
108 /* Remove this item from the queue */
109 spin_lock_irqsave(&mac
->lock
, flags
);
110 net
->authenticating
= 0;
111 ieee80211softmac_call_events_locked(mac
, IEEE80211SOFTMAC_EVENT_AUTH_TIMEOUT
, net
);
112 cancel_delayed_work(&auth
->work
); /* just to make sure... */
113 list_del(&auth
->list
);
114 spin_unlock_irqrestore(&mac
->lock
, flags
);
119 /* Sends a response to an auth challenge (for shared key auth). */
121 ieee80211softmac_auth_challenge_response(void *_aq
)
123 struct ieee80211softmac_auth_queue_item
*aq
= _aq
;
125 /* Send our response */
126 ieee80211softmac_send_mgt_frame(aq
->mac
, aq
->net
, IEEE80211_STYPE_AUTH
, aq
->state
);
129 /* Handle the auth response from the AP
130 * This should be registered with ieee80211 as handle_auth
133 ieee80211softmac_auth_resp(struct net_device
*dev
, struct ieee80211_auth
*auth
)
136 struct list_head
*list_ptr
;
137 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
138 struct ieee80211softmac_auth_queue_item
*aq
= NULL
;
139 struct ieee80211softmac_network
*net
= NULL
;
143 if (unlikely(!mac
->running
))
146 /* Find correct auth queue item */
147 spin_lock_irqsave(&mac
->lock
, flags
);
148 list_for_each(list_ptr
, &mac
->auth_queue
) {
149 aq
= list_entry(list_ptr
, struct ieee80211softmac_auth_queue_item
, list
);
151 if (!memcmp(net
->bssid
, auth
->header
.addr2
, ETH_ALEN
))
156 spin_unlock_irqrestore(&mac
->lock
, flags
);
158 /* Make sure that we've got an auth queue item for this request */
161 dprintkl(KERN_DEBUG PFX
"Authentication response received from "MAC_FMT
" but no queue item exists.\n", MAC_ARG(auth
->header
.addr2
));
166 /* Check for out of order authentication */
167 if(!net
->authenticating
)
169 dprintkl(KERN_DEBUG PFX
"Authentication response received from "MAC_FMT
" but did not request authentication.\n",MAC_ARG(auth
->header
.addr2
));
173 /* Parse the auth packet */
174 switch(auth
->algorithm
) {
176 /* Check the status code of the response */
178 switch(auth
->status
) {
179 case WLAN_STATUS_SUCCESS
:
180 /* Update the status to Authenticated */
181 spin_lock_irqsave(&mac
->lock
, flags
);
182 net
->authenticating
= 0;
183 net
->authenticated
= 1;
184 spin_unlock_irqrestore(&mac
->lock
, flags
);
187 printkl(KERN_NOTICE PFX
"Open Authentication completed with "MAC_FMT
"\n", MAC_ARG(net
->bssid
));
188 ieee80211softmac_call_events(mac
, IEEE80211SOFTMAC_EVENT_AUTHENTICATED
, net
);
191 /* Lock and reset flags */
192 spin_lock_irqsave(&mac
->lock
, flags
);
193 net
->authenticated
= 0;
194 net
->authenticating
= 0;
195 spin_unlock_irqrestore(&mac
->lock
, flags
);
197 printkl(KERN_NOTICE PFX
"Open Authentication with "MAC_FMT
" failed, error code: %i\n",
198 MAC_ARG(net
->bssid
), le16_to_cpup(&auth
->status
));
199 /* Count the error? */
204 case WLAN_AUTH_SHARED_KEY
:
205 /* Figure out where we are in the process */
206 switch(auth
->transaction
) {
207 case IEEE80211SOFTMAC_AUTH_SHARED_CHALLENGE
:
208 /* Check to make sure we have a challenge IE */
209 data
= (u8
*)auth
->info_element
;
210 if (*data
++ != MFIE_TYPE_CHALLENGE
) {
211 printkl(KERN_NOTICE PFX
"Shared Key Authentication failed due to a missing challenge.\n");
214 /* Save the challenge */
215 spin_lock_irqsave(&mac
->lock
, flags
);
216 net
->challenge_len
= *data
++;
217 if (net
->challenge_len
> WLAN_AUTH_CHALLENGE_LEN
)
218 net
->challenge_len
= WLAN_AUTH_CHALLENGE_LEN
;
219 kfree(net
->challenge
);
220 net
->challenge
= kmemdup(data
, net
->challenge_len
,
222 if (net
->challenge
== NULL
) {
223 printkl(KERN_NOTICE PFX
"Shared Key "
224 "Authentication failed due to "
225 "memory shortage.\n");
226 spin_unlock_irqrestore(&mac
->lock
, flags
);
229 aq
->state
= IEEE80211SOFTMAC_AUTH_SHARED_RESPONSE
;
231 /* We reuse the work struct from the auth request here.
232 * It is safe to do so as each one is per-request, and
233 * at this point (dealing with authentication response)
234 * we have obviously already sent the initial auth
236 cancel_delayed_work(&aq
->work
);
237 INIT_WORK(&aq
->work
, &ieee80211softmac_auth_challenge_response
, (void *)aq
);
238 schedule_work(&aq
->work
);
239 spin_unlock_irqrestore(&mac
->lock
, flags
);
241 case IEEE80211SOFTMAC_AUTH_SHARED_PASS
:
242 kfree(net
->challenge
);
243 net
->challenge
= NULL
;
244 net
->challenge_len
= 0;
245 /* Check the status code of the response */
246 switch(auth
->status
) {
247 case WLAN_STATUS_SUCCESS
:
248 /* Update the status to Authenticated */
249 spin_lock_irqsave(&mac
->lock
, flags
);
250 net
->authenticating
= 0;
251 net
->authenticated
= 1;
252 spin_unlock_irqrestore(&mac
->lock
, flags
);
253 printkl(KERN_NOTICE PFX
"Shared Key Authentication completed with "MAC_FMT
"\n",
254 MAC_ARG(net
->bssid
));
255 ieee80211softmac_call_events(mac
, IEEE80211SOFTMAC_EVENT_AUTHENTICATED
, net
);
258 printkl(KERN_NOTICE PFX
"Shared Key Authentication with "MAC_FMT
" failed, error code: %i\n",
259 MAC_ARG(net
->bssid
), le16_to_cpup(&auth
->status
));
260 /* Lock and reset flags */
261 spin_lock_irqsave(&mac
->lock
, flags
);
262 net
->authenticating
= 0;
263 net
->authenticated
= 0;
264 spin_unlock_irqrestore(&mac
->lock
, flags
);
265 /* Count the error? */
271 printkl(KERN_WARNING PFX
"Unhandled Authentication Step: %i\n", auth
->transaction
);
283 /* Cancel the timeout */
284 spin_lock_irqsave(&mac
->lock
, flags
);
285 cancel_delayed_work(&aq
->work
);
286 /* Remove this item from the queue */
288 spin_unlock_irqrestore(&mac
->lock
, flags
);
296 * Handle deauthorization
299 ieee80211softmac_deauth_from_net(struct ieee80211softmac_device
*mac
,
300 struct ieee80211softmac_network
*net
)
302 struct ieee80211softmac_auth_queue_item
*aq
= NULL
;
303 struct list_head
*list_ptr
;
306 /* deauthentication implies disassociation */
307 ieee80211softmac_disassoc(mac
);
309 /* Lock and reset status flags */
310 spin_lock_irqsave(&mac
->lock
, flags
);
311 net
->authenticating
= 0;
312 net
->authenticated
= 0;
314 /* Find correct auth queue item, if it exists */
315 list_for_each(list_ptr
, &mac
->auth_queue
) {
316 aq
= list_entry(list_ptr
, struct ieee80211softmac_auth_queue_item
, list
);
317 if (!memcmp(net
->bssid
, aq
->net
->bssid
, ETH_ALEN
))
323 /* Cancel pending work */
325 /* Not entirely safe? What about running work? */
326 cancel_delayed_work(&aq
->work
);
328 /* Free our network ref */
329 ieee80211softmac_del_network_locked(mac
, net
);
330 if(net
->challenge
!= NULL
)
331 kfree(net
->challenge
);
334 /* can't transmit data right now... */
335 netif_carrier_off(mac
->dev
);
336 spin_unlock_irqrestore(&mac
->lock
, flags
);
340 * Sends a deauth request to the desired AP
343 ieee80211softmac_deauth_req(struct ieee80211softmac_device
*mac
,
344 struct ieee80211softmac_network
*net
, int reason
)
348 /* Make sure the network is authenticated */
349 if (!net
->authenticated
)
351 dprintkl(KERN_DEBUG PFX
"Can't send deauthentication packet, network is not authenticated.\n");
356 /* Send the de-auth packet */
357 if((ret
= ieee80211softmac_send_mgt_frame(mac
, net
, IEEE80211_STYPE_DEAUTH
, reason
)))
360 ieee80211softmac_deauth_from_net(mac
, net
);
365 * This should be registered with ieee80211 as handle_deauth
368 ieee80211softmac_deauth_resp(struct net_device
*dev
, struct ieee80211_deauth
*deauth
)
371 struct ieee80211softmac_network
*net
= NULL
;
372 struct ieee80211softmac_device
*mac
= ieee80211_priv(dev
);
374 if (unlikely(!mac
->running
))
378 dprintk("deauth without deauth packet. eek!\n");
382 net
= ieee80211softmac_get_network_by_bssid(mac
, deauth
->header
.addr2
);
385 dprintkl(KERN_DEBUG PFX
"Received deauthentication packet from "MAC_FMT
", but that network is unknown.\n",
386 MAC_ARG(deauth
->header
.addr2
));
390 /* Make sure the network is authenticated */
391 if(!net
->authenticated
)
393 dprintkl(KERN_DEBUG PFX
"Can't perform deauthentication, network is not authenticated.\n");
398 ieee80211softmac_deauth_from_net(mac
, net
);
400 /* let's try to re-associate */
401 schedule_work(&mac
->associnfo
.work
);