blob: 8b5eddfba1e5dfed07b1fb528204c125db2d7331 [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 Bergf62fab72013-02-21 20:09:09 +0100163 struct cfg80211_assoc_request req = {};
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)
Johannes Bergf62fab72013-02-21 20:09:09 +0100190 req.prev_bssid = wdev->conn->prev_bssid;
191 req.ie = params->ie;
192 req.ie_len = params->ie_len;
193 req.use_mfp = params->mfp != NL80211_MFP_NO;
194 req.crypto = params->crypto;
195 req.flags = params->flags;
196 req.ht_capa = params->ht_capa;
197 req.ht_capa_mask = params->ht_capa_mask;
198 req.vht_capa = params->vht_capa;
199 req.vht_capa_mask = params->vht_capa_mask;
200
201 err = __cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
202 params->bssid, params->ssid,
203 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200204 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200205 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200206 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300207 WLAN_REASON_DEAUTH_LEAVING,
208 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200209 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900210 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
211 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
212 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300213 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900214 /* return an error so that we call __cfg80211_connect_result() */
215 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200216 default:
217 return 0;
218 }
219}
220
221void cfg80211_conn_work(struct work_struct *work)
222{
Johannes Berg79c97e92009-07-07 03:56:12 +0200223 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200224 container_of(work, struct cfg80211_registered_device, conn_work);
225 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100226 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200227
228 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200229 cfg80211_lock_rdev(rdev);
230 mutex_lock(&rdev->devlist_mtx);
Artem Savkov90e09702013-03-30 21:20:14 +0400231 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200232
Johannes Berg89a54e42012-06-15 14:33:17 +0200233 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 wdev_lock(wdev);
235 if (!netif_running(wdev->netdev)) {
236 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200237 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200238 }
Johannes Berg217c1572013-03-20 14:05:52 +0100239 if (wdev->sme_state != CFG80211_SME_CONNECTING || !wdev->conn) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200240 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200241 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200242 }
Johannes Berg7400f422009-10-31 07:40:37 +0100243 if (wdev->conn->params.bssid) {
244 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
245 bssid = bssid_buf;
246 }
Johannes Berg6829c872009-07-02 09:13:27 +0200247 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200248 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900249 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200250 NULL, 0, NULL, 0,
251 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200252 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200253 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200254 }
255
Artem Savkov90e09702013-03-30 21:20:14 +0400256 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg79c97e92009-07-07 03:56:12 +0200257 mutex_unlock(&rdev->devlist_mtx);
258 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 rtnl_unlock();
260}
261
Johannes Bergbbac31f2009-09-16 09:04:26 -0700262static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200263{
Johannes Berg79c97e92009-07-07 03:56:12 +0200264 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200265 struct cfg80211_bss *bss;
266 u16 capa = WLAN_CAPABILITY_ESS;
267
Johannes Berg667503dd2009-07-07 03:56:11 +0200268 ASSERT_WDEV_LOCK(wdev);
269
Johannes Berg6829c872009-07-02 09:13:27 +0200270 if (wdev->conn->params.privacy)
271 capa |= WLAN_CAPABILITY_PRIVACY;
272
Jouni Malinened9d0102011-05-16 19:40:15 +0300273 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
274 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200275 wdev->conn->params.ssid,
276 wdev->conn->params.ssid_len,
277 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
278 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200279 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700280 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200281
282 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
283 wdev->conn->params.bssid = wdev->conn->bssid;
284 wdev->conn->params.channel = bss->channel;
285 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200286 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200287
Johannes Bergbbac31f2009-09-16 09:04:26 -0700288 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200289}
290
Johannes Berg667503dd2009-07-07 03:56:11 +0200291static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200292{
293 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200294 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700295 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200296
Johannes Berg667503dd2009-07-07 03:56:11 +0200297 ASSERT_WDEV_LOCK(wdev);
298
Johannes Berg6829c872009-07-02 09:13:27 +0200299 if (wdev->sme_state != CFG80211_SME_CONNECTING)
300 return;
301
Zhu Yid4b1a682009-07-16 17:34:14 +0800302 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200303 return;
304
305 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
306 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
307 return;
308
Johannes Bergbbac31f2009-09-16 09:04:26 -0700309 bss = cfg80211_get_conn_bss(wdev);
310 if (bss) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100311 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700312 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200313 /* not found */
314 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200315 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200316 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200317 __cfg80211_connect_result(
318 wdev->netdev,
319 wdev->conn->params.bssid,
320 NULL, 0, NULL, 0,
321 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200322 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200323 }
324}
325
Johannes Berg667503dd2009-07-07 03:56:11 +0200326void cfg80211_sme_scan_done(struct net_device *dev)
327{
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329
330 wdev_lock(wdev);
331 __cfg80211_sme_scan_done(dev);
332 wdev_unlock(wdev);
333}
334
335void cfg80211_sme_rx_auth(struct net_device *dev,
336 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200337{
338 struct wireless_dev *wdev = dev->ieee80211_ptr;
339 struct wiphy *wiphy = wdev->wiphy;
340 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
341 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
342 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
343
Johannes Berg667503dd2009-07-07 03:56:11 +0200344 ASSERT_WDEV_LOCK(wdev);
345
Johannes Berg6829c872009-07-02 09:13:27 +0200346 /* should only RX auth frames when connecting */
347 if (wdev->sme_state != CFG80211_SME_CONNECTING)
348 return;
349
350 if (WARN_ON(!wdev->conn))
351 return;
352
353 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
354 wdev->conn->auto_auth &&
355 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
356 /* select automatically between only open, shared, leap */
357 switch (wdev->conn->params.auth_type) {
358 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200359 if (wdev->connect_keys)
360 wdev->conn->params.auth_type =
361 NL80211_AUTHTYPE_SHARED_KEY;
362 else
363 wdev->conn->params.auth_type =
364 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200365 break;
366 case NL80211_AUTHTYPE_SHARED_KEY:
367 wdev->conn->params.auth_type =
368 NL80211_AUTHTYPE_NETWORK_EAP;
369 break;
370 default:
371 /* huh? */
372 wdev->conn->params.auth_type =
373 NL80211_AUTHTYPE_OPEN_SYSTEM;
374 break;
375 }
376 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
377 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200378 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200379 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200380 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200381 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200382 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
383 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
384 schedule_work(&rdev->conn_work);
385 }
386}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200387
Johannes Bergf401a6f2009-08-07 14:51:05 +0200388bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
389{
390 struct wiphy *wiphy = wdev->wiphy;
391 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
392
393 if (WARN_ON(!wdev->conn))
394 return false;
395
396 if (!wdev->conn->prev_bssid_valid)
397 return false;
398
399 /*
400 * Some stupid APs don't accept reassoc, so we
401 * need to fall back to trying regular assoc.
402 */
403 wdev->conn->prev_bssid_valid = false;
404 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
405 schedule_work(&rdev->conn_work);
406
407 return true;
408}
409
Johannes Berg7d930bc2009-10-20 15:08:53 +0900410void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
411{
412 struct wiphy *wiphy = wdev->wiphy;
413 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
414
415 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
416 schedule_work(&rdev->conn_work);
417}
418
Johannes Berg667503dd2009-07-07 03:56:11 +0200419void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
420 const u8 *req_ie, size_t req_ie_len,
421 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200422 u16 status, bool wextev,
423 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200424{
425 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100426 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200427#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200428 union iwreq_data wrqu;
429#endif
430
Johannes Berg667503dd2009-07-07 03:56:11 +0200431 ASSERT_WDEV_LOCK(wdev);
432
Johannes Berg074ac8d2010-09-16 14:58:22 +0200433 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
434 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200435 return;
436
Johannes Bergf7969962009-08-21 12:23:49 +0200437 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200438 return;
439
440 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200441 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200442 resp_ie, resp_ie_len,
443 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200444
Johannes Berg3d23e342009-09-29 23:27:28 +0200445#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200446 if (wextev) {
447 if (req_ie && status == WLAN_STATUS_SUCCESS) {
448 memset(&wrqu, 0, sizeof(wrqu));
449 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800450 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200451 }
452
453 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
454 memset(&wrqu, 0, sizeof(wrqu));
455 wrqu.data.length = resp_ie_len;
456 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
457 }
458
459 memset(&wrqu, 0, sizeof(wrqu));
460 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200461 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200462 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200463 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
464 wdev->wext.prev_bssid_valid = true;
465 }
Johannes Berge45cd822009-07-02 09:58:04 +0200466 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
467 }
468#endif
469
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200470 if (wdev->current_bss) {
471 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100472 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200473 wdev->current_bss = NULL;
474 }
475
Johannes Berg19957bb2009-07-02 17:20:43 +0200476 if (wdev->conn)
477 wdev->conn->state = CFG80211_CONN_IDLE;
478
Johannes Bergfffd0932009-07-08 14:22:54 +0200479 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200480 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100481 if (wdev->conn)
482 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200483 kfree(wdev->conn);
484 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200485 kfree(wdev->connect_keys);
486 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200487 wdev->ssid_len = 0;
Johannes Berg5b112d32013-02-01 01:49:58 +0100488 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200489 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200490 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200491
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200492 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300493 bss = cfg80211_get_bss(wdev->wiphy,
494 wdev->conn ? wdev->conn->params.channel :
495 NULL,
496 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200497 wdev->ssid, wdev->ssid_len,
498 WLAN_CAPABILITY_ESS,
499 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200500
501 if (WARN_ON(!bss))
502 return;
503
504 cfg80211_hold_bss(bss_from_pub(bss));
505 wdev->current_bss = bss_from_pub(bss);
506
Johannes Bergfffd0932009-07-08 14:22:54 +0200507 wdev->sme_state = CFG80211_SME_CONNECTED;
508 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700509
Johannes Berg9caf0362012-11-29 01:25:20 +0100510 rcu_read_lock();
511 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
512 if (!country_ie) {
513 rcu_read_unlock();
514 return;
515 }
516
517 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
518 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700519
520 if (!country_ie)
521 return;
522
523 /*
524 * ieee80211_bss_get_ie() ensures we can access:
525 * - country_ie + 2, the start of the country ie data, and
526 * - and country_ie[1] which is the IE length
527 */
Johannes Berg1a919312012-12-03 17:21:11 +0100528 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
529 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100530 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200531}
Johannes Bergf2129352009-07-01 21:26:56 +0200532
533void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
534 const u8 *req_ie, size_t req_ie_len,
535 const u8 *resp_ie, size_t resp_ie_len,
536 u16 status, gfp_t gfp)
537{
Johannes Berg667503dd2009-07-07 03:56:11 +0200538 struct wireless_dev *wdev = dev->ieee80211_ptr;
539 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
540 struct cfg80211_event *ev;
541 unsigned long flags;
542
Johannes Bergf7969962009-08-21 12:23:49 +0200543 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
544
Johannes Berg667503dd2009-07-07 03:56:11 +0200545 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
546 if (!ev)
547 return;
548
549 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800550 if (bssid)
551 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700552 if (req_ie_len) {
553 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
554 ev->cr.req_ie_len = req_ie_len;
555 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
556 }
557 if (resp_ie_len) {
558 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
559 ev->cr.resp_ie_len = resp_ie_len;
560 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
561 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200562 ev->cr.status = status;
563
564 spin_lock_irqsave(&wdev->event_lock, flags);
565 list_add_tail(&ev->list, &wdev->event_list);
566 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100567 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200568}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200569EXPORT_SYMBOL(cfg80211_connect_result);
570
Jouni Malinened9d0102011-05-16 19:40:15 +0300571void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530572 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200573 const u8 *req_ie, size_t req_ie_len,
574 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200575{
Johannes Berg3d23e342009-09-29 23:27:28 +0200576#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200577 union iwreq_data wrqu;
578#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200579 ASSERT_WDEV_LOCK(wdev);
580
Johannes Berg074ac8d2010-09-16 14:58:22 +0200581 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
582 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530583 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200584
Johannes Bergf7969962009-08-21 12:23:49 +0200585 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530586 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200587
588 /* internal error -- how did we get to CONNECTED w/o BSS? */
589 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530590 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591 }
592
593 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100594 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595 wdev->current_bss = NULL;
596
Johannes Berg19957bb2009-07-02 17:20:43 +0200597 cfg80211_hold_bss(bss_from_pub(bss));
598 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200599
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530600 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200601 req_ie, req_ie_len, resp_ie, resp_ie_len,
602 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200603
Johannes Berg3d23e342009-09-29 23:27:28 +0200604#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200605 if (req_ie) {
606 memset(&wrqu, 0, sizeof(wrqu));
607 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800608 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200609 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200610 }
611
612 if (resp_ie) {
613 memset(&wrqu, 0, sizeof(wrqu));
614 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200615 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
616 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200617 }
618
619 memset(&wrqu, 0, sizeof(wrqu));
620 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530621 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
622 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200623 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200624 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200625#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530626
627 return;
628out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100629 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200630}
Johannes Berg667503dd2009-07-07 03:56:11 +0200631
Jouni Malinened9d0102011-05-16 19:40:15 +0300632void cfg80211_roamed(struct net_device *dev,
633 struct ieee80211_channel *channel,
634 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200635 const u8 *req_ie, size_t req_ie_len,
636 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
637{
638 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530639 struct cfg80211_bss *bss;
640
641 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
642
643 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
644 wdev->ssid_len, WLAN_CAPABILITY_ESS,
645 WLAN_CAPABILITY_ESS);
646 if (WARN_ON(!bss))
647 return;
648
649 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
650 resp_ie_len, gfp);
651}
652EXPORT_SYMBOL(cfg80211_roamed);
653
654void cfg80211_roamed_bss(struct net_device *dev,
655 struct cfg80211_bss *bss, const u8 *req_ie,
656 size_t req_ie_len, const u8 *resp_ie,
657 size_t resp_ie_len, gfp_t gfp)
658{
659 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200660 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
661 struct cfg80211_event *ev;
662 unsigned long flags;
663
Johannes Bergf7969962009-08-21 12:23:49 +0200664 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
665
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530666 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200667 return;
668
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530669 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
670 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100671 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530672 return;
673 }
674
Johannes Berg667503dd2009-07-07 03:56:11 +0200675 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200676 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
677 ev->rm.req_ie_len = req_ie_len;
678 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
679 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
680 ev->rm.resp_ie_len = resp_ie_len;
681 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530682 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200683
684 spin_lock_irqsave(&wdev->event_lock, flags);
685 list_add_tail(&ev->list, &wdev->event_list);
686 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100687 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200688}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530689EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200690
Johannes Berg667503dd2009-07-07 03:56:11 +0200691void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200692 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200693{
694 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200695 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
696 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200697#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200698 union iwreq_data wrqu;
699#endif
700
Johannes Berg667503dd2009-07-07 03:56:11 +0200701 ASSERT_WDEV_LOCK(wdev);
702
Johannes Berg074ac8d2010-09-16 14:58:22 +0200703 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
704 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200705 return;
706
Johannes Bergf7969962009-08-21 12:23:49 +0200707 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200708 return;
709
710 if (wdev->current_bss) {
711 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100712 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713 }
714
715 wdev->current_bss = NULL;
716 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200717 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200718
Johannes Berg6829c872009-07-02 09:13:27 +0200719 if (wdev->conn) {
720 kfree(wdev->conn->ie);
721 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200722 kfree(wdev->conn);
723 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200724 }
725
Johannes Bergfffd0932009-07-08 14:22:54 +0200726 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
727
728 /*
729 * Delete all the keys ... pairwise keys can't really
730 * exist any more anyway, but default keys might.
731 */
732 if (rdev->ops->del_key)
733 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300734 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200735
Johannes Berg3d23e342009-09-29 23:27:28 +0200736#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200737 memset(&wrqu, 0, sizeof(wrqu));
738 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
739 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800740 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200741#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500742
743 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744}
745
746void cfg80211_disconnected(struct net_device *dev, u16 reason,
747 u8 *ie, size_t ie_len, gfp_t gfp)
748{
Johannes Berg667503dd2009-07-07 03:56:11 +0200749 struct wireless_dev *wdev = dev->ieee80211_ptr;
750 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
751 struct cfg80211_event *ev;
752 unsigned long flags;
753
Johannes Bergf7969962009-08-21 12:23:49 +0200754 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
755
Johannes Berg667503dd2009-07-07 03:56:11 +0200756 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
757 if (!ev)
758 return;
759
760 ev->type = EVENT_DISCONNECTED;
761 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
762 ev->dc.ie_len = ie_len;
763 memcpy((void *)ev->dc.ie, ie, ie_len);
764 ev->dc.reason = reason;
765
766 spin_lock_irqsave(&wdev->event_lock, flags);
767 list_add_tail(&ev->list, &wdev->event_list);
768 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100769 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200770}
771EXPORT_SYMBOL(cfg80211_disconnected);
772
Johannes Berg667503dd2009-07-07 03:56:11 +0200773int __cfg80211_connect(struct cfg80211_registered_device *rdev,
774 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200775 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200776 struct cfg80211_cached_keys *connkeys,
777 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200778{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200779 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700780 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200781 int err;
782
783 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200784
785 if (wdev->sme_state != CFG80211_SME_IDLE)
786 return -EALREADY;
787
Johannes Bergfffd0932009-07-08 14:22:54 +0200788 if (WARN_ON(wdev->connect_keys)) {
789 kfree(wdev->connect_keys);
790 wdev->connect_keys = NULL;
791 }
792
Ben Greear7e7c8922011-11-18 11:31:59 -0800793 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
794 rdev->wiphy.ht_capa_mod_mask);
795
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 if (connkeys && connkeys->def >= 0) {
797 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200798 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200799
800 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200801 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200802 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200803 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
804 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200805 connect->key_idx = idx;
806 connect->key = connkeys->params[idx].key;
807 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200808
809 /*
810 * If ciphers are not set (e.g. when going through
811 * iwconfig), we have to set them appropriately here.
812 */
813 if (connect->crypto.cipher_group == 0)
814 connect->crypto.cipher_group = cipher;
815
816 if (connect->crypto.n_ciphers_pairwise == 0) {
817 connect->crypto.n_ciphers_pairwise = 1;
818 connect->crypto.ciphers_pairwise[0] = cipher;
819 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200820 }
821 }
822
Samuel Ortizb23aa672009-07-01 21:26:54 +0200823 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200824 if (!rdev->ops->auth || !rdev->ops->assoc)
825 return -EOPNOTSUPP;
826
Johannes Berg19957bb2009-07-02 17:20:43 +0200827 if (WARN_ON(wdev->conn))
828 return -EINPROGRESS;
829
830 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
831 if (!wdev->conn)
832 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200833
834 /*
835 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
836 */
837 memcpy(&wdev->conn->params, connect, sizeof(*connect));
838 if (connect->bssid) {
839 wdev->conn->params.bssid = wdev->conn->bssid;
840 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
841 }
842
843 if (connect->ie) {
844 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
845 GFP_KERNEL);
846 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200847 if (!wdev->conn->ie) {
848 kfree(wdev->conn);
849 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200850 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200851 }
Johannes Berg6829c872009-07-02 09:13:27 +0200852 }
853
854 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
855 wdev->conn->auto_auth = true;
856 /* start with open system ... should mostly work */
857 wdev->conn->params.auth_type =
858 NL80211_AUTHTYPE_OPEN_SYSTEM;
859 } else {
860 wdev->conn->auto_auth = false;
861 }
862
863 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
864 wdev->ssid_len = connect->ssid_len;
865 wdev->conn->params.ssid = wdev->ssid;
866 wdev->conn->params.ssid_len = connect->ssid_len;
867
Johannes Berg8bb89482009-09-26 14:42:53 +0200868 /* see if we have the bss already */
869 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200870
871 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200872 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200873
Johannes Bergf401a6f2009-08-07 14:51:05 +0200874 if (prev_bssid) {
875 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
876 wdev->conn->prev_bssid_valid = true;
877 }
878
Johannes Bergbbac31f2009-09-16 09:04:26 -0700879 /* we're good if we have a matching bss struct */
880 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200881 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
882 err = cfg80211_conn_do_work(wdev);
Johannes Berg5b112d32013-02-01 01:49:58 +0100883 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200884 } else {
885 /* otherwise we'll need to scan for the AP first */
886 err = cfg80211_conn_scan(wdev);
887 /*
888 * If we can't scan right now, then we need to scan again
889 * after the current scan finished, since the parameters
890 * changed (unless we find a good AP anyway).
891 */
892 if (err == -EBUSY) {
893 err = 0;
894 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
895 }
896 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200897 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100898 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200899 kfree(wdev->conn);
900 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200901 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200902 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200903 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200904 }
Johannes Berg6829c872009-07-02 09:13:27 +0200905
906 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 } else {
908 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200909 wdev->connect_keys = connkeys;
Hila Gonene35e4d22012-06-27 17:19:42 +0300910 err = rdev_connect(rdev, dev, connect);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200911 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200912 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913 wdev->sme_state = CFG80211_SME_IDLE;
914 return err;
915 }
Johannes Berg6829c872009-07-02 09:13:27 +0200916
917 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
918 wdev->ssid_len = connect->ssid_len;
919
920 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200922}
923
Johannes Berg667503dd2009-07-07 03:56:11 +0200924int cfg80211_connect(struct cfg80211_registered_device *rdev,
925 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200926 struct cfg80211_connect_params *connect,
927 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200928{
929 int err;
930
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200931 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100932 /* might request scan - scan_mtx -> wdev_mtx dependency */
933 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200934 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200935 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200936 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergf9f47522013-03-19 15:04:07 +0100937 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200938 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200939
940 return err;
941}
942
943int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
944 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200945{
Johannes Berg6829c872009-07-02 09:13:27 +0200946 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947 int err;
948
Johannes Berg667503dd2009-07-07 03:56:11 +0200949 ASSERT_WDEV_LOCK(wdev);
950
Johannes Berg6829c872009-07-02 09:13:27 +0200951 if (wdev->sme_state == CFG80211_SME_IDLE)
952 return -EINVAL;
953
Johannes Bergfffd0932009-07-08 14:22:54 +0200954 kfree(wdev->connect_keys);
955 wdev->connect_keys = NULL;
956
Samuel Ortizb23aa672009-07-01 21:26:54 +0200957 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200958 if (!rdev->ops->deauth)
959 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200960
Johannes Berg19957bb2009-07-02 17:20:43 +0200961 /* was it connected by userspace SME? */
962 if (!wdev->conn) {
963 cfg80211_mlme_down(rdev, dev);
Johannes Berg24308162013-05-14 09:19:53 +0200964 goto disconnect;
Johannes Berg19957bb2009-07-02 17:20:43 +0200965 }
Johannes Berg6829c872009-07-02 09:13:27 +0200966
967 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
968 (wdev->conn->state == CFG80211_CONN_SCANNING ||
969 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
970 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100971 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200972 kfree(wdev->conn);
973 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200974 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200975 return 0;
976 }
977
Johannes Berg6829c872009-07-02 09:13:27 +0200978 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200979 err = __cfg80211_mlme_deauth(rdev, dev,
980 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300981 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200982 if (err)
983 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200984 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300985 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200986 if (err)
987 return err;
988 }
989
Johannes Berg24308162013-05-14 09:19:53 +0200990 disconnect:
Johannes Berg6829c872009-07-02 09:13:27 +0200991 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200992 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200993 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200994 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
995 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200996 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200997
998 return 0;
999}
Johannes Berg19957bb2009-07-02 17:20:43 +02001000
Johannes Berg667503dd2009-07-07 03:56:11 +02001001int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1002 struct net_device *dev,
1003 u16 reason, bool wextev)
1004{
1005 int err;
1006
1007 wdev_lock(dev->ieee80211_ptr);
1008 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1009 wdev_unlock(dev->ieee80211_ptr);
1010
1011 return err;
1012}
1013
Johannes Berg95de8172012-01-20 13:55:25 +01001014void cfg80211_sme_disassoc(struct net_device *dev,
1015 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001016{
1017 struct wireless_dev *wdev = dev->ieee80211_ptr;
1018 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1019 u8 bssid[ETH_ALEN];
1020
Johannes Berg667503dd2009-07-07 03:56:11 +02001021 ASSERT_WDEV_LOCK(wdev);
1022
Johannes Berg19957bb2009-07-02 17:20:43 +02001023 if (!wdev->conn)
1024 return;
1025
1026 if (wdev->conn->state == CFG80211_CONN_IDLE)
1027 return;
1028
1029 /*
1030 * Ok, so the association was made by this SME -- we don't
1031 * want it any more so deauthenticate too.
1032 */
1033
Johannes Berg95de8172012-01-20 13:55:25 +01001034 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001035
Johannes Berg95de8172012-01-20 13:55:25 +01001036 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1037 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001038}