blob: 9ddc00e3c23928e441bc502f322eb5f59e7533b3 [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 Berg6829c8782009-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,
29 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020030 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c8782009-07-02 09:13:27 +020031 u8 *ie;
32 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020033 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c8782009-07-02 09:13:27 +020034};
35
36
37static int cfg80211_conn_scan(struct wireless_dev *wdev)
38{
Johannes Berg79c97e92009-07-07 03:56:12 +020039 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +020040 struct cfg80211_scan_request *request;
41 int n_channels, err;
42
43 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020044 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020045 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +020046
Johannes Berg79c97e92009-07-07 03:56:12 +020047 if (rdev->scan_req)
Johannes Berg6829c8782009-07-02 09:13:27 +020048 return -EBUSY;
49
50 if (wdev->conn->params.channel) {
51 n_channels = 1;
52 } else {
53 enum ieee80211_band band;
54 n_channels = 0;
55
56 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
57 if (!wdev->wiphy->bands[band])
58 continue;
59 n_channels += wdev->wiphy->bands[band]->n_channels;
60 }
61 }
62 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
63 sizeof(request->channels[0]) * n_channels,
64 GFP_KERNEL);
65 if (!request)
66 return -ENOMEM;
67
Johannes Berg6829c8782009-07-02 09:13:27 +020068 if (wdev->conn->params.channel)
69 request->channels[0] = wdev->conn->params.channel;
70 else {
71 int i = 0, j;
72 enum ieee80211_band band;
73
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 if (!wdev->wiphy->bands[band])
76 continue;
77 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
78 i++, j++)
79 request->channels[i] =
80 &wdev->wiphy->bands[band]->channels[j];
81 }
82 }
83 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +020084 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c8782009-07-02 09:13:27 +020085 request->n_ssids = 1;
86
87 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
88 wdev->conn->params.ssid_len);
89 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
90
Johannes Berg463d0182009-07-14 00:33:35 +020091 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +020092 request->wiphy = &rdev->wiphy;
Johannes Berg6829c8782009-07-02 09:13:27 +020093
Johannes Berg79c97e92009-07-07 03:56:12 +020094 rdev->scan_req = request;
Johannes Berg6829c8782009-07-02 09:13:27 +020095
Johannes Berg79c97e92009-07-07 03:56:12 +020096 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c8782009-07-02 09:13:27 +020097 if (!err) {
98 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +020099 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200100 dev_hold(wdev->netdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200101 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200102 rdev->scan_req = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200103 kfree(request);
104 }
105 return err;
106}
107
108static int cfg80211_conn_do_work(struct wireless_dev *wdev)
109{
Johannes Berg79c97e92009-07-07 03:56:12 +0200110 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200111 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200112 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200113 int err;
Johannes Berg6829c8782009-07-02 09:13:27 +0200114
Johannes Berg667503d2009-07-07 03:56:11 +0200115 ASSERT_WDEV_LOCK(wdev);
116
Johannes Berg6829c8782009-07-02 09:13:27 +0200117 if (!wdev->conn)
118 return 0;
119
Johannes Berg19957bb2009-07-02 17:20:43 +0200120 params = &wdev->conn->params;
121
Johannes Berg6829c8782009-07-02 09:13:27 +0200122 switch (wdev->conn->state) {
123 case CFG80211_CONN_SCAN_AGAIN:
124 return cfg80211_conn_scan(wdev);
125 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200127 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200128 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200129 params->channel, params->auth_type,
130 params->bssid,
131 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200132 NULL, 0,
133 params->key, params->key_len,
134 params->key_idx);
Johannes Berg6829c8782009-07-02 09:13:27 +0200135 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200137 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200138 if (wdev->conn->prev_bssid_valid)
139 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200140 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200141 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200142 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200143 params->ssid, params->ssid_len,
144 params->ie, params->ie_len,
145 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200146 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200147 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200148 NULL, 0,
149 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200150 return err;
Johannes Berg6829c8782009-07-02 09:13:27 +0200151 default:
152 return 0;
153 }
154}
155
156void cfg80211_conn_work(struct work_struct *work)
157{
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 struct cfg80211_registered_device *rdev =
Johannes Berg6829c8782009-07-02 09:13:27 +0200159 container_of(work, struct cfg80211_registered_device, conn_work);
160 struct wireless_dev *wdev;
161
162 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200163 cfg80211_lock_rdev(rdev);
164 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c8782009-07-02 09:13:27 +0200165
Johannes Berg79c97e92009-07-07 03:56:12 +0200166 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200167 wdev_lock(wdev);
168 if (!netif_running(wdev->netdev)) {
169 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200170 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200171 }
172 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
173 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200174 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200175 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200176 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200177 __cfg80211_connect_result(
178 wdev->netdev,
179 wdev->conn->params.bssid,
180 NULL, 0, NULL, 0,
181 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200182 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200183 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200184 }
185
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 mutex_unlock(&rdev->devlist_mtx);
187 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200188 rtnl_unlock();
189}
190
191static bool cfg80211_get_conn_bss(struct wireless_dev *wdev)
192{
Johannes Berg79c97e92009-07-07 03:56:12 +0200193 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +0200194 struct cfg80211_bss *bss;
195 u16 capa = WLAN_CAPABILITY_ESS;
196
Johannes Berg667503d2009-07-07 03:56:11 +0200197 ASSERT_WDEV_LOCK(wdev);
198
Johannes Berg6829c8782009-07-02 09:13:27 +0200199 if (wdev->conn->params.privacy)
200 capa |= WLAN_CAPABILITY_PRIVACY;
201
202 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
203 wdev->conn->params.ssid,
204 wdev->conn->params.ssid_len,
205 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
206 capa);
Johannes Berg6829c8782009-07-02 09:13:27 +0200207 if (!bss)
208 return false;
209
210 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
211 wdev->conn->params.bssid = wdev->conn->bssid;
212 wdev->conn->params.channel = bss->channel;
213 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200214 schedule_work(&rdev->conn_work);
Johannes Berg6829c8782009-07-02 09:13:27 +0200215
216 cfg80211_put_bss(bss);
217 return true;
218}
219
Johannes Berg667503d2009-07-07 03:56:11 +0200220static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c8782009-07-02 09:13:27 +0200221{
222 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200223 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +0200224
Johannes Berg667503d2009-07-07 03:56:11 +0200225 ASSERT_WDEV_LOCK(wdev);
226
Johannes Berg6829c8782009-07-02 09:13:27 +0200227 if (wdev->sme_state != CFG80211_SME_CONNECTING)
228 return;
229
Zhu Yid4b1a682009-07-16 17:34:14 +0800230 if (!wdev->conn)
Johannes Berg6829c8782009-07-02 09:13:27 +0200231 return;
232
233 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
234 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
235 return;
236
237 if (!cfg80211_get_conn_bss(wdev)) {
238 /* not found */
239 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200240 schedule_work(&rdev->conn_work);
Johannes Berg6829c8782009-07-02 09:13:27 +0200241 else
Johannes Berg667503d2009-07-07 03:56:11 +0200242 __cfg80211_connect_result(
243 wdev->netdev,
244 wdev->conn->params.bssid,
245 NULL, 0, NULL, 0,
246 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200247 false, NULL);
Johannes Berg6829c8782009-07-02 09:13:27 +0200248 }
249}
250
Johannes Berg667503d2009-07-07 03:56:11 +0200251void cfg80211_sme_scan_done(struct net_device *dev)
252{
253 struct wireless_dev *wdev = dev->ieee80211_ptr;
254
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200255 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200256 wdev_lock(wdev);
257 __cfg80211_sme_scan_done(dev);
258 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200259 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200260}
261
262void cfg80211_sme_rx_auth(struct net_device *dev,
263 const u8 *buf, size_t len)
Johannes Berg6829c8782009-07-02 09:13:27 +0200264{
265 struct wireless_dev *wdev = dev->ieee80211_ptr;
266 struct wiphy *wiphy = wdev->wiphy;
267 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
268 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
269 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
270
Johannes Berg667503d2009-07-07 03:56:11 +0200271 ASSERT_WDEV_LOCK(wdev);
272
Johannes Berg6829c8782009-07-02 09:13:27 +0200273 /* should only RX auth frames when connecting */
274 if (wdev->sme_state != CFG80211_SME_CONNECTING)
275 return;
276
277 if (WARN_ON(!wdev->conn))
278 return;
279
280 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
281 wdev->conn->auto_auth &&
282 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
283 /* select automatically between only open, shared, leap */
284 switch (wdev->conn->params.auth_type) {
285 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200286 if (wdev->connect_keys)
287 wdev->conn->params.auth_type =
288 NL80211_AUTHTYPE_SHARED_KEY;
289 else
290 wdev->conn->params.auth_type =
291 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200292 break;
293 case NL80211_AUTHTYPE_SHARED_KEY:
294 wdev->conn->params.auth_type =
295 NL80211_AUTHTYPE_NETWORK_EAP;
296 break;
297 default:
298 /* huh? */
299 wdev->conn->params.auth_type =
300 NL80211_AUTHTYPE_OPEN_SYSTEM;
301 break;
302 }
303 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
304 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200305 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200306 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200307 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200308 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c8782009-07-02 09:13:27 +0200309 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
310 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
311 schedule_work(&rdev->conn_work);
312 }
313}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200314
Johannes Bergf401a6f2009-08-07 14:51:05 +0200315bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
316{
317 struct wiphy *wiphy = wdev->wiphy;
318 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
319
320 if (WARN_ON(!wdev->conn))
321 return false;
322
323 if (!wdev->conn->prev_bssid_valid)
324 return false;
325
326 /*
327 * Some stupid APs don't accept reassoc, so we
328 * need to fall back to trying regular assoc.
329 */
330 wdev->conn->prev_bssid_valid = false;
331 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
332 schedule_work(&rdev->conn_work);
333
334 return true;
335}
336
Johannes Berg667503d2009-07-07 03:56:11 +0200337void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
338 const u8 *req_ie, size_t req_ie_len,
339 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200340 u16 status, bool wextev,
341 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200342{
343 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700344 u8 *country_ie;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200345#ifdef CONFIG_WIRELESS_EXT
346 union iwreq_data wrqu;
347#endif
348
Johannes Berg667503d2009-07-07 03:56:11 +0200349 ASSERT_WDEV_LOCK(wdev);
350
Samuel Ortizb23aa672009-07-01 21:26:54 +0200351 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
352 return;
353
Johannes Bergea416a72009-08-17 12:22:14 +0200354 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING))
355 return;
356
357 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200358 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200359 resp_ie, resp_ie_len,
360 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200361
362#ifdef CONFIG_WIRELESS_EXT
363 if (wextev) {
364 if (req_ie && status == WLAN_STATUS_SUCCESS) {
365 memset(&wrqu, 0, sizeof(wrqu));
366 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800367 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200368 }
369
370 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
371 memset(&wrqu, 0, sizeof(wrqu));
372 wrqu.data.length = resp_ie_len;
373 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
374 }
375
376 memset(&wrqu, 0, sizeof(wrqu));
377 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200378 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200379 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200380 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
381 wdev->wext.prev_bssid_valid = true;
382 }
Johannes Berge45cd822009-07-02 09:58:04 +0200383 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
384 }
385#endif
386
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200387 if (wdev->current_bss) {
388 cfg80211_unhold_bss(wdev->current_bss);
389 cfg80211_put_bss(&wdev->current_bss->pub);
390 wdev->current_bss = NULL;
391 }
392
Johannes Berg19957bb2009-07-02 17:20:43 +0200393 if (wdev->conn)
394 wdev->conn->state = CFG80211_CONN_IDLE;
395
Johannes Bergfffd0932009-07-08 14:22:54 +0200396 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200397 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100398 if (wdev->conn)
399 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200400 kfree(wdev->conn);
401 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200402 kfree(wdev->connect_keys);
403 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200404 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200405 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200406 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200407
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200408 if (!bss)
409 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
410 wdev->ssid, wdev->ssid_len,
411 WLAN_CAPABILITY_ESS,
412 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200413
414 if (WARN_ON(!bss))
415 return;
416
417 cfg80211_hold_bss(bss_from_pub(bss));
418 wdev->current_bss = bss_from_pub(bss);
419
Johannes Bergfffd0932009-07-08 14:22:54 +0200420 wdev->sme_state = CFG80211_SME_CONNECTED;
421 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700422
423 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
424
425 if (!country_ie)
426 return;
427
428 /*
429 * ieee80211_bss_get_ie() ensures we can access:
430 * - country_ie + 2, the start of the country ie data, and
431 * - and country_ie[1] which is the IE length
432 */
433 regulatory_hint_11d(wdev->wiphy,
434 country_ie + 2,
435 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200436}
Johannes Bergf2129352009-07-01 21:26:56 +0200437
438void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
439 const u8 *req_ie, size_t req_ie_len,
440 const u8 *resp_ie, size_t resp_ie_len,
441 u16 status, gfp_t gfp)
442{
Johannes Berg667503d2009-07-07 03:56:11 +0200443 struct wireless_dev *wdev = dev->ieee80211_ptr;
444 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
445 struct cfg80211_event *ev;
446 unsigned long flags;
447
448 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
449 if (!ev)
450 return;
451
452 ev->type = EVENT_CONNECT_RESULT;
453 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
454 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
455 ev->cr.req_ie_len = req_ie_len;
456 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
457 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
458 ev->cr.resp_ie_len = resp_ie_len;
459 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
460 ev->cr.status = status;
461
462 spin_lock_irqsave(&wdev->event_lock, flags);
463 list_add_tail(&ev->list, &wdev->event_list);
464 spin_unlock_irqrestore(&wdev->event_lock, flags);
465 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200466}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200467EXPORT_SYMBOL(cfg80211_connect_result);
468
Johannes Berg667503d2009-07-07 03:56:11 +0200469void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
470 const u8 *req_ie, size_t req_ie_len,
471 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200472{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200473 struct cfg80211_bss *bss;
474#ifdef CONFIG_WIRELESS_EXT
475 union iwreq_data wrqu;
476#endif
477
Johannes Berg667503d2009-07-07 03:56:11 +0200478 ASSERT_WDEV_LOCK(wdev);
479
Samuel Ortizb23aa672009-07-01 21:26:54 +0200480 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
481 return;
482
483 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
484 return;
485
486 /* internal error -- how did we get to CONNECTED w/o BSS? */
487 if (WARN_ON(!wdev->current_bss)) {
488 return;
489 }
490
491 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200492 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200493 wdev->current_bss = NULL;
494
495 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
496 wdev->ssid, wdev->ssid_len,
497 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
498
499 if (WARN_ON(!bss))
500 return;
501
Johannes Berg19957bb2009-07-02 17:20:43 +0200502 cfg80211_hold_bss(bss_from_pub(bss));
503 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200504
Johannes Berg667503d2009-07-07 03:56:11 +0200505 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
506 req_ie, req_ie_len, resp_ie, resp_ie_len,
507 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200508
509#ifdef CONFIG_WIRELESS_EXT
510 if (req_ie) {
511 memset(&wrqu, 0, sizeof(wrqu));
512 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800513 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200514 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200515 }
516
517 if (resp_ie) {
518 memset(&wrqu, 0, sizeof(wrqu));
519 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200520 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
521 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200522 }
523
524 memset(&wrqu, 0, sizeof(wrqu));
525 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
526 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200527 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
528 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200529 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200530#endif
531}
Johannes Berg667503d2009-07-07 03:56:11 +0200532
533void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
534 const u8 *req_ie, size_t req_ie_len,
535 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
536{
537 struct wireless_dev *wdev = dev->ieee80211_ptr;
538 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
539 struct cfg80211_event *ev;
540 unsigned long flags;
541
542 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
543 if (!ev)
544 return;
545
546 ev->type = EVENT_ROAMED;
547 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
548 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
549 ev->rm.req_ie_len = req_ie_len;
550 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
551 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
552 ev->rm.resp_ie_len = resp_ie_len;
553 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
554
555 spin_lock_irqsave(&wdev->event_lock, flags);
556 list_add_tail(&ev->list, &wdev->event_list);
557 spin_unlock_irqrestore(&wdev->event_lock, flags);
558 schedule_work(&rdev->event_work);
559}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200560EXPORT_SYMBOL(cfg80211_roamed);
561
Johannes Berg667503d2009-07-07 03:56:11 +0200562void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c8782009-07-02 09:13:27 +0200563 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200564{
565 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200566 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
567 int i;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200568#ifdef CONFIG_WIRELESS_EXT
569 union iwreq_data wrqu;
570#endif
571
Johannes Berg667503d2009-07-07 03:56:11 +0200572 ASSERT_WDEV_LOCK(wdev);
573
Samuel Ortizb23aa672009-07-01 21:26:54 +0200574 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
575 return;
576
577 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
578 return;
579
580 if (wdev->current_bss) {
581 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200582 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200583 }
584
585 wdev->current_bss = NULL;
586 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200587 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200588
Johannes Berg6829c8782009-07-02 09:13:27 +0200589 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200590 const u8 *bssid;
591 int ret;
592
Johannes Berg6829c8782009-07-02 09:13:27 +0200593 kfree(wdev->conn->ie);
594 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200595 kfree(wdev->conn);
596 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200597
598 /*
599 * If this disconnect was due to a disassoc, we
600 * we might still have an auth BSS around. For
601 * the userspace SME that's currently expected,
602 * but for the kernel SME (nl80211 CONNECT or
603 * wireless extensions) we want to clear up all
604 * state.
605 */
606 for (i = 0; i < MAX_AUTH_BSSES; i++) {
607 if (!wdev->auth_bsses[i])
608 continue;
609 bssid = wdev->auth_bsses[i]->pub.bssid;
610 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
611 WLAN_REASON_DEAUTH_LEAVING);
612 WARN(ret, "deauth failed: %d\n", ret);
613 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200614 }
615
Johannes Bergfffd0932009-07-08 14:22:54 +0200616 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
617
618 /*
619 * Delete all the keys ... pairwise keys can't really
620 * exist any more anyway, but default keys might.
621 */
622 if (rdev->ops->del_key)
623 for (i = 0; i < 6; i++)
624 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200625
626#ifdef CONFIG_WIRELESS_EXT
627 memset(&wrqu, 0, sizeof(wrqu));
628 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
629 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
630#endif
631}
632
633void cfg80211_disconnected(struct net_device *dev, u16 reason,
634 u8 *ie, size_t ie_len, gfp_t gfp)
635{
Johannes Berg667503d2009-07-07 03:56:11 +0200636 struct wireless_dev *wdev = dev->ieee80211_ptr;
637 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
638 struct cfg80211_event *ev;
639 unsigned long flags;
640
641 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
642 if (!ev)
643 return;
644
645 ev->type = EVENT_DISCONNECTED;
646 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
647 ev->dc.ie_len = ie_len;
648 memcpy((void *)ev->dc.ie, ie, ie_len);
649 ev->dc.reason = reason;
650
651 spin_lock_irqsave(&wdev->event_lock, flags);
652 list_add_tail(&ev->list, &wdev->event_list);
653 spin_unlock_irqrestore(&wdev->event_lock, flags);
654 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655}
656EXPORT_SYMBOL(cfg80211_disconnected);
657
Johannes Berg667503d2009-07-07 03:56:11 +0200658int __cfg80211_connect(struct cfg80211_registered_device *rdev,
659 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200660 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200661 struct cfg80211_cached_keys *connkeys,
662 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200663{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200664 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200665 struct ieee80211_channel *chan;
Johannes Berg667503d2009-07-07 03:56:11 +0200666 int err;
667
668 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200669
670 if (wdev->sme_state != CFG80211_SME_IDLE)
671 return -EALREADY;
672
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200673 chan = rdev_fixed_channel(rdev, wdev);
674 if (chan && chan != connect->channel)
675 return -EBUSY;
676
Johannes Bergfffd0932009-07-08 14:22:54 +0200677 if (WARN_ON(wdev->connect_keys)) {
678 kfree(wdev->connect_keys);
679 wdev->connect_keys = NULL;
680 }
681
682 if (connkeys && connkeys->def >= 0) {
683 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200684 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200685
686 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200687 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200688 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200689 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
690 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200691 connect->key_idx = idx;
692 connect->key = connkeys->params[idx].key;
693 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200694
695 /*
696 * If ciphers are not set (e.g. when going through
697 * iwconfig), we have to set them appropriately here.
698 */
699 if (connect->crypto.cipher_group == 0)
700 connect->crypto.cipher_group = cipher;
701
702 if (connect->crypto.n_ciphers_pairwise == 0) {
703 connect->crypto.n_ciphers_pairwise = 1;
704 connect->crypto.ciphers_pairwise[0] = cipher;
705 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200706 }
707 }
708
Samuel Ortizb23aa672009-07-01 21:26:54 +0200709 if (!rdev->ops->connect) {
Johannes Berg6829c8782009-07-02 09:13:27 +0200710 if (!rdev->ops->auth || !rdev->ops->assoc)
711 return -EOPNOTSUPP;
712
Johannes Berg19957bb2009-07-02 17:20:43 +0200713 if (WARN_ON(wdev->conn))
714 return -EINPROGRESS;
715
716 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
717 if (!wdev->conn)
718 return -ENOMEM;
Johannes Berg6829c8782009-07-02 09:13:27 +0200719
720 /*
721 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
722 */
723 memcpy(&wdev->conn->params, connect, sizeof(*connect));
724 if (connect->bssid) {
725 wdev->conn->params.bssid = wdev->conn->bssid;
726 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
727 }
728
729 if (connect->ie) {
730 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
731 GFP_KERNEL);
732 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200733 if (!wdev->conn->ie) {
734 kfree(wdev->conn);
735 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200736 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200737 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200738 }
739
740 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
741 wdev->conn->auto_auth = true;
742 /* start with open system ... should mostly work */
743 wdev->conn->params.auth_type =
744 NL80211_AUTHTYPE_OPEN_SYSTEM;
745 } else {
746 wdev->conn->auto_auth = false;
747 }
748
749 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
750 wdev->ssid_len = connect->ssid_len;
751 wdev->conn->params.ssid = wdev->ssid;
752 wdev->conn->params.ssid_len = connect->ssid_len;
753
754 /* don't care about result -- but fill bssid & channel */
755 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
756 cfg80211_get_conn_bss(wdev);
757
758 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 wdev->connect_keys = connkeys;
Johannes Berg6829c8782009-07-02 09:13:27 +0200760
Johannes Bergf401a6f2009-08-07 14:51:05 +0200761 if (prev_bssid) {
762 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
763 wdev->conn->prev_bssid_valid = true;
764 }
765
Johannes Berg6829c8782009-07-02 09:13:27 +0200766 /* we're good if we have both BSSID and channel */
767 if (wdev->conn->params.bssid && wdev->conn->params.channel) {
768 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
769 err = cfg80211_conn_do_work(wdev);
770 } else {
771 /* otherwise we'll need to scan for the AP first */
772 err = cfg80211_conn_scan(wdev);
773 /*
774 * If we can't scan right now, then we need to scan again
775 * after the current scan finished, since the parameters
776 * changed (unless we find a good AP anyway).
777 */
778 if (err == -EBUSY) {
779 err = 0;
780 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
781 }
782 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200783 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100784 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200785 kfree(wdev->conn);
786 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200787 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200788 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200789 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200790 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200791
792 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200793 } else {
794 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200795 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200796 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
797 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200798 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200799 wdev->sme_state = CFG80211_SME_IDLE;
800 return err;
801 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200802
803 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
804 wdev->ssid_len = connect->ssid_len;
805
806 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200807 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200808}
809
Johannes Berg667503d2009-07-07 03:56:11 +0200810int cfg80211_connect(struct cfg80211_registered_device *rdev,
811 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200812 struct cfg80211_connect_params *connect,
813 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200814{
815 int err;
816
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200817 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200818 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200819 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200820 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200821 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200822
823 return err;
824}
825
826int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
827 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200828{
Johannes Berg6829c8782009-07-02 09:13:27 +0200829 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200830 int err;
831
Johannes Berg667503d2009-07-07 03:56:11 +0200832 ASSERT_WDEV_LOCK(wdev);
833
Johannes Berg6829c8782009-07-02 09:13:27 +0200834 if (wdev->sme_state == CFG80211_SME_IDLE)
835 return -EINVAL;
836
Johannes Bergfffd0932009-07-08 14:22:54 +0200837 kfree(wdev->connect_keys);
838 wdev->connect_keys = NULL;
839
Samuel Ortizb23aa672009-07-01 21:26:54 +0200840 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200841 if (!rdev->ops->deauth)
842 return -EOPNOTSUPP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200843
Johannes Berg19957bb2009-07-02 17:20:43 +0200844 /* was it connected by userspace SME? */
845 if (!wdev->conn) {
846 cfg80211_mlme_down(rdev, dev);
847 return 0;
848 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200849
850 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
851 (wdev->conn->state == CFG80211_CONN_SCANNING ||
852 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
853 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100854 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200855 kfree(wdev->conn);
856 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200857 wdev->ssid_len = 0;
Johannes Berg6829c8782009-07-02 09:13:27 +0200858 return 0;
859 }
860
Johannes Berg6829c8782009-07-02 09:13:27 +0200861 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200862 err = __cfg80211_mlme_deauth(rdev, dev,
863 wdev->conn->params.bssid,
864 NULL, 0, reason);
Johannes Berg6829c8782009-07-02 09:13:27 +0200865 if (err)
866 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200867 } else {
868 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
869 if (err)
870 return err;
871 }
872
Johannes Berg6829c8782009-07-02 09:13:27 +0200873 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200874 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200875 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200876 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
877 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200878 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200879
880 return 0;
881}
Johannes Berg19957bb2009-07-02 17:20:43 +0200882
Johannes Berg667503d2009-07-07 03:56:11 +0200883int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
884 struct net_device *dev,
885 u16 reason, bool wextev)
886{
887 int err;
888
889 wdev_lock(dev->ieee80211_ptr);
890 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
891 wdev_unlock(dev->ieee80211_ptr);
892
893 return err;
894}
895
Johannes Berg19957bb2009-07-02 17:20:43 +0200896void cfg80211_sme_disassoc(struct net_device *dev, int idx)
897{
898 struct wireless_dev *wdev = dev->ieee80211_ptr;
899 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
900 u8 bssid[ETH_ALEN];
901
Johannes Berg667503d2009-07-07 03:56:11 +0200902 ASSERT_WDEV_LOCK(wdev);
903
Johannes Berg19957bb2009-07-02 17:20:43 +0200904 if (!wdev->conn)
905 return;
906
907 if (wdev->conn->state == CFG80211_CONN_IDLE)
908 return;
909
910 /*
911 * Ok, so the association was made by this SME -- we don't
912 * want it any more so deauthenticate too.
913 */
914
915 if (!wdev->auth_bsses[idx])
916 return;
917
918 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200919 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
920 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200921 /* whatever -- assume gone anyway */
922 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
923 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
924 wdev->auth_bsses[idx] = NULL;
925 }
926}