]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - net/wireless/sme.c
3728d2b88b25b0c8e651f1b31681d0659187fd26
[linux-2.6.git] / net / wireless / sme.c
1 /*
2  * SME code for cfg80211's connect emulation.
3  *
4  * Copyright 2009       Johannes Berg <johannes@sipsolutions.net>
5  * Copyright (C) 2009   Intel Corporation. All rights reserved.
6  */
7
8 #include <linux/etherdevice.h>
9 #include <linux/if_arp.h>
10 #include <linux/workqueue.h>
11 #include <linux/wireless.h>
12 #include <net/iw_handler.h>
13 #include <net/cfg80211.h>
14 #include <net/rtnetlink.h>
15 #include "nl80211.h"
16
17 struct cfg80211_conn {
18         struct cfg80211_connect_params params;
19         /* these are sub-states of the _CONNECTING sme_state */
20         enum {
21                 CFG80211_CONN_IDLE,
22                 CFG80211_CONN_SCANNING,
23                 CFG80211_CONN_SCAN_AGAIN,
24                 CFG80211_CONN_AUTHENTICATE_NEXT,
25                 CFG80211_CONN_AUTHENTICATING,
26                 CFG80211_CONN_ASSOCIATE_NEXT,
27                 CFG80211_CONN_ASSOCIATING,
28         } state;
29         u8 bssid[ETH_ALEN];
30         u8 *ie;
31         size_t ie_len;
32         bool auto_auth;
33 };
34
35
36 static int cfg80211_conn_scan(struct wireless_dev *wdev)
37 {
38         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
39         struct cfg80211_scan_request *request;
40         int n_channels, err;
41
42         ASSERT_RTNL();
43         ASSERT_RDEV_LOCK(rdev);
44         ASSERT_WDEV_LOCK(wdev);
45
46         if (rdev->scan_req)
47                 return -EBUSY;
48
49         if (wdev->conn->params.channel) {
50                 n_channels = 1;
51         } else {
52                 enum ieee80211_band band;
53                 n_channels = 0;
54
55                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
56                         if (!wdev->wiphy->bands[band])
57                                 continue;
58                         n_channels += wdev->wiphy->bands[band]->n_channels;
59                 }
60         }
61         request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
62                           sizeof(request->channels[0]) * n_channels,
63                           GFP_KERNEL);
64         if (!request)
65                 return -ENOMEM;
66
67         request->channels = (void *)((char *)request + sizeof(*request));
68         if (wdev->conn->params.channel)
69                 request->channels[0] = wdev->conn->params.channel;
70         else {
71                 int i = 0, j;
72                 enum ieee80211_band band;
73
74                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75                         if (!wdev->wiphy->bands[band])
76                                 continue;
77                         for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
78                              i++, j++)
79                                 request->channels[i] =
80                                         &wdev->wiphy->bands[band]->channels[j];
81                 }
82         }
83         request->n_channels = n_channels;
84         request->ssids = (void *)(request->channels + n_channels);
85         request->n_ssids = 1;
86
87         memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
88                 wdev->conn->params.ssid_len);
89         request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
90
91         request->dev = wdev->netdev;
92         request->wiphy = &rdev->wiphy;
93
94         rdev->scan_req = request;
95
96         err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
97         if (!err) {
98                 wdev->conn->state = CFG80211_CONN_SCANNING;
99                 nl80211_send_scan_start(rdev, wdev->netdev);
100                 dev_hold(wdev->netdev);
101         } else {
102                 rdev->scan_req = NULL;
103                 kfree(request);
104         }
105         return err;
106 }
107
108 static int cfg80211_conn_do_work(struct wireless_dev *wdev)
109 {
110         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
111         struct cfg80211_connect_params *params;
112         int err;
113
114         ASSERT_WDEV_LOCK(wdev);
115
116         if (!wdev->conn)
117                 return 0;
118
119         params = &wdev->conn->params;
120
121         switch (wdev->conn->state) {
122         case CFG80211_CONN_SCAN_AGAIN:
123                 return cfg80211_conn_scan(wdev);
124         case CFG80211_CONN_AUTHENTICATE_NEXT:
125                 BUG_ON(!rdev->ops->auth);
126                 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
127                 return __cfg80211_mlme_auth(rdev, wdev->netdev,
128                                             params->channel, params->auth_type,
129                                             params->bssid,
130                                             params->ssid, params->ssid_len,
131                                             NULL, 0,
132                                             params->key, params->key_len,
133                                             params->key_idx);
134         case CFG80211_CONN_ASSOCIATE_NEXT:
135                 BUG_ON(!rdev->ops->assoc);
136                 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
137                 /*
138                  * We could, later, implement roaming here and then actually
139                  * set prev_bssid to non-NULL. But then we need to be aware
140                  * that some APs don't like that -- so we'd need to retry
141                  * the association.
142                  */
143                 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
144                                             params->channel, params->bssid,
145                                             NULL,
146                                             params->ssid, params->ssid_len,
147                                             params->ie, params->ie_len,
148                                             false, &params->crypto);
149                 if (err)
150                         __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
151                                                NULL, 0,
152                                                WLAN_REASON_DEAUTH_LEAVING);
153                 return err;
154         default:
155                 return 0;
156         }
157 }
158
159 void cfg80211_conn_work(struct work_struct *work)
160 {
161         struct cfg80211_registered_device *rdev =
162                 container_of(work, struct cfg80211_registered_device, conn_work);
163         struct wireless_dev *wdev;
164
165         rtnl_lock();
166         cfg80211_lock_rdev(rdev);
167         mutex_lock(&rdev->devlist_mtx);
168
169         list_for_each_entry(wdev, &rdev->netdev_list, list) {
170                 wdev_lock(wdev);
171                 if (!netif_running(wdev->netdev)) {
172                         wdev_unlock(wdev);
173                         continue;
174                 }
175                 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
176                         wdev_unlock(wdev);
177                         continue;
178                 }
179                 if (cfg80211_conn_do_work(wdev))
180                         __cfg80211_connect_result(
181                                         wdev->netdev,
182                                         wdev->conn->params.bssid,
183                                         NULL, 0, NULL, 0,
184                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
185                                         false, NULL);
186                 wdev_unlock(wdev);
187         }
188
189         mutex_unlock(&rdev->devlist_mtx);
190         cfg80211_unlock_rdev(rdev);
191         rtnl_unlock();
192 }
193
194 static bool cfg80211_get_conn_bss(struct wireless_dev *wdev)
195 {
196         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
197         struct cfg80211_bss *bss;
198         u16 capa = WLAN_CAPABILITY_ESS;
199
200         ASSERT_WDEV_LOCK(wdev);
201
202         if (wdev->conn->params.privacy)
203                 capa |= WLAN_CAPABILITY_PRIVACY;
204
205         bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
206                                wdev->conn->params.ssid,
207                                wdev->conn->params.ssid_len,
208                                WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
209                                capa);
210         if (!bss)
211                 return false;
212
213         memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
214         wdev->conn->params.bssid = wdev->conn->bssid;
215         wdev->conn->params.channel = bss->channel;
216         wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
217         schedule_work(&rdev->conn_work);
218
219         cfg80211_put_bss(bss);
220         return true;
221 }
222
223 static void __cfg80211_sme_scan_done(struct net_device *dev)
224 {
225         struct wireless_dev *wdev = dev->ieee80211_ptr;
226         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
227
228         ASSERT_WDEV_LOCK(wdev);
229
230         if (wdev->sme_state != CFG80211_SME_CONNECTING)
231                 return;
232
233         if (!wdev->conn)
234                 return;
235
236         if (wdev->conn->state != CFG80211_CONN_SCANNING &&
237             wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
238                 return;
239
240         if (!cfg80211_get_conn_bss(wdev)) {
241                 /* not found */
242                 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
243                         schedule_work(&rdev->conn_work);
244                 else
245                         __cfg80211_connect_result(
246                                         wdev->netdev,
247                                         wdev->conn->params.bssid,
248                                         NULL, 0, NULL, 0,
249                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
250                                         false, NULL);
251         }
252 }
253
254 void cfg80211_sme_scan_done(struct net_device *dev)
255 {
256         struct wireless_dev *wdev = dev->ieee80211_ptr;
257
258         wdev_lock(wdev);
259         __cfg80211_sme_scan_done(dev);
260         wdev_unlock(wdev);
261 }
262
263 void cfg80211_sme_rx_auth(struct net_device *dev,
264                           const u8 *buf, size_t len)
265 {
266         struct wireless_dev *wdev = dev->ieee80211_ptr;
267         struct wiphy *wiphy = wdev->wiphy;
268         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
269         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
270         u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
271
272         ASSERT_WDEV_LOCK(wdev);
273
274         /* should only RX auth frames when connecting */
275         if (wdev->sme_state != CFG80211_SME_CONNECTING)
276                 return;
277
278         if (WARN_ON(!wdev->conn))
279                 return;
280
281         if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
282             wdev->conn->auto_auth &&
283             wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
284                 /* select automatically between only open, shared, leap */
285                 switch (wdev->conn->params.auth_type) {
286                 case NL80211_AUTHTYPE_OPEN_SYSTEM:
287                         if (wdev->connect_keys)
288                                 wdev->conn->params.auth_type =
289                                         NL80211_AUTHTYPE_SHARED_KEY;
290                         else
291                                 wdev->conn->params.auth_type =
292                                         NL80211_AUTHTYPE_NETWORK_EAP;
293                         break;
294                 case NL80211_AUTHTYPE_SHARED_KEY:
295                         wdev->conn->params.auth_type =
296                                 NL80211_AUTHTYPE_NETWORK_EAP;
297                         break;
298                 default:
299                         /* huh? */
300                         wdev->conn->params.auth_type =
301                                 NL80211_AUTHTYPE_OPEN_SYSTEM;
302                         break;
303                 }
304                 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
305                 schedule_work(&rdev->conn_work);
306         } else if (status_code != WLAN_STATUS_SUCCESS) {
307                 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
308                                           status_code, false, NULL);
309         } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
310                  wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
311                 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
312                 schedule_work(&rdev->conn_work);
313         }
314 }
315
316 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
317                                const u8 *req_ie, size_t req_ie_len,
318                                const u8 *resp_ie, size_t resp_ie_len,
319                                u16 status, bool wextev,
320                                struct cfg80211_bss *bss)
321 {
322         struct wireless_dev *wdev = dev->ieee80211_ptr;
323 #ifdef CONFIG_WIRELESS_EXT
324         union iwreq_data wrqu;
325 #endif
326
327         ASSERT_WDEV_LOCK(wdev);
328
329         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
330                 return;
331
332         if (wdev->sme_state == CFG80211_SME_CONNECTED)
333                 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), dev,
334                                     bssid, req_ie, req_ie_len,
335                                     resp_ie, resp_ie_len, GFP_KERNEL);
336         else
337                 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
338                                             bssid, req_ie, req_ie_len,
339                                             resp_ie, resp_ie_len,
340                                             status, GFP_KERNEL);
341
342 #ifdef CONFIG_WIRELESS_EXT
343         if (wextev) {
344                 if (req_ie && status == WLAN_STATUS_SUCCESS) {
345                         memset(&wrqu, 0, sizeof(wrqu));
346                         wrqu.data.length = req_ie_len;
347                         wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
348                 }
349
350                 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
351                         memset(&wrqu, 0, sizeof(wrqu));
352                         wrqu.data.length = resp_ie_len;
353                         wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
354                 }
355
356                 memset(&wrqu, 0, sizeof(wrqu));
357                 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
358                 if (bssid && status == WLAN_STATUS_SUCCESS)
359                         memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
360                 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
361         }
362 #endif
363
364         if (wdev->current_bss) {
365                 cfg80211_unhold_bss(wdev->current_bss);
366                 cfg80211_put_bss(&wdev->current_bss->pub);
367                 wdev->current_bss = NULL;
368         }
369
370         if (status == WLAN_STATUS_SUCCESS &&
371             wdev->sme_state == CFG80211_SME_IDLE)
372                 goto success;
373
374         if (wdev->sme_state != CFG80211_SME_CONNECTING)
375                 return;
376
377         if (wdev->conn)
378                 wdev->conn->state = CFG80211_CONN_IDLE;
379
380         if (status != WLAN_STATUS_SUCCESS) {
381                 wdev->sme_state = CFG80211_SME_IDLE;
382                 kfree(wdev->conn);
383                 wdev->conn = NULL;
384                 kfree(wdev->connect_keys);
385                 wdev->connect_keys = NULL;
386                 return;
387         }
388
389  success:
390         if (!bss)
391                 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
392                                        wdev->ssid, wdev->ssid_len,
393                                        WLAN_CAPABILITY_ESS,
394                                        WLAN_CAPABILITY_ESS);
395
396         if (WARN_ON(!bss))
397                 return;
398
399         cfg80211_hold_bss(bss_from_pub(bss));
400         wdev->current_bss = bss_from_pub(bss);
401
402         wdev->sme_state = CFG80211_SME_CONNECTED;
403         cfg80211_upload_connect_keys(wdev);
404 }
405
406 void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
407                              const u8 *req_ie, size_t req_ie_len,
408                              const u8 *resp_ie, size_t resp_ie_len,
409                              u16 status, gfp_t gfp)
410 {
411         struct wireless_dev *wdev = dev->ieee80211_ptr;
412         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
413         struct cfg80211_event *ev;
414         unsigned long flags;
415
416         ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
417         if (!ev)
418                 return;
419
420         ev->type = EVENT_CONNECT_RESULT;
421         memcpy(ev->cr.bssid, bssid, ETH_ALEN);
422         ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
423         ev->cr.req_ie_len = req_ie_len;
424         memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
425         ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
426         ev->cr.resp_ie_len = resp_ie_len;
427         memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
428         ev->cr.status = status;
429
430         spin_lock_irqsave(&wdev->event_lock, flags);
431         list_add_tail(&ev->list, &wdev->event_list);
432         spin_unlock_irqrestore(&wdev->event_lock, flags);
433         schedule_work(&rdev->event_work);
434 }
435 EXPORT_SYMBOL(cfg80211_connect_result);
436
437 void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
438                        const u8 *req_ie, size_t req_ie_len,
439                        const u8 *resp_ie, size_t resp_ie_len)
440 {
441         struct cfg80211_bss *bss;
442 #ifdef CONFIG_WIRELESS_EXT
443         union iwreq_data wrqu;
444 #endif
445
446         ASSERT_WDEV_LOCK(wdev);
447
448         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
449                 return;
450
451         if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
452                 return;
453
454         /* internal error -- how did we get to CONNECTED w/o BSS? */
455         if (WARN_ON(!wdev->current_bss)) {
456                 return;
457         }
458
459         cfg80211_unhold_bss(wdev->current_bss);
460         cfg80211_put_bss(&wdev->current_bss->pub);
461         wdev->current_bss = NULL;
462
463         bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
464                                wdev->ssid, wdev->ssid_len,
465                                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
466
467         if (WARN_ON(!bss))
468                 return;
469
470         cfg80211_hold_bss(bss_from_pub(bss));
471         wdev->current_bss = bss_from_pub(bss);
472
473         nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
474                             req_ie, req_ie_len, resp_ie, resp_ie_len,
475                             GFP_KERNEL);
476
477 #ifdef CONFIG_WIRELESS_EXT
478         if (req_ie) {
479                 memset(&wrqu, 0, sizeof(wrqu));
480                 wrqu.data.length = req_ie_len;
481                 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
482                                     &wrqu, req_ie);
483         }
484
485         if (resp_ie) {
486                 memset(&wrqu, 0, sizeof(wrqu));
487                 wrqu.data.length = resp_ie_len;
488                 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
489                                     &wrqu, resp_ie);
490         }
491
492         memset(&wrqu, 0, sizeof(wrqu));
493         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
494         memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
495         wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
496 #endif
497 }
498
499 void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
500                      const u8 *req_ie, size_t req_ie_len,
501                      const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
502 {
503         struct wireless_dev *wdev = dev->ieee80211_ptr;
504         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
505         struct cfg80211_event *ev;
506         unsigned long flags;
507
508         ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
509         if (!ev)
510                 return;
511
512         ev->type = EVENT_ROAMED;
513         memcpy(ev->rm.bssid, bssid, ETH_ALEN);
514         ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
515         ev->rm.req_ie_len = req_ie_len;
516         memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
517         ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
518         ev->rm.resp_ie_len = resp_ie_len;
519         memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
520
521         spin_lock_irqsave(&wdev->event_lock, flags);
522         list_add_tail(&ev->list, &wdev->event_list);
523         spin_unlock_irqrestore(&wdev->event_lock, flags);
524         schedule_work(&rdev->event_work);
525 }
526 EXPORT_SYMBOL(cfg80211_roamed);
527
528 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
529                              size_t ie_len, u16 reason, bool from_ap)
530 {
531         struct wireless_dev *wdev = dev->ieee80211_ptr;
532         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
533         int i;
534 #ifdef CONFIG_WIRELESS_EXT
535         union iwreq_data wrqu;
536 #endif
537
538         ASSERT_WDEV_LOCK(wdev);
539
540         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
541                 return;
542
543         if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
544                 return;
545
546         if (wdev->current_bss) {
547                 cfg80211_unhold_bss(wdev->current_bss);
548                 cfg80211_put_bss(&wdev->current_bss->pub);
549         }
550
551         wdev->current_bss = NULL;
552         wdev->sme_state = CFG80211_SME_IDLE;
553
554         if (wdev->conn) {
555                 kfree(wdev->conn->ie);
556                 wdev->conn->ie = NULL;
557                 kfree(wdev->conn);
558                 wdev->conn = NULL;
559         }
560
561         nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
562
563         /*
564          * Delete all the keys ... pairwise keys can't really
565          * exist any more anyway, but default keys might.
566          */
567         if (rdev->ops->del_key)
568                 for (i = 0; i < 6; i++)
569                         rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
570
571 #ifdef CONFIG_WIRELESS_EXT
572         memset(&wrqu, 0, sizeof(wrqu));
573         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
574         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
575 #endif
576 }
577
578 void cfg80211_disconnected(struct net_device *dev, u16 reason,
579                            u8 *ie, size_t ie_len, gfp_t gfp)
580 {
581         struct wireless_dev *wdev = dev->ieee80211_ptr;
582         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
583         struct cfg80211_event *ev;
584         unsigned long flags;
585
586         ev = kzalloc(sizeof(*ev) + ie_len, gfp);
587         if (!ev)
588                 return;
589
590         ev->type = EVENT_DISCONNECTED;
591         ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
592         ev->dc.ie_len = ie_len;
593         memcpy((void *)ev->dc.ie, ie, ie_len);
594         ev->dc.reason = reason;
595
596         spin_lock_irqsave(&wdev->event_lock, flags);
597         list_add_tail(&ev->list, &wdev->event_list);
598         spin_unlock_irqrestore(&wdev->event_lock, flags);
599         schedule_work(&rdev->event_work);
600 }
601 EXPORT_SYMBOL(cfg80211_disconnected);
602
603 int __cfg80211_connect(struct cfg80211_registered_device *rdev,
604                        struct net_device *dev,
605                        struct cfg80211_connect_params *connect,
606                        struct cfg80211_cached_keys *connkeys)
607 {
608         struct wireless_dev *wdev = dev->ieee80211_ptr;
609         int err;
610
611         ASSERT_WDEV_LOCK(wdev);
612
613         if (wdev->sme_state != CFG80211_SME_IDLE)
614                 return -EALREADY;
615
616         if (WARN_ON(wdev->connect_keys)) {
617                 kfree(wdev->connect_keys);
618                 wdev->connect_keys = NULL;
619         }
620
621         if (connkeys && connkeys->def >= 0) {
622                 int idx;
623
624                 idx = connkeys->def;
625                 /* If given a WEP key we may need it for shared key auth */
626                 if (connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP40 ||
627                     connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP104) {
628                         connect->key_idx = idx;
629                         connect->key = connkeys->params[idx].key;
630                         connect->key_len = connkeys->params[idx].key_len;
631                 }
632         }
633
634         if (!rdev->ops->connect) {
635                 if (!rdev->ops->auth || !rdev->ops->assoc)
636                         return -EOPNOTSUPP;
637
638                 if (WARN_ON(wdev->conn))
639                         return -EINPROGRESS;
640
641                 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
642                 if (!wdev->conn)
643                         return -ENOMEM;
644
645                 /*
646                  * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
647                  */
648                 memcpy(&wdev->conn->params, connect, sizeof(*connect));
649                 if (connect->bssid) {
650                         wdev->conn->params.bssid = wdev->conn->bssid;
651                         memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
652                 }
653
654                 if (connect->ie) {
655                         wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
656                                                 GFP_KERNEL);
657                         wdev->conn->params.ie = wdev->conn->ie;
658                         if (!wdev->conn->ie) {
659                                 kfree(wdev->conn);
660                                 wdev->conn = NULL;
661                                 return -ENOMEM;
662                         }
663                 }
664
665                 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
666                         wdev->conn->auto_auth = true;
667                         /* start with open system ... should mostly work */
668                         wdev->conn->params.auth_type =
669                                 NL80211_AUTHTYPE_OPEN_SYSTEM;
670                 } else {
671                         wdev->conn->auto_auth = false;
672                 }
673
674                 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
675                 wdev->ssid_len = connect->ssid_len;
676                 wdev->conn->params.ssid = wdev->ssid;
677                 wdev->conn->params.ssid_len = connect->ssid_len;
678
679                 /* don't care about result -- but fill bssid & channel */
680                 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
681                         cfg80211_get_conn_bss(wdev);
682
683                 wdev->sme_state = CFG80211_SME_CONNECTING;
684                 wdev->connect_keys = connkeys;
685
686                 /* we're good if we have both BSSID and channel */
687                 if (wdev->conn->params.bssid && wdev->conn->params.channel) {
688                         wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
689                         err = cfg80211_conn_do_work(wdev);
690                 } else {
691                         /* otherwise we'll need to scan for the AP first */
692                         err = cfg80211_conn_scan(wdev);
693                         /*
694                          * If we can't scan right now, then we need to scan again
695                          * after the current scan finished, since the parameters
696                          * changed (unless we find a good AP anyway).
697                          */
698                         if (err == -EBUSY) {
699                                 err = 0;
700                                 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
701                         }
702                 }
703                 if (err) {
704                         kfree(wdev->conn);
705                         wdev->conn = NULL;
706                         wdev->sme_state = CFG80211_SME_IDLE;
707                         wdev->connect_keys = NULL;
708                 }
709
710                 return err;
711         } else {
712                 wdev->sme_state = CFG80211_SME_CONNECTING;
713                 wdev->connect_keys = connkeys;
714                 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
715                 if (err) {
716                         wdev->connect_keys = NULL;
717                         wdev->sme_state = CFG80211_SME_IDLE;
718                         return err;
719                 }
720
721                 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
722                 wdev->ssid_len = connect->ssid_len;
723
724                 return 0;
725         }
726 }
727
728 int cfg80211_connect(struct cfg80211_registered_device *rdev,
729                      struct net_device *dev,
730                      struct cfg80211_connect_params *connect,
731                      struct cfg80211_cached_keys *connkeys)
732 {
733         int err;
734
735         wdev_lock(dev->ieee80211_ptr);
736         err = __cfg80211_connect(rdev, dev, connect, connkeys);
737         wdev_unlock(dev->ieee80211_ptr);
738
739         return err;
740 }
741
742 int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
743                           struct net_device *dev, u16 reason, bool wextev)
744 {
745         struct wireless_dev *wdev = dev->ieee80211_ptr;
746         int err;
747
748         ASSERT_WDEV_LOCK(wdev);
749
750         if (wdev->sme_state == CFG80211_SME_IDLE)
751                 return -EINVAL;
752
753         kfree(wdev->connect_keys);
754         wdev->connect_keys = NULL;
755
756         if (!rdev->ops->disconnect) {
757                 if (!rdev->ops->deauth)
758                         return -EOPNOTSUPP;
759
760                 /* was it connected by userspace SME? */
761                 if (!wdev->conn) {
762                         cfg80211_mlme_down(rdev, dev);
763                         return 0;
764                 }
765
766                 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
767                     (wdev->conn->state == CFG80211_CONN_SCANNING ||
768                      wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
769                         wdev->sme_state = CFG80211_SME_IDLE;
770                         kfree(wdev->conn);
771                         wdev->conn = NULL;
772                         return 0;
773                 }
774
775                 /* wdev->conn->params.bssid must be set if > SCANNING */
776                 err = __cfg80211_mlme_deauth(rdev, dev,
777                                              wdev->conn->params.bssid,
778                                              NULL, 0, reason);
779                 if (err)
780                         return err;
781         } else {
782                 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
783                 if (err)
784                         return err;
785         }
786
787         if (wdev->sme_state == CFG80211_SME_CONNECTED)
788                 __cfg80211_disconnected(dev, NULL, 0, 0, false);
789         else if (wdev->sme_state == CFG80211_SME_CONNECTING)
790                 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
791                                           WLAN_STATUS_UNSPECIFIED_FAILURE,
792                                           wextev, NULL);
793
794         return 0;
795 }
796
797 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
798                         struct net_device *dev,
799                         u16 reason, bool wextev)
800 {
801         int err;
802
803         wdev_lock(dev->ieee80211_ptr);
804         err = __cfg80211_disconnect(rdev, dev, reason, wextev);
805         wdev_unlock(dev->ieee80211_ptr);
806
807         return err;
808 }
809
810 void cfg80211_sme_disassoc(struct net_device *dev, int idx)
811 {
812         struct wireless_dev *wdev = dev->ieee80211_ptr;
813         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
814         u8 bssid[ETH_ALEN];
815
816         ASSERT_WDEV_LOCK(wdev);
817
818         if (!wdev->conn)
819                 return;
820
821         if (wdev->conn->state == CFG80211_CONN_IDLE)
822                 return;
823
824         /*
825          * Ok, so the association was made by this SME -- we don't
826          * want it any more so deauthenticate too.
827          */
828
829         if (!wdev->auth_bsses[idx])
830                 return;
831
832         memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
833         if (__cfg80211_mlme_deauth(rdev, dev, bssid,
834                                    NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
835                 /* whatever -- assume gone anyway */
836                 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
837                 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
838                 wdev->auth_bsses[idx] = NULL;
839         }
840 }