blob: f0c900ce2fb9ee2dd0606fa651c15b078eb497e0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020011#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020012#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020015#include <net/cfg80211.h>
16#include <net/rtnetlink.h>
17#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070018#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020019
Johannes Berg6829c872009-07-02 09:13:27 +020020struct cfg80211_conn {
21 struct cfg80211_connect_params params;
22 /* these are sub-states of the _CONNECTING sme_state */
23 enum {
24 CFG80211_CONN_IDLE,
25 CFG80211_CONN_SCANNING,
26 CFG80211_CONN_SCAN_AGAIN,
27 CFG80211_CONN_AUTHENTICATE_NEXT,
28 CFG80211_CONN_AUTHENTICATING,
29 CFG80211_CONN_ASSOCIATE_NEXT,
30 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090031 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020032 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020033 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020034 u8 *ie;
35 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020036 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020037};
38
John W. Linville20925fe2010-07-20 12:32:52 -040039static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050040{
41 struct cfg80211_registered_device *rdev;
42 struct wireless_dev *wdev;
43 bool is_all_idle = true;
44
45 mutex_lock(&cfg80211_mutex);
46
47 /*
48 * All devices must be idle as otherwise if you are actively
49 * scanning some new beacon hints could be learned and would
50 * count as new regulatory hints.
51 */
52 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53 cfg80211_lock_rdev(rdev);
54 list_for_each_entry(wdev, &rdev->netdev_list, list) {
55 wdev_lock(wdev);
56 if (wdev->sme_state != CFG80211_SME_IDLE)
57 is_all_idle = false;
58 wdev_unlock(wdev);
59 }
60 cfg80211_unlock_rdev(rdev);
61 }
62
63 mutex_unlock(&cfg80211_mutex);
64
65 return is_all_idle;
66}
67
68static void disconnect_work(struct work_struct *work)
69{
70 if (!cfg80211_is_all_idle())
71 return;
72
73 regulatory_hint_disconnect();
74}
75
76static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020077
78static int cfg80211_conn_scan(struct wireless_dev *wdev)
79{
Johannes Berg79c97e92009-07-07 03:56:12 +020080 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020081 struct cfg80211_scan_request *request;
82 int n_channels, err;
83
84 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020085 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020086 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020087
Johannes Berg79c97e92009-07-07 03:56:12 +020088 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020089 return -EBUSY;
90
91 if (wdev->conn->params.channel) {
92 n_channels = 1;
93 } else {
94 enum ieee80211_band band;
95 n_channels = 0;
96
97 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
98 if (!wdev->wiphy->bands[band])
99 continue;
100 n_channels += wdev->wiphy->bands[band]->n_channels;
101 }
102 }
103 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
104 sizeof(request->channels[0]) * n_channels,
105 GFP_KERNEL);
106 if (!request)
107 return -ENOMEM;
108
Johannes Berg6829c872009-07-02 09:13:27 +0200109 if (wdev->conn->params.channel)
110 request->channels[0] = wdev->conn->params.channel;
111 else {
112 int i = 0, j;
113 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530114 struct ieee80211_supported_band *bands;
115 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200116
117 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530118 bands = wdev->wiphy->bands[band];
119 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200120 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530121 for (j = 0; j < bands->n_channels; j++) {
122 channel = &bands->channels[j];
123 if (channel->flags & IEEE80211_CHAN_DISABLED)
124 continue;
125 request->channels[i++] = channel;
126 }
127 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200128 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530129 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200130 }
131 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200132 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200133 request->n_ssids = 1;
134
135 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
136 wdev->conn->params.ssid_len);
137 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
138
Johannes Berg463d0182009-07-14 00:33:35 +0200139 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200140 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200141
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200145 if (!err) {
146 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200147 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200148 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200149 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200151 kfree(request);
152 }
153 return err;
154}
155
156static int cfg80211_conn_do_work(struct wireless_dev *wdev)
157{
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200160 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200162
Johannes Berg667503d2009-07-07 03:56:11 +0200163 ASSERT_WDEV_LOCK(wdev);
164
Johannes Berg6829c872009-07-02 09:13:27 +0200165 if (!wdev->conn)
166 return 0;
167
Johannes Berg19957bb2009-07-02 17:20:43 +0200168 params = &wdev->conn->params;
169
Johannes Berg6829c872009-07-02 09:13:27 +0200170 switch (wdev->conn->state) {
171 case CFG80211_CONN_SCAN_AGAIN:
172 return cfg80211_conn_scan(wdev);
173 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200174 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200175 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200177 params->channel, params->auth_type,
178 params->bssid,
179 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200180 NULL, 0,
181 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300182 params->key_idx, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200183 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200184 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200186 if (wdev->conn->prev_bssid_valid)
187 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200188 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200189 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200190 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200191 params->ssid, params->ssid_len,
192 params->ie, params->ie_len,
Ben Greear7e7c8922011-11-18 11:31:59 -0800193 false, &params->crypto,
194 params->flags, &params->ht_capa,
195 &params->ht_capa_mask);
Johannes Berg19957bb2009-07-02 17:20:43 +0200196 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200197 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200198 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300199 WLAN_REASON_DEAUTH_LEAVING,
200 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200201 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900202 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
203 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
204 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300205 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900206 /* return an error so that we call __cfg80211_connect_result() */
207 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200208 default:
209 return 0;
210 }
211}
212
213void cfg80211_conn_work(struct work_struct *work)
214{
Johannes Berg79c97e92009-07-07 03:56:12 +0200215 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200216 container_of(work, struct cfg80211_registered_device, conn_work);
217 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100218 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200219
220 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200221 cfg80211_lock_rdev(rdev);
222 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200223
Johannes Berg79c97e92009-07-07 03:56:12 +0200224 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200225 wdev_lock(wdev);
226 if (!netif_running(wdev->netdev)) {
227 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200228 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200229 }
230 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
231 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200232 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200233 }
Johannes Berg7400f422009-10-31 07:40:37 +0100234 if (wdev->conn->params.bssid) {
235 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
236 bssid = bssid_buf;
237 }
Johannes Berg6829c872009-07-02 09:13:27 +0200238 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200239 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900240 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200241 NULL, 0, NULL, 0,
242 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200243 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200244 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200245 }
246
Johannes Berg79c97e92009-07-07 03:56:12 +0200247 mutex_unlock(&rdev->devlist_mtx);
248 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200249 rtnl_unlock();
250}
251
Johannes Bergbbac31f2009-09-16 09:04:26 -0700252static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200253{
Johannes Berg79c97e92009-07-07 03:56:12 +0200254 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200255 struct cfg80211_bss *bss;
256 u16 capa = WLAN_CAPABILITY_ESS;
257
Johannes Berg667503d2009-07-07 03:56:11 +0200258 ASSERT_WDEV_LOCK(wdev);
259
Johannes Berg6829c872009-07-02 09:13:27 +0200260 if (wdev->conn->params.privacy)
261 capa |= WLAN_CAPABILITY_PRIVACY;
262
Jouni Malinened9d0102011-05-16 19:40:15 +0300263 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
264 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200265 wdev->conn->params.ssid,
266 wdev->conn->params.ssid_len,
267 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
268 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200269 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700270 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200271
272 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
273 wdev->conn->params.bssid = wdev->conn->bssid;
274 wdev->conn->params.channel = bss->channel;
275 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200276 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200277
Johannes Bergbbac31f2009-09-16 09:04:26 -0700278 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200279}
280
Johannes Berg667503d2009-07-07 03:56:11 +0200281static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200282{
283 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200284 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700285 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200286
Johannes Berg667503d2009-07-07 03:56:11 +0200287 ASSERT_WDEV_LOCK(wdev);
288
Johannes Berg6829c872009-07-02 09:13:27 +0200289 if (wdev->sme_state != CFG80211_SME_CONNECTING)
290 return;
291
Zhu Yid4b1a682009-07-16 17:34:14 +0800292 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200293 return;
294
295 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
296 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
297 return;
298
Johannes Bergbbac31f2009-09-16 09:04:26 -0700299 bss = cfg80211_get_conn_bss(wdev);
300 if (bss) {
301 cfg80211_put_bss(bss);
302 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200303 /* not found */
304 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200305 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200306 else
Johannes Berg667503d2009-07-07 03:56:11 +0200307 __cfg80211_connect_result(
308 wdev->netdev,
309 wdev->conn->params.bssid,
310 NULL, 0, NULL, 0,
311 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200312 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200313 }
314}
315
Johannes Berg667503d2009-07-07 03:56:11 +0200316void cfg80211_sme_scan_done(struct net_device *dev)
317{
318 struct wireless_dev *wdev = dev->ieee80211_ptr;
319
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200320 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200321 wdev_lock(wdev);
322 __cfg80211_sme_scan_done(dev);
323 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200324 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200325}
326
327void cfg80211_sme_rx_auth(struct net_device *dev,
328 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200329{
330 struct wireless_dev *wdev = dev->ieee80211_ptr;
331 struct wiphy *wiphy = wdev->wiphy;
332 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
333 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
334 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
335
Johannes Berg667503d2009-07-07 03:56:11 +0200336 ASSERT_WDEV_LOCK(wdev);
337
Johannes Berg6829c872009-07-02 09:13:27 +0200338 /* should only RX auth frames when connecting */
339 if (wdev->sme_state != CFG80211_SME_CONNECTING)
340 return;
341
342 if (WARN_ON(!wdev->conn))
343 return;
344
345 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
346 wdev->conn->auto_auth &&
347 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
348 /* select automatically between only open, shared, leap */
349 switch (wdev->conn->params.auth_type) {
350 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200351 if (wdev->connect_keys)
352 wdev->conn->params.auth_type =
353 NL80211_AUTHTYPE_SHARED_KEY;
354 else
355 wdev->conn->params.auth_type =
356 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200357 break;
358 case NL80211_AUTHTYPE_SHARED_KEY:
359 wdev->conn->params.auth_type =
360 NL80211_AUTHTYPE_NETWORK_EAP;
361 break;
362 default:
363 /* huh? */
364 wdev->conn->params.auth_type =
365 NL80211_AUTHTYPE_OPEN_SYSTEM;
366 break;
367 }
368 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
369 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200370 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200371 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200372 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200373 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200374 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
375 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
376 schedule_work(&rdev->conn_work);
377 }
378}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200379
Johannes Bergf401a6f2009-08-07 14:51:05 +0200380bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
381{
382 struct wiphy *wiphy = wdev->wiphy;
383 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
384
385 if (WARN_ON(!wdev->conn))
386 return false;
387
388 if (!wdev->conn->prev_bssid_valid)
389 return false;
390
391 /*
392 * Some stupid APs don't accept reassoc, so we
393 * need to fall back to trying regular assoc.
394 */
395 wdev->conn->prev_bssid_valid = false;
396 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
397 schedule_work(&rdev->conn_work);
398
399 return true;
400}
401
Johannes Berg7d930bc2009-10-20 15:08:53 +0900402void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
403{
404 struct wiphy *wiphy = wdev->wiphy;
405 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
406
407 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
408 schedule_work(&rdev->conn_work);
409}
410
Johannes Berg667503d2009-07-07 03:56:11 +0200411void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
412 const u8 *req_ie, size_t req_ie_len,
413 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200414 u16 status, bool wextev,
415 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200416{
417 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700418 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200419#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200420 union iwreq_data wrqu;
421#endif
422
Johannes Berg667503d2009-07-07 03:56:11 +0200423 ASSERT_WDEV_LOCK(wdev);
424
Johannes Berg074ac8d2010-09-16 14:58:22 +0200425 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
426 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200427 return;
428
Johannes Bergf7969962009-08-21 12:23:49 +0200429 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200430 return;
431
432 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200433 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200434 resp_ie, resp_ie_len,
435 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200436
Johannes Berg3d23e342009-09-29 23:27:28 +0200437#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200438 if (wextev) {
439 if (req_ie && status == WLAN_STATUS_SUCCESS) {
440 memset(&wrqu, 0, sizeof(wrqu));
441 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800442 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200443 }
444
445 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
446 memset(&wrqu, 0, sizeof(wrqu));
447 wrqu.data.length = resp_ie_len;
448 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
449 }
450
451 memset(&wrqu, 0, sizeof(wrqu));
452 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200453 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200454 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200455 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
456 wdev->wext.prev_bssid_valid = true;
457 }
Johannes Berge45cd822009-07-02 09:58:04 +0200458 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
459 }
460#endif
461
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200462 if (wdev->current_bss) {
463 cfg80211_unhold_bss(wdev->current_bss);
464 cfg80211_put_bss(&wdev->current_bss->pub);
465 wdev->current_bss = NULL;
466 }
467
Johannes Berg19957bb2009-07-02 17:20:43 +0200468 if (wdev->conn)
469 wdev->conn->state = CFG80211_CONN_IDLE;
470
Johannes Bergfffd0932009-07-08 14:22:54 +0200471 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200472 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100473 if (wdev->conn)
474 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200475 kfree(wdev->conn);
476 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200477 kfree(wdev->connect_keys);
478 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200479 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200480 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200481 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200482
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200483 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300484 bss = cfg80211_get_bss(wdev->wiphy,
485 wdev->conn ? wdev->conn->params.channel :
486 NULL,
487 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200488 wdev->ssid, wdev->ssid_len,
489 WLAN_CAPABILITY_ESS,
490 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200491
492 if (WARN_ON(!bss))
493 return;
494
495 cfg80211_hold_bss(bss_from_pub(bss));
496 wdev->current_bss = bss_from_pub(bss);
497
Johannes Bergfffd0932009-07-08 14:22:54 +0200498 wdev->sme_state = CFG80211_SME_CONNECTED;
499 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700500
501 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
502
503 if (!country_ie)
504 return;
505
506 /*
507 * ieee80211_bss_get_ie() ensures we can access:
508 * - country_ie + 2, the start of the country ie data, and
509 * - and country_ie[1] which is the IE length
510 */
511 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500512 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700513 country_ie + 2,
514 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200515}
Johannes Bergf2129352009-07-01 21:26:56 +0200516
517void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
518 const u8 *req_ie, size_t req_ie_len,
519 const u8 *resp_ie, size_t resp_ie_len,
520 u16 status, gfp_t gfp)
521{
Johannes Berg667503d2009-07-07 03:56:11 +0200522 struct wireless_dev *wdev = dev->ieee80211_ptr;
523 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
524 struct cfg80211_event *ev;
525 unsigned long flags;
526
Johannes Bergf7969962009-08-21 12:23:49 +0200527 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
528
Johannes Berg667503d2009-07-07 03:56:11 +0200529 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
530 if (!ev)
531 return;
532
533 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800534 if (bssid)
535 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700536 if (req_ie_len) {
537 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
538 ev->cr.req_ie_len = req_ie_len;
539 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
540 }
541 if (resp_ie_len) {
542 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
543 ev->cr.resp_ie_len = resp_ie_len;
544 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
545 }
Johannes Berg667503d2009-07-07 03:56:11 +0200546 ev->cr.status = status;
547
548 spin_lock_irqsave(&wdev->event_lock, flags);
549 list_add_tail(&ev->list, &wdev->event_list);
550 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100551 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200552}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200553EXPORT_SYMBOL(cfg80211_connect_result);
554
Jouni Malinened9d0102011-05-16 19:40:15 +0300555void __cfg80211_roamed(struct wireless_dev *wdev,
556 struct ieee80211_channel *channel,
557 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200558 const u8 *req_ie, size_t req_ie_len,
559 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200560{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200561 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200562#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200563 union iwreq_data wrqu;
564#endif
565
Johannes Berg667503d2009-07-07 03:56:11 +0200566 ASSERT_WDEV_LOCK(wdev);
567
Johannes Berg074ac8d2010-09-16 14:58:22 +0200568 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
569 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570 return;
571
Johannes Bergf7969962009-08-21 12:23:49 +0200572 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200573 return;
574
575 /* internal error -- how did we get to CONNECTED w/o BSS? */
576 if (WARN_ON(!wdev->current_bss)) {
577 return;
578 }
579
580 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200581 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200582 wdev->current_bss = NULL;
583
Jouni Malinened9d0102011-05-16 19:40:15 +0300584 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
Samuel Ortizb23aa672009-07-01 21:26:54 +0200585 wdev->ssid, wdev->ssid_len,
586 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
587
588 if (WARN_ON(!bss))
589 return;
590
Johannes Berg19957bb2009-07-02 17:20:43 +0200591 cfg80211_hold_bss(bss_from_pub(bss));
592 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593
Johannes Berg667503d2009-07-07 03:56:11 +0200594 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
595 req_ie, req_ie_len, resp_ie, resp_ie_len,
596 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200597
Johannes Berg3d23e342009-09-29 23:27:28 +0200598#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200599 if (req_ie) {
600 memset(&wrqu, 0, sizeof(wrqu));
601 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800602 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200603 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200604 }
605
606 if (resp_ie) {
607 memset(&wrqu, 0, sizeof(wrqu));
608 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200609 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
610 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200611 }
612
613 memset(&wrqu, 0, sizeof(wrqu));
614 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
615 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200616 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
617 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200618 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200619#endif
620}
Johannes Berg667503d2009-07-07 03:56:11 +0200621
Jouni Malinened9d0102011-05-16 19:40:15 +0300622void cfg80211_roamed(struct net_device *dev,
623 struct ieee80211_channel *channel,
624 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200625 const u8 *req_ie, size_t req_ie_len,
626 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
627{
628 struct wireless_dev *wdev = dev->ieee80211_ptr;
629 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
630 struct cfg80211_event *ev;
631 unsigned long flags;
632
Johannes Bergf7969962009-08-21 12:23:49 +0200633 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
634
Johannes Berg667503d2009-07-07 03:56:11 +0200635 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
636 if (!ev)
637 return;
638
639 ev->type = EVENT_ROAMED;
Jouni Malinened9d0102011-05-16 19:40:15 +0300640 ev->rm.channel = channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200641 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
642 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
643 ev->rm.req_ie_len = req_ie_len;
644 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
645 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
646 ev->rm.resp_ie_len = resp_ie_len;
647 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
648
649 spin_lock_irqsave(&wdev->event_lock, flags);
650 list_add_tail(&ev->list, &wdev->event_list);
651 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100652 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200653}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200654EXPORT_SYMBOL(cfg80211_roamed);
655
Johannes Berg667503d2009-07-07 03:56:11 +0200656void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200657 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200658{
659 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200660 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
661 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200662#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200663 union iwreq_data wrqu;
664#endif
665
Johannes Berg667503d2009-07-07 03:56:11 +0200666 ASSERT_WDEV_LOCK(wdev);
667
Johannes Berg074ac8d2010-09-16 14:58:22 +0200668 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
669 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200670 return;
671
Johannes Bergf7969962009-08-21 12:23:49 +0200672 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200673 return;
674
675 if (wdev->current_bss) {
676 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200677 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200678 }
679
680 wdev->current_bss = NULL;
681 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200682 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200683
Johannes Berg6829c872009-07-02 09:13:27 +0200684 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200685 const u8 *bssid;
686 int ret;
687
Johannes Berg6829c872009-07-02 09:13:27 +0200688 kfree(wdev->conn->ie);
689 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200690 kfree(wdev->conn);
691 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200692
693 /*
694 * If this disconnect was due to a disassoc, we
695 * we might still have an auth BSS around. For
696 * the userspace SME that's currently expected,
697 * but for the kernel SME (nl80211 CONNECT or
698 * wireless extensions) we want to clear up all
699 * state.
700 */
701 for (i = 0; i < MAX_AUTH_BSSES; i++) {
702 if (!wdev->auth_bsses[i])
703 continue;
704 bssid = wdev->auth_bsses[i]->pub.bssid;
705 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300706 WLAN_REASON_DEAUTH_LEAVING,
707 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200708 WARN(ret, "deauth failed: %d\n", ret);
709 }
Johannes Berg6829c872009-07-02 09:13:27 +0200710 }
711
Johannes Bergfffd0932009-07-08 14:22:54 +0200712 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
713
714 /*
715 * Delete all the keys ... pairwise keys can't really
716 * exist any more anyway, but default keys might.
717 */
718 if (rdev->ops->del_key)
719 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200720 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200721
Johannes Berg3d23e342009-09-29 23:27:28 +0200722#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200723 memset(&wrqu, 0, sizeof(wrqu));
724 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
725 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800726 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200727#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500728
729 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200730}
731
732void cfg80211_disconnected(struct net_device *dev, u16 reason,
733 u8 *ie, size_t ie_len, gfp_t gfp)
734{
Johannes Berg667503d2009-07-07 03:56:11 +0200735 struct wireless_dev *wdev = dev->ieee80211_ptr;
736 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
737 struct cfg80211_event *ev;
738 unsigned long flags;
739
Johannes Bergf7969962009-08-21 12:23:49 +0200740 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
741
Johannes Berg667503d2009-07-07 03:56:11 +0200742 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
743 if (!ev)
744 return;
745
746 ev->type = EVENT_DISCONNECTED;
747 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
748 ev->dc.ie_len = ie_len;
749 memcpy((void *)ev->dc.ie, ie, ie_len);
750 ev->dc.reason = reason;
751
752 spin_lock_irqsave(&wdev->event_lock, flags);
753 list_add_tail(&ev->list, &wdev->event_list);
754 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100755 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200756}
757EXPORT_SYMBOL(cfg80211_disconnected);
758
Johannes Berg667503d2009-07-07 03:56:11 +0200759int __cfg80211_connect(struct cfg80211_registered_device *rdev,
760 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200762 struct cfg80211_cached_keys *connkeys,
763 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200764{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200765 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700766 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200767 int err;
768
769 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200770
771 if (wdev->sme_state != CFG80211_SME_IDLE)
772 return -EALREADY;
773
Johannes Bergfffd0932009-07-08 14:22:54 +0200774 if (WARN_ON(wdev->connect_keys)) {
775 kfree(wdev->connect_keys);
776 wdev->connect_keys = NULL;
777 }
778
Ben Greear7e7c8922011-11-18 11:31:59 -0800779 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
780 rdev->wiphy.ht_capa_mod_mask);
781
Johannes Bergfffd0932009-07-08 14:22:54 +0200782 if (connkeys && connkeys->def >= 0) {
783 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200784 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200785
786 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200787 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200788 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200789 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
790 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200791 connect->key_idx = idx;
792 connect->key = connkeys->params[idx].key;
793 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200794
795 /*
796 * If ciphers are not set (e.g. when going through
797 * iwconfig), we have to set them appropriately here.
798 */
799 if (connect->crypto.cipher_group == 0)
800 connect->crypto.cipher_group = cipher;
801
802 if (connect->crypto.n_ciphers_pairwise == 0) {
803 connect->crypto.n_ciphers_pairwise = 1;
804 connect->crypto.ciphers_pairwise[0] = cipher;
805 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200806 }
807 }
808
Samuel Ortizb23aa672009-07-01 21:26:54 +0200809 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200810 if (!rdev->ops->auth || !rdev->ops->assoc)
811 return -EOPNOTSUPP;
812
Johannes Berg19957bb2009-07-02 17:20:43 +0200813 if (WARN_ON(wdev->conn))
814 return -EINPROGRESS;
815
816 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
817 if (!wdev->conn)
818 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200819
820 /*
821 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
822 */
823 memcpy(&wdev->conn->params, connect, sizeof(*connect));
824 if (connect->bssid) {
825 wdev->conn->params.bssid = wdev->conn->bssid;
826 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
827 }
828
829 if (connect->ie) {
830 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
831 GFP_KERNEL);
832 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200833 if (!wdev->conn->ie) {
834 kfree(wdev->conn);
835 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200836 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200837 }
Johannes Berg6829c872009-07-02 09:13:27 +0200838 }
839
840 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
841 wdev->conn->auto_auth = true;
842 /* start with open system ... should mostly work */
843 wdev->conn->params.auth_type =
844 NL80211_AUTHTYPE_OPEN_SYSTEM;
845 } else {
846 wdev->conn->auto_auth = false;
847 }
848
849 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
850 wdev->ssid_len = connect->ssid_len;
851 wdev->conn->params.ssid = wdev->ssid;
852 wdev->conn->params.ssid_len = connect->ssid_len;
853
Johannes Berg8bb89482009-09-26 14:42:53 +0200854 /* see if we have the bss already */
855 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200856
857 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200858 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200859
Johannes Bergf401a6f2009-08-07 14:51:05 +0200860 if (prev_bssid) {
861 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
862 wdev->conn->prev_bssid_valid = true;
863 }
864
Johannes Bergbbac31f2009-09-16 09:04:26 -0700865 /* we're good if we have a matching bss struct */
866 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200867 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
868 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700869 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200870 } else {
871 /* otherwise we'll need to scan for the AP first */
872 err = cfg80211_conn_scan(wdev);
873 /*
874 * If we can't scan right now, then we need to scan again
875 * after the current scan finished, since the parameters
876 * changed (unless we find a good AP anyway).
877 */
878 if (err == -EBUSY) {
879 err = 0;
880 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
881 }
882 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200883 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100884 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200885 kfree(wdev->conn);
886 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200887 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200888 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200889 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200890 }
Johannes Berg6829c872009-07-02 09:13:27 +0200891
892 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200893 } else {
894 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200895 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200896 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
897 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200898 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200899 wdev->sme_state = CFG80211_SME_IDLE;
900 return err;
901 }
Johannes Berg6829c872009-07-02 09:13:27 +0200902
903 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
904 wdev->ssid_len = connect->ssid_len;
905
906 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200908}
909
Johannes Berg667503d2009-07-07 03:56:11 +0200910int cfg80211_connect(struct cfg80211_registered_device *rdev,
911 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200912 struct cfg80211_connect_params *connect,
913 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200914{
915 int err;
916
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200917 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200918 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200919 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200920 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200921 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200922
923 return err;
924}
925
926int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
927 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200928{
Johannes Berg6829c872009-07-02 09:13:27 +0200929 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200930 int err;
931
Johannes Berg667503d2009-07-07 03:56:11 +0200932 ASSERT_WDEV_LOCK(wdev);
933
Johannes Berg6829c872009-07-02 09:13:27 +0200934 if (wdev->sme_state == CFG80211_SME_IDLE)
935 return -EINVAL;
936
Johannes Bergfffd0932009-07-08 14:22:54 +0200937 kfree(wdev->connect_keys);
938 wdev->connect_keys = NULL;
939
Samuel Ortizb23aa672009-07-01 21:26:54 +0200940 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200941 if (!rdev->ops->deauth)
942 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200943
Johannes Berg19957bb2009-07-02 17:20:43 +0200944 /* was it connected by userspace SME? */
945 if (!wdev->conn) {
946 cfg80211_mlme_down(rdev, dev);
947 return 0;
948 }
Johannes Berg6829c872009-07-02 09:13:27 +0200949
950 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
951 (wdev->conn->state == CFG80211_CONN_SCANNING ||
952 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
953 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100954 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200955 kfree(wdev->conn);
956 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200957 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200958 return 0;
959 }
960
Johannes Berg6829c872009-07-02 09:13:27 +0200961 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200962 err = __cfg80211_mlme_deauth(rdev, dev,
963 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300964 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200965 if (err)
966 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200967 } else {
968 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
969 if (err)
970 return err;
971 }
972
Johannes Berg6829c872009-07-02 09:13:27 +0200973 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200974 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200975 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200976 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
977 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200978 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200979
980 return 0;
981}
Johannes Berg19957bb2009-07-02 17:20:43 +0200982
Johannes Berg667503d2009-07-07 03:56:11 +0200983int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
984 struct net_device *dev,
985 u16 reason, bool wextev)
986{
987 int err;
988
989 wdev_lock(dev->ieee80211_ptr);
990 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
991 wdev_unlock(dev->ieee80211_ptr);
992
993 return err;
994}
995
Johannes Berg19957bb2009-07-02 17:20:43 +0200996void cfg80211_sme_disassoc(struct net_device *dev, int idx)
997{
998 struct wireless_dev *wdev = dev->ieee80211_ptr;
999 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1000 u8 bssid[ETH_ALEN];
1001
Johannes Berg667503d2009-07-07 03:56:11 +02001002 ASSERT_WDEV_LOCK(wdev);
1003
Johannes Berg19957bb2009-07-02 17:20:43 +02001004 if (!wdev->conn)
1005 return;
1006
1007 if (wdev->conn->state == CFG80211_CONN_IDLE)
1008 return;
1009
1010 /*
1011 * Ok, so the association was made by this SME -- we don't
1012 * want it any more so deauthenticate too.
1013 */
1014
1015 if (!wdev->auth_bsses[idx])
1016 return;
1017
1018 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +02001019 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001020 NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
1021 false)) {
Johannes Berg19957bb2009-07-02 17:20:43 +02001022 /* whatever -- assume gone anyway */
1023 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
1024 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1025 wdev->auth_bsses[idx] = NULL;
1026 }
1027}