blob: 0115d07d2c1a04fbc1a4d582264c5550298655b6 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
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>
Johannes Berga9a11622009-07-27 12:01:53 +020011#include <linux/wireless.h>
12#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <net/cfg80211.h>
14#include <net/rtnetlink.h>
15#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070016#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020017
Johannes Berg6829c872009-07-02 09:13:27 +020018struct 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,
Johannes Berg7d930bc2009-10-20 15:08:53 +090029 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020030 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020031 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020032 u8 *ie;
33 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020034 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020035};
36
37
38static int cfg80211_conn_scan(struct wireless_dev *wdev)
39{
Johannes Berg79c97e92009-07-07 03:56:12 +020040 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020041 struct cfg80211_scan_request *request;
42 int n_channels, err;
43
44 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020045 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +020046 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020047
Johannes Berg79c97e92009-07-07 03:56:12 +020048 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020049 return -EBUSY;
50
51 if (wdev->conn->params.channel) {
52 n_channels = 1;
53 } else {
54 enum ieee80211_band band;
55 n_channels = 0;
56
57 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
58 if (!wdev->wiphy->bands[band])
59 continue;
60 n_channels += wdev->wiphy->bands[band]->n_channels;
61 }
62 }
63 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
64 sizeof(request->channels[0]) * n_channels,
65 GFP_KERNEL);
66 if (!request)
67 return -ENOMEM;
68
Johannes Berg6829c872009-07-02 09:13:27 +020069 if (wdev->conn->params.channel)
70 request->channels[0] = wdev->conn->params.channel;
71 else {
72 int i = 0, j;
73 enum ieee80211_band band;
74
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 if (!wdev->wiphy->bands[band])
77 continue;
78 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
79 i++, j++)
80 request->channels[i] =
81 &wdev->wiphy->bands[band]->channels[j];
82 }
83 }
84 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +020085 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +020086 request->n_ssids = 1;
87
88 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
89 wdev->conn->params.ssid_len);
90 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
91
Johannes Berg463d0182009-07-14 00:33:35 +020092 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +020093 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +020094
Johannes Berg79c97e92009-07-07 03:56:12 +020095 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +020096
Johannes Berg79c97e92009-07-07 03:56:12 +020097 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +020098 if (!err) {
99 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200100 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200101 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200102 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200103 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200104 kfree(request);
105 }
106 return err;
107}
108
109static int cfg80211_conn_do_work(struct wireless_dev *wdev)
110{
Johannes Berg79c97e92009-07-07 03:56:12 +0200111 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200112 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200113 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200114 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200115
Johannes Berg667503dd2009-07-07 03:56:11 +0200116 ASSERT_WDEV_LOCK(wdev);
117
Johannes Berg6829c872009-07-02 09:13:27 +0200118 if (!wdev->conn)
119 return 0;
120
Johannes Berg19957bb2009-07-02 17:20:43 +0200121 params = &wdev->conn->params;
122
Johannes Berg6829c872009-07-02 09:13:27 +0200123 switch (wdev->conn->state) {
124 case CFG80211_CONN_SCAN_AGAIN:
125 return cfg80211_conn_scan(wdev);
126 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200127 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200128 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200129 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200130 params->channel, params->auth_type,
131 params->bssid,
132 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200133 NULL, 0,
134 params->key, params->key_len,
135 params->key_idx);
Johannes Berg6829c872009-07-02 09:13:27 +0200136 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200137 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200138 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200139 if (wdev->conn->prev_bssid_valid)
140 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200142 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200143 prev_bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200144 params->ssid, params->ssid_len,
145 params->ie, params->ie_len,
146 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200147 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200148 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200149 NULL, 0,
150 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900152 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
153 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
154 NULL, 0,
155 WLAN_REASON_DEAUTH_LEAVING);
156 /* return an error so that we call __cfg80211_connect_result() */
157 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200158 default:
159 return 0;
160 }
161}
162
163void cfg80211_conn_work(struct work_struct *work)
164{
Johannes Berg79c97e92009-07-07 03:56:12 +0200165 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200166 container_of(work, struct cfg80211_registered_device, conn_work);
167 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100168 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200169
170 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200171 cfg80211_lock_rdev(rdev);
172 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200173
Johannes Berg79c97e92009-07-07 03:56:12 +0200174 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200175 wdev_lock(wdev);
176 if (!netif_running(wdev->netdev)) {
177 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200178 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200179 }
180 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
181 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200182 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200183 }
Johannes Berg7400f422009-10-31 07:40:37 +0100184 if (wdev->conn->params.bssid) {
185 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
186 bssid = bssid_buf;
187 }
Johannes Berg6829c872009-07-02 09:13:27 +0200188 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200189 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900190 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200191 NULL, 0, NULL, 0,
192 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200193 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200194 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200195 }
196
Johannes Berg79c97e92009-07-07 03:56:12 +0200197 mutex_unlock(&rdev->devlist_mtx);
198 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200199 rtnl_unlock();
200}
201
Johannes Bergbbac31f2009-09-16 09:04:26 -0700202static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200203{
Johannes Berg79c97e92009-07-07 03:56:12 +0200204 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200205 struct cfg80211_bss *bss;
206 u16 capa = WLAN_CAPABILITY_ESS;
207
Johannes Berg667503dd2009-07-07 03:56:11 +0200208 ASSERT_WDEV_LOCK(wdev);
209
Johannes Berg6829c872009-07-02 09:13:27 +0200210 if (wdev->conn->params.privacy)
211 capa |= WLAN_CAPABILITY_PRIVACY;
212
213 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
214 wdev->conn->params.ssid,
215 wdev->conn->params.ssid_len,
216 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
217 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200218 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700219 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200220
221 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
222 wdev->conn->params.bssid = wdev->conn->bssid;
223 wdev->conn->params.channel = bss->channel;
224 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200225 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200226
Johannes Bergbbac31f2009-09-16 09:04:26 -0700227 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200228}
229
Johannes Berg667503dd2009-07-07 03:56:11 +0200230static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200231{
232 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200233 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700234 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200235
Johannes Berg667503dd2009-07-07 03:56:11 +0200236 ASSERT_WDEV_LOCK(wdev);
237
Johannes Berg6829c872009-07-02 09:13:27 +0200238 if (wdev->sme_state != CFG80211_SME_CONNECTING)
239 return;
240
Zhu Yid4b1a682009-07-16 17:34:14 +0800241 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200242 return;
243
244 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
245 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
246 return;
247
Johannes Bergbbac31f2009-09-16 09:04:26 -0700248 bss = cfg80211_get_conn_bss(wdev);
249 if (bss) {
250 cfg80211_put_bss(bss);
251 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200252 /* not found */
253 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200254 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200255 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200256 __cfg80211_connect_result(
257 wdev->netdev,
258 wdev->conn->params.bssid,
259 NULL, 0, NULL, 0,
260 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200261 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200262 }
263}
264
Johannes Berg667503dd2009-07-07 03:56:11 +0200265void cfg80211_sme_scan_done(struct net_device *dev)
266{
267 struct wireless_dev *wdev = dev->ieee80211_ptr;
268
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200269 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200270 wdev_lock(wdev);
271 __cfg80211_sme_scan_done(dev);
272 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200273 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200274}
275
276void cfg80211_sme_rx_auth(struct net_device *dev,
277 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200278{
279 struct wireless_dev *wdev = dev->ieee80211_ptr;
280 struct wiphy *wiphy = wdev->wiphy;
281 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
282 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
283 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
284
Johannes Berg667503dd2009-07-07 03:56:11 +0200285 ASSERT_WDEV_LOCK(wdev);
286
Johannes Berg6829c872009-07-02 09:13:27 +0200287 /* should only RX auth frames when connecting */
288 if (wdev->sme_state != CFG80211_SME_CONNECTING)
289 return;
290
291 if (WARN_ON(!wdev->conn))
292 return;
293
294 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
295 wdev->conn->auto_auth &&
296 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
297 /* select automatically between only open, shared, leap */
298 switch (wdev->conn->params.auth_type) {
299 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200300 if (wdev->connect_keys)
301 wdev->conn->params.auth_type =
302 NL80211_AUTHTYPE_SHARED_KEY;
303 else
304 wdev->conn->params.auth_type =
305 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200306 break;
307 case NL80211_AUTHTYPE_SHARED_KEY:
308 wdev->conn->params.auth_type =
309 NL80211_AUTHTYPE_NETWORK_EAP;
310 break;
311 default:
312 /* huh? */
313 wdev->conn->params.auth_type =
314 NL80211_AUTHTYPE_OPEN_SYSTEM;
315 break;
316 }
317 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
318 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200319 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200320 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200321 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200322 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200323 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
324 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
325 schedule_work(&rdev->conn_work);
326 }
327}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200328
Johannes Bergf401a6f2009-08-07 14:51:05 +0200329bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
330{
331 struct wiphy *wiphy = wdev->wiphy;
332 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
333
334 if (WARN_ON(!wdev->conn))
335 return false;
336
337 if (!wdev->conn->prev_bssid_valid)
338 return false;
339
340 /*
341 * Some stupid APs don't accept reassoc, so we
342 * need to fall back to trying regular assoc.
343 */
344 wdev->conn->prev_bssid_valid = false;
345 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
346 schedule_work(&rdev->conn_work);
347
348 return true;
349}
350
Johannes Berg7d930bc2009-10-20 15:08:53 +0900351void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
352{
353 struct wiphy *wiphy = wdev->wiphy;
354 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
355
356 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
357 schedule_work(&rdev->conn_work);
358}
359
Johannes Berg667503dd2009-07-07 03:56:11 +0200360void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
361 const u8 *req_ie, size_t req_ie_len,
362 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200363 u16 status, bool wextev,
364 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200365{
366 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700367 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200368#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200369 union iwreq_data wrqu;
370#endif
371
Johannes Berg667503dd2009-07-07 03:56:11 +0200372 ASSERT_WDEV_LOCK(wdev);
373
Samuel Ortizb23aa672009-07-01 21:26:54 +0200374 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
375 return;
376
Johannes Bergf7969962009-08-21 12:23:49 +0200377 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200378 return;
379
380 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200381 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200382 resp_ie, resp_ie_len,
383 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200384
Johannes Berg3d23e342009-09-29 23:27:28 +0200385#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200386 if (wextev) {
387 if (req_ie && status == WLAN_STATUS_SUCCESS) {
388 memset(&wrqu, 0, sizeof(wrqu));
389 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800390 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200391 }
392
393 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
394 memset(&wrqu, 0, sizeof(wrqu));
395 wrqu.data.length = resp_ie_len;
396 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
397 }
398
399 memset(&wrqu, 0, sizeof(wrqu));
400 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200401 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200402 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200403 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
404 wdev->wext.prev_bssid_valid = true;
405 }
Johannes Berge45cd822009-07-02 09:58:04 +0200406 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
407 }
408#endif
409
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200410 if (wdev->current_bss) {
411 cfg80211_unhold_bss(wdev->current_bss);
412 cfg80211_put_bss(&wdev->current_bss->pub);
413 wdev->current_bss = NULL;
414 }
415
Johannes Berg19957bb2009-07-02 17:20:43 +0200416 if (wdev->conn)
417 wdev->conn->state = CFG80211_CONN_IDLE;
418
Johannes Bergfffd0932009-07-08 14:22:54 +0200419 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200420 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100421 if (wdev->conn)
422 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200423 kfree(wdev->conn);
424 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200425 kfree(wdev->connect_keys);
426 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200427 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200428 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200429 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200430
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200431 if (!bss)
432 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
433 wdev->ssid, wdev->ssid_len,
434 WLAN_CAPABILITY_ESS,
435 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200436
437 if (WARN_ON(!bss))
438 return;
439
440 cfg80211_hold_bss(bss_from_pub(bss));
441 wdev->current_bss = bss_from_pub(bss);
442
Johannes Bergfffd0932009-07-08 14:22:54 +0200443 wdev->sme_state = CFG80211_SME_CONNECTED;
444 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700445
446 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
447
448 if (!country_ie)
449 return;
450
451 /*
452 * ieee80211_bss_get_ie() ensures we can access:
453 * - country_ie + 2, the start of the country ie data, and
454 * - and country_ie[1] which is the IE length
455 */
456 regulatory_hint_11d(wdev->wiphy,
457 country_ie + 2,
458 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200459}
Johannes Bergf2129352009-07-01 21:26:56 +0200460
461void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
462 const u8 *req_ie, size_t req_ie_len,
463 const u8 *resp_ie, size_t resp_ie_len,
464 u16 status, gfp_t gfp)
465{
Johannes Berg667503dd2009-07-07 03:56:11 +0200466 struct wireless_dev *wdev = dev->ieee80211_ptr;
467 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
468 struct cfg80211_event *ev;
469 unsigned long flags;
470
Johannes Bergf7969962009-08-21 12:23:49 +0200471 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
472
Johannes Berg667503dd2009-07-07 03:56:11 +0200473 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
474 if (!ev)
475 return;
476
477 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800478 if (bssid)
479 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200480 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
481 ev->cr.req_ie_len = req_ie_len;
482 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
483 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
484 ev->cr.resp_ie_len = resp_ie_len;
485 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
486 ev->cr.status = status;
487
488 spin_lock_irqsave(&wdev->event_lock, flags);
489 list_add_tail(&ev->list, &wdev->event_list);
490 spin_unlock_irqrestore(&wdev->event_lock, flags);
491 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200492}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200493EXPORT_SYMBOL(cfg80211_connect_result);
494
Johannes Berg667503dd2009-07-07 03:56:11 +0200495void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
496 const u8 *req_ie, size_t req_ie_len,
497 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200498{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200499 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200500#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200501 union iwreq_data wrqu;
502#endif
503
Johannes Berg667503dd2009-07-07 03:56:11 +0200504 ASSERT_WDEV_LOCK(wdev);
505
Samuel Ortizb23aa672009-07-01 21:26:54 +0200506 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
507 return;
508
Johannes Bergf7969962009-08-21 12:23:49 +0200509 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200510 return;
511
512 /* internal error -- how did we get to CONNECTED w/o BSS? */
513 if (WARN_ON(!wdev->current_bss)) {
514 return;
515 }
516
517 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200518 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200519 wdev->current_bss = NULL;
520
521 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
522 wdev->ssid, wdev->ssid_len,
523 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
524
525 if (WARN_ON(!bss))
526 return;
527
Johannes Berg19957bb2009-07-02 17:20:43 +0200528 cfg80211_hold_bss(bss_from_pub(bss));
529 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200530
Johannes Berg667503dd2009-07-07 03:56:11 +0200531 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
532 req_ie, req_ie_len, resp_ie, resp_ie_len,
533 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200534
Johannes Berg3d23e342009-09-29 23:27:28 +0200535#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200536 if (req_ie) {
537 memset(&wrqu, 0, sizeof(wrqu));
538 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800539 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200540 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200541 }
542
543 if (resp_ie) {
544 memset(&wrqu, 0, sizeof(wrqu));
545 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200546 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
547 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200548 }
549
550 memset(&wrqu, 0, sizeof(wrqu));
551 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
552 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200553 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
554 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200555 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200556#endif
557}
Johannes Berg667503dd2009-07-07 03:56:11 +0200558
559void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
560 const u8 *req_ie, size_t req_ie_len,
561 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
562{
563 struct wireless_dev *wdev = dev->ieee80211_ptr;
564 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
565 struct cfg80211_event *ev;
566 unsigned long flags;
567
Johannes Bergf7969962009-08-21 12:23:49 +0200568 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
569
Johannes Berg667503dd2009-07-07 03:56:11 +0200570 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
571 if (!ev)
572 return;
573
574 ev->type = EVENT_ROAMED;
575 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
576 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
577 ev->rm.req_ie_len = req_ie_len;
578 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
579 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
580 ev->rm.resp_ie_len = resp_ie_len;
581 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
582
583 spin_lock_irqsave(&wdev->event_lock, flags);
584 list_add_tail(&ev->list, &wdev->event_list);
585 spin_unlock_irqrestore(&wdev->event_lock, flags);
586 schedule_work(&rdev->event_work);
587}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200588EXPORT_SYMBOL(cfg80211_roamed);
589
Johannes Berg667503dd2009-07-07 03:56:11 +0200590void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200591 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200592{
593 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200594 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
595 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200596#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200597 union iwreq_data wrqu;
598#endif
599
Johannes Berg667503dd2009-07-07 03:56:11 +0200600 ASSERT_WDEV_LOCK(wdev);
601
Samuel Ortizb23aa672009-07-01 21:26:54 +0200602 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
603 return;
604
Johannes Bergf7969962009-08-21 12:23:49 +0200605 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200606 return;
607
608 if (wdev->current_bss) {
609 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200610 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200611 }
612
613 wdev->current_bss = NULL;
614 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200615 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200616
Johannes Berg6829c872009-07-02 09:13:27 +0200617 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200618 const u8 *bssid;
619 int ret;
620
Johannes Berg6829c872009-07-02 09:13:27 +0200621 kfree(wdev->conn->ie);
622 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200623 kfree(wdev->conn);
624 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200625
626 /*
627 * If this disconnect was due to a disassoc, we
628 * we might still have an auth BSS around. For
629 * the userspace SME that's currently expected,
630 * but for the kernel SME (nl80211 CONNECT or
631 * wireless extensions) we want to clear up all
632 * state.
633 */
634 for (i = 0; i < MAX_AUTH_BSSES; i++) {
635 if (!wdev->auth_bsses[i])
636 continue;
637 bssid = wdev->auth_bsses[i]->pub.bssid;
638 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
639 WLAN_REASON_DEAUTH_LEAVING);
640 WARN(ret, "deauth failed: %d\n", ret);
641 }
Johannes Berg6829c872009-07-02 09:13:27 +0200642 }
643
Johannes Bergfffd0932009-07-08 14:22:54 +0200644 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
645
646 /*
647 * Delete all the keys ... pairwise keys can't really
648 * exist any more anyway, but default keys might.
649 */
650 if (rdev->ops->del_key)
651 for (i = 0; i < 6; i++)
652 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200653
Johannes Berg3d23e342009-09-29 23:27:28 +0200654#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655 memset(&wrqu, 0, sizeof(wrqu));
656 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
657 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
658#endif
659}
660
661void cfg80211_disconnected(struct net_device *dev, u16 reason,
662 u8 *ie, size_t ie_len, gfp_t gfp)
663{
Johannes Berg667503dd2009-07-07 03:56:11 +0200664 struct wireless_dev *wdev = dev->ieee80211_ptr;
665 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
666 struct cfg80211_event *ev;
667 unsigned long flags;
668
Johannes Bergf7969962009-08-21 12:23:49 +0200669 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
670
Johannes Berg667503dd2009-07-07 03:56:11 +0200671 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
672 if (!ev)
673 return;
674
675 ev->type = EVENT_DISCONNECTED;
676 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
677 ev->dc.ie_len = ie_len;
678 memcpy((void *)ev->dc.ie, ie, ie_len);
679 ev->dc.reason = reason;
680
681 spin_lock_irqsave(&wdev->event_lock, flags);
682 list_add_tail(&ev->list, &wdev->event_list);
683 spin_unlock_irqrestore(&wdev->event_lock, flags);
684 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200685}
686EXPORT_SYMBOL(cfg80211_disconnected);
687
Johannes Berg667503dd2009-07-07 03:56:11 +0200688int __cfg80211_connect(struct cfg80211_registered_device *rdev,
689 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200690 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200691 struct cfg80211_cached_keys *connkeys,
692 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200693{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200694 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200695 struct ieee80211_channel *chan;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700696 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200697 int err;
698
699 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200700
701 if (wdev->sme_state != CFG80211_SME_IDLE)
702 return -EALREADY;
703
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200704 chan = rdev_fixed_channel(rdev, wdev);
705 if (chan && chan != connect->channel)
706 return -EBUSY;
707
Johannes Bergfffd0932009-07-08 14:22:54 +0200708 if (WARN_ON(wdev->connect_keys)) {
709 kfree(wdev->connect_keys);
710 wdev->connect_keys = NULL;
711 }
712
713 if (connkeys && connkeys->def >= 0) {
714 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200715 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200716
717 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200718 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200719 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200720 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
721 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200722 connect->key_idx = idx;
723 connect->key = connkeys->params[idx].key;
724 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200725
726 /*
727 * If ciphers are not set (e.g. when going through
728 * iwconfig), we have to set them appropriately here.
729 */
730 if (connect->crypto.cipher_group == 0)
731 connect->crypto.cipher_group = cipher;
732
733 if (connect->crypto.n_ciphers_pairwise == 0) {
734 connect->crypto.n_ciphers_pairwise = 1;
735 connect->crypto.ciphers_pairwise[0] = cipher;
736 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200737 }
738 }
739
Samuel Ortizb23aa672009-07-01 21:26:54 +0200740 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200741 if (!rdev->ops->auth || !rdev->ops->assoc)
742 return -EOPNOTSUPP;
743
Johannes Berg19957bb2009-07-02 17:20:43 +0200744 if (WARN_ON(wdev->conn))
745 return -EINPROGRESS;
746
747 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
748 if (!wdev->conn)
749 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200750
751 /*
752 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
753 */
754 memcpy(&wdev->conn->params, connect, sizeof(*connect));
755 if (connect->bssid) {
756 wdev->conn->params.bssid = wdev->conn->bssid;
757 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
758 }
759
760 if (connect->ie) {
761 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
762 GFP_KERNEL);
763 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200764 if (!wdev->conn->ie) {
765 kfree(wdev->conn);
766 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200767 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200768 }
Johannes Berg6829c872009-07-02 09:13:27 +0200769 }
770
771 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
772 wdev->conn->auto_auth = true;
773 /* start with open system ... should mostly work */
774 wdev->conn->params.auth_type =
775 NL80211_AUTHTYPE_OPEN_SYSTEM;
776 } else {
777 wdev->conn->auto_auth = false;
778 }
779
780 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
781 wdev->ssid_len = connect->ssid_len;
782 wdev->conn->params.ssid = wdev->ssid;
783 wdev->conn->params.ssid_len = connect->ssid_len;
784
Johannes Berg8bb89482009-09-26 14:42:53 +0200785 /* see if we have the bss already */
786 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200787
788 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200789 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200790
Johannes Bergf401a6f2009-08-07 14:51:05 +0200791 if (prev_bssid) {
792 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
793 wdev->conn->prev_bssid_valid = true;
794 }
795
Johannes Bergbbac31f2009-09-16 09:04:26 -0700796 /* we're good if we have a matching bss struct */
797 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200798 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
799 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700800 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200801 } else {
802 /* otherwise we'll need to scan for the AP first */
803 err = cfg80211_conn_scan(wdev);
804 /*
805 * If we can't scan right now, then we need to scan again
806 * after the current scan finished, since the parameters
807 * changed (unless we find a good AP anyway).
808 */
809 if (err == -EBUSY) {
810 err = 0;
811 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
812 }
813 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200814 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100815 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200816 kfree(wdev->conn);
817 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200818 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200819 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200820 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200821 }
Johannes Berg6829c872009-07-02 09:13:27 +0200822
823 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200824 } else {
825 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200826 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200827 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
828 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200830 wdev->sme_state = CFG80211_SME_IDLE;
831 return err;
832 }
Johannes Berg6829c872009-07-02 09:13:27 +0200833
834 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
835 wdev->ssid_len = connect->ssid_len;
836
837 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200839}
840
Johannes Berg667503dd2009-07-07 03:56:11 +0200841int cfg80211_connect(struct cfg80211_registered_device *rdev,
842 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200843 struct cfg80211_connect_params *connect,
844 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200845{
846 int err;
847
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200848 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200849 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200850 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200851 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200852 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200853
854 return err;
855}
856
857int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
858 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200859{
Johannes Berg6829c872009-07-02 09:13:27 +0200860 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200861 int err;
862
Johannes Berg667503dd2009-07-07 03:56:11 +0200863 ASSERT_WDEV_LOCK(wdev);
864
Johannes Berg6829c872009-07-02 09:13:27 +0200865 if (wdev->sme_state == CFG80211_SME_IDLE)
866 return -EINVAL;
867
Johannes Bergfffd0932009-07-08 14:22:54 +0200868 kfree(wdev->connect_keys);
869 wdev->connect_keys = NULL;
870
Samuel Ortizb23aa672009-07-01 21:26:54 +0200871 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200872 if (!rdev->ops->deauth)
873 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200874
Johannes Berg19957bb2009-07-02 17:20:43 +0200875 /* was it connected by userspace SME? */
876 if (!wdev->conn) {
877 cfg80211_mlme_down(rdev, dev);
878 return 0;
879 }
Johannes Berg6829c872009-07-02 09:13:27 +0200880
881 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
882 (wdev->conn->state == CFG80211_CONN_SCANNING ||
883 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
884 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100885 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200886 kfree(wdev->conn);
887 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200888 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200889 return 0;
890 }
891
Johannes Berg6829c872009-07-02 09:13:27 +0200892 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200893 err = __cfg80211_mlme_deauth(rdev, dev,
894 wdev->conn->params.bssid,
895 NULL, 0, reason);
Johannes Berg6829c872009-07-02 09:13:27 +0200896 if (err)
897 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200898 } else {
899 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
900 if (err)
901 return err;
902 }
903
Johannes Berg6829c872009-07-02 09:13:27 +0200904 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200905 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200906 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200907 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
908 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200909 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200910
911 return 0;
912}
Johannes Berg19957bb2009-07-02 17:20:43 +0200913
Johannes Berg667503dd2009-07-07 03:56:11 +0200914int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
915 struct net_device *dev,
916 u16 reason, bool wextev)
917{
918 int err;
919
920 wdev_lock(dev->ieee80211_ptr);
921 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
922 wdev_unlock(dev->ieee80211_ptr);
923
924 return err;
925}
926
Johannes Berg19957bb2009-07-02 17:20:43 +0200927void cfg80211_sme_disassoc(struct net_device *dev, int idx)
928{
929 struct wireless_dev *wdev = dev->ieee80211_ptr;
930 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
931 u8 bssid[ETH_ALEN];
932
Johannes Berg667503dd2009-07-07 03:56:11 +0200933 ASSERT_WDEV_LOCK(wdev);
934
Johannes Berg19957bb2009-07-02 17:20:43 +0200935 if (!wdev->conn)
936 return;
937
938 if (wdev->conn->state == CFG80211_CONN_IDLE)
939 return;
940
941 /*
942 * Ok, so the association was made by this SME -- we don't
943 * want it any more so deauthenticate too.
944 */
945
946 if (!wdev->auth_bsses[idx])
947 return;
948
949 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200950 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
951 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200952 /* whatever -- assume gone anyway */
953 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
954 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
955 wdev->auth_bsses[idx] = NULL;
956 }
957}