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