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