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