blob: 17fde0da1b08658db9882f000a51928dbd9ad339 [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
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050037bool cfg80211_is_all_idle(void)
38{
39 struct cfg80211_registered_device *rdev;
40 struct wireless_dev *wdev;
41 bool is_all_idle = true;
42
43 mutex_lock(&cfg80211_mutex);
44
45 /*
46 * All devices must be idle as otherwise if you are actively
47 * scanning some new beacon hints could be learned and would
48 * count as new regulatory hints.
49 */
50 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
51 cfg80211_lock_rdev(rdev);
52 list_for_each_entry(wdev, &rdev->netdev_list, list) {
53 wdev_lock(wdev);
54 if (wdev->sme_state != CFG80211_SME_IDLE)
55 is_all_idle = false;
56 wdev_unlock(wdev);
57 }
58 cfg80211_unlock_rdev(rdev);
59 }
60
61 mutex_unlock(&cfg80211_mutex);
62
63 return is_all_idle;
64}
65
66static void disconnect_work(struct work_struct *work)
67{
68 if (!cfg80211_is_all_idle())
69 return;
70
71 regulatory_hint_disconnect();
72}
73
74static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020075
76static int cfg80211_conn_scan(struct wireless_dev *wdev)
77{
Johannes Berg79c97e92009-07-07 03:56:12 +020078 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020079 struct cfg80211_scan_request *request;
80 int n_channels, err;
81
82 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020083 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +020084 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020085
Johannes Berg79c97e92009-07-07 03:56:12 +020086 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020087 return -EBUSY;
88
89 if (wdev->conn->params.channel) {
90 n_channels = 1;
91 } else {
92 enum ieee80211_band band;
93 n_channels = 0;
94
95 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
96 if (!wdev->wiphy->bands[band])
97 continue;
98 n_channels += wdev->wiphy->bands[band]->n_channels;
99 }
100 }
101 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
102 sizeof(request->channels[0]) * n_channels,
103 GFP_KERNEL);
104 if (!request)
105 return -ENOMEM;
106
Johannes Berg6829c872009-07-02 09:13:27 +0200107 if (wdev->conn->params.channel)
108 request->channels[0] = wdev->conn->params.channel;
109 else {
110 int i = 0, j;
111 enum ieee80211_band band;
112
113 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
114 if (!wdev->wiphy->bands[band])
115 continue;
116 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
117 i++, j++)
118 request->channels[i] =
119 &wdev->wiphy->bands[band]->channels[j];
120 }
121 }
122 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200123 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200124 request->n_ssids = 1;
125
126 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
127 wdev->conn->params.ssid_len);
128 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
129
Johannes Berg463d0182009-07-14 00:33:35 +0200130 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200131 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200132
Johannes Berg79c97e92009-07-07 03:56:12 +0200133 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200134
Johannes Berg79c97e92009-07-07 03:56:12 +0200135 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200136 if (!err) {
137 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200138 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200139 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200140 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200142 kfree(request);
143 }
144 return err;
145}
146
147static int cfg80211_conn_do_work(struct wireless_dev *wdev)
148{
Johannes Berg79c97e92009-07-07 03:56:12 +0200149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200150 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200151 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200153
Johannes Berg667503dd2009-07-07 03:56:11 +0200154 ASSERT_WDEV_LOCK(wdev);
155
Johannes Berg6829c872009-07-02 09:13:27 +0200156 if (!wdev->conn)
157 return 0;
158
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 params = &wdev->conn->params;
160
Johannes Berg6829c872009-07-02 09:13:27 +0200161 switch (wdev->conn->state) {
162 case CFG80211_CONN_SCAN_AGAIN:
163 return cfg80211_conn_scan(wdev);
164 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200165 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200166 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200167 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200168 params->channel, params->auth_type,
169 params->bssid,
170 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200171 NULL, 0,
172 params->key, params->key_len,
173 params->key_idx);
Johannes Berg6829c872009-07-02 09:13:27 +0200174 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200175 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200176 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200177 if (wdev->conn->prev_bssid_valid)
178 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200179 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200180 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200181 prev_bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200182 params->ssid, params->ssid_len,
183 params->ie, params->ie_len,
184 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200187 NULL, 0,
188 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200189 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900190 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
191 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
192 NULL, 0,
193 WLAN_REASON_DEAUTH_LEAVING);
194 /* return an error so that we call __cfg80211_connect_result() */
195 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200196 default:
197 return 0;
198 }
199}
200
201void cfg80211_conn_work(struct work_struct *work)
202{
Johannes Berg79c97e92009-07-07 03:56:12 +0200203 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200204 container_of(work, struct cfg80211_registered_device, conn_work);
205 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100206 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200207
208 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200209 cfg80211_lock_rdev(rdev);
210 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200211
Johannes Berg79c97e92009-07-07 03:56:12 +0200212 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200213 wdev_lock(wdev);
214 if (!netif_running(wdev->netdev)) {
215 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200216 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200217 }
218 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
219 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200220 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200221 }
Johannes Berg7400f422009-10-31 07:40:37 +0100222 if (wdev->conn->params.bssid) {
223 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
224 bssid = bssid_buf;
225 }
Johannes Berg6829c872009-07-02 09:13:27 +0200226 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200227 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900228 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200229 NULL, 0, NULL, 0,
230 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200231 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 }
234
Johannes Berg79c97e92009-07-07 03:56:12 +0200235 mutex_unlock(&rdev->devlist_mtx);
236 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200237 rtnl_unlock();
238}
239
Johannes Bergbbac31f2009-09-16 09:04:26 -0700240static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200241{
Johannes Berg79c97e92009-07-07 03:56:12 +0200242 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200243 struct cfg80211_bss *bss;
244 u16 capa = WLAN_CAPABILITY_ESS;
245
Johannes Berg667503dd2009-07-07 03:56:11 +0200246 ASSERT_WDEV_LOCK(wdev);
247
Johannes Berg6829c872009-07-02 09:13:27 +0200248 if (wdev->conn->params.privacy)
249 capa |= WLAN_CAPABILITY_PRIVACY;
250
251 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
252 wdev->conn->params.ssid,
253 wdev->conn->params.ssid_len,
254 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
255 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200256 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700257 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200258
259 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
260 wdev->conn->params.bssid = wdev->conn->bssid;
261 wdev->conn->params.channel = bss->channel;
262 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200263 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200264
Johannes Bergbbac31f2009-09-16 09:04:26 -0700265 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200266}
267
Johannes Berg667503dd2009-07-07 03:56:11 +0200268static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200269{
270 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200271 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700272 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200273
Johannes Berg667503dd2009-07-07 03:56:11 +0200274 ASSERT_WDEV_LOCK(wdev);
275
Johannes Berg6829c872009-07-02 09:13:27 +0200276 if (wdev->sme_state != CFG80211_SME_CONNECTING)
277 return;
278
Zhu Yid4b1a682009-07-16 17:34:14 +0800279 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200280 return;
281
282 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
283 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
284 return;
285
Johannes Bergbbac31f2009-09-16 09:04:26 -0700286 bss = cfg80211_get_conn_bss(wdev);
287 if (bss) {
288 cfg80211_put_bss(bss);
289 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200290 /* not found */
291 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200292 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200293 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200294 __cfg80211_connect_result(
295 wdev->netdev,
296 wdev->conn->params.bssid,
297 NULL, 0, NULL, 0,
298 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200299 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200300 }
301}
302
Johannes Berg667503dd2009-07-07 03:56:11 +0200303void cfg80211_sme_scan_done(struct net_device *dev)
304{
305 struct wireless_dev *wdev = dev->ieee80211_ptr;
306
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200307 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200308 wdev_lock(wdev);
309 __cfg80211_sme_scan_done(dev);
310 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200311 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200312}
313
314void cfg80211_sme_rx_auth(struct net_device *dev,
315 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200316{
317 struct wireless_dev *wdev = dev->ieee80211_ptr;
318 struct wiphy *wiphy = wdev->wiphy;
319 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
320 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
321 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
322
Johannes Berg667503dd2009-07-07 03:56:11 +0200323 ASSERT_WDEV_LOCK(wdev);
324
Johannes Berg6829c872009-07-02 09:13:27 +0200325 /* should only RX auth frames when connecting */
326 if (wdev->sme_state != CFG80211_SME_CONNECTING)
327 return;
328
329 if (WARN_ON(!wdev->conn))
330 return;
331
332 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
333 wdev->conn->auto_auth &&
334 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
335 /* select automatically between only open, shared, leap */
336 switch (wdev->conn->params.auth_type) {
337 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200338 if (wdev->connect_keys)
339 wdev->conn->params.auth_type =
340 NL80211_AUTHTYPE_SHARED_KEY;
341 else
342 wdev->conn->params.auth_type =
343 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200344 break;
345 case NL80211_AUTHTYPE_SHARED_KEY:
346 wdev->conn->params.auth_type =
347 NL80211_AUTHTYPE_NETWORK_EAP;
348 break;
349 default:
350 /* huh? */
351 wdev->conn->params.auth_type =
352 NL80211_AUTHTYPE_OPEN_SYSTEM;
353 break;
354 }
355 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
356 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200357 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200358 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200359 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200360 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200361 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
362 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
363 schedule_work(&rdev->conn_work);
364 }
365}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200366
Johannes Bergf401a6f2009-08-07 14:51:05 +0200367bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
368{
369 struct wiphy *wiphy = wdev->wiphy;
370 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
371
372 if (WARN_ON(!wdev->conn))
373 return false;
374
375 if (!wdev->conn->prev_bssid_valid)
376 return false;
377
378 /*
379 * Some stupid APs don't accept reassoc, so we
380 * need to fall back to trying regular assoc.
381 */
382 wdev->conn->prev_bssid_valid = false;
383 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
384 schedule_work(&rdev->conn_work);
385
386 return true;
387}
388
Johannes Berg7d930bc2009-10-20 15:08:53 +0900389void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
390{
391 struct wiphy *wiphy = wdev->wiphy;
392 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
393
394 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
395 schedule_work(&rdev->conn_work);
396}
397
Johannes Berg667503dd2009-07-07 03:56:11 +0200398void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
399 const u8 *req_ie, size_t req_ie_len,
400 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200401 u16 status, bool wextev,
402 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200403{
404 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700405 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200406#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200407 union iwreq_data wrqu;
408#endif
409
Johannes Berg667503dd2009-07-07 03:56:11 +0200410 ASSERT_WDEV_LOCK(wdev);
411
Samuel Ortizb23aa672009-07-01 21:26:54 +0200412 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
413 return;
414
Johannes Bergf7969962009-08-21 12:23:49 +0200415 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200416 return;
417
418 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200419 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200420 resp_ie, resp_ie_len,
421 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200422
Johannes Berg3d23e342009-09-29 23:27:28 +0200423#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200424 if (wextev) {
425 if (req_ie && status == WLAN_STATUS_SUCCESS) {
426 memset(&wrqu, 0, sizeof(wrqu));
427 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800428 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200429 }
430
431 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
432 memset(&wrqu, 0, sizeof(wrqu));
433 wrqu.data.length = resp_ie_len;
434 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
435 }
436
437 memset(&wrqu, 0, sizeof(wrqu));
438 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200439 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200440 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200441 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
442 wdev->wext.prev_bssid_valid = true;
443 }
Johannes Berge45cd822009-07-02 09:58:04 +0200444 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
445 }
446#endif
447
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200448 if (wdev->current_bss) {
449 cfg80211_unhold_bss(wdev->current_bss);
450 cfg80211_put_bss(&wdev->current_bss->pub);
451 wdev->current_bss = NULL;
452 }
453
Johannes Berg19957bb2009-07-02 17:20:43 +0200454 if (wdev->conn)
455 wdev->conn->state = CFG80211_CONN_IDLE;
456
Johannes Bergfffd0932009-07-08 14:22:54 +0200457 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200458 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100459 if (wdev->conn)
460 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200461 kfree(wdev->conn);
462 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200463 kfree(wdev->connect_keys);
464 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200465 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200466 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200467 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200468
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200469 if (!bss)
470 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
471 wdev->ssid, wdev->ssid_len,
472 WLAN_CAPABILITY_ESS,
473 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200474
475 if (WARN_ON(!bss))
476 return;
477
478 cfg80211_hold_bss(bss_from_pub(bss));
479 wdev->current_bss = bss_from_pub(bss);
480
Johannes Bergfffd0932009-07-08 14:22:54 +0200481 wdev->sme_state = CFG80211_SME_CONNECTED;
482 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700483
484 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
485
486 if (!country_ie)
487 return;
488
489 /*
490 * ieee80211_bss_get_ie() ensures we can access:
491 * - country_ie + 2, the start of the country ie data, and
492 * - and country_ie[1] which is the IE length
493 */
494 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500495 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700496 country_ie + 2,
497 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200498}
Johannes Bergf2129352009-07-01 21:26:56 +0200499
500void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
501 const u8 *req_ie, size_t req_ie_len,
502 const u8 *resp_ie, size_t resp_ie_len,
503 u16 status, gfp_t gfp)
504{
Johannes Berg667503dd2009-07-07 03:56:11 +0200505 struct wireless_dev *wdev = dev->ieee80211_ptr;
506 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
507 struct cfg80211_event *ev;
508 unsigned long flags;
509
Johannes Bergf7969962009-08-21 12:23:49 +0200510 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
511
Johannes Berg667503dd2009-07-07 03:56:11 +0200512 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
513 if (!ev)
514 return;
515
516 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800517 if (bssid)
518 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200519 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
520 ev->cr.req_ie_len = req_ie_len;
521 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
522 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
523 ev->cr.resp_ie_len = resp_ie_len;
524 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
525 ev->cr.status = status;
526
527 spin_lock_irqsave(&wdev->event_lock, flags);
528 list_add_tail(&ev->list, &wdev->event_list);
529 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100530 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200531}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200532EXPORT_SYMBOL(cfg80211_connect_result);
533
Johannes Berg667503dd2009-07-07 03:56:11 +0200534void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
535 const u8 *req_ie, size_t req_ie_len,
536 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200537{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200538 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200539#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200540 union iwreq_data wrqu;
541#endif
542
Johannes Berg667503dd2009-07-07 03:56:11 +0200543 ASSERT_WDEV_LOCK(wdev);
544
Samuel Ortizb23aa672009-07-01 21:26:54 +0200545 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
546 return;
547
Johannes Bergf7969962009-08-21 12:23:49 +0200548 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200549 return;
550
551 /* internal error -- how did we get to CONNECTED w/o BSS? */
552 if (WARN_ON(!wdev->current_bss)) {
553 return;
554 }
555
556 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200557 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200558 wdev->current_bss = NULL;
559
560 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
561 wdev->ssid, wdev->ssid_len,
562 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
563
564 if (WARN_ON(!bss))
565 return;
566
Johannes Berg19957bb2009-07-02 17:20:43 +0200567 cfg80211_hold_bss(bss_from_pub(bss));
568 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200569
Johannes Berg667503dd2009-07-07 03:56:11 +0200570 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
571 req_ie, req_ie_len, resp_ie, resp_ie_len,
572 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200573
Johannes Berg3d23e342009-09-29 23:27:28 +0200574#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200575 if (req_ie) {
576 memset(&wrqu, 0, sizeof(wrqu));
577 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800578 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200579 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200580 }
581
582 if (resp_ie) {
583 memset(&wrqu, 0, sizeof(wrqu));
584 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200585 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
586 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200587 }
588
589 memset(&wrqu, 0, sizeof(wrqu));
590 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
591 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200592 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
593 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200594 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595#endif
596}
Johannes Berg667503dd2009-07-07 03:56:11 +0200597
598void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
599 const u8 *req_ie, size_t req_ie_len,
600 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
601{
602 struct wireless_dev *wdev = dev->ieee80211_ptr;
603 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
604 struct cfg80211_event *ev;
605 unsigned long flags;
606
Johannes Bergf7969962009-08-21 12:23:49 +0200607 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
608
Johannes Berg667503dd2009-07-07 03:56:11 +0200609 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
610 if (!ev)
611 return;
612
613 ev->type = EVENT_ROAMED;
614 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
615 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
616 ev->rm.req_ie_len = req_ie_len;
617 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
618 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
619 ev->rm.resp_ie_len = resp_ie_len;
620 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
621
622 spin_lock_irqsave(&wdev->event_lock, flags);
623 list_add_tail(&ev->list, &wdev->event_list);
624 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100625 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200626}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200627EXPORT_SYMBOL(cfg80211_roamed);
628
Johannes Berg667503dd2009-07-07 03:56:11 +0200629void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200630 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200631{
632 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200633 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
634 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200635#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200636 union iwreq_data wrqu;
637#endif
638
Johannes Berg667503dd2009-07-07 03:56:11 +0200639 ASSERT_WDEV_LOCK(wdev);
640
Samuel Ortizb23aa672009-07-01 21:26:54 +0200641 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
642 return;
643
Johannes Bergf7969962009-08-21 12:23:49 +0200644 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200645 return;
646
647 if (wdev->current_bss) {
648 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200649 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200650 }
651
652 wdev->current_bss = NULL;
653 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200654 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655
Johannes Berg6829c872009-07-02 09:13:27 +0200656 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200657 const u8 *bssid;
658 int ret;
659
Johannes Berg6829c872009-07-02 09:13:27 +0200660 kfree(wdev->conn->ie);
661 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200662 kfree(wdev->conn);
663 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200664
665 /*
666 * If this disconnect was due to a disassoc, we
667 * we might still have an auth BSS around. For
668 * the userspace SME that's currently expected,
669 * but for the kernel SME (nl80211 CONNECT or
670 * wireless extensions) we want to clear up all
671 * state.
672 */
673 for (i = 0; i < MAX_AUTH_BSSES; i++) {
674 if (!wdev->auth_bsses[i])
675 continue;
676 bssid = wdev->auth_bsses[i]->pub.bssid;
677 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
678 WLAN_REASON_DEAUTH_LEAVING);
679 WARN(ret, "deauth failed: %d\n", ret);
680 }
Johannes Berg6829c872009-07-02 09:13:27 +0200681 }
682
Johannes Bergfffd0932009-07-08 14:22:54 +0200683 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
684
685 /*
686 * Delete all the keys ... pairwise keys can't really
687 * exist any more anyway, but default keys might.
688 */
689 if (rdev->ops->del_key)
690 for (i = 0; i < 6; i++)
691 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200692
Johannes Berg3d23e342009-09-29 23:27:28 +0200693#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200694 memset(&wrqu, 0, sizeof(wrqu));
695 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
696 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800697 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200698#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500699
700 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200701}
702
703void cfg80211_disconnected(struct net_device *dev, u16 reason,
704 u8 *ie, size_t ie_len, gfp_t gfp)
705{
Johannes Berg667503dd2009-07-07 03:56:11 +0200706 struct wireless_dev *wdev = dev->ieee80211_ptr;
707 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
708 struct cfg80211_event *ev;
709 unsigned long flags;
710
Johannes Bergf7969962009-08-21 12:23:49 +0200711 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
712
Johannes Berg667503dd2009-07-07 03:56:11 +0200713 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
714 if (!ev)
715 return;
716
717 ev->type = EVENT_DISCONNECTED;
718 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
719 ev->dc.ie_len = ie_len;
720 memcpy((void *)ev->dc.ie, ie, ie_len);
721 ev->dc.reason = reason;
722
723 spin_lock_irqsave(&wdev->event_lock, flags);
724 list_add_tail(&ev->list, &wdev->event_list);
725 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100726 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200727}
728EXPORT_SYMBOL(cfg80211_disconnected);
729
Johannes Berg667503dd2009-07-07 03:56:11 +0200730int __cfg80211_connect(struct cfg80211_registered_device *rdev,
731 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200732 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200733 struct cfg80211_cached_keys *connkeys,
734 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200735{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200737 struct ieee80211_channel *chan;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700738 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200739 int err;
740
741 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200742
743 if (wdev->sme_state != CFG80211_SME_IDLE)
744 return -EALREADY;
745
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200746 chan = rdev_fixed_channel(rdev, wdev);
747 if (chan && chan != connect->channel)
748 return -EBUSY;
749
Johannes Bergfffd0932009-07-08 14:22:54 +0200750 if (WARN_ON(wdev->connect_keys)) {
751 kfree(wdev->connect_keys);
752 wdev->connect_keys = NULL;
753 }
754
755 if (connkeys && connkeys->def >= 0) {
756 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200757 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200758
759 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200760 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200762 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
763 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200764 connect->key_idx = idx;
765 connect->key = connkeys->params[idx].key;
766 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200767
768 /*
769 * If ciphers are not set (e.g. when going through
770 * iwconfig), we have to set them appropriately here.
771 */
772 if (connect->crypto.cipher_group == 0)
773 connect->crypto.cipher_group = cipher;
774
775 if (connect->crypto.n_ciphers_pairwise == 0) {
776 connect->crypto.n_ciphers_pairwise = 1;
777 connect->crypto.ciphers_pairwise[0] = cipher;
778 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200779 }
780 }
781
Samuel Ortizb23aa672009-07-01 21:26:54 +0200782 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200783 if (!rdev->ops->auth || !rdev->ops->assoc)
784 return -EOPNOTSUPP;
785
Johannes Berg19957bb2009-07-02 17:20:43 +0200786 if (WARN_ON(wdev->conn))
787 return -EINPROGRESS;
788
789 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
790 if (!wdev->conn)
791 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200792
793 /*
794 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
795 */
796 memcpy(&wdev->conn->params, connect, sizeof(*connect));
797 if (connect->bssid) {
798 wdev->conn->params.bssid = wdev->conn->bssid;
799 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
800 }
801
802 if (connect->ie) {
803 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
804 GFP_KERNEL);
805 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200806 if (!wdev->conn->ie) {
807 kfree(wdev->conn);
808 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200809 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200810 }
Johannes Berg6829c872009-07-02 09:13:27 +0200811 }
812
813 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
814 wdev->conn->auto_auth = true;
815 /* start with open system ... should mostly work */
816 wdev->conn->params.auth_type =
817 NL80211_AUTHTYPE_OPEN_SYSTEM;
818 } else {
819 wdev->conn->auto_auth = false;
820 }
821
822 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
823 wdev->ssid_len = connect->ssid_len;
824 wdev->conn->params.ssid = wdev->ssid;
825 wdev->conn->params.ssid_len = connect->ssid_len;
826
Johannes Berg8bb89482009-09-26 14:42:53 +0200827 /* see if we have the bss already */
828 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200829
830 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200831 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200832
Johannes Bergf401a6f2009-08-07 14:51:05 +0200833 if (prev_bssid) {
834 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
835 wdev->conn->prev_bssid_valid = true;
836 }
837
Johannes Bergbbac31f2009-09-16 09:04:26 -0700838 /* we're good if we have a matching bss struct */
839 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200840 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
841 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700842 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200843 } else {
844 /* otherwise we'll need to scan for the AP first */
845 err = cfg80211_conn_scan(wdev);
846 /*
847 * If we can't scan right now, then we need to scan again
848 * after the current scan finished, since the parameters
849 * changed (unless we find a good AP anyway).
850 */
851 if (err == -EBUSY) {
852 err = 0;
853 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
854 }
855 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200856 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100857 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200858 kfree(wdev->conn);
859 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200860 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200861 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200862 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200863 }
Johannes Berg6829c872009-07-02 09:13:27 +0200864
865 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200866 } else {
867 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200868 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200869 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
870 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200871 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200872 wdev->sme_state = CFG80211_SME_IDLE;
873 return err;
874 }
Johannes Berg6829c872009-07-02 09:13:27 +0200875
876 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
877 wdev->ssid_len = connect->ssid_len;
878
879 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200880 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200881}
882
Johannes Berg667503dd2009-07-07 03:56:11 +0200883int cfg80211_connect(struct cfg80211_registered_device *rdev,
884 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200885 struct cfg80211_connect_params *connect,
886 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200887{
888 int err;
889
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200890 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200891 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200892 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200893 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200894 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200895
896 return err;
897}
898
899int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
900 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901{
Johannes Berg6829c872009-07-02 09:13:27 +0200902 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903 int err;
904
Johannes Berg667503dd2009-07-07 03:56:11 +0200905 ASSERT_WDEV_LOCK(wdev);
906
Johannes Berg6829c872009-07-02 09:13:27 +0200907 if (wdev->sme_state == CFG80211_SME_IDLE)
908 return -EINVAL;
909
Johannes Bergfffd0932009-07-08 14:22:54 +0200910 kfree(wdev->connect_keys);
911 wdev->connect_keys = NULL;
912
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200914 if (!rdev->ops->deauth)
915 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200916
Johannes Berg19957bb2009-07-02 17:20:43 +0200917 /* was it connected by userspace SME? */
918 if (!wdev->conn) {
919 cfg80211_mlme_down(rdev, dev);
920 return 0;
921 }
Johannes Berg6829c872009-07-02 09:13:27 +0200922
923 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
924 (wdev->conn->state == CFG80211_CONN_SCANNING ||
925 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
926 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100927 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200928 kfree(wdev->conn);
929 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200930 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200931 return 0;
932 }
933
Johannes Berg6829c872009-07-02 09:13:27 +0200934 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200935 err = __cfg80211_mlme_deauth(rdev, dev,
936 wdev->conn->params.bssid,
937 NULL, 0, reason);
Johannes Berg6829c872009-07-02 09:13:27 +0200938 if (err)
939 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200940 } else {
941 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
942 if (err)
943 return err;
944 }
945
Johannes Berg6829c872009-07-02 09:13:27 +0200946 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200947 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200948 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200949 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
950 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200951 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200952
953 return 0;
954}
Johannes Berg19957bb2009-07-02 17:20:43 +0200955
Johannes Berg667503dd2009-07-07 03:56:11 +0200956int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
957 struct net_device *dev,
958 u16 reason, bool wextev)
959{
960 int err;
961
962 wdev_lock(dev->ieee80211_ptr);
963 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
964 wdev_unlock(dev->ieee80211_ptr);
965
966 return err;
967}
968
Johannes Berg19957bb2009-07-02 17:20:43 +0200969void cfg80211_sme_disassoc(struct net_device *dev, int idx)
970{
971 struct wireless_dev *wdev = dev->ieee80211_ptr;
972 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
973 u8 bssid[ETH_ALEN];
974
Johannes Berg667503dd2009-07-07 03:56:11 +0200975 ASSERT_WDEV_LOCK(wdev);
976
Johannes Berg19957bb2009-07-02 17:20:43 +0200977 if (!wdev->conn)
978 return;
979
980 if (wdev->conn->state == CFG80211_CONN_IDLE)
981 return;
982
983 /*
984 * Ok, so the association was made by this SME -- we don't
985 * want it any more so deauthenticate too.
986 */
987
988 if (!wdev->auth_bsses[idx])
989 return;
990
991 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200992 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
993 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200994 /* whatever -- assume gone anyway */
995 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
996 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
997 wdev->auth_bsses[idx] = NULL;
998 }
999}