blob: 0acfdc9beacf8a397b8122c236cd6156d6c78b7e [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"
Samuel Ortizb23aa672009-07-01 21:26:54 +020019
Johannes Berg6829c872009-07-02 09:13:27 +020020struct cfg80211_conn {
21 struct cfg80211_connect_params params;
22 /* these are sub-states of the _CONNECTING sme_state */
23 enum {
24 CFG80211_CONN_IDLE,
25 CFG80211_CONN_SCANNING,
26 CFG80211_CONN_SCAN_AGAIN,
27 CFG80211_CONN_AUTHENTICATE_NEXT,
28 CFG80211_CONN_AUTHENTICATING,
29 CFG80211_CONN_ASSOCIATE_NEXT,
30 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090031 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020032 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020033 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020034 u8 *ie;
35 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020036 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020037};
38
John W. Linville20925fee2010-07-20 12:32:52 -040039static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050040{
41 struct cfg80211_registered_device *rdev;
42 struct wireless_dev *wdev;
43 bool is_all_idle = true;
44
45 mutex_lock(&cfg80211_mutex);
46
47 /*
48 * All devices must be idle as otherwise if you are actively
49 * scanning some new beacon hints could be learned and would
50 * count as new regulatory hints.
51 */
52 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53 cfg80211_lock_rdev(rdev);
54 list_for_each_entry(wdev, &rdev->netdev_list, list) {
55 wdev_lock(wdev);
56 if (wdev->sme_state != CFG80211_SME_IDLE)
57 is_all_idle = false;
58 wdev_unlock(wdev);
59 }
60 cfg80211_unlock_rdev(rdev);
61 }
62
63 mutex_unlock(&cfg80211_mutex);
64
65 return is_all_idle;
66}
67
68static void disconnect_work(struct work_struct *work)
69{
70 if (!cfg80211_is_all_idle())
71 return;
72
73 regulatory_hint_disconnect();
74}
75
76static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020077
78static int cfg80211_conn_scan(struct wireless_dev *wdev)
79{
Johannes Berg79c97e92009-07-07 03:56:12 +020080 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020081 struct cfg80211_scan_request *request;
82 int n_channels, err;
83
84 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020085 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020086 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020087
Johannes Berg79c97e92009-07-07 03:56:12 +020088 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020089 return -EBUSY;
90
91 if (wdev->conn->params.channel) {
92 n_channels = 1;
93 } else {
94 enum ieee80211_band band;
95 n_channels = 0;
96
97 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
98 if (!wdev->wiphy->bands[band])
99 continue;
100 n_channels += wdev->wiphy->bands[band]->n_channels;
101 }
102 }
103 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
104 sizeof(request->channels[0]) * n_channels,
105 GFP_KERNEL);
106 if (!request)
107 return -ENOMEM;
108
Johannes Berg6829c872009-07-02 09:13:27 +0200109 if (wdev->conn->params.channel)
110 request->channels[0] = wdev->conn->params.channel;
111 else {
112 int i = 0, j;
113 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530114 struct ieee80211_supported_band *bands;
115 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200116
117 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530118 bands = wdev->wiphy->bands[band];
119 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200120 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530121 for (j = 0; j < bands->n_channels; j++) {
122 channel = &bands->channels[j];
123 if (channel->flags & IEEE80211_CHAN_DISABLED)
124 continue;
125 request->channels[i++] = channel;
126 }
127 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200128 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530129 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200130 }
131 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200132 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200133 request->n_ssids = 1;
134
135 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
136 wdev->conn->params.ssid_len);
137 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
138
Johannes Berg463d0182009-07-14 00:33:35 +0200139 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200140 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200141
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200145 if (!err) {
146 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200147 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200148 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200149 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200151 kfree(request);
152 }
153 return err;
154}
155
156static int cfg80211_conn_do_work(struct wireless_dev *wdev)
157{
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200160 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200162
Johannes Berg667503d2009-07-07 03:56:11 +0200163 ASSERT_WDEV_LOCK(wdev);
164
Johannes Berg6829c872009-07-02 09:13:27 +0200165 if (!wdev->conn)
166 return 0;
167
Johannes Berg19957bb2009-07-02 17:20:43 +0200168 params = &wdev->conn->params;
169
Johannes Berg6829c872009-07-02 09:13:27 +0200170 switch (wdev->conn->state) {
171 case CFG80211_CONN_SCAN_AGAIN:
172 return cfg80211_conn_scan(wdev);
173 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200174 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200175 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200177 params->channel, params->auth_type,
178 params->bssid,
179 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200180 NULL, 0,
181 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300182 params->key_idx, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200183 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200184 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200186 if (wdev->conn->prev_bssid_valid)
187 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200188 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200189 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200190 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200191 params->ssid, params->ssid_len,
192 params->ie, params->ie_len,
193 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200194 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200195 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200196 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300197 WLAN_REASON_DEAUTH_LEAVING,
198 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200199 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900200 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
201 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300203 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900204 /* return an error so that we call __cfg80211_connect_result() */
205 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200206 default:
207 return 0;
208 }
209}
210
211void cfg80211_conn_work(struct work_struct *work)
212{
Johannes Berg79c97e92009-07-07 03:56:12 +0200213 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200214 container_of(work, struct cfg80211_registered_device, conn_work);
215 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100216 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200217
218 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200219 cfg80211_lock_rdev(rdev);
220 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200221
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200223 wdev_lock(wdev);
224 if (!netif_running(wdev->netdev)) {
225 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200226 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200227 }
228 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
229 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200230 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200231 }
Johannes Berg7400f422009-10-31 07:40:37 +0100232 if (wdev->conn->params.bssid) {
233 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
234 bssid = bssid_buf;
235 }
Johannes Berg6829c872009-07-02 09:13:27 +0200236 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200237 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900238 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200239 NULL, 0, NULL, 0,
240 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200241 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200242 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200243 }
244
Johannes Berg79c97e92009-07-07 03:56:12 +0200245 mutex_unlock(&rdev->devlist_mtx);
246 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200247 rtnl_unlock();
248}
249
Johannes Bergbbac31f2009-09-16 09:04:26 -0700250static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200251{
Johannes Berg79c97e92009-07-07 03:56:12 +0200252 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200253 struct cfg80211_bss *bss;
254 u16 capa = WLAN_CAPABILITY_ESS;
255
Johannes Berg667503d2009-07-07 03:56:11 +0200256 ASSERT_WDEV_LOCK(wdev);
257
Johannes Berg6829c872009-07-02 09:13:27 +0200258 if (wdev->conn->params.privacy)
259 capa |= WLAN_CAPABILITY_PRIVACY;
260
Jouni Malinened9d0102011-05-16 19:40:15 +0300261 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
262 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200263 wdev->conn->params.ssid,
264 wdev->conn->params.ssid_len,
265 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
266 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200267 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700268 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200269
270 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
271 wdev->conn->params.bssid = wdev->conn->bssid;
272 wdev->conn->params.channel = bss->channel;
273 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200274 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200275
Johannes Bergbbac31f2009-09-16 09:04:26 -0700276 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200277}
278
Johannes Berg667503d2009-07-07 03:56:11 +0200279static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200280{
281 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200282 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700283 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200284
Johannes Berg667503d2009-07-07 03:56:11 +0200285 ASSERT_WDEV_LOCK(wdev);
286
Johannes Berg6829c872009-07-02 09:13:27 +0200287 if (wdev->sme_state != CFG80211_SME_CONNECTING)
288 return;
289
Zhu Yid4b1a682009-07-16 17:34:14 +0800290 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200291 return;
292
293 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
294 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
295 return;
296
Johannes Bergbbac31f2009-09-16 09:04:26 -0700297 bss = cfg80211_get_conn_bss(wdev);
298 if (bss) {
299 cfg80211_put_bss(bss);
300 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200301 /* not found */
302 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200303 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200304 else
Johannes Berg667503d2009-07-07 03:56:11 +0200305 __cfg80211_connect_result(
306 wdev->netdev,
307 wdev->conn->params.bssid,
308 NULL, 0, NULL, 0,
309 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200310 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200311 }
312}
313
Johannes Berg667503d2009-07-07 03:56:11 +0200314void cfg80211_sme_scan_done(struct net_device *dev)
315{
316 struct wireless_dev *wdev = dev->ieee80211_ptr;
317
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200318 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200319 wdev_lock(wdev);
320 __cfg80211_sme_scan_done(dev);
321 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200322 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200323}
324
325void cfg80211_sme_rx_auth(struct net_device *dev,
326 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200327{
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329 struct wiphy *wiphy = wdev->wiphy;
330 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
331 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
332 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
333
Johannes Berg667503d2009-07-07 03:56:11 +0200334 ASSERT_WDEV_LOCK(wdev);
335
Johannes Berg6829c872009-07-02 09:13:27 +0200336 /* should only RX auth frames when connecting */
337 if (wdev->sme_state != CFG80211_SME_CONNECTING)
338 return;
339
340 if (WARN_ON(!wdev->conn))
341 return;
342
343 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
344 wdev->conn->auto_auth &&
345 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
346 /* select automatically between only open, shared, leap */
347 switch (wdev->conn->params.auth_type) {
348 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200349 if (wdev->connect_keys)
350 wdev->conn->params.auth_type =
351 NL80211_AUTHTYPE_SHARED_KEY;
352 else
353 wdev->conn->params.auth_type =
354 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200355 break;
356 case NL80211_AUTHTYPE_SHARED_KEY:
357 wdev->conn->params.auth_type =
358 NL80211_AUTHTYPE_NETWORK_EAP;
359 break;
360 default:
361 /* huh? */
362 wdev->conn->params.auth_type =
363 NL80211_AUTHTYPE_OPEN_SYSTEM;
364 break;
365 }
366 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
367 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200368 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200369 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200370 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200371 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200372 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
373 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
374 schedule_work(&rdev->conn_work);
375 }
376}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200377
Johannes Bergf401a6f2009-08-07 14:51:05 +0200378bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
379{
380 struct wiphy *wiphy = wdev->wiphy;
381 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
382
383 if (WARN_ON(!wdev->conn))
384 return false;
385
386 if (!wdev->conn->prev_bssid_valid)
387 return false;
388
389 /*
390 * Some stupid APs don't accept reassoc, so we
391 * need to fall back to trying regular assoc.
392 */
393 wdev->conn->prev_bssid_valid = false;
394 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
395 schedule_work(&rdev->conn_work);
396
397 return true;
398}
399
Johannes Berg7d930bc2009-10-20 15:08:53 +0900400void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
401{
402 struct wiphy *wiphy = wdev->wiphy;
403 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
404
405 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
406 schedule_work(&rdev->conn_work);
407}
408
Johannes Berg667503d2009-07-07 03:56:11 +0200409void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
410 const u8 *req_ie, size_t req_ie_len,
411 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200412 u16 status, bool wextev,
413 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200414{
415 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700416 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200417#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200418 union iwreq_data wrqu;
419#endif
420
Johannes Berg667503d2009-07-07 03:56:11 +0200421 ASSERT_WDEV_LOCK(wdev);
422
Johannes Berg074ac8d2010-09-16 14:58:22 +0200423 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
424 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200425 return;
426
Johannes Bergf7969962009-08-21 12:23:49 +0200427 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200428 return;
429
430 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200431 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200432 resp_ie, resp_ie_len,
433 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200434
Johannes Berg3d23e342009-09-29 23:27:28 +0200435#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200436 if (wextev) {
437 if (req_ie && status == WLAN_STATUS_SUCCESS) {
438 memset(&wrqu, 0, sizeof(wrqu));
439 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800440 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200441 }
442
443 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
444 memset(&wrqu, 0, sizeof(wrqu));
445 wrqu.data.length = resp_ie_len;
446 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
447 }
448
449 memset(&wrqu, 0, sizeof(wrqu));
450 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200451 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200452 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200453 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
454 wdev->wext.prev_bssid_valid = true;
455 }
Johannes Berge45cd822009-07-02 09:58:04 +0200456 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
457 }
458#endif
459
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200460 if (wdev->current_bss) {
461 cfg80211_unhold_bss(wdev->current_bss);
462 cfg80211_put_bss(&wdev->current_bss->pub);
463 wdev->current_bss = NULL;
464 }
465
Johannes Berg19957bb2009-07-02 17:20:43 +0200466 if (wdev->conn)
467 wdev->conn->state = CFG80211_CONN_IDLE;
468
Johannes Bergfffd0932009-07-08 14:22:54 +0200469 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200470 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100471 if (wdev->conn)
472 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200473 kfree(wdev->conn);
474 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200475 kfree(wdev->connect_keys);
476 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200477 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200479 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200480
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200481 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300482 bss = cfg80211_get_bss(wdev->wiphy,
483 wdev->conn ? wdev->conn->params.channel :
484 NULL,
485 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200486 wdev->ssid, wdev->ssid_len,
487 WLAN_CAPABILITY_ESS,
488 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200489
490 if (WARN_ON(!bss))
491 return;
492
493 cfg80211_hold_bss(bss_from_pub(bss));
494 wdev->current_bss = bss_from_pub(bss);
495
Johannes Bergfffd0932009-07-08 14:22:54 +0200496 wdev->sme_state = CFG80211_SME_CONNECTED;
497 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700498
499 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
500
501 if (!country_ie)
502 return;
503
504 /*
505 * ieee80211_bss_get_ie() ensures we can access:
506 * - country_ie + 2, the start of the country ie data, and
507 * - and country_ie[1] which is the IE length
508 */
509 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500510 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700511 country_ie + 2,
512 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200513}
Johannes Bergf2129352009-07-01 21:26:56 +0200514
515void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
516 const u8 *req_ie, size_t req_ie_len,
517 const u8 *resp_ie, size_t resp_ie_len,
518 u16 status, gfp_t gfp)
519{
Johannes Berg667503d2009-07-07 03:56:11 +0200520 struct wireless_dev *wdev = dev->ieee80211_ptr;
521 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
522 struct cfg80211_event *ev;
523 unsigned long flags;
524
Johannes Bergf7969962009-08-21 12:23:49 +0200525 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
526
Johannes Berg667503d2009-07-07 03:56:11 +0200527 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
528 if (!ev)
529 return;
530
531 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800532 if (bssid)
533 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700534 if (req_ie_len) {
535 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
536 ev->cr.req_ie_len = req_ie_len;
537 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
538 }
539 if (resp_ie_len) {
540 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
541 ev->cr.resp_ie_len = resp_ie_len;
542 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
543 }
Johannes Berg667503d2009-07-07 03:56:11 +0200544 ev->cr.status = status;
545
546 spin_lock_irqsave(&wdev->event_lock, flags);
547 list_add_tail(&ev->list, &wdev->event_list);
548 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100549 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200550}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200551EXPORT_SYMBOL(cfg80211_connect_result);
552
Jouni Malinened9d0102011-05-16 19:40:15 +0300553void __cfg80211_roamed(struct wireless_dev *wdev,
554 struct ieee80211_channel *channel,
555 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200556 const u8 *req_ie, size_t req_ie_len,
557 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200558{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200559 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200560#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200561 union iwreq_data wrqu;
562#endif
563
Johannes Berg667503d2009-07-07 03:56:11 +0200564 ASSERT_WDEV_LOCK(wdev);
565
Johannes Berg074ac8d2010-09-16 14:58:22 +0200566 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
567 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200568 return;
569
Johannes Bergf7969962009-08-21 12:23:49 +0200570 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200571 return;
572
573 /* internal error -- how did we get to CONNECTED w/o BSS? */
574 if (WARN_ON(!wdev->current_bss)) {
575 return;
576 }
577
578 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200579 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200580 wdev->current_bss = NULL;
581
Jouni Malinened9d0102011-05-16 19:40:15 +0300582 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
Samuel Ortizb23aa672009-07-01 21:26:54 +0200583 wdev->ssid, wdev->ssid_len,
584 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
585
586 if (WARN_ON(!bss))
587 return;
588
Johannes Berg19957bb2009-07-02 17:20:43 +0200589 cfg80211_hold_bss(bss_from_pub(bss));
590 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591
Johannes Berg667503d2009-07-07 03:56:11 +0200592 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
593 req_ie, req_ie_len, resp_ie, resp_ie_len,
594 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595
Johannes Berg3d23e342009-09-29 23:27:28 +0200596#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200597 if (req_ie) {
598 memset(&wrqu, 0, sizeof(wrqu));
599 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800600 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200601 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200602 }
603
604 if (resp_ie) {
605 memset(&wrqu, 0, sizeof(wrqu));
606 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200607 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
608 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200609 }
610
611 memset(&wrqu, 0, sizeof(wrqu));
612 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
613 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200614 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
615 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200616 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200617#endif
618}
Johannes Berg667503d2009-07-07 03:56:11 +0200619
Jouni Malinened9d0102011-05-16 19:40:15 +0300620void cfg80211_roamed(struct net_device *dev,
621 struct ieee80211_channel *channel,
622 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200623 const u8 *req_ie, size_t req_ie_len,
624 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
625{
626 struct wireless_dev *wdev = dev->ieee80211_ptr;
627 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
628 struct cfg80211_event *ev;
629 unsigned long flags;
630
Johannes Bergf7969962009-08-21 12:23:49 +0200631 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
632
Johannes Berg667503d2009-07-07 03:56:11 +0200633 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
634 if (!ev)
635 return;
636
637 ev->type = EVENT_ROAMED;
Jouni Malinened9d0102011-05-16 19:40:15 +0300638 ev->rm.channel = channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200639 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
640 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
641 ev->rm.req_ie_len = req_ie_len;
642 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
643 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
644 ev->rm.resp_ie_len = resp_ie_len;
645 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
646
647 spin_lock_irqsave(&wdev->event_lock, flags);
648 list_add_tail(&ev->list, &wdev->event_list);
649 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100650 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200651}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200652EXPORT_SYMBOL(cfg80211_roamed);
653
Johannes Berg667503d2009-07-07 03:56:11 +0200654void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200655 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200656{
657 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200658 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
659 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200660#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200661 union iwreq_data wrqu;
662#endif
663
Johannes Berg667503d2009-07-07 03:56:11 +0200664 ASSERT_WDEV_LOCK(wdev);
665
Johannes Berg074ac8d2010-09-16 14:58:22 +0200666 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
667 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200668 return;
669
Johannes Bergf7969962009-08-21 12:23:49 +0200670 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200671 return;
672
673 if (wdev->current_bss) {
674 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200675 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200676 }
677
678 wdev->current_bss = NULL;
679 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200680 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200681
Johannes Berg6829c872009-07-02 09:13:27 +0200682 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200683 const u8 *bssid;
684 int ret;
685
Johannes Berg6829c872009-07-02 09:13:27 +0200686 kfree(wdev->conn->ie);
687 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200688 kfree(wdev->conn);
689 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200690
691 /*
692 * If this disconnect was due to a disassoc, we
693 * we might still have an auth BSS around. For
694 * the userspace SME that's currently expected,
695 * but for the kernel SME (nl80211 CONNECT or
696 * wireless extensions) we want to clear up all
697 * state.
698 */
699 for (i = 0; i < MAX_AUTH_BSSES; i++) {
700 if (!wdev->auth_bsses[i])
701 continue;
702 bssid = wdev->auth_bsses[i]->pub.bssid;
703 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300704 WLAN_REASON_DEAUTH_LEAVING,
705 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200706 WARN(ret, "deauth failed: %d\n", ret);
707 }
Johannes Berg6829c872009-07-02 09:13:27 +0200708 }
709
Johannes Bergfffd0932009-07-08 14:22:54 +0200710 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
711
712 /*
713 * Delete all the keys ... pairwise keys can't really
714 * exist any more anyway, but default keys might.
715 */
716 if (rdev->ops->del_key)
717 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200718 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200719
Johannes Berg3d23e342009-09-29 23:27:28 +0200720#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200721 memset(&wrqu, 0, sizeof(wrqu));
722 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
723 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800724 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200725#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500726
727 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200728}
729
730void cfg80211_disconnected(struct net_device *dev, u16 reason,
731 u8 *ie, size_t ie_len, gfp_t gfp)
732{
Johannes Berg667503d2009-07-07 03:56:11 +0200733 struct wireless_dev *wdev = dev->ieee80211_ptr;
734 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
735 struct cfg80211_event *ev;
736 unsigned long flags;
737
Johannes Bergf7969962009-08-21 12:23:49 +0200738 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
739
Johannes Berg667503d2009-07-07 03:56:11 +0200740 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
741 if (!ev)
742 return;
743
744 ev->type = EVENT_DISCONNECTED;
745 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
746 ev->dc.ie_len = ie_len;
747 memcpy((void *)ev->dc.ie, ie, ie_len);
748 ev->dc.reason = reason;
749
750 spin_lock_irqsave(&wdev->event_lock, flags);
751 list_add_tail(&ev->list, &wdev->event_list);
752 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100753 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200754}
755EXPORT_SYMBOL(cfg80211_disconnected);
756
Johannes Berg667503d2009-07-07 03:56:11 +0200757int __cfg80211_connect(struct cfg80211_registered_device *rdev,
758 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200760 struct cfg80211_cached_keys *connkeys,
761 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200763 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700764 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200765 int err;
766
767 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200768
769 if (wdev->sme_state != CFG80211_SME_IDLE)
770 return -EALREADY;
771
Johannes Bergfffd0932009-07-08 14:22:54 +0200772 if (WARN_ON(wdev->connect_keys)) {
773 kfree(wdev->connect_keys);
774 wdev->connect_keys = NULL;
775 }
776
777 if (connkeys && connkeys->def >= 0) {
778 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200779 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200780
781 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200782 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200783 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200784 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
785 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200786 connect->key_idx = idx;
787 connect->key = connkeys->params[idx].key;
788 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200789
790 /*
791 * If ciphers are not set (e.g. when going through
792 * iwconfig), we have to set them appropriately here.
793 */
794 if (connect->crypto.cipher_group == 0)
795 connect->crypto.cipher_group = cipher;
796
797 if (connect->crypto.n_ciphers_pairwise == 0) {
798 connect->crypto.n_ciphers_pairwise = 1;
799 connect->crypto.ciphers_pairwise[0] = cipher;
800 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 }
802 }
803
Samuel Ortizb23aa672009-07-01 21:26:54 +0200804 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200805 if (!rdev->ops->auth || !rdev->ops->assoc)
806 return -EOPNOTSUPP;
807
Johannes Berg19957bb2009-07-02 17:20:43 +0200808 if (WARN_ON(wdev->conn))
809 return -EINPROGRESS;
810
811 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
812 if (!wdev->conn)
813 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200814
815 /*
816 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
817 */
818 memcpy(&wdev->conn->params, connect, sizeof(*connect));
819 if (connect->bssid) {
820 wdev->conn->params.bssid = wdev->conn->bssid;
821 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
822 }
823
824 if (connect->ie) {
825 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
826 GFP_KERNEL);
827 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200828 if (!wdev->conn->ie) {
829 kfree(wdev->conn);
830 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200831 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200832 }
Johannes Berg6829c872009-07-02 09:13:27 +0200833 }
834
835 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
836 wdev->conn->auto_auth = true;
837 /* start with open system ... should mostly work */
838 wdev->conn->params.auth_type =
839 NL80211_AUTHTYPE_OPEN_SYSTEM;
840 } else {
841 wdev->conn->auto_auth = false;
842 }
843
844 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
845 wdev->ssid_len = connect->ssid_len;
846 wdev->conn->params.ssid = wdev->ssid;
847 wdev->conn->params.ssid_len = connect->ssid_len;
848
Johannes Berg8bb89482009-09-26 14:42:53 +0200849 /* see if we have the bss already */
850 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200851
852 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200854
Johannes Bergf401a6f2009-08-07 14:51:05 +0200855 if (prev_bssid) {
856 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
857 wdev->conn->prev_bssid_valid = true;
858 }
859
Johannes Bergbbac31f2009-09-16 09:04:26 -0700860 /* we're good if we have a matching bss struct */
861 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200862 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
863 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700864 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200865 } else {
866 /* otherwise we'll need to scan for the AP first */
867 err = cfg80211_conn_scan(wdev);
868 /*
869 * If we can't scan right now, then we need to scan again
870 * after the current scan finished, since the parameters
871 * changed (unless we find a good AP anyway).
872 */
873 if (err == -EBUSY) {
874 err = 0;
875 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
876 }
877 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200878 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100879 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200880 kfree(wdev->conn);
881 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200882 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200883 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200884 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200885 }
Johannes Berg6829c872009-07-02 09:13:27 +0200886
887 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200888 } else {
889 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200890 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200891 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
892 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200893 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200894 wdev->sme_state = CFG80211_SME_IDLE;
895 return err;
896 }
Johannes Berg6829c872009-07-02 09:13:27 +0200897
898 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
899 wdev->ssid_len = connect->ssid_len;
900
901 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903}
904
Johannes Berg667503d2009-07-07 03:56:11 +0200905int cfg80211_connect(struct cfg80211_registered_device *rdev,
906 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200907 struct cfg80211_connect_params *connect,
908 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200909{
910 int err;
911
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200912 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200913 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200914 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200915 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200916 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200917
918 return err;
919}
920
921int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
922 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200923{
Johannes Berg6829c872009-07-02 09:13:27 +0200924 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200925 int err;
926
Johannes Berg667503d2009-07-07 03:56:11 +0200927 ASSERT_WDEV_LOCK(wdev);
928
Johannes Berg6829c872009-07-02 09:13:27 +0200929 if (wdev->sme_state == CFG80211_SME_IDLE)
930 return -EINVAL;
931
Johannes Bergfffd0932009-07-08 14:22:54 +0200932 kfree(wdev->connect_keys);
933 wdev->connect_keys = NULL;
934
Samuel Ortizb23aa672009-07-01 21:26:54 +0200935 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200936 if (!rdev->ops->deauth)
937 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200938
Johannes Berg19957bb2009-07-02 17:20:43 +0200939 /* was it connected by userspace SME? */
940 if (!wdev->conn) {
941 cfg80211_mlme_down(rdev, dev);
942 return 0;
943 }
Johannes Berg6829c872009-07-02 09:13:27 +0200944
945 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
946 (wdev->conn->state == CFG80211_CONN_SCANNING ||
947 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
948 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100949 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200950 kfree(wdev->conn);
951 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200952 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200953 return 0;
954 }
955
Johannes Berg6829c872009-07-02 09:13:27 +0200956 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200957 err = __cfg80211_mlme_deauth(rdev, dev,
958 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300959 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200960 if (err)
961 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200962 } else {
963 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
964 if (err)
965 return err;
966 }
967
Johannes Berg6829c872009-07-02 09:13:27 +0200968 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200969 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200970 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200971 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
972 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200973 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200974
975 return 0;
976}
Johannes Berg19957bb2009-07-02 17:20:43 +0200977
Johannes Berg667503d2009-07-07 03:56:11 +0200978int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
979 struct net_device *dev,
980 u16 reason, bool wextev)
981{
982 int err;
983
984 wdev_lock(dev->ieee80211_ptr);
985 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
986 wdev_unlock(dev->ieee80211_ptr);
987
988 return err;
989}
990
Johannes Berg19957bb2009-07-02 17:20:43 +0200991void cfg80211_sme_disassoc(struct net_device *dev, int idx)
992{
993 struct wireless_dev *wdev = dev->ieee80211_ptr;
994 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
995 u8 bssid[ETH_ALEN];
996
Johannes Berg667503d2009-07-07 03:56:11 +0200997 ASSERT_WDEV_LOCK(wdev);
998
Johannes Berg19957bb2009-07-02 17:20:43 +0200999 if (!wdev->conn)
1000 return;
1001
1002 if (wdev->conn->state == CFG80211_CONN_IDLE)
1003 return;
1004
1005 /*
1006 * Ok, so the association was made by this SME -- we don't
1007 * want it any more so deauthenticate too.
1008 */
1009
1010 if (!wdev->auth_bsses[idx])
1011 return;
1012
1013 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +02001014 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001015 NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
1016 false)) {
Johannes Berg19957bb2009-07-02 17:20:43 +02001017 /* whatever -- assume gone anyway */
1018 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
1019 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1020 wdev->auth_bsses[idx] = NULL;
1021 }
1022}