blob: 17465777eb476578487d495abc28efa3d163ab66 [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>
10#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020011#include <linux/wireless.h>
12#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <net/cfg80211.h>
14#include <net/rtnetlink.h>
15#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070016#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020017
Johannes Berg6829c872009-07-02 09:13:27 +020018struct cfg80211_conn {
19 struct cfg80211_connect_params params;
20 /* these are sub-states of the _CONNECTING sme_state */
21 enum {
22 CFG80211_CONN_IDLE,
23 CFG80211_CONN_SCANNING,
24 CFG80211_CONN_SCAN_AGAIN,
25 CFG80211_CONN_AUTHENTICATE_NEXT,
26 CFG80211_CONN_AUTHENTICATING,
27 CFG80211_CONN_ASSOCIATE_NEXT,
28 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090029 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020030 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020031 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020032 u8 *ie;
33 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020034 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020035};
36
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050037bool cfg80211_is_all_idle(void)
38{
39 struct cfg80211_registered_device *rdev;
40 struct wireless_dev *wdev;
41 bool is_all_idle = true;
42
43 mutex_lock(&cfg80211_mutex);
44
45 /*
46 * All devices must be idle as otherwise if you are actively
47 * scanning some new beacon hints could be learned and would
48 * count as new regulatory hints.
49 */
50 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
51 cfg80211_lock_rdev(rdev);
52 list_for_each_entry(wdev, &rdev->netdev_list, list) {
53 wdev_lock(wdev);
54 if (wdev->sme_state != CFG80211_SME_IDLE)
55 is_all_idle = false;
56 wdev_unlock(wdev);
57 }
58 cfg80211_unlock_rdev(rdev);
59 }
60
61 mutex_unlock(&cfg80211_mutex);
62
63 return is_all_idle;
64}
65
66static void disconnect_work(struct work_struct *work)
67{
68 if (!cfg80211_is_all_idle())
69 return;
70
71 regulatory_hint_disconnect();
72}
73
74static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020075
76static int cfg80211_conn_scan(struct wireless_dev *wdev)
77{
Johannes Berg79c97e92009-07-07 03:56:12 +020078 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020079 struct cfg80211_scan_request *request;
80 int n_channels, err;
81
82 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020083 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020084 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020085
Johannes Berg79c97e92009-07-07 03:56:12 +020086 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020087 return -EBUSY;
88
89 if (wdev->conn->params.channel) {
90 n_channels = 1;
91 } else {
92 enum ieee80211_band band;
93 n_channels = 0;
94
95 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
96 if (!wdev->wiphy->bands[band])
97 continue;
98 n_channels += wdev->wiphy->bands[band]->n_channels;
99 }
100 }
101 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
102 sizeof(request->channels[0]) * n_channels,
103 GFP_KERNEL);
104 if (!request)
105 return -ENOMEM;
106
Johannes Berg6829c872009-07-02 09:13:27 +0200107 if (wdev->conn->params.channel)
108 request->channels[0] = wdev->conn->params.channel;
109 else {
110 int i = 0, j;
111 enum ieee80211_band band;
112
113 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
114 if (!wdev->wiphy->bands[band])
115 continue;
116 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
117 i++, j++)
118 request->channels[i] =
119 &wdev->wiphy->bands[band]->channels[j];
120 }
121 }
122 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200123 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200124 request->n_ssids = 1;
125
126 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
127 wdev->conn->params.ssid_len);
128 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
129
Johannes Berg463d0182009-07-14 00:33:35 +0200130 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200131 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200132
Johannes Berg79c97e92009-07-07 03:56:12 +0200133 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200134
Johannes Berg79c97e92009-07-07 03:56:12 +0200135 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200136 if (!err) {
137 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200138 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200139 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200140 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200142 kfree(request);
143 }
144 return err;
145}
146
147static int cfg80211_conn_do_work(struct wireless_dev *wdev)
148{
Johannes Berg79c97e92009-07-07 03:56:12 +0200149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200150 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200151 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200153
Johannes Berg667503d2009-07-07 03:56:11 +0200154 ASSERT_WDEV_LOCK(wdev);
155
Johannes Berg6829c872009-07-02 09:13:27 +0200156 if (!wdev->conn)
157 return 0;
158
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 params = &wdev->conn->params;
160
Johannes Berg6829c872009-07-02 09:13:27 +0200161 switch (wdev->conn->state) {
162 case CFG80211_CONN_SCAN_AGAIN:
163 return cfg80211_conn_scan(wdev);
164 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200165 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200166 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200167 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200168 params->channel, params->auth_type,
169 params->bssid,
170 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200171 NULL, 0,
172 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300173 params->key_idx, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200174 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200175 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200176 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200177 if (wdev->conn->prev_bssid_valid)
178 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200179 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200180 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200181 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200182 params->ssid, params->ssid_len,
183 params->ie, params->ie_len,
184 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200187 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300188 WLAN_REASON_DEAUTH_LEAVING,
189 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200190 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900191 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
192 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
193 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300194 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900195 /* return an error so that we call __cfg80211_connect_result() */
196 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200197 default:
198 return 0;
199 }
200}
201
202void cfg80211_conn_work(struct work_struct *work)
203{
Johannes Berg79c97e92009-07-07 03:56:12 +0200204 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200205 container_of(work, struct cfg80211_registered_device, conn_work);
206 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100207 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200208
209 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200210 cfg80211_lock_rdev(rdev);
211 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200212
Johannes Berg79c97e92009-07-07 03:56:12 +0200213 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200214 wdev_lock(wdev);
215 if (!netif_running(wdev->netdev)) {
216 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200217 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200218 }
219 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
220 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200221 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200222 }
Johannes Berg7400f422009-10-31 07:40:37 +0100223 if (wdev->conn->params.bssid) {
224 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
225 bssid = bssid_buf;
226 }
Johannes Berg6829c872009-07-02 09:13:27 +0200227 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200228 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900229 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200230 NULL, 0, NULL, 0,
231 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200232 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200233 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200234 }
235
Johannes Berg79c97e92009-07-07 03:56:12 +0200236 mutex_unlock(&rdev->devlist_mtx);
237 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200238 rtnl_unlock();
239}
240
Johannes Bergbbac31f2009-09-16 09:04:26 -0700241static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200242{
Johannes Berg79c97e92009-07-07 03:56:12 +0200243 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200244 struct cfg80211_bss *bss;
245 u16 capa = WLAN_CAPABILITY_ESS;
246
Johannes Berg667503d2009-07-07 03:56:11 +0200247 ASSERT_WDEV_LOCK(wdev);
248
Johannes Berg6829c872009-07-02 09:13:27 +0200249 if (wdev->conn->params.privacy)
250 capa |= WLAN_CAPABILITY_PRIVACY;
251
252 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
253 wdev->conn->params.ssid,
254 wdev->conn->params.ssid_len,
255 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
256 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200257 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700258 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200259
260 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
261 wdev->conn->params.bssid = wdev->conn->bssid;
262 wdev->conn->params.channel = bss->channel;
263 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200264 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200265
Johannes Bergbbac31f2009-09-16 09:04:26 -0700266 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200267}
268
Johannes Berg667503d2009-07-07 03:56:11 +0200269static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200270{
271 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200272 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700273 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200274
Johannes Berg667503d2009-07-07 03:56:11 +0200275 ASSERT_WDEV_LOCK(wdev);
276
Johannes Berg6829c872009-07-02 09:13:27 +0200277 if (wdev->sme_state != CFG80211_SME_CONNECTING)
278 return;
279
Zhu Yid4b1a682009-07-16 17:34:14 +0800280 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200281 return;
282
283 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
284 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
285 return;
286
Johannes Bergbbac31f2009-09-16 09:04:26 -0700287 bss = cfg80211_get_conn_bss(wdev);
288 if (bss) {
289 cfg80211_put_bss(bss);
290 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200291 /* not found */
292 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200293 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200294 else
Johannes Berg667503d2009-07-07 03:56:11 +0200295 __cfg80211_connect_result(
296 wdev->netdev,
297 wdev->conn->params.bssid,
298 NULL, 0, NULL, 0,
299 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200300 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200301 }
302}
303
Johannes Berg667503d2009-07-07 03:56:11 +0200304void cfg80211_sme_scan_done(struct net_device *dev)
305{
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
307
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200308 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200309 wdev_lock(wdev);
310 __cfg80211_sme_scan_done(dev);
311 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200312 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200313}
314
315void cfg80211_sme_rx_auth(struct net_device *dev,
316 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200317{
318 struct wireless_dev *wdev = dev->ieee80211_ptr;
319 struct wiphy *wiphy = wdev->wiphy;
320 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
321 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
322 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
323
Johannes Berg667503d2009-07-07 03:56:11 +0200324 ASSERT_WDEV_LOCK(wdev);
325
Johannes Berg6829c872009-07-02 09:13:27 +0200326 /* should only RX auth frames when connecting */
327 if (wdev->sme_state != CFG80211_SME_CONNECTING)
328 return;
329
330 if (WARN_ON(!wdev->conn))
331 return;
332
333 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
334 wdev->conn->auto_auth &&
335 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
336 /* select automatically between only open, shared, leap */
337 switch (wdev->conn->params.auth_type) {
338 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200339 if (wdev->connect_keys)
340 wdev->conn->params.auth_type =
341 NL80211_AUTHTYPE_SHARED_KEY;
342 else
343 wdev->conn->params.auth_type =
344 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200345 break;
346 case NL80211_AUTHTYPE_SHARED_KEY:
347 wdev->conn->params.auth_type =
348 NL80211_AUTHTYPE_NETWORK_EAP;
349 break;
350 default:
351 /* huh? */
352 wdev->conn->params.auth_type =
353 NL80211_AUTHTYPE_OPEN_SYSTEM;
354 break;
355 }
356 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
357 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200358 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200359 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200360 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200361 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200362 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
363 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
364 schedule_work(&rdev->conn_work);
365 }
366}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200367
Johannes Bergf401a6f2009-08-07 14:51:05 +0200368bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
369{
370 struct wiphy *wiphy = wdev->wiphy;
371 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
372
373 if (WARN_ON(!wdev->conn))
374 return false;
375
376 if (!wdev->conn->prev_bssid_valid)
377 return false;
378
379 /*
380 * Some stupid APs don't accept reassoc, so we
381 * need to fall back to trying regular assoc.
382 */
383 wdev->conn->prev_bssid_valid = false;
384 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
385 schedule_work(&rdev->conn_work);
386
387 return true;
388}
389
Johannes Berg7d930bc2009-10-20 15:08:53 +0900390void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
391{
392 struct wiphy *wiphy = wdev->wiphy;
393 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
394
395 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
396 schedule_work(&rdev->conn_work);
397}
398
Johannes Berg667503d2009-07-07 03:56:11 +0200399void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
400 const u8 *req_ie, size_t req_ie_len,
401 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200402 u16 status, bool wextev,
403 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200404{
405 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700406 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200407#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200408 union iwreq_data wrqu;
409#endif
410
Johannes Berg667503d2009-07-07 03:56:11 +0200411 ASSERT_WDEV_LOCK(wdev);
412
Samuel Ortizb23aa672009-07-01 21:26:54 +0200413 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
414 return;
415
Johannes Bergf7969962009-08-21 12:23:49 +0200416 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200417 return;
418
419 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200420 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200421 resp_ie, resp_ie_len,
422 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200423
Johannes Berg3d23e342009-09-29 23:27:28 +0200424#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200425 if (wextev) {
426 if (req_ie && status == WLAN_STATUS_SUCCESS) {
427 memset(&wrqu, 0, sizeof(wrqu));
428 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800429 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200430 }
431
432 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
433 memset(&wrqu, 0, sizeof(wrqu));
434 wrqu.data.length = resp_ie_len;
435 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
436 }
437
438 memset(&wrqu, 0, sizeof(wrqu));
439 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200440 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200441 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200442 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
443 wdev->wext.prev_bssid_valid = true;
444 }
Johannes Berge45cd822009-07-02 09:58:04 +0200445 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
446 }
447#endif
448
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200449 if (wdev->current_bss) {
450 cfg80211_unhold_bss(wdev->current_bss);
451 cfg80211_put_bss(&wdev->current_bss->pub);
452 wdev->current_bss = NULL;
453 }
454
Johannes Berg19957bb2009-07-02 17:20:43 +0200455 if (wdev->conn)
456 wdev->conn->state = CFG80211_CONN_IDLE;
457
Johannes Bergfffd0932009-07-08 14:22:54 +0200458 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200459 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100460 if (wdev->conn)
461 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200462 kfree(wdev->conn);
463 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200464 kfree(wdev->connect_keys);
465 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200466 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200467 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200468 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200469
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200470 if (!bss)
471 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
472 wdev->ssid, wdev->ssid_len,
473 WLAN_CAPABILITY_ESS,
474 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200475
476 if (WARN_ON(!bss))
477 return;
478
479 cfg80211_hold_bss(bss_from_pub(bss));
480 wdev->current_bss = bss_from_pub(bss);
481
Johannes Bergfffd0932009-07-08 14:22:54 +0200482 wdev->sme_state = CFG80211_SME_CONNECTED;
483 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700484
485 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
486
487 if (!country_ie)
488 return;
489
490 /*
491 * ieee80211_bss_get_ie() ensures we can access:
492 * - country_ie + 2, the start of the country ie data, and
493 * - and country_ie[1] which is the IE length
494 */
495 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500496 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700497 country_ie + 2,
498 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200499}
Johannes Bergf2129352009-07-01 21:26:56 +0200500
501void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
502 const u8 *req_ie, size_t req_ie_len,
503 const u8 *resp_ie, size_t resp_ie_len,
504 u16 status, gfp_t gfp)
505{
Johannes Berg667503d2009-07-07 03:56:11 +0200506 struct wireless_dev *wdev = dev->ieee80211_ptr;
507 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
508 struct cfg80211_event *ev;
509 unsigned long flags;
510
Johannes Bergf7969962009-08-21 12:23:49 +0200511 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
512
Johannes Berg667503d2009-07-07 03:56:11 +0200513 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
514 if (!ev)
515 return;
516
517 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800518 if (bssid)
519 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200520 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
521 ev->cr.req_ie_len = req_ie_len;
522 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
523 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
524 ev->cr.resp_ie_len = resp_ie_len;
525 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
526 ev->cr.status = status;
527
528 spin_lock_irqsave(&wdev->event_lock, flags);
529 list_add_tail(&ev->list, &wdev->event_list);
530 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100531 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200532}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200533EXPORT_SYMBOL(cfg80211_connect_result);
534
Johannes Berg667503d2009-07-07 03:56:11 +0200535void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
536 const u8 *req_ie, size_t req_ie_len,
537 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200538{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200539 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200540#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200541 union iwreq_data wrqu;
542#endif
543
Johannes Berg667503d2009-07-07 03:56:11 +0200544 ASSERT_WDEV_LOCK(wdev);
545
Samuel Ortizb23aa672009-07-01 21:26:54 +0200546 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
547 return;
548
Johannes Bergf7969962009-08-21 12:23:49 +0200549 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200550 return;
551
552 /* internal error -- how did we get to CONNECTED w/o BSS? */
553 if (WARN_ON(!wdev->current_bss)) {
554 return;
555 }
556
557 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200558 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200559 wdev->current_bss = NULL;
560
561 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
562 wdev->ssid, wdev->ssid_len,
563 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
564
565 if (WARN_ON(!bss))
566 return;
567
Johannes Berg19957bb2009-07-02 17:20:43 +0200568 cfg80211_hold_bss(bss_from_pub(bss));
569 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570
Johannes Berg667503d2009-07-07 03:56:11 +0200571 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
572 req_ie, req_ie_len, resp_ie, resp_ie_len,
573 GFP_KERNEL);
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 if (req_ie) {
577 memset(&wrqu, 0, sizeof(wrqu));
578 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800579 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200580 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200581 }
582
583 if (resp_ie) {
584 memset(&wrqu, 0, sizeof(wrqu));
585 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200586 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
587 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200588 }
589
590 memset(&wrqu, 0, sizeof(wrqu));
591 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
592 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200593 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
594 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200595 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200596#endif
597}
Johannes Berg667503d2009-07-07 03:56:11 +0200598
599void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
600 const u8 *req_ie, size_t req_ie_len,
601 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
602{
603 struct wireless_dev *wdev = dev->ieee80211_ptr;
604 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
605 struct cfg80211_event *ev;
606 unsigned long flags;
607
Johannes Bergf7969962009-08-21 12:23:49 +0200608 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
609
Johannes Berg667503d2009-07-07 03:56:11 +0200610 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
611 if (!ev)
612 return;
613
614 ev->type = EVENT_ROAMED;
615 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
616 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
617 ev->rm.req_ie_len = req_ie_len;
618 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
619 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
620 ev->rm.resp_ie_len = resp_ie_len;
621 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
622
623 spin_lock_irqsave(&wdev->event_lock, flags);
624 list_add_tail(&ev->list, &wdev->event_list);
625 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100626 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200627}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200628EXPORT_SYMBOL(cfg80211_roamed);
629
Johannes Berg667503d2009-07-07 03:56:11 +0200630void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200631 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200632{
633 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200634 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
635 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200636#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200637 union iwreq_data wrqu;
638#endif
639
Johannes Berg667503d2009-07-07 03:56:11 +0200640 ASSERT_WDEV_LOCK(wdev);
641
Samuel Ortizb23aa672009-07-01 21:26:54 +0200642 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
643 return;
644
Johannes Bergf7969962009-08-21 12:23:49 +0200645 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200646 return;
647
648 if (wdev->current_bss) {
649 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200650 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200651 }
652
653 wdev->current_bss = NULL;
654 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200655 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200656
Johannes Berg6829c872009-07-02 09:13:27 +0200657 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200658 const u8 *bssid;
659 int ret;
660
Johannes Berg6829c872009-07-02 09:13:27 +0200661 kfree(wdev->conn->ie);
662 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200663 kfree(wdev->conn);
664 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200665
666 /*
667 * If this disconnect was due to a disassoc, we
668 * we might still have an auth BSS around. For
669 * the userspace SME that's currently expected,
670 * but for the kernel SME (nl80211 CONNECT or
671 * wireless extensions) we want to clear up all
672 * state.
673 */
674 for (i = 0; i < MAX_AUTH_BSSES; i++) {
675 if (!wdev->auth_bsses[i])
676 continue;
677 bssid = wdev->auth_bsses[i]->pub.bssid;
678 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300679 WLAN_REASON_DEAUTH_LEAVING,
680 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200681 WARN(ret, "deauth failed: %d\n", ret);
682 }
Johannes Berg6829c872009-07-02 09:13:27 +0200683 }
684
Johannes Bergfffd0932009-07-08 14:22:54 +0200685 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
686
687 /*
688 * Delete all the keys ... pairwise keys can't really
689 * exist any more anyway, but default keys might.
690 */
691 if (rdev->ops->del_key)
692 for (i = 0; i < 6; i++)
693 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200694
Johannes Berg3d23e342009-09-29 23:27:28 +0200695#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200696 memset(&wrqu, 0, sizeof(wrqu));
697 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
698 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800699 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200700#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500701
702 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200703}
704
705void cfg80211_disconnected(struct net_device *dev, u16 reason,
706 u8 *ie, size_t ie_len, gfp_t gfp)
707{
Johannes Berg667503d2009-07-07 03:56:11 +0200708 struct wireless_dev *wdev = dev->ieee80211_ptr;
709 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
710 struct cfg80211_event *ev;
711 unsigned long flags;
712
Johannes Bergf7969962009-08-21 12:23:49 +0200713 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
714
Johannes Berg667503d2009-07-07 03:56:11 +0200715 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
716 if (!ev)
717 return;
718
719 ev->type = EVENT_DISCONNECTED;
720 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
721 ev->dc.ie_len = ie_len;
722 memcpy((void *)ev->dc.ie, ie, ie_len);
723 ev->dc.reason = reason;
724
725 spin_lock_irqsave(&wdev->event_lock, flags);
726 list_add_tail(&ev->list, &wdev->event_list);
727 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100728 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200729}
730EXPORT_SYMBOL(cfg80211_disconnected);
731
Johannes Berg667503d2009-07-07 03:56:11 +0200732int __cfg80211_connect(struct cfg80211_registered_device *rdev,
733 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200734 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200735 struct cfg80211_cached_keys *connkeys,
736 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200737{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200738 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200739 struct ieee80211_channel *chan;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700740 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200741 int err;
742
743 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744
745 if (wdev->sme_state != CFG80211_SME_IDLE)
746 return -EALREADY;
747
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200748 chan = rdev_fixed_channel(rdev, wdev);
749 if (chan && chan != connect->channel)
750 return -EBUSY;
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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-07-02 09:13:27 +0200811 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200812 }
Johannes Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-07-02 09:13:27 +0200933 return 0;
934 }
935
Johannes Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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}