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