blob: 88fc9aa54fe0a416ba613864959fb4d362abe503 [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. Linville20925fee2010-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 Berg667503d2009-07-07 03:56:11 +020087 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020088
Johannes Berg79c97e92009-07-07 03:56:12 +020089 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020090 return -EBUSY;
91
92 if (wdev->conn->params.channel) {
93 n_channels = 1;
94 } else {
95 enum ieee80211_band band;
96 n_channels = 0;
97
98 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
99 if (!wdev->wiphy->bands[band])
100 continue;
101 n_channels += wdev->wiphy->bands[band]->n_channels;
102 }
103 }
104 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
105 sizeof(request->channels[0]) * n_channels,
106 GFP_KERNEL);
107 if (!request)
108 return -ENOMEM;
109
Johannes Berg6829c872009-07-02 09:13:27 +0200110 if (wdev->conn->params.channel)
111 request->channels[0] = wdev->conn->params.channel;
112 else {
113 int i = 0, j;
114 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530115 struct ieee80211_supported_band *bands;
116 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200117
118 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530119 bands = wdev->wiphy->bands[band];
120 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200121 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530122 for (j = 0; j < bands->n_channels; j++) {
123 channel = &bands->channels[j];
124 if (channel->flags & IEEE80211_CHAN_DISABLED)
125 continue;
126 request->channels[i++] = channel;
127 }
128 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200129 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530130 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200131 }
132 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200133 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200134 request->n_ssids = 1;
135
136 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
137 wdev->conn->params.ssid_len);
138 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
139
Johannes Bergfd014282012-06-18 19:17:03 +0200140 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700142 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200145
Hila Gonene35e4d22012-06-27 17:19:42 +0300146 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200147 if (!err) {
148 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200149 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200150 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200151 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200152 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200153 kfree(request);
154 }
155 return err;
156}
157
158static int cfg80211_conn_do_work(struct wireless_dev *wdev)
159{
Johannes Berg79c97e92009-07-07 03:56:12 +0200160 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100162 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200164
Johannes Berg667503d2009-07-07 03:56:11 +0200165 ASSERT_WDEV_LOCK(wdev);
166
Johannes Berg6829c872009-07-02 09:13:27 +0200167 if (!wdev->conn)
168 return 0;
169
Johannes Berg19957bb2009-07-02 17:20:43 +0200170 params = &wdev->conn->params;
171
Johannes Berg6829c872009-07-02 09:13:27 +0200172 switch (wdev->conn->state) {
173 case CFG80211_CONN_SCAN_AGAIN:
174 return cfg80211_conn_scan(wdev);
175 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200177 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200178 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200179 params->channel, params->auth_type,
180 params->bssid,
181 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200182 NULL, 0,
183 params->key, params->key_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300184 params->key_idx, NULL, 0);
Johannes Berg6829c872009-07-02 09:13:27 +0200185 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200187 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200188 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100189 req.prev_bssid = wdev->conn->prev_bssid;
190 req.ie = params->ie;
191 req.ie_len = params->ie_len;
192 req.use_mfp = params->mfp != NL80211_MFP_NO;
193 req.crypto = params->crypto;
194 req.flags = params->flags;
195 req.ht_capa = params->ht_capa;
196 req.ht_capa_mask = params->ht_capa_mask;
197 req.vht_capa = params->vht_capa;
198 req.vht_capa_mask = params->vht_capa_mask;
199
200 err = __cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
201 params->bssid, params->ssid,
202 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200203 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200204 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200205 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300206 WLAN_REASON_DEAUTH_LEAVING,
207 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200208 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900209 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
210 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300212 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900213 /* return an error so that we call __cfg80211_connect_result() */
214 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200215 default:
216 return 0;
217 }
218}
219
220void cfg80211_conn_work(struct work_struct *work)
221{
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200223 container_of(work, struct cfg80211_registered_device, conn_work);
224 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100225 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200226
227 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200228 cfg80211_lock_rdev(rdev);
229 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200230
Johannes Berg89a54e42012-06-15 14:33:17 +0200231 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200232 wdev_lock(wdev);
233 if (!netif_running(wdev->netdev)) {
234 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200235 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200236 }
Johannes Berg217c1572013-03-20 14:05:52 +0100237 if (wdev->sme_state != CFG80211_SME_CONNECTING || !wdev->conn) {
Johannes Berg667503d2009-07-07 03:56:11 +0200238 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200239 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200240 }
Johannes Berg7400f422009-10-31 07:40:37 +0100241 if (wdev->conn->params.bssid) {
242 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
243 bssid = bssid_buf;
244 }
Johannes Berg6829c872009-07-02 09:13:27 +0200245 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200246 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900247 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200248 NULL, 0, NULL, 0,
249 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200250 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200251 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200252 }
253
Johannes Berg79c97e92009-07-07 03:56:12 +0200254 mutex_unlock(&rdev->devlist_mtx);
255 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200256 rtnl_unlock();
257}
258
Johannes Bergbbac31f2009-09-16 09:04:26 -0700259static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200260{
Johannes Berg79c97e92009-07-07 03:56:12 +0200261 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200262 struct cfg80211_bss *bss;
263 u16 capa = WLAN_CAPABILITY_ESS;
264
Johannes Berg667503d2009-07-07 03:56:11 +0200265 ASSERT_WDEV_LOCK(wdev);
266
Johannes Berg6829c872009-07-02 09:13:27 +0200267 if (wdev->conn->params.privacy)
268 capa |= WLAN_CAPABILITY_PRIVACY;
269
Jouni Malinened9d0102011-05-16 19:40:15 +0300270 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
271 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200272 wdev->conn->params.ssid,
273 wdev->conn->params.ssid_len,
274 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
275 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200276 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700277 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200278
279 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
280 wdev->conn->params.bssid = wdev->conn->bssid;
281 wdev->conn->params.channel = bss->channel;
282 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200283 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200284
Johannes Bergbbac31f2009-09-16 09:04:26 -0700285 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200286}
287
Johannes Berg667503d2009-07-07 03:56:11 +0200288static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200289{
290 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200291 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700292 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200293
Johannes Berg667503d2009-07-07 03:56:11 +0200294 ASSERT_WDEV_LOCK(wdev);
295
Johannes Berg6829c872009-07-02 09:13:27 +0200296 if (wdev->sme_state != CFG80211_SME_CONNECTING)
297 return;
298
Zhu Yid4b1a682009-07-16 17:34:14 +0800299 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200300 return;
301
302 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
303 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
304 return;
305
Johannes Bergbbac31f2009-09-16 09:04:26 -0700306 bss = cfg80211_get_conn_bss(wdev);
307 if (bss) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100308 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700309 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200310 /* not found */
311 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200312 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200313 else
Johannes Berg667503d2009-07-07 03:56:11 +0200314 __cfg80211_connect_result(
315 wdev->netdev,
316 wdev->conn->params.bssid,
317 NULL, 0, NULL, 0,
318 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200319 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200320 }
321}
322
Johannes Berg667503d2009-07-07 03:56:11 +0200323void cfg80211_sme_scan_done(struct net_device *dev)
324{
325 struct wireless_dev *wdev = dev->ieee80211_ptr;
326
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200327 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200328 wdev_lock(wdev);
329 __cfg80211_sme_scan_done(dev);
330 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200331 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200332}
333
334void cfg80211_sme_rx_auth(struct net_device *dev,
335 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200336{
337 struct wireless_dev *wdev = dev->ieee80211_ptr;
338 struct wiphy *wiphy = wdev->wiphy;
339 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
340 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
341 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
342
Johannes Berg667503d2009-07-07 03:56:11 +0200343 ASSERT_WDEV_LOCK(wdev);
344
Johannes Berg6829c872009-07-02 09:13:27 +0200345 /* should only RX auth frames when connecting */
346 if (wdev->sme_state != CFG80211_SME_CONNECTING)
347 return;
348
349 if (WARN_ON(!wdev->conn))
350 return;
351
352 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
353 wdev->conn->auto_auth &&
354 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
355 /* select automatically between only open, shared, leap */
356 switch (wdev->conn->params.auth_type) {
357 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200358 if (wdev->connect_keys)
359 wdev->conn->params.auth_type =
360 NL80211_AUTHTYPE_SHARED_KEY;
361 else
362 wdev->conn->params.auth_type =
363 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200364 break;
365 case NL80211_AUTHTYPE_SHARED_KEY:
366 wdev->conn->params.auth_type =
367 NL80211_AUTHTYPE_NETWORK_EAP;
368 break;
369 default:
370 /* huh? */
371 wdev->conn->params.auth_type =
372 NL80211_AUTHTYPE_OPEN_SYSTEM;
373 break;
374 }
375 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
376 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200377 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200378 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200379 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200380 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200381 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
382 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
383 schedule_work(&rdev->conn_work);
384 }
385}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200386
Johannes Bergf401a6f2009-08-07 14:51:05 +0200387bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
388{
389 struct wiphy *wiphy = wdev->wiphy;
390 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
391
392 if (WARN_ON(!wdev->conn))
393 return false;
394
395 if (!wdev->conn->prev_bssid_valid)
396 return false;
397
398 /*
399 * Some stupid APs don't accept reassoc, so we
400 * need to fall back to trying regular assoc.
401 */
402 wdev->conn->prev_bssid_valid = false;
403 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
404 schedule_work(&rdev->conn_work);
405
406 return true;
407}
408
Johannes Berg7d930bc2009-10-20 15:08:53 +0900409void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
410{
411 struct wiphy *wiphy = wdev->wiphy;
412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
413
414 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
415 schedule_work(&rdev->conn_work);
416}
417
Johannes Berg667503d2009-07-07 03:56:11 +0200418void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
419 const u8 *req_ie, size_t req_ie_len,
420 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200421 u16 status, bool wextev,
422 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200423{
424 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100425 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200426#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200427 union iwreq_data wrqu;
428#endif
429
Johannes Berg667503d2009-07-07 03:56:11 +0200430 ASSERT_WDEV_LOCK(wdev);
431
Johannes Berg074ac8d2010-09-16 14:58:22 +0200432 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
433 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200434 return;
435
Johannes Bergf7969962009-08-21 12:23:49 +0200436 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200437 return;
438
439 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200440 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200441 resp_ie, resp_ie_len,
442 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200443
Johannes Berg3d23e342009-09-29 23:27:28 +0200444#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200445 if (wextev) {
446 if (req_ie && status == WLAN_STATUS_SUCCESS) {
447 memset(&wrqu, 0, sizeof(wrqu));
448 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800449 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200450 }
451
452 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
453 memset(&wrqu, 0, sizeof(wrqu));
454 wrqu.data.length = resp_ie_len;
455 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
456 }
457
458 memset(&wrqu, 0, sizeof(wrqu));
459 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200460 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200461 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200462 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
463 wdev->wext.prev_bssid_valid = true;
464 }
Johannes Berge45cd822009-07-02 09:58:04 +0200465 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
466 }
467#endif
468
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200469 if (wdev->current_bss) {
470 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100471 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200472 wdev->current_bss = NULL;
473 }
474
Johannes Berg19957bb2009-07-02 17:20:43 +0200475 if (wdev->conn)
476 wdev->conn->state = CFG80211_CONN_IDLE;
477
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200479 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100480 if (wdev->conn)
481 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200482 kfree(wdev->conn);
483 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200484 kfree(wdev->connect_keys);
485 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200486 wdev->ssid_len = 0;
Johannes Berg5b112d32013-02-01 01:49:58 +0100487 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200488 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200489 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200490
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200491 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300492 bss = cfg80211_get_bss(wdev->wiphy,
493 wdev->conn ? wdev->conn->params.channel :
494 NULL,
495 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200496 wdev->ssid, wdev->ssid_len,
497 WLAN_CAPABILITY_ESS,
498 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200499
500 if (WARN_ON(!bss))
501 return;
502
503 cfg80211_hold_bss(bss_from_pub(bss));
504 wdev->current_bss = bss_from_pub(bss);
505
Johannes Bergfffd0932009-07-08 14:22:54 +0200506 wdev->sme_state = CFG80211_SME_CONNECTED;
507 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700508
Johannes Berg9caf0362012-11-29 01:25:20 +0100509 rcu_read_lock();
510 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
511 if (!country_ie) {
512 rcu_read_unlock();
513 return;
514 }
515
516 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
517 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700518
519 if (!country_ie)
520 return;
521
522 /*
523 * ieee80211_bss_get_ie() ensures we can access:
524 * - country_ie + 2, the start of the country ie data, and
525 * - and country_ie[1] which is the IE length
526 */
Johannes Berg1a919312012-12-03 17:21:11 +0100527 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
528 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100529 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200530}
Johannes Bergf2129352009-07-01 21:26:56 +0200531
532void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
533 const u8 *req_ie, size_t req_ie_len,
534 const u8 *resp_ie, size_t resp_ie_len,
535 u16 status, gfp_t gfp)
536{
Johannes Berg667503d2009-07-07 03:56:11 +0200537 struct wireless_dev *wdev = dev->ieee80211_ptr;
538 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
539 struct cfg80211_event *ev;
540 unsigned long flags;
541
Johannes Bergf7969962009-08-21 12:23:49 +0200542 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
543
Johannes Berg667503d2009-07-07 03:56:11 +0200544 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
545 if (!ev)
546 return;
547
548 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800549 if (bssid)
550 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700551 if (req_ie_len) {
552 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
553 ev->cr.req_ie_len = req_ie_len;
554 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
555 }
556 if (resp_ie_len) {
557 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
558 ev->cr.resp_ie_len = resp_ie_len;
559 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
560 }
Johannes Berg667503d2009-07-07 03:56:11 +0200561 ev->cr.status = status;
562
563 spin_lock_irqsave(&wdev->event_lock, flags);
564 list_add_tail(&ev->list, &wdev->event_list);
565 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100566 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200567}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200568EXPORT_SYMBOL(cfg80211_connect_result);
569
Jouni Malinened9d0102011-05-16 19:40:15 +0300570void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530571 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200572 const u8 *req_ie, size_t req_ie_len,
573 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200574{
Johannes Berg3d23e342009-09-29 23:27:28 +0200575#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200576 union iwreq_data wrqu;
577#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200578 ASSERT_WDEV_LOCK(wdev);
579
Johannes Berg074ac8d2010-09-16 14:58:22 +0200580 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
581 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530582 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200583
Johannes Bergf7969962009-08-21 12:23:49 +0200584 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530585 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586
587 /* internal error -- how did we get to CONNECTED w/o BSS? */
588 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530589 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200590 }
591
592 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100593 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200594 wdev->current_bss = NULL;
595
Johannes Berg19957bb2009-07-02 17:20:43 +0200596 cfg80211_hold_bss(bss_from_pub(bss));
597 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200598
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530599 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200600 req_ie, req_ie_len, resp_ie, resp_ie_len,
601 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200602
Johannes Berg3d23e342009-09-29 23:27:28 +0200603#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200604 if (req_ie) {
605 memset(&wrqu, 0, sizeof(wrqu));
606 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800607 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200608 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200609 }
610
611 if (resp_ie) {
612 memset(&wrqu, 0, sizeof(wrqu));
613 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200614 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
615 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200616 }
617
618 memset(&wrqu, 0, sizeof(wrqu));
619 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530620 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
621 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200622 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200623 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200624#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530625
626 return;
627out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100628 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200629}
Johannes Berg667503d2009-07-07 03:56:11 +0200630
Jouni Malinened9d0102011-05-16 19:40:15 +0300631void cfg80211_roamed(struct net_device *dev,
632 struct ieee80211_channel *channel,
633 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200634 const u8 *req_ie, size_t req_ie_len,
635 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
636{
637 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530638 struct cfg80211_bss *bss;
639
640 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
641
642 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
643 wdev->ssid_len, WLAN_CAPABILITY_ESS,
644 WLAN_CAPABILITY_ESS);
645 if (WARN_ON(!bss))
646 return;
647
648 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
649 resp_ie_len, gfp);
650}
651EXPORT_SYMBOL(cfg80211_roamed);
652
653void cfg80211_roamed_bss(struct net_device *dev,
654 struct cfg80211_bss *bss, const u8 *req_ie,
655 size_t req_ie_len, const u8 *resp_ie,
656 size_t resp_ie_len, gfp_t gfp)
657{
658 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200659 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
660 struct cfg80211_event *ev;
661 unsigned long flags;
662
Johannes Bergf7969962009-08-21 12:23:49 +0200663 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
664
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530665 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200666 return;
667
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530668 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
669 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100670 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530671 return;
672 }
673
Johannes Berg667503d2009-07-07 03:56:11 +0200674 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200675 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
676 ev->rm.req_ie_len = req_ie_len;
677 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
678 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
679 ev->rm.resp_ie_len = resp_ie_len;
680 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530681 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200682
683 spin_lock_irqsave(&wdev->event_lock, flags);
684 list_add_tail(&ev->list, &wdev->event_list);
685 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100686 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200687}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530688EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200689
Johannes Berg667503d2009-07-07 03:56:11 +0200690void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200691 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200692{
693 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200694 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
695 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200696#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200697 union iwreq_data wrqu;
698#endif
699
Johannes Berg667503d2009-07-07 03:56:11 +0200700 ASSERT_WDEV_LOCK(wdev);
701
Johannes Berg074ac8d2010-09-16 14:58:22 +0200702 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
703 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200704 return;
705
Johannes Bergf7969962009-08-21 12:23:49 +0200706 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200707 return;
708
709 if (wdev->current_bss) {
710 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100711 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200712 }
713
714 wdev->current_bss = NULL;
715 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200716 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200717
Johannes Berg6829c872009-07-02 09:13:27 +0200718 if (wdev->conn) {
719 kfree(wdev->conn->ie);
720 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200721 kfree(wdev->conn);
722 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200723 }
724
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
726
727 /*
728 * Delete all the keys ... pairwise keys can't really
729 * exist any more anyway, but default keys might.
730 */
731 if (rdev->ops->del_key)
732 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300733 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200734
Johannes Berg3d23e342009-09-29 23:27:28 +0200735#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736 memset(&wrqu, 0, sizeof(wrqu));
737 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
738 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800739 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200740#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500741
742 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743}
744
745void cfg80211_disconnected(struct net_device *dev, u16 reason,
746 u8 *ie, size_t ie_len, gfp_t gfp)
747{
Johannes Berg667503d2009-07-07 03:56:11 +0200748 struct wireless_dev *wdev = dev->ieee80211_ptr;
749 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
750 struct cfg80211_event *ev;
751 unsigned long flags;
752
Johannes Bergf7969962009-08-21 12:23:49 +0200753 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
754
Johannes Berg667503d2009-07-07 03:56:11 +0200755 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
756 if (!ev)
757 return;
758
759 ev->type = EVENT_DISCONNECTED;
760 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
761 ev->dc.ie_len = ie_len;
762 memcpy((void *)ev->dc.ie, ie, ie_len);
763 ev->dc.reason = reason;
764
765 spin_lock_irqsave(&wdev->event_lock, flags);
766 list_add_tail(&ev->list, &wdev->event_list);
767 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100768 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200769}
770EXPORT_SYMBOL(cfg80211_disconnected);
771
Johannes Berg667503d2009-07-07 03:56:11 +0200772int __cfg80211_connect(struct cfg80211_registered_device *rdev,
773 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200774 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200775 struct cfg80211_cached_keys *connkeys,
776 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200777{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200778 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700779 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200780 int err;
781
782 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200783
784 if (wdev->sme_state != CFG80211_SME_IDLE)
785 return -EALREADY;
786
Johannes Bergfffd0932009-07-08 14:22:54 +0200787 if (WARN_ON(wdev->connect_keys)) {
788 kfree(wdev->connect_keys);
789 wdev->connect_keys = NULL;
790 }
791
Ben Greear7e7c8922011-11-18 11:31:59 -0800792 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
793 rdev->wiphy.ht_capa_mod_mask);
794
Johannes Bergfffd0932009-07-08 14:22:54 +0200795 if (connkeys && connkeys->def >= 0) {
796 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200797 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200798
799 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200800 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200802 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
803 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200804 connect->key_idx = idx;
805 connect->key = connkeys->params[idx].key;
806 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200807
808 /*
809 * If ciphers are not set (e.g. when going through
810 * iwconfig), we have to set them appropriately here.
811 */
812 if (connect->crypto.cipher_group == 0)
813 connect->crypto.cipher_group = cipher;
814
815 if (connect->crypto.n_ciphers_pairwise == 0) {
816 connect->crypto.n_ciphers_pairwise = 1;
817 connect->crypto.ciphers_pairwise[0] = cipher;
818 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200819 }
820 }
821
Samuel Ortizb23aa672009-07-01 21:26:54 +0200822 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200823 if (!rdev->ops->auth || !rdev->ops->assoc)
824 return -EOPNOTSUPP;
825
Johannes Berg19957bb2009-07-02 17:20:43 +0200826 if (WARN_ON(wdev->conn))
827 return -EINPROGRESS;
828
829 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
830 if (!wdev->conn)
831 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200832
833 /*
834 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
835 */
836 memcpy(&wdev->conn->params, connect, sizeof(*connect));
837 if (connect->bssid) {
838 wdev->conn->params.bssid = wdev->conn->bssid;
839 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
840 }
841
842 if (connect->ie) {
843 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
844 GFP_KERNEL);
845 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200846 if (!wdev->conn->ie) {
847 kfree(wdev->conn);
848 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200849 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200850 }
Johannes Berg6829c872009-07-02 09:13:27 +0200851 }
852
853 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
854 wdev->conn->auto_auth = true;
855 /* start with open system ... should mostly work */
856 wdev->conn->params.auth_type =
857 NL80211_AUTHTYPE_OPEN_SYSTEM;
858 } else {
859 wdev->conn->auto_auth = false;
860 }
861
862 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
863 wdev->ssid_len = connect->ssid_len;
864 wdev->conn->params.ssid = wdev->ssid;
865 wdev->conn->params.ssid_len = connect->ssid_len;
866
Johannes Berg8bb89482009-09-26 14:42:53 +0200867 /* see if we have the bss already */
868 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200869
870 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200871 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200872
Johannes Bergf401a6f2009-08-07 14:51:05 +0200873 if (prev_bssid) {
874 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
875 wdev->conn->prev_bssid_valid = true;
876 }
877
Johannes Bergbbac31f2009-09-16 09:04:26 -0700878 /* we're good if we have a matching bss struct */
879 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200880 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
881 err = cfg80211_conn_do_work(wdev);
Johannes Berg5b112d32013-02-01 01:49:58 +0100882 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200883 } else {
884 /* otherwise we'll need to scan for the AP first */
885 err = cfg80211_conn_scan(wdev);
886 /*
887 * If we can't scan right now, then we need to scan again
888 * after the current scan finished, since the parameters
889 * changed (unless we find a good AP anyway).
890 */
891 if (err == -EBUSY) {
892 err = 0;
893 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
894 }
895 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200896 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100897 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200898 kfree(wdev->conn);
899 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200900 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200901 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200902 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200903 }
Johannes Berg6829c872009-07-02 09:13:27 +0200904
905 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200906 } else {
907 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200908 wdev->connect_keys = connkeys;
Hila Gonene35e4d22012-06-27 17:19:42 +0300909 err = rdev_connect(rdev, dev, connect);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200910 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200911 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912 wdev->sme_state = CFG80211_SME_IDLE;
913 return err;
914 }
Johannes Berg6829c872009-07-02 09:13:27 +0200915
916 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
917 wdev->ssid_len = connect->ssid_len;
918
919 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200920 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921}
922
Johannes Berg667503d2009-07-07 03:56:11 +0200923int cfg80211_connect(struct cfg80211_registered_device *rdev,
924 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200925 struct cfg80211_connect_params *connect,
926 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200927{
928 int err;
929
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200930 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200931 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200932 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200933 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200934 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200935
936 return err;
937}
938
939int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
940 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200941{
Johannes Berg6829c872009-07-02 09:13:27 +0200942 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200943 int err;
944
Johannes Berg667503d2009-07-07 03:56:11 +0200945 ASSERT_WDEV_LOCK(wdev);
946
Johannes Berg6829c872009-07-02 09:13:27 +0200947 if (wdev->sme_state == CFG80211_SME_IDLE)
948 return -EINVAL;
949
Johannes Bergfffd0932009-07-08 14:22:54 +0200950 kfree(wdev->connect_keys);
951 wdev->connect_keys = NULL;
952
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200954 if (!rdev->ops->deauth)
955 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200956
Johannes Berg19957bb2009-07-02 17:20:43 +0200957 /* was it connected by userspace SME? */
958 if (!wdev->conn) {
959 cfg80211_mlme_down(rdev, dev);
960 return 0;
961 }
Johannes Berg6829c872009-07-02 09:13:27 +0200962
963 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
964 (wdev->conn->state == CFG80211_CONN_SCANNING ||
965 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
966 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100967 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200968 kfree(wdev->conn);
969 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200970 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200971 return 0;
972 }
973
Johannes Berg6829c872009-07-02 09:13:27 +0200974 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200975 err = __cfg80211_mlme_deauth(rdev, dev,
976 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300977 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200978 if (err)
979 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300981 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200982 if (err)
983 return err;
984 }
985
Johannes Berg6829c872009-07-02 09:13:27 +0200986 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200987 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200988 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200989 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
990 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200991 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200992
993 return 0;
994}
Johannes Berg19957bb2009-07-02 17:20:43 +0200995
Johannes Berg667503d2009-07-07 03:56:11 +0200996int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
997 struct net_device *dev,
998 u16 reason, bool wextev)
999{
1000 int err;
1001
1002 wdev_lock(dev->ieee80211_ptr);
1003 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1004 wdev_unlock(dev->ieee80211_ptr);
1005
1006 return err;
1007}
1008
Johannes Berg95de8172012-01-20 13:55:25 +01001009void cfg80211_sme_disassoc(struct net_device *dev,
1010 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001011{
1012 struct wireless_dev *wdev = dev->ieee80211_ptr;
1013 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1014 u8 bssid[ETH_ALEN];
1015
Johannes Berg667503d2009-07-07 03:56:11 +02001016 ASSERT_WDEV_LOCK(wdev);
1017
Johannes Berg19957bb2009-07-02 17:20:43 +02001018 if (!wdev->conn)
1019 return;
1020
1021 if (wdev->conn->state == CFG80211_CONN_IDLE)
1022 return;
1023
1024 /*
1025 * Ok, so the association was made by this SME -- we don't
1026 * want it any more so deauthenticate too.
1027 */
1028
Johannes Berg95de8172012-01-20 13:55:25 +01001029 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001030
Johannes Berg95de8172012-01-20 13:55:25 +01001031 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1032 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001033}