blob: a8c2d6b877aeda894cb80328d015bc3494e000c9 [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>
13#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020014#include <net/cfg80211.h>
15#include <net/rtnetlink.h>
16#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070017#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020018
Johannes Berg6829c8782009-07-02 09:13:27 +020019struct cfg80211_conn {
20 struct cfg80211_connect_params params;
21 /* these are sub-states of the _CONNECTING sme_state */
22 enum {
23 CFG80211_CONN_IDLE,
24 CFG80211_CONN_SCANNING,
25 CFG80211_CONN_SCAN_AGAIN,
26 CFG80211_CONN_AUTHENTICATE_NEXT,
27 CFG80211_CONN_AUTHENTICATING,
28 CFG80211_CONN_ASSOCIATE_NEXT,
29 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090030 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c8782009-07-02 09:13:27 +020031 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020032 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c8782009-07-02 09:13:27 +020033 u8 *ie;
34 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020035 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c8782009-07-02 09:13:27 +020036};
37
John W. Linville20925fe2010-07-20 12:32:52 -040038static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050039{
40 struct cfg80211_registered_device *rdev;
41 struct wireless_dev *wdev;
42 bool is_all_idle = true;
43
44 mutex_lock(&cfg80211_mutex);
45
46 /*
47 * All devices must be idle as otherwise if you are actively
48 * scanning some new beacon hints could be learned and would
49 * count as new regulatory hints.
50 */
51 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
52 cfg80211_lock_rdev(rdev);
53 list_for_each_entry(wdev, &rdev->netdev_list, list) {
54 wdev_lock(wdev);
55 if (wdev->sme_state != CFG80211_SME_IDLE)
56 is_all_idle = false;
57 wdev_unlock(wdev);
58 }
59 cfg80211_unlock_rdev(rdev);
60 }
61
62 mutex_unlock(&cfg80211_mutex);
63
64 return is_all_idle;
65}
66
67static void disconnect_work(struct work_struct *work)
68{
69 if (!cfg80211_is_all_idle())
70 return;
71
72 regulatory_hint_disconnect();
73}
74
75static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c8782009-07-02 09:13:27 +020076
77static int cfg80211_conn_scan(struct wireless_dev *wdev)
78{
Johannes Berg79c97e92009-07-07 03:56:12 +020079 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +020080 struct cfg80211_scan_request *request;
81 int n_channels, err;
82
83 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020084 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020085 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +020086
Johannes Berg79c97e92009-07-07 03:56:12 +020087 if (rdev->scan_req)
Johannes Berg6829c8782009-07-02 09:13:27 +020088 return -EBUSY;
89
90 if (wdev->conn->params.channel) {
91 n_channels = 1;
92 } else {
93 enum ieee80211_band band;
94 n_channels = 0;
95
96 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
97 if (!wdev->wiphy->bands[band])
98 continue;
99 n_channels += wdev->wiphy->bands[band]->n_channels;
100 }
101 }
102 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
103 sizeof(request->channels[0]) * n_channels,
104 GFP_KERNEL);
105 if (!request)
106 return -ENOMEM;
107
Johannes Berg6829c8782009-07-02 09:13:27 +0200108 if (wdev->conn->params.channel)
109 request->channels[0] = wdev->conn->params.channel;
110 else {
111 int i = 0, j;
112 enum ieee80211_band band;
113
114 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
115 if (!wdev->wiphy->bands[band])
116 continue;
117 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
118 i++, j++)
119 request->channels[i] =
120 &wdev->wiphy->bands[band]->channels[j];
121 }
122 }
123 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200124 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c8782009-07-02 09:13:27 +0200125 request->n_ssids = 1;
126
127 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
128 wdev->conn->params.ssid_len);
129 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
130
Johannes Berg463d0182009-07-14 00:33:35 +0200131 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200132 request->wiphy = &rdev->wiphy;
Johannes Berg6829c8782009-07-02 09:13:27 +0200133
Johannes Berg79c97e92009-07-07 03:56:12 +0200134 rdev->scan_req = request;
Johannes Berg6829c8782009-07-02 09:13:27 +0200135
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c8782009-07-02 09:13:27 +0200137 if (!err) {
138 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200139 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200140 dev_hold(wdev->netdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200141 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 rdev->scan_req = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200143 kfree(request);
144 }
145 return err;
146}
147
148static int cfg80211_conn_do_work(struct wireless_dev *wdev)
149{
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200152 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200153 int err;
Johannes Berg6829c8782009-07-02 09:13:27 +0200154
Johannes Berg667503d2009-07-07 03:56:11 +0200155 ASSERT_WDEV_LOCK(wdev);
156
Johannes Berg6829c8782009-07-02 09:13:27 +0200157 if (!wdev->conn)
158 return 0;
159
Johannes Berg19957bb2009-07-02 17:20:43 +0200160 params = &wdev->conn->params;
161
Johannes Berg6829c8782009-07-02 09:13:27 +0200162 switch (wdev->conn->state) {
163 case CFG80211_CONN_SCAN_AGAIN:
164 return cfg80211_conn_scan(wdev);
165 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200166 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200167 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200168 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200169 params->channel, params->auth_type,
170 params->bssid,
171 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200172 NULL, 0,
173 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300174 params->key_idx, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200175 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200177 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200178 if (wdev->conn->prev_bssid_valid)
179 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200180 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200181 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200182 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200183 params->ssid, params->ssid_len,
184 params->ie, params->ie_len,
185 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200186 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200187 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200188 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300189 WLAN_REASON_DEAUTH_LEAVING,
190 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200191 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900192 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
193 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300195 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900196 /* return an error so that we call __cfg80211_connect_result() */
197 return -EINVAL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200198 default:
199 return 0;
200 }
201}
202
203void cfg80211_conn_work(struct work_struct *work)
204{
Johannes Berg79c97e92009-07-07 03:56:12 +0200205 struct cfg80211_registered_device *rdev =
Johannes Berg6829c8782009-07-02 09:13:27 +0200206 container_of(work, struct cfg80211_registered_device, conn_work);
207 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100208 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200209
210 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200211 cfg80211_lock_rdev(rdev);
212 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c8782009-07-02 09:13:27 +0200213
Johannes Berg79c97e92009-07-07 03:56:12 +0200214 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200215 wdev_lock(wdev);
216 if (!netif_running(wdev->netdev)) {
217 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200218 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200219 }
220 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
221 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200222 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200223 }
Johannes Berg7400f422009-10-31 07:40:37 +0100224 if (wdev->conn->params.bssid) {
225 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
226 bssid = bssid_buf;
227 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200228 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200229 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900230 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200231 NULL, 0, NULL, 0,
232 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200233 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200234 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200235 }
236
Johannes Berg79c97e92009-07-07 03:56:12 +0200237 mutex_unlock(&rdev->devlist_mtx);
238 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200239 rtnl_unlock();
240}
241
Johannes Bergbbac31f2009-09-16 09:04:26 -0700242static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c8782009-07-02 09:13:27 +0200243{
Johannes Berg79c97e92009-07-07 03:56:12 +0200244 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +0200245 struct cfg80211_bss *bss;
246 u16 capa = WLAN_CAPABILITY_ESS;
247
Johannes Berg667503d2009-07-07 03:56:11 +0200248 ASSERT_WDEV_LOCK(wdev);
249
Johannes Berg6829c8782009-07-02 09:13:27 +0200250 if (wdev->conn->params.privacy)
251 capa |= WLAN_CAPABILITY_PRIVACY;
252
253 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
254 wdev->conn->params.ssid,
255 wdev->conn->params.ssid_len,
256 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
257 capa);
Johannes Berg6829c8782009-07-02 09:13:27 +0200258 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700259 return NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200260
261 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
262 wdev->conn->params.bssid = wdev->conn->bssid;
263 wdev->conn->params.channel = bss->channel;
264 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200265 schedule_work(&rdev->conn_work);
Johannes Berg6829c8782009-07-02 09:13:27 +0200266
Johannes Bergbbac31f2009-09-16 09:04:26 -0700267 return bss;
Johannes Berg6829c8782009-07-02 09:13:27 +0200268}
269
Johannes Berg667503d2009-07-07 03:56:11 +0200270static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c8782009-07-02 09:13:27 +0200271{
272 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200273 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700274 struct cfg80211_bss *bss;
Johannes Berg6829c8782009-07-02 09:13:27 +0200275
Johannes Berg667503d2009-07-07 03:56:11 +0200276 ASSERT_WDEV_LOCK(wdev);
277
Johannes Berg6829c8782009-07-02 09:13:27 +0200278 if (wdev->sme_state != CFG80211_SME_CONNECTING)
279 return;
280
Zhu Yid4b1a682009-07-16 17:34:14 +0800281 if (!wdev->conn)
Johannes Berg6829c8782009-07-02 09:13:27 +0200282 return;
283
284 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
285 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
286 return;
287
Johannes Bergbbac31f2009-09-16 09:04:26 -0700288 bss = cfg80211_get_conn_bss(wdev);
289 if (bss) {
290 cfg80211_put_bss(bss);
291 } else {
Johannes Berg6829c8782009-07-02 09:13:27 +0200292 /* not found */
293 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200294 schedule_work(&rdev->conn_work);
Johannes Berg6829c8782009-07-02 09:13:27 +0200295 else
Johannes Berg667503d2009-07-07 03:56:11 +0200296 __cfg80211_connect_result(
297 wdev->netdev,
298 wdev->conn->params.bssid,
299 NULL, 0, NULL, 0,
300 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200301 false, NULL);
Johannes Berg6829c8782009-07-02 09:13:27 +0200302 }
303}
304
Johannes Berg667503d2009-07-07 03:56:11 +0200305void cfg80211_sme_scan_done(struct net_device *dev)
306{
307 struct wireless_dev *wdev = dev->ieee80211_ptr;
308
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200309 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200310 wdev_lock(wdev);
311 __cfg80211_sme_scan_done(dev);
312 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200313 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200314}
315
316void cfg80211_sme_rx_auth(struct net_device *dev,
317 const u8 *buf, size_t len)
Johannes Berg6829c8782009-07-02 09:13:27 +0200318{
319 struct wireless_dev *wdev = dev->ieee80211_ptr;
320 struct wiphy *wiphy = wdev->wiphy;
321 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
322 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
323 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
324
Johannes Berg667503d2009-07-07 03:56:11 +0200325 ASSERT_WDEV_LOCK(wdev);
326
Johannes Berg6829c8782009-07-02 09:13:27 +0200327 /* should only RX auth frames when connecting */
328 if (wdev->sme_state != CFG80211_SME_CONNECTING)
329 return;
330
331 if (WARN_ON(!wdev->conn))
332 return;
333
334 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
335 wdev->conn->auto_auth &&
336 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
337 /* select automatically between only open, shared, leap */
338 switch (wdev->conn->params.auth_type) {
339 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200340 if (wdev->connect_keys)
341 wdev->conn->params.auth_type =
342 NL80211_AUTHTYPE_SHARED_KEY;
343 else
344 wdev->conn->params.auth_type =
345 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200346 break;
347 case NL80211_AUTHTYPE_SHARED_KEY:
348 wdev->conn->params.auth_type =
349 NL80211_AUTHTYPE_NETWORK_EAP;
350 break;
351 default:
352 /* huh? */
353 wdev->conn->params.auth_type =
354 NL80211_AUTHTYPE_OPEN_SYSTEM;
355 break;
356 }
357 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
358 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200359 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200360 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200361 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200362 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c8782009-07-02 09:13:27 +0200363 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
364 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
365 schedule_work(&rdev->conn_work);
366 }
367}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200368
Johannes Bergf401a6f2009-08-07 14:51:05 +0200369bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
370{
371 struct wiphy *wiphy = wdev->wiphy;
372 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
373
374 if (WARN_ON(!wdev->conn))
375 return false;
376
377 if (!wdev->conn->prev_bssid_valid)
378 return false;
379
380 /*
381 * Some stupid APs don't accept reassoc, so we
382 * need to fall back to trying regular assoc.
383 */
384 wdev->conn->prev_bssid_valid = false;
385 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
386 schedule_work(&rdev->conn_work);
387
388 return true;
389}
390
Johannes Berg7d930bc2009-10-20 15:08:53 +0900391void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
392{
393 struct wiphy *wiphy = wdev->wiphy;
394 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
395
396 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
397 schedule_work(&rdev->conn_work);
398}
399
Johannes Berg667503d2009-07-07 03:56:11 +0200400void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
401 const u8 *req_ie, size_t req_ie_len,
402 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200403 u16 status, bool wextev,
404 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200405{
406 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700407 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200408#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200409 union iwreq_data wrqu;
410#endif
411
Johannes Berg667503d2009-07-07 03:56:11 +0200412 ASSERT_WDEV_LOCK(wdev);
413
Samuel Ortizb23aa672009-07-01 21:26:54 +0200414 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
415 return;
416
Johannes Bergf7969962009-08-21 12:23:49 +0200417 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200418 return;
419
420 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200421 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200422 resp_ie, resp_ie_len,
423 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200424
Johannes Berg3d23e342009-09-29 23:27:28 +0200425#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200426 if (wextev) {
427 if (req_ie && status == WLAN_STATUS_SUCCESS) {
428 memset(&wrqu, 0, sizeof(wrqu));
429 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800430 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200431 }
432
433 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
434 memset(&wrqu, 0, sizeof(wrqu));
435 wrqu.data.length = resp_ie_len;
436 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
437 }
438
439 memset(&wrqu, 0, sizeof(wrqu));
440 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200441 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200442 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200443 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
444 wdev->wext.prev_bssid_valid = true;
445 }
Johannes Berge45cd822009-07-02 09:58:04 +0200446 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
447 }
448#endif
449
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200450 if (wdev->current_bss) {
451 cfg80211_unhold_bss(wdev->current_bss);
452 cfg80211_put_bss(&wdev->current_bss->pub);
453 wdev->current_bss = NULL;
454 }
455
Johannes Berg19957bb2009-07-02 17:20:43 +0200456 if (wdev->conn)
457 wdev->conn->state = CFG80211_CONN_IDLE;
458
Johannes Bergfffd0932009-07-08 14:22:54 +0200459 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200460 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100461 if (wdev->conn)
462 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200463 kfree(wdev->conn);
464 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200465 kfree(wdev->connect_keys);
466 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200467 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200468 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200469 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200470
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200471 if (!bss)
472 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
473 wdev->ssid, wdev->ssid_len,
474 WLAN_CAPABILITY_ESS,
475 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200476
477 if (WARN_ON(!bss))
478 return;
479
480 cfg80211_hold_bss(bss_from_pub(bss));
481 wdev->current_bss = bss_from_pub(bss);
482
Johannes Bergfffd0932009-07-08 14:22:54 +0200483 wdev->sme_state = CFG80211_SME_CONNECTED;
484 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700485
486 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
487
488 if (!country_ie)
489 return;
490
491 /*
492 * ieee80211_bss_get_ie() ensures we can access:
493 * - country_ie + 2, the start of the country ie data, and
494 * - and country_ie[1] which is the IE length
495 */
496 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500497 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700498 country_ie + 2,
499 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200500}
Johannes Bergf2129352009-07-01 21:26:56 +0200501
502void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
503 const u8 *req_ie, size_t req_ie_len,
504 const u8 *resp_ie, size_t resp_ie_len,
505 u16 status, gfp_t gfp)
506{
Johannes Berg667503d2009-07-07 03:56:11 +0200507 struct wireless_dev *wdev = dev->ieee80211_ptr;
508 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
509 struct cfg80211_event *ev;
510 unsigned long flags;
511
Johannes Bergf7969962009-08-21 12:23:49 +0200512 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
513
Johannes Berg667503d2009-07-07 03:56:11 +0200514 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
515 if (!ev)
516 return;
517
518 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800519 if (bssid)
520 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700521 if (req_ie_len) {
522 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
523 ev->cr.req_ie_len = req_ie_len;
524 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
525 }
526 if (resp_ie_len) {
527 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
528 ev->cr.resp_ie_len = resp_ie_len;
529 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
530 }
Johannes Berg667503d2009-07-07 03:56:11 +0200531 ev->cr.status = status;
532
533 spin_lock_irqsave(&wdev->event_lock, flags);
534 list_add_tail(&ev->list, &wdev->event_list);
535 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100536 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200537}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200538EXPORT_SYMBOL(cfg80211_connect_result);
539
Johannes Berg667503d2009-07-07 03:56:11 +0200540void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
541 const u8 *req_ie, size_t req_ie_len,
542 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200543{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200544 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200545#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200546 union iwreq_data wrqu;
547#endif
548
Johannes Berg667503d2009-07-07 03:56:11 +0200549 ASSERT_WDEV_LOCK(wdev);
550
Samuel Ortizb23aa672009-07-01 21:26:54 +0200551 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
552 return;
553
Johannes Bergf7969962009-08-21 12:23:49 +0200554 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200555 return;
556
557 /* internal error -- how did we get to CONNECTED w/o BSS? */
558 if (WARN_ON(!wdev->current_bss)) {
559 return;
560 }
561
562 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200563 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200564 wdev->current_bss = NULL;
565
566 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
567 wdev->ssid, wdev->ssid_len,
568 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
569
570 if (WARN_ON(!bss))
571 return;
572
Johannes Berg19957bb2009-07-02 17:20:43 +0200573 cfg80211_hold_bss(bss_from_pub(bss));
574 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200575
Johannes Berg667503d2009-07-07 03:56:11 +0200576 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
577 req_ie, req_ie_len, resp_ie, resp_ie_len,
578 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200579
Johannes Berg3d23e342009-09-29 23:27:28 +0200580#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200581 if (req_ie) {
582 memset(&wrqu, 0, sizeof(wrqu));
583 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800584 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200585 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586 }
587
588 if (resp_ie) {
589 memset(&wrqu, 0, sizeof(wrqu));
590 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200591 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
592 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593 }
594
595 memset(&wrqu, 0, sizeof(wrqu));
596 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
597 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200598 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
599 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200600 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200601#endif
602}
Johannes Berg667503d2009-07-07 03:56:11 +0200603
604void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
605 const u8 *req_ie, size_t req_ie_len,
606 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
607{
608 struct wireless_dev *wdev = dev->ieee80211_ptr;
609 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
610 struct cfg80211_event *ev;
611 unsigned long flags;
612
Johannes Bergf7969962009-08-21 12:23:49 +0200613 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
614
Johannes Berg667503d2009-07-07 03:56:11 +0200615 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
616 if (!ev)
617 return;
618
619 ev->type = EVENT_ROAMED;
620 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
621 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
622 ev->rm.req_ie_len = req_ie_len;
623 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
624 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
625 ev->rm.resp_ie_len = resp_ie_len;
626 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
627
628 spin_lock_irqsave(&wdev->event_lock, flags);
629 list_add_tail(&ev->list, &wdev->event_list);
630 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100631 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200632}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200633EXPORT_SYMBOL(cfg80211_roamed);
634
Johannes Berg667503d2009-07-07 03:56:11 +0200635void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c8782009-07-02 09:13:27 +0200636 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200637{
638 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200639 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
640 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200641#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200642 union iwreq_data wrqu;
643#endif
644
Johannes Berg667503d2009-07-07 03:56:11 +0200645 ASSERT_WDEV_LOCK(wdev);
646
Samuel Ortizb23aa672009-07-01 21:26:54 +0200647 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
648 return;
649
Johannes Bergf7969962009-08-21 12:23:49 +0200650 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200651 return;
652
653 if (wdev->current_bss) {
654 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200655 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200656 }
657
658 wdev->current_bss = NULL;
659 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200660 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200661
Johannes Berg6829c8782009-07-02 09:13:27 +0200662 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200663 const u8 *bssid;
664 int ret;
665
Johannes Berg6829c8782009-07-02 09:13:27 +0200666 kfree(wdev->conn->ie);
667 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200668 kfree(wdev->conn);
669 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200670
671 /*
672 * If this disconnect was due to a disassoc, we
673 * we might still have an auth BSS around. For
674 * the userspace SME that's currently expected,
675 * but for the kernel SME (nl80211 CONNECT or
676 * wireless extensions) we want to clear up all
677 * state.
678 */
679 for (i = 0; i < MAX_AUTH_BSSES; i++) {
680 if (!wdev->auth_bsses[i])
681 continue;
682 bssid = wdev->auth_bsses[i]->pub.bssid;
683 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300684 WLAN_REASON_DEAUTH_LEAVING,
685 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200686 WARN(ret, "deauth failed: %d\n", ret);
687 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200688 }
689
Johannes Bergfffd0932009-07-08 14:22:54 +0200690 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
691
692 /*
693 * Delete all the keys ... pairwise keys can't really
694 * exist any more anyway, but default keys might.
695 */
696 if (rdev->ops->del_key)
697 for (i = 0; i < 6; i++)
698 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200699
Johannes Berg3d23e342009-09-29 23:27:28 +0200700#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200701 memset(&wrqu, 0, sizeof(wrqu));
702 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
703 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800704 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200705#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500706
707 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200708}
709
710void cfg80211_disconnected(struct net_device *dev, u16 reason,
711 u8 *ie, size_t ie_len, gfp_t gfp)
712{
Johannes Berg667503d2009-07-07 03:56:11 +0200713 struct wireless_dev *wdev = dev->ieee80211_ptr;
714 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
715 struct cfg80211_event *ev;
716 unsigned long flags;
717
Johannes Bergf7969962009-08-21 12:23:49 +0200718 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
719
Johannes Berg667503d2009-07-07 03:56:11 +0200720 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
721 if (!ev)
722 return;
723
724 ev->type = EVENT_DISCONNECTED;
725 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
726 ev->dc.ie_len = ie_len;
727 memcpy((void *)ev->dc.ie, ie, ie_len);
728 ev->dc.reason = reason;
729
730 spin_lock_irqsave(&wdev->event_lock, flags);
731 list_add_tail(&ev->list, &wdev->event_list);
732 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100733 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200734}
735EXPORT_SYMBOL(cfg80211_disconnected);
736
Johannes Berg667503d2009-07-07 03:56:11 +0200737int __cfg80211_connect(struct cfg80211_registered_device *rdev,
738 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200739 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200740 struct cfg80211_cached_keys *connkeys,
741 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200742{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700744 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200745 int err;
746
747 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748
749 if (wdev->sme_state != CFG80211_SME_IDLE)
750 return -EALREADY;
751
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 if (WARN_ON(wdev->connect_keys)) {
753 kfree(wdev->connect_keys);
754 wdev->connect_keys = NULL;
755 }
756
757 if (connkeys && connkeys->def >= 0) {
758 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200759 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200760
761 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200762 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200763 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200764 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
765 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200766 connect->key_idx = idx;
767 connect->key = connkeys->params[idx].key;
768 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200769
770 /*
771 * If ciphers are not set (e.g. when going through
772 * iwconfig), we have to set them appropriately here.
773 */
774 if (connect->crypto.cipher_group == 0)
775 connect->crypto.cipher_group = cipher;
776
777 if (connect->crypto.n_ciphers_pairwise == 0) {
778 connect->crypto.n_ciphers_pairwise = 1;
779 connect->crypto.ciphers_pairwise[0] = cipher;
780 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200781 }
782 }
783
Samuel Ortizb23aa672009-07-01 21:26:54 +0200784 if (!rdev->ops->connect) {
Johannes Berg6829c8782009-07-02 09:13:27 +0200785 if (!rdev->ops->auth || !rdev->ops->assoc)
786 return -EOPNOTSUPP;
787
Johannes Berg19957bb2009-07-02 17:20:43 +0200788 if (WARN_ON(wdev->conn))
789 return -EINPROGRESS;
790
791 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
792 if (!wdev->conn)
793 return -ENOMEM;
Johannes Berg6829c8782009-07-02 09:13:27 +0200794
795 /*
796 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
797 */
798 memcpy(&wdev->conn->params, connect, sizeof(*connect));
799 if (connect->bssid) {
800 wdev->conn->params.bssid = wdev->conn->bssid;
801 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
802 }
803
804 if (connect->ie) {
805 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
806 GFP_KERNEL);
807 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200808 if (!wdev->conn->ie) {
809 kfree(wdev->conn);
810 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200811 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200812 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200813 }
814
815 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
816 wdev->conn->auto_auth = true;
817 /* start with open system ... should mostly work */
818 wdev->conn->params.auth_type =
819 NL80211_AUTHTYPE_OPEN_SYSTEM;
820 } else {
821 wdev->conn->auto_auth = false;
822 }
823
824 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
825 wdev->ssid_len = connect->ssid_len;
826 wdev->conn->params.ssid = wdev->ssid;
827 wdev->conn->params.ssid_len = connect->ssid_len;
828
Johannes Berg8bb89482009-09-26 14:42:53 +0200829 /* see if we have the bss already */
830 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200831
832 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200833 wdev->connect_keys = connkeys;
Johannes Berg6829c8782009-07-02 09:13:27 +0200834
Johannes Bergf401a6f2009-08-07 14:51:05 +0200835 if (prev_bssid) {
836 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
837 wdev->conn->prev_bssid_valid = true;
838 }
839
Johannes Bergbbac31f2009-09-16 09:04:26 -0700840 /* we're good if we have a matching bss struct */
841 if (bss) {
Johannes Berg6829c8782009-07-02 09:13:27 +0200842 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
843 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700844 cfg80211_put_bss(bss);
Johannes Berg6829c8782009-07-02 09:13:27 +0200845 } else {
846 /* otherwise we'll need to scan for the AP first */
847 err = cfg80211_conn_scan(wdev);
848 /*
849 * If we can't scan right now, then we need to scan again
850 * after the current scan finished, since the parameters
851 * changed (unless we find a good AP anyway).
852 */
853 if (err == -EBUSY) {
854 err = 0;
855 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
856 }
857 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200858 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100859 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200860 kfree(wdev->conn);
861 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200862 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200863 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200864 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200865 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200866
867 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200868 } else {
869 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200870 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200871 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
872 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200873 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200874 wdev->sme_state = CFG80211_SME_IDLE;
875 return err;
876 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200877
878 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
879 wdev->ssid_len = connect->ssid_len;
880
881 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200882 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200883}
884
Johannes Berg667503d2009-07-07 03:56:11 +0200885int cfg80211_connect(struct cfg80211_registered_device *rdev,
886 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200887 struct cfg80211_connect_params *connect,
888 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200889{
890 int err;
891
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200892 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200893 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200894 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200895 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200896 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200897
898 return err;
899}
900
901int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
902 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903{
Johannes Berg6829c8782009-07-02 09:13:27 +0200904 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200905 int err;
906
Johannes Berg667503d2009-07-07 03:56:11 +0200907 ASSERT_WDEV_LOCK(wdev);
908
Johannes Berg6829c8782009-07-02 09:13:27 +0200909 if (wdev->sme_state == CFG80211_SME_IDLE)
910 return -EINVAL;
911
Johannes Bergfffd0932009-07-08 14:22:54 +0200912 kfree(wdev->connect_keys);
913 wdev->connect_keys = NULL;
914
Samuel Ortizb23aa672009-07-01 21:26:54 +0200915 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200916 if (!rdev->ops->deauth)
917 return -EOPNOTSUPP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200918
Johannes Berg19957bb2009-07-02 17:20:43 +0200919 /* was it connected by userspace SME? */
920 if (!wdev->conn) {
921 cfg80211_mlme_down(rdev, dev);
922 return 0;
923 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200924
925 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
926 (wdev->conn->state == CFG80211_CONN_SCANNING ||
927 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
928 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100929 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200930 kfree(wdev->conn);
931 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200932 wdev->ssid_len = 0;
Johannes Berg6829c8782009-07-02 09:13:27 +0200933 return 0;
934 }
935
Johannes Berg6829c8782009-07-02 09:13:27 +0200936 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200937 err = __cfg80211_mlme_deauth(rdev, dev,
938 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300939 NULL, 0, reason, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200940 if (err)
941 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200942 } else {
943 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
944 if (err)
945 return err;
946 }
947
Johannes Berg6829c8782009-07-02 09:13:27 +0200948 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200949 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200950 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200951 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
952 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200953 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200954
955 return 0;
956}
Johannes Berg19957bb2009-07-02 17:20:43 +0200957
Johannes Berg667503d2009-07-07 03:56:11 +0200958int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
959 struct net_device *dev,
960 u16 reason, bool wextev)
961{
962 int err;
963
964 wdev_lock(dev->ieee80211_ptr);
965 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
966 wdev_unlock(dev->ieee80211_ptr);
967
968 return err;
969}
970
Johannes Berg19957bb2009-07-02 17:20:43 +0200971void cfg80211_sme_disassoc(struct net_device *dev, int idx)
972{
973 struct wireless_dev *wdev = dev->ieee80211_ptr;
974 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
975 u8 bssid[ETH_ALEN];
976
Johannes Berg667503d2009-07-07 03:56:11 +0200977 ASSERT_WDEV_LOCK(wdev);
978
Johannes Berg19957bb2009-07-02 17:20:43 +0200979 if (!wdev->conn)
980 return;
981
982 if (wdev->conn->state == CFG80211_CONN_IDLE)
983 return;
984
985 /*
986 * Ok, so the association was made by this SME -- we don't
987 * want it any more so deauthenticate too.
988 */
989
990 if (!wdev->auth_bsses[idx])
991 return;
992
993 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200994 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300995 NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
996 false)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200997 /* whatever -- assume gone anyway */
998 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
999 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1000 wdev->auth_bsses[idx] = NULL;
1001 }
1002}