blob: 09d994d192ffa1c78b963933daad73e4240dfe4b [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"
Hila Gonene35e4d22012-06-27 17:19:42 +030019#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020020
Johannes Berg6829c872009-07-02 09:13:27 +020021struct cfg80211_conn {
22 struct cfg80211_connect_params params;
23 /* these are sub-states of the _CONNECTING sme_state */
24 enum {
25 CFG80211_CONN_IDLE,
26 CFG80211_CONN_SCANNING,
27 CFG80211_CONN_SCAN_AGAIN,
28 CFG80211_CONN_AUTHENTICATE_NEXT,
29 CFG80211_CONN_AUTHENTICATING,
30 CFG80211_CONN_ASSOCIATE_NEXT,
31 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090032 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020033 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020034 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020035 u8 *ie;
36 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020037 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020038};
39
John W. Linville20925fe2010-07-20 12:32:52 -040040static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050041{
42 struct cfg80211_registered_device *rdev;
43 struct wireless_dev *wdev;
44 bool is_all_idle = true;
45
46 mutex_lock(&cfg80211_mutex);
47
48 /*
49 * All devices must be idle as otherwise if you are actively
50 * scanning some new beacon hints could be learned and would
51 * count as new regulatory hints.
52 */
53 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
54 cfg80211_lock_rdev(rdev);
Johannes Berg89a54e42012-06-15 14:33:17 +020055 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050056 wdev_lock(wdev);
57 if (wdev->sme_state != CFG80211_SME_IDLE)
58 is_all_idle = false;
59 wdev_unlock(wdev);
60 }
61 cfg80211_unlock_rdev(rdev);
62 }
63
64 mutex_unlock(&cfg80211_mutex);
65
66 return is_all_idle;
67}
68
69static void disconnect_work(struct work_struct *work)
70{
71 if (!cfg80211_is_all_idle())
72 return;
73
74 regulatory_hint_disconnect();
75}
76
77static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020078
79static int cfg80211_conn_scan(struct wireless_dev *wdev)
80{
Johannes Berg79c97e92009-07-07 03:56:12 +020081 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020082 struct cfg80211_scan_request *request;
83 int n_channels, err;
84
85 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020086 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +020087 ASSERT_WDEV_LOCK(wdev);
Johannes Bergf9f47522013-03-19 15:04:07 +010088 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +020089
Johannes Berg79c97e92009-07-07 03:56:12 +020090 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020091 return -EBUSY;
92
93 if (wdev->conn->params.channel) {
94 n_channels = 1;
95 } else {
96 enum ieee80211_band band;
97 n_channels = 0;
98
99 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
100 if (!wdev->wiphy->bands[band])
101 continue;
102 n_channels += wdev->wiphy->bands[band]->n_channels;
103 }
104 }
105 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
106 sizeof(request->channels[0]) * n_channels,
107 GFP_KERNEL);
108 if (!request)
109 return -ENOMEM;
110
Johannes Berg6829c872009-07-02 09:13:27 +0200111 if (wdev->conn->params.channel)
112 request->channels[0] = wdev->conn->params.channel;
113 else {
114 int i = 0, j;
115 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530116 struct ieee80211_supported_band *bands;
117 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200118
119 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530120 bands = wdev->wiphy->bands[band];
121 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200122 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530123 for (j = 0; j < bands->n_channels; j++) {
124 channel = &bands->channels[j];
125 if (channel->flags & IEEE80211_CHAN_DISABLED)
126 continue;
127 request->channels[i++] = channel;
128 }
129 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200130 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530131 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200132 }
133 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200134 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200135 request->n_ssids = 1;
136
137 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
138 wdev->conn->params.ssid_len);
139 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
140
Johannes Bergfd014282012-06-18 19:17:03 +0200141 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700143 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200144
Johannes Berg79c97e92009-07-07 03:56:12 +0200145 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200146
Hila Gonene35e4d22012-06-27 17:19:42 +0300147 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200148 if (!err) {
149 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200150 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200151 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200152 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200153 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200154 kfree(request);
155 }
156 return err;
157}
158
159static int cfg80211_conn_do_work(struct wireless_dev *wdev)
160{
Johannes Berg79c97e92009-07-07 03:56:12 +0200161 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200162 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200163 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200164 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200165
Johannes Berg667503dd2009-07-07 03:56:11 +0200166 ASSERT_WDEV_LOCK(wdev);
167
Johannes Berg6829c872009-07-02 09:13:27 +0200168 if (!wdev->conn)
169 return 0;
170
Johannes Berg19957bb2009-07-02 17:20:43 +0200171 params = &wdev->conn->params;
172
Johannes Berg6829c872009-07-02 09:13:27 +0200173 switch (wdev->conn->state) {
174 case CFG80211_CONN_SCAN_AGAIN:
175 return cfg80211_conn_scan(wdev);
176 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200177 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200178 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200179 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200180 params->channel, params->auth_type,
181 params->bssid,
182 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200183 NULL, 0,
184 params->key, params->key_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300185 params->key_idx, NULL, 0);
Johannes Berg6829c872009-07-02 09:13:27 +0200186 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200187 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200188 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200189 if (wdev->conn->prev_bssid_valid)
190 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200191 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200192 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200193 prev_bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200194 params->ssid, params->ssid_len,
195 params->ie, params->ie_len,
Jouni Malinencee00a92013-01-15 17:15:57 +0200196 params->mfp != NL80211_MFP_NO,
197 &params->crypto,
Ben Greear7e7c8922011-11-18 11:31:59 -0800198 params->flags, &params->ht_capa,
199 &params->ht_capa_mask);
Johannes Berg19957bb2009-07-02 17:20:43 +0200200 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200201 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200202 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300203 WLAN_REASON_DEAUTH_LEAVING,
204 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200205 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900206 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
207 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
208 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300209 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900210 /* return an error so that we call __cfg80211_connect_result() */
211 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200212 default:
213 return 0;
214 }
215}
216
217void cfg80211_conn_work(struct work_struct *work)
218{
Johannes Berg79c97e92009-07-07 03:56:12 +0200219 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200220 container_of(work, struct cfg80211_registered_device, conn_work);
221 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100222 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200223
224 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200225 cfg80211_lock_rdev(rdev);
226 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200227
Johannes Berg89a54e42012-06-15 14:33:17 +0200228 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200229 wdev_lock(wdev);
230 if (!netif_running(wdev->netdev)) {
231 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200232 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200233 }
234 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
235 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200236 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200237 }
Johannes Berg7400f422009-10-31 07:40:37 +0100238 if (wdev->conn->params.bssid) {
239 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
240 bssid = bssid_buf;
241 }
Johannes Berg6829c872009-07-02 09:13:27 +0200242 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200243 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900244 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200245 NULL, 0, NULL, 0,
246 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200247 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200248 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200249 }
250
Johannes Berg79c97e92009-07-07 03:56:12 +0200251 mutex_unlock(&rdev->devlist_mtx);
252 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200253 rtnl_unlock();
254}
255
Johannes Bergbbac31f2009-09-16 09:04:26 -0700256static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200257{
Johannes Berg79c97e92009-07-07 03:56:12 +0200258 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 struct cfg80211_bss *bss;
260 u16 capa = WLAN_CAPABILITY_ESS;
261
Johannes Berg667503dd2009-07-07 03:56:11 +0200262 ASSERT_WDEV_LOCK(wdev);
263
Johannes Berg6829c872009-07-02 09:13:27 +0200264 if (wdev->conn->params.privacy)
265 capa |= WLAN_CAPABILITY_PRIVACY;
266
Jouni Malinened9d0102011-05-16 19:40:15 +0300267 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
268 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200269 wdev->conn->params.ssid,
270 wdev->conn->params.ssid_len,
271 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
272 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200273 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700274 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200275
276 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
277 wdev->conn->params.bssid = wdev->conn->bssid;
278 wdev->conn->params.channel = bss->channel;
279 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200280 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200281
Johannes Bergbbac31f2009-09-16 09:04:26 -0700282 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200283}
284
Johannes Berg667503dd2009-07-07 03:56:11 +0200285static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200286{
287 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200288 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700289 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200290
Johannes Berg667503dd2009-07-07 03:56:11 +0200291 ASSERT_WDEV_LOCK(wdev);
292
Johannes Berg6829c872009-07-02 09:13:27 +0200293 if (wdev->sme_state != CFG80211_SME_CONNECTING)
294 return;
295
Zhu Yid4b1a682009-07-16 17:34:14 +0800296 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200297 return;
298
299 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
300 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
301 return;
302
Johannes Bergbbac31f2009-09-16 09:04:26 -0700303 bss = cfg80211_get_conn_bss(wdev);
304 if (bss) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100305 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700306 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200307 /* not found */
308 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200309 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200310 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200311 __cfg80211_connect_result(
312 wdev->netdev,
313 wdev->conn->params.bssid,
314 NULL, 0, NULL, 0,
315 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200316 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200317 }
318}
319
Johannes Berg667503dd2009-07-07 03:56:11 +0200320void cfg80211_sme_scan_done(struct net_device *dev)
321{
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
323
324 wdev_lock(wdev);
325 __cfg80211_sme_scan_done(dev);
326 wdev_unlock(wdev);
327}
328
329void cfg80211_sme_rx_auth(struct net_device *dev,
330 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200331{
332 struct wireless_dev *wdev = dev->ieee80211_ptr;
333 struct wiphy *wiphy = wdev->wiphy;
334 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
335 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
336 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
337
Johannes Berg667503dd2009-07-07 03:56:11 +0200338 ASSERT_WDEV_LOCK(wdev);
339
Johannes Berg6829c872009-07-02 09:13:27 +0200340 /* should only RX auth frames when connecting */
341 if (wdev->sme_state != CFG80211_SME_CONNECTING)
342 return;
343
344 if (WARN_ON(!wdev->conn))
345 return;
346
347 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
348 wdev->conn->auto_auth &&
349 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
350 /* select automatically between only open, shared, leap */
351 switch (wdev->conn->params.auth_type) {
352 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200353 if (wdev->connect_keys)
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_SHARED_KEY;
356 else
357 wdev->conn->params.auth_type =
358 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200359 break;
360 case NL80211_AUTHTYPE_SHARED_KEY:
361 wdev->conn->params.auth_type =
362 NL80211_AUTHTYPE_NETWORK_EAP;
363 break;
364 default:
365 /* huh? */
366 wdev->conn->params.auth_type =
367 NL80211_AUTHTYPE_OPEN_SYSTEM;
368 break;
369 }
370 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
371 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200372 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200373 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200374 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200375 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200376 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
377 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
378 schedule_work(&rdev->conn_work);
379 }
380}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200381
Johannes Bergf401a6f2009-08-07 14:51:05 +0200382bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
383{
384 struct wiphy *wiphy = wdev->wiphy;
385 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
386
387 if (WARN_ON(!wdev->conn))
388 return false;
389
390 if (!wdev->conn->prev_bssid_valid)
391 return false;
392
393 /*
394 * Some stupid APs don't accept reassoc, so we
395 * need to fall back to trying regular assoc.
396 */
397 wdev->conn->prev_bssid_valid = false;
398 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
399 schedule_work(&rdev->conn_work);
400
401 return true;
402}
403
Johannes Berg7d930bc2009-10-20 15:08:53 +0900404void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
405{
406 struct wiphy *wiphy = wdev->wiphy;
407 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
408
409 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
410 schedule_work(&rdev->conn_work);
411}
412
Johannes Berg667503dd2009-07-07 03:56:11 +0200413void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
414 const u8 *req_ie, size_t req_ie_len,
415 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200416 u16 status, bool wextev,
417 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200418{
419 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100420 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200421#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200422 union iwreq_data wrqu;
423#endif
424
Johannes Berg667503dd2009-07-07 03:56:11 +0200425 ASSERT_WDEV_LOCK(wdev);
426
Johannes Berg074ac8d2010-09-16 14:58:22 +0200427 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
428 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200429 return;
430
Johannes Bergf7969962009-08-21 12:23:49 +0200431 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200432 return;
433
434 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200435 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200436 resp_ie, resp_ie_len,
437 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200438
Johannes Berg3d23e342009-09-29 23:27:28 +0200439#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200440 if (wextev) {
441 if (req_ie && status == WLAN_STATUS_SUCCESS) {
442 memset(&wrqu, 0, sizeof(wrqu));
443 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800444 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200445 }
446
447 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
448 memset(&wrqu, 0, sizeof(wrqu));
449 wrqu.data.length = resp_ie_len;
450 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
451 }
452
453 memset(&wrqu, 0, sizeof(wrqu));
454 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200455 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200456 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200457 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
458 wdev->wext.prev_bssid_valid = true;
459 }
Johannes Berge45cd822009-07-02 09:58:04 +0200460 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
461 }
462#endif
463
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200464 if (wdev->current_bss) {
465 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100466 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200467 wdev->current_bss = NULL;
468 }
469
Johannes Berg19957bb2009-07-02 17:20:43 +0200470 if (wdev->conn)
471 wdev->conn->state = CFG80211_CONN_IDLE;
472
Johannes Bergfffd0932009-07-08 14:22:54 +0200473 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200474 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100475 if (wdev->conn)
476 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200477 kfree(wdev->conn);
478 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200479 kfree(wdev->connect_keys);
480 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200481 wdev->ssid_len = 0;
Johannes Berg5b112d32013-02-01 01:49:58 +0100482 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200483 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200484 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200485
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200486 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300487 bss = cfg80211_get_bss(wdev->wiphy,
488 wdev->conn ? wdev->conn->params.channel :
489 NULL,
490 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200491 wdev->ssid, wdev->ssid_len,
492 WLAN_CAPABILITY_ESS,
493 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200494
495 if (WARN_ON(!bss))
496 return;
497
498 cfg80211_hold_bss(bss_from_pub(bss));
499 wdev->current_bss = bss_from_pub(bss);
500
Johannes Bergfffd0932009-07-08 14:22:54 +0200501 wdev->sme_state = CFG80211_SME_CONNECTED;
502 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700503
Johannes Berg9caf0362012-11-29 01:25:20 +0100504 rcu_read_lock();
505 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
506 if (!country_ie) {
507 rcu_read_unlock();
508 return;
509 }
510
511 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
512 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700513
514 if (!country_ie)
515 return;
516
517 /*
518 * ieee80211_bss_get_ie() ensures we can access:
519 * - country_ie + 2, the start of the country ie data, and
520 * - and country_ie[1] which is the IE length
521 */
Johannes Berg1a919312012-12-03 17:21:11 +0100522 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
523 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100524 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200525}
Johannes Bergf2129352009-07-01 21:26:56 +0200526
527void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
528 const u8 *req_ie, size_t req_ie_len,
529 const u8 *resp_ie, size_t resp_ie_len,
530 u16 status, gfp_t gfp)
531{
Johannes Berg667503dd2009-07-07 03:56:11 +0200532 struct wireless_dev *wdev = dev->ieee80211_ptr;
533 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
534 struct cfg80211_event *ev;
535 unsigned long flags;
536
Johannes Bergf7969962009-08-21 12:23:49 +0200537 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
538
Johannes Berg667503dd2009-07-07 03:56:11 +0200539 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
540 if (!ev)
541 return;
542
543 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800544 if (bssid)
545 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700546 if (req_ie_len) {
547 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
548 ev->cr.req_ie_len = req_ie_len;
549 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
550 }
551 if (resp_ie_len) {
552 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
553 ev->cr.resp_ie_len = resp_ie_len;
554 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
555 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200556 ev->cr.status = status;
557
558 spin_lock_irqsave(&wdev->event_lock, flags);
559 list_add_tail(&ev->list, &wdev->event_list);
560 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100561 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200562}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200563EXPORT_SYMBOL(cfg80211_connect_result);
564
Jouni Malinened9d0102011-05-16 19:40:15 +0300565void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530566 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200567 const u8 *req_ie, size_t req_ie_len,
568 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200569{
Johannes Berg3d23e342009-09-29 23:27:28 +0200570#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200571 union iwreq_data wrqu;
572#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200573 ASSERT_WDEV_LOCK(wdev);
574
Johannes Berg074ac8d2010-09-16 14:58:22 +0200575 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
576 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530577 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200578
Johannes Bergf7969962009-08-21 12:23:49 +0200579 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530580 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200581
582 /* internal error -- how did we get to CONNECTED w/o BSS? */
583 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530584 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200585 }
586
587 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100588 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200589 wdev->current_bss = NULL;
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
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530594 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200595 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 Berg667503dd2009-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 Berg667503dd2009-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;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530615 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
616 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200617 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200618 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200619#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530620
621 return;
622out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100623 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200624}
Johannes Berg667503dd2009-07-07 03:56:11 +0200625
Jouni Malinened9d0102011-05-16 19:40:15 +0300626void cfg80211_roamed(struct net_device *dev,
627 struct ieee80211_channel *channel,
628 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200629 const u8 *req_ie, size_t req_ie_len,
630 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
631{
632 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530633 struct cfg80211_bss *bss;
634
635 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
636
637 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
638 wdev->ssid_len, WLAN_CAPABILITY_ESS,
639 WLAN_CAPABILITY_ESS);
640 if (WARN_ON(!bss))
641 return;
642
643 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
644 resp_ie_len, gfp);
645}
646EXPORT_SYMBOL(cfg80211_roamed);
647
648void cfg80211_roamed_bss(struct net_device *dev,
649 struct cfg80211_bss *bss, const u8 *req_ie,
650 size_t req_ie_len, const u8 *resp_ie,
651 size_t resp_ie_len, gfp_t gfp)
652{
653 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200654 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
655 struct cfg80211_event *ev;
656 unsigned long flags;
657
Johannes Bergf7969962009-08-21 12:23:49 +0200658 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
659
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530660 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200661 return;
662
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530663 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
664 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100665 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530666 return;
667 }
668
Johannes Berg667503dd2009-07-07 03:56:11 +0200669 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200670 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
671 ev->rm.req_ie_len = req_ie_len;
672 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
673 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
674 ev->rm.resp_ie_len = resp_ie_len;
675 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530676 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200677
678 spin_lock_irqsave(&wdev->event_lock, flags);
679 list_add_tail(&ev->list, &wdev->event_list);
680 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100681 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200682}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530683EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200684
Johannes Berg667503dd2009-07-07 03:56:11 +0200685void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200686 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200687{
688 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200689 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
690 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200691#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200692 union iwreq_data wrqu;
693#endif
694
Johannes Berg667503dd2009-07-07 03:56:11 +0200695 ASSERT_WDEV_LOCK(wdev);
696
Johannes Berg074ac8d2010-09-16 14:58:22 +0200697 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
698 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200699 return;
700
Johannes Bergf7969962009-08-21 12:23:49 +0200701 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200702 return;
703
704 if (wdev->current_bss) {
705 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100706 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200707 }
708
709 wdev->current_bss = NULL;
710 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200711 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200712
Johannes Berg6829c872009-07-02 09:13:27 +0200713 if (wdev->conn) {
714 kfree(wdev->conn->ie);
715 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200716 kfree(wdev->conn);
717 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200718 }
719
Johannes Bergfffd0932009-07-08 14:22:54 +0200720 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
721
722 /*
723 * Delete all the keys ... pairwise keys can't really
724 * exist any more anyway, but default keys might.
725 */
726 if (rdev->ops->del_key)
727 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300728 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200729
Johannes Berg3d23e342009-09-29 23:27:28 +0200730#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200731 memset(&wrqu, 0, sizeof(wrqu));
732 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
733 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800734 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200735#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500736
737 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200738}
739
740void cfg80211_disconnected(struct net_device *dev, u16 reason,
741 u8 *ie, size_t ie_len, gfp_t gfp)
742{
Johannes Berg667503dd2009-07-07 03:56:11 +0200743 struct wireless_dev *wdev = dev->ieee80211_ptr;
744 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
745 struct cfg80211_event *ev;
746 unsigned long flags;
747
Johannes Bergf7969962009-08-21 12:23:49 +0200748 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
749
Johannes Berg667503dd2009-07-07 03:56:11 +0200750 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
751 if (!ev)
752 return;
753
754 ev->type = EVENT_DISCONNECTED;
755 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
756 ev->dc.ie_len = ie_len;
757 memcpy((void *)ev->dc.ie, ie, ie_len);
758 ev->dc.reason = reason;
759
760 spin_lock_irqsave(&wdev->event_lock, flags);
761 list_add_tail(&ev->list, &wdev->event_list);
762 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100763 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200764}
765EXPORT_SYMBOL(cfg80211_disconnected);
766
Johannes Berg667503dd2009-07-07 03:56:11 +0200767int __cfg80211_connect(struct cfg80211_registered_device *rdev,
768 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200770 struct cfg80211_cached_keys *connkeys,
771 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200772{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700774 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200775 int err;
776
777 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200778
779 if (wdev->sme_state != CFG80211_SME_IDLE)
780 return -EALREADY;
781
Johannes Bergfffd0932009-07-08 14:22:54 +0200782 if (WARN_ON(wdev->connect_keys)) {
783 kfree(wdev->connect_keys);
784 wdev->connect_keys = NULL;
785 }
786
Ben Greear7e7c8922011-11-18 11:31:59 -0800787 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
788 rdev->wiphy.ht_capa_mod_mask);
789
Johannes Bergfffd0932009-07-08 14:22:54 +0200790 if (connkeys && connkeys->def >= 0) {
791 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200792 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200793
794 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200795 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200797 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
798 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200799 connect->key_idx = idx;
800 connect->key = connkeys->params[idx].key;
801 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200802
803 /*
804 * If ciphers are not set (e.g. when going through
805 * iwconfig), we have to set them appropriately here.
806 */
807 if (connect->crypto.cipher_group == 0)
808 connect->crypto.cipher_group = cipher;
809
810 if (connect->crypto.n_ciphers_pairwise == 0) {
811 connect->crypto.n_ciphers_pairwise = 1;
812 connect->crypto.ciphers_pairwise[0] = cipher;
813 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200814 }
815 }
816
Samuel Ortizb23aa672009-07-01 21:26:54 +0200817 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200818 if (!rdev->ops->auth || !rdev->ops->assoc)
819 return -EOPNOTSUPP;
820
Johannes Berg19957bb2009-07-02 17:20:43 +0200821 if (WARN_ON(wdev->conn))
822 return -EINPROGRESS;
823
824 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
825 if (!wdev->conn)
826 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200827
828 /*
829 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
830 */
831 memcpy(&wdev->conn->params, connect, sizeof(*connect));
832 if (connect->bssid) {
833 wdev->conn->params.bssid = wdev->conn->bssid;
834 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
835 }
836
837 if (connect->ie) {
838 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
839 GFP_KERNEL);
840 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200841 if (!wdev->conn->ie) {
842 kfree(wdev->conn);
843 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200844 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200845 }
Johannes Berg6829c872009-07-02 09:13:27 +0200846 }
847
848 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
849 wdev->conn->auto_auth = true;
850 /* start with open system ... should mostly work */
851 wdev->conn->params.auth_type =
852 NL80211_AUTHTYPE_OPEN_SYSTEM;
853 } else {
854 wdev->conn->auto_auth = false;
855 }
856
857 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
858 wdev->ssid_len = connect->ssid_len;
859 wdev->conn->params.ssid = wdev->ssid;
860 wdev->conn->params.ssid_len = connect->ssid_len;
861
Johannes Berg8bb89482009-09-26 14:42:53 +0200862 /* see if we have the bss already */
863 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200864
865 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200866 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200867
Johannes Bergf401a6f2009-08-07 14:51:05 +0200868 if (prev_bssid) {
869 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
870 wdev->conn->prev_bssid_valid = true;
871 }
872
Johannes Bergbbac31f2009-09-16 09:04:26 -0700873 /* we're good if we have a matching bss struct */
874 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200875 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
876 err = cfg80211_conn_do_work(wdev);
Johannes Berg5b112d32013-02-01 01:49:58 +0100877 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200878 } else {
879 /* otherwise we'll need to scan for the AP first */
880 err = cfg80211_conn_scan(wdev);
881 /*
882 * If we can't scan right now, then we need to scan again
883 * after the current scan finished, since the parameters
884 * changed (unless we find a good AP anyway).
885 */
886 if (err == -EBUSY) {
887 err = 0;
888 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
889 }
890 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200891 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100892 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200893 kfree(wdev->conn);
894 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200895 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200896 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200897 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200898 }
Johannes Berg6829c872009-07-02 09:13:27 +0200899
900 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901 } else {
902 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200903 wdev->connect_keys = connkeys;
Hila Gonene35e4d22012-06-27 17:19:42 +0300904 err = rdev_connect(rdev, dev, connect);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200905 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200906 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 wdev->sme_state = CFG80211_SME_IDLE;
908 return err;
909 }
Johannes Berg6829c872009-07-02 09:13:27 +0200910
911 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
912 wdev->ssid_len = connect->ssid_len;
913
914 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200915 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916}
917
Johannes Berg667503dd2009-07-07 03:56:11 +0200918int cfg80211_connect(struct cfg80211_registered_device *rdev,
919 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200920 struct cfg80211_connect_params *connect,
921 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200922{
923 int err;
924
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200925 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100926 /* might request scan - scan_mtx -> wdev_mtx dependency */
927 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200928 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200929 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200930 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergf9f47522013-03-19 15:04:07 +0100931 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200932 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200933
934 return err;
935}
936
937int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
938 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200939{
Johannes Berg6829c872009-07-02 09:13:27 +0200940 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200941 int err;
942
Johannes Berg667503dd2009-07-07 03:56:11 +0200943 ASSERT_WDEV_LOCK(wdev);
944
Johannes Berg6829c872009-07-02 09:13:27 +0200945 if (wdev->sme_state == CFG80211_SME_IDLE)
946 return -EINVAL;
947
Johannes Bergfffd0932009-07-08 14:22:54 +0200948 kfree(wdev->connect_keys);
949 wdev->connect_keys = NULL;
950
Samuel Ortizb23aa672009-07-01 21:26:54 +0200951 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200952 if (!rdev->ops->deauth)
953 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200954
Johannes Berg19957bb2009-07-02 17:20:43 +0200955 /* was it connected by userspace SME? */
956 if (!wdev->conn) {
957 cfg80211_mlme_down(rdev, dev);
958 return 0;
959 }
Johannes Berg6829c872009-07-02 09:13:27 +0200960
961 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
962 (wdev->conn->state == CFG80211_CONN_SCANNING ||
963 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
964 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100965 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200966 kfree(wdev->conn);
967 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200968 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200969 return 0;
970 }
971
Johannes Berg6829c872009-07-02 09:13:27 +0200972 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200973 err = __cfg80211_mlme_deauth(rdev, dev,
974 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300975 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200976 if (err)
977 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200978 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300979 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980 if (err)
981 return err;
982 }
983
Johannes Berg6829c872009-07-02 09:13:27 +0200984 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200985 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200986 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200987 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
988 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200989 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200990
991 return 0;
992}
Johannes Berg19957bb2009-07-02 17:20:43 +0200993
Johannes Berg667503dd2009-07-07 03:56:11 +0200994int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
995 struct net_device *dev,
996 u16 reason, bool wextev)
997{
998 int err;
999
1000 wdev_lock(dev->ieee80211_ptr);
1001 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1002 wdev_unlock(dev->ieee80211_ptr);
1003
1004 return err;
1005}
1006
Johannes Berg95de8172012-01-20 13:55:25 +01001007void cfg80211_sme_disassoc(struct net_device *dev,
1008 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001009{
1010 struct wireless_dev *wdev = dev->ieee80211_ptr;
1011 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1012 u8 bssid[ETH_ALEN];
1013
Johannes Berg667503dd2009-07-07 03:56:11 +02001014 ASSERT_WDEV_LOCK(wdev);
1015
Johannes Berg19957bb2009-07-02 17:20:43 +02001016 if (!wdev->conn)
1017 return;
1018
1019 if (wdev->conn->state == CFG80211_CONN_IDLE)
1020 return;
1021
1022 /*
1023 * Ok, so the association was made by this SME -- we don't
1024 * want it any more so deauthenticate too.
1025 */
1026
Johannes Berg95de8172012-01-20 13:55:25 +01001027 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001028
Johannes Berg95de8172012-01-20 13:55:25 +01001029 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1030 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001031}