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