blob: 6e86d5acf145bef1d7ba2e842fd61f1b50610c69 [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 Berg6829c872009-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 Berg6829c872009-07-02 09:13:27 +020031 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020032 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg6829c872009-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 Berg667503dd2009-07-07 03:56:11 +020085 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020086
Johannes Berg79c97e92009-07-07 03:56:12 +020087 if (rdev->scan_req)
Johannes Berg6829c872009-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 Berg6829c872009-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;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530113 struct ieee80211_supported_band *bands;
114 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200115
116 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530117 bands = wdev->wiphy->bands[band];
118 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200119 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530120 for (j = 0; j < bands->n_channels; j++) {
121 channel = &bands->channels[j];
122 if (channel->flags & IEEE80211_CHAN_DISABLED)
123 continue;
124 request->channels[i++] = channel;
125 }
126 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200127 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530128 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200129 }
130 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200131 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200132 request->n_ssids = 1;
133
134 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
135 wdev->conn->params.ssid_len);
136 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
137
Johannes Berg463d0182009-07-14 00:33:35 +0200138 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200139 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200140
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200142
Johannes Berg79c97e92009-07-07 03:56:12 +0200143 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200144 if (!err) {
145 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200146 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200147 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200148 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200149 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200150 kfree(request);
151 }
152 return err;
153}
154
155static int cfg80211_conn_do_work(struct wireless_dev *wdev)
156{
Johannes Berg79c97e92009-07-07 03:56:12 +0200157 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200158 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200159 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200160 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200161
Johannes Berg667503dd2009-07-07 03:56:11 +0200162 ASSERT_WDEV_LOCK(wdev);
163
Johannes Berg6829c872009-07-02 09:13:27 +0200164 if (!wdev->conn)
165 return 0;
166
Johannes Berg19957bb2009-07-02 17:20:43 +0200167 params = &wdev->conn->params;
168
Johannes Berg6829c872009-07-02 09:13:27 +0200169 switch (wdev->conn->state) {
170 case CFG80211_CONN_SCAN_AGAIN:
171 return cfg80211_conn_scan(wdev);
172 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200173 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200174 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200175 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200176 params->channel, params->auth_type,
177 params->bssid,
178 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200179 NULL, 0,
180 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300181 params->key_idx, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200182 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200183 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200184 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200185 if (wdev->conn->prev_bssid_valid)
186 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200187 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200188 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200189 prev_bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200190 params->ssid, params->ssid_len,
191 params->ie, params->ie_len,
192 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200193 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200194 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200195 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300196 WLAN_REASON_DEAUTH_LEAVING,
197 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200198 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900199 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
200 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
201 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300202 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900203 /* return an error so that we call __cfg80211_connect_result() */
204 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200205 default:
206 return 0;
207 }
208}
209
210void cfg80211_conn_work(struct work_struct *work)
211{
Johannes Berg79c97e92009-07-07 03:56:12 +0200212 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200213 container_of(work, struct cfg80211_registered_device, conn_work);
214 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100215 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200216
217 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200218 cfg80211_lock_rdev(rdev);
219 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200220
Johannes Berg79c97e92009-07-07 03:56:12 +0200221 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200222 wdev_lock(wdev);
223 if (!netif_running(wdev->netdev)) {
224 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200225 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200226 }
227 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
228 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200229 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200230 }
Johannes Berg7400f422009-10-31 07:40:37 +0100231 if (wdev->conn->params.bssid) {
232 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
233 bssid = bssid_buf;
234 }
Johannes Berg6829c872009-07-02 09:13:27 +0200235 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200236 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900237 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200238 NULL, 0, NULL, 0,
239 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200240 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200241 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200242 }
243
Johannes Berg79c97e92009-07-07 03:56:12 +0200244 mutex_unlock(&rdev->devlist_mtx);
245 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200246 rtnl_unlock();
247}
248
Johannes Bergbbac31f2009-09-16 09:04:26 -0700249static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200250{
Johannes Berg79c97e92009-07-07 03:56:12 +0200251 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200252 struct cfg80211_bss *bss;
253 u16 capa = WLAN_CAPABILITY_ESS;
254
Johannes Berg667503dd2009-07-07 03:56:11 +0200255 ASSERT_WDEV_LOCK(wdev);
256
Johannes Berg6829c872009-07-02 09:13:27 +0200257 if (wdev->conn->params.privacy)
258 capa |= WLAN_CAPABILITY_PRIVACY;
259
Jouni Malinened9d0102011-05-16 19:40:15 +0300260 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
261 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200262 wdev->conn->params.ssid,
263 wdev->conn->params.ssid_len,
264 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
265 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200266 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700267 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200268
269 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
270 wdev->conn->params.bssid = wdev->conn->bssid;
271 wdev->conn->params.channel = bss->channel;
272 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200273 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200274
Johannes Bergbbac31f2009-09-16 09:04:26 -0700275 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200276}
277
Johannes Berg667503dd2009-07-07 03:56:11 +0200278static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200279{
280 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200281 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700282 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200283
Johannes Berg667503dd2009-07-07 03:56:11 +0200284 ASSERT_WDEV_LOCK(wdev);
285
Johannes Berg6829c872009-07-02 09:13:27 +0200286 if (wdev->sme_state != CFG80211_SME_CONNECTING)
287 return;
288
Zhu Yid4b1a682009-07-16 17:34:14 +0800289 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200290 return;
291
292 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
293 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
294 return;
295
Johannes Bergbbac31f2009-09-16 09:04:26 -0700296 bss = cfg80211_get_conn_bss(wdev);
297 if (bss) {
298 cfg80211_put_bss(bss);
299 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200300 /* not found */
301 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200302 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200303 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200304 __cfg80211_connect_result(
305 wdev->netdev,
306 wdev->conn->params.bssid,
307 NULL, 0, NULL, 0,
308 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200309 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200310 }
311}
312
Johannes Berg667503dd2009-07-07 03:56:11 +0200313void cfg80211_sme_scan_done(struct net_device *dev)
314{
315 struct wireless_dev *wdev = dev->ieee80211_ptr;
316
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200317 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200318 wdev_lock(wdev);
319 __cfg80211_sme_scan_done(dev);
320 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200321 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200322}
323
324void cfg80211_sme_rx_auth(struct net_device *dev,
325 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200326{
327 struct wireless_dev *wdev = dev->ieee80211_ptr;
328 struct wiphy *wiphy = wdev->wiphy;
329 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
330 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
331 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
332
Johannes Berg667503dd2009-07-07 03:56:11 +0200333 ASSERT_WDEV_LOCK(wdev);
334
Johannes Berg6829c872009-07-02 09:13:27 +0200335 /* should only RX auth frames when connecting */
336 if (wdev->sme_state != CFG80211_SME_CONNECTING)
337 return;
338
339 if (WARN_ON(!wdev->conn))
340 return;
341
342 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
343 wdev->conn->auto_auth &&
344 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
345 /* select automatically between only open, shared, leap */
346 switch (wdev->conn->params.auth_type) {
347 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200348 if (wdev->connect_keys)
349 wdev->conn->params.auth_type =
350 NL80211_AUTHTYPE_SHARED_KEY;
351 else
352 wdev->conn->params.auth_type =
353 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200354 break;
355 case NL80211_AUTHTYPE_SHARED_KEY:
356 wdev->conn->params.auth_type =
357 NL80211_AUTHTYPE_NETWORK_EAP;
358 break;
359 default:
360 /* huh? */
361 wdev->conn->params.auth_type =
362 NL80211_AUTHTYPE_OPEN_SYSTEM;
363 break;
364 }
365 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
366 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200367 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200368 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200369 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200370 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200371 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
372 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
373 schedule_work(&rdev->conn_work);
374 }
375}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200376
Johannes Bergf401a6f2009-08-07 14:51:05 +0200377bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
378{
379 struct wiphy *wiphy = wdev->wiphy;
380 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
381
382 if (WARN_ON(!wdev->conn))
383 return false;
384
385 if (!wdev->conn->prev_bssid_valid)
386 return false;
387
388 /*
389 * Some stupid APs don't accept reassoc, so we
390 * need to fall back to trying regular assoc.
391 */
392 wdev->conn->prev_bssid_valid = false;
393 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
394 schedule_work(&rdev->conn_work);
395
396 return true;
397}
398
Johannes Berg7d930bc2009-10-20 15:08:53 +0900399void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
400{
401 struct wiphy *wiphy = wdev->wiphy;
402 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
403
404 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
405 schedule_work(&rdev->conn_work);
406}
407
Johannes Berg667503dd2009-07-07 03:56:11 +0200408void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
409 const u8 *req_ie, size_t req_ie_len,
410 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200411 u16 status, bool wextev,
412 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200413{
414 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700415 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200416#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200417 union iwreq_data wrqu;
418#endif
419
Johannes Berg667503dd2009-07-07 03:56:11 +0200420 ASSERT_WDEV_LOCK(wdev);
421
Johannes Berg074ac8d2010-09-16 14:58:22 +0200422 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
423 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200424 return;
425
Johannes Bergf7969962009-08-21 12:23:49 +0200426 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200427 return;
428
429 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200430 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200431 resp_ie, resp_ie_len,
432 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200433
Johannes Berg3d23e342009-09-29 23:27:28 +0200434#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200435 if (wextev) {
436 if (req_ie && status == WLAN_STATUS_SUCCESS) {
437 memset(&wrqu, 0, sizeof(wrqu));
438 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800439 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200440 }
441
442 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
443 memset(&wrqu, 0, sizeof(wrqu));
444 wrqu.data.length = resp_ie_len;
445 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
446 }
447
448 memset(&wrqu, 0, sizeof(wrqu));
449 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200450 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200451 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200452 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
453 wdev->wext.prev_bssid_valid = true;
454 }
Johannes Berge45cd822009-07-02 09:58:04 +0200455 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
456 }
457#endif
458
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200459 if (wdev->current_bss) {
460 cfg80211_unhold_bss(wdev->current_bss);
461 cfg80211_put_bss(&wdev->current_bss->pub);
462 wdev->current_bss = NULL;
463 }
464
Johannes Berg19957bb2009-07-02 17:20:43 +0200465 if (wdev->conn)
466 wdev->conn->state = CFG80211_CONN_IDLE;
467
Johannes Bergfffd0932009-07-08 14:22:54 +0200468 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200469 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100470 if (wdev->conn)
471 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200472 kfree(wdev->conn);
473 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200474 kfree(wdev->connect_keys);
475 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200476 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200477 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200478 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200479
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200480 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300481 bss = cfg80211_get_bss(wdev->wiphy,
482 wdev->conn ? wdev->conn->params.channel :
483 NULL,
484 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200485 wdev->ssid, wdev->ssid_len,
486 WLAN_CAPABILITY_ESS,
487 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200488
489 if (WARN_ON(!bss))
490 return;
491
492 cfg80211_hold_bss(bss_from_pub(bss));
493 wdev->current_bss = bss_from_pub(bss);
494
Johannes Bergfffd0932009-07-08 14:22:54 +0200495 wdev->sme_state = CFG80211_SME_CONNECTED;
496 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700497
498 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
499
500 if (!country_ie)
501 return;
502
503 /*
504 * ieee80211_bss_get_ie() ensures we can access:
505 * - country_ie + 2, the start of the country ie data, and
506 * - and country_ie[1] which is the IE length
507 */
508 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500509 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700510 country_ie + 2,
511 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200512}
Johannes Bergf2129352009-07-01 21:26:56 +0200513
514void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
515 const u8 *req_ie, size_t req_ie_len,
516 const u8 *resp_ie, size_t resp_ie_len,
517 u16 status, gfp_t gfp)
518{
Johannes Berg667503dd2009-07-07 03:56:11 +0200519 struct wireless_dev *wdev = dev->ieee80211_ptr;
520 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
521 struct cfg80211_event *ev;
522 unsigned long flags;
523
Johannes Bergf7969962009-08-21 12:23:49 +0200524 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
525
Johannes Berg667503dd2009-07-07 03:56:11 +0200526 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
527 if (!ev)
528 return;
529
530 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800531 if (bssid)
532 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700533 if (req_ie_len) {
534 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
535 ev->cr.req_ie_len = req_ie_len;
536 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
537 }
538 if (resp_ie_len) {
539 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
540 ev->cr.resp_ie_len = resp_ie_len;
541 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
542 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200543 ev->cr.status = status;
544
545 spin_lock_irqsave(&wdev->event_lock, flags);
546 list_add_tail(&ev->list, &wdev->event_list);
547 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100548 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200549}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200550EXPORT_SYMBOL(cfg80211_connect_result);
551
Jouni Malinened9d0102011-05-16 19:40:15 +0300552void __cfg80211_roamed(struct wireless_dev *wdev,
553 struct ieee80211_channel *channel,
554 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200555 const u8 *req_ie, size_t req_ie_len,
556 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200557{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200558 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200559#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200560 union iwreq_data wrqu;
561#endif
562
Johannes Berg667503dd2009-07-07 03:56:11 +0200563 ASSERT_WDEV_LOCK(wdev);
564
Johannes Berg074ac8d2010-09-16 14:58:22 +0200565 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
566 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200567 return;
568
Johannes Bergf7969962009-08-21 12:23:49 +0200569 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570 return;
571
572 /* internal error -- how did we get to CONNECTED w/o BSS? */
573 if (WARN_ON(!wdev->current_bss)) {
574 return;
575 }
576
577 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200578 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200579 wdev->current_bss = NULL;
580
Jouni Malinened9d0102011-05-16 19:40:15 +0300581 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
Samuel Ortizb23aa672009-07-01 21:26:54 +0200582 wdev->ssid, wdev->ssid_len,
583 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
584
585 if (WARN_ON(!bss))
586 return;
587
Johannes Berg19957bb2009-07-02 17:20:43 +0200588 cfg80211_hold_bss(bss_from_pub(bss));
589 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200590
Johannes Berg667503dd2009-07-07 03:56:11 +0200591 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
592 req_ie, req_ie_len, resp_ie, resp_ie_len,
593 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200594
Johannes Berg3d23e342009-09-29 23:27:28 +0200595#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200596 if (req_ie) {
597 memset(&wrqu, 0, sizeof(wrqu));
598 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800599 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200600 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200601 }
602
603 if (resp_ie) {
604 memset(&wrqu, 0, sizeof(wrqu));
605 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200606 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
607 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200608 }
609
610 memset(&wrqu, 0, sizeof(wrqu));
611 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
612 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200613 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
614 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200615 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200616#endif
617}
Johannes Berg667503dd2009-07-07 03:56:11 +0200618
Jouni Malinened9d0102011-05-16 19:40:15 +0300619void cfg80211_roamed(struct net_device *dev,
620 struct ieee80211_channel *channel,
621 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200622 const u8 *req_ie, size_t req_ie_len,
623 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
624{
625 struct wireless_dev *wdev = dev->ieee80211_ptr;
626 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
627 struct cfg80211_event *ev;
628 unsigned long flags;
629
Johannes Bergf7969962009-08-21 12:23:49 +0200630 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
631
Johannes Berg667503dd2009-07-07 03:56:11 +0200632 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
633 if (!ev)
634 return;
635
636 ev->type = EVENT_ROAMED;
Jouni Malinened9d0102011-05-16 19:40:15 +0300637 ev->rm.channel = channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200638 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
639 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
640 ev->rm.req_ie_len = req_ie_len;
641 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
642 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
643 ev->rm.resp_ie_len = resp_ie_len;
644 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
645
646 spin_lock_irqsave(&wdev->event_lock, flags);
647 list_add_tail(&ev->list, &wdev->event_list);
648 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100649 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200650}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200651EXPORT_SYMBOL(cfg80211_roamed);
652
Johannes Berg667503dd2009-07-07 03:56:11 +0200653void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200654 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655{
656 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200657 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
658 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200659#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200660 union iwreq_data wrqu;
661#endif
662
Johannes Berg667503dd2009-07-07 03:56:11 +0200663 ASSERT_WDEV_LOCK(wdev);
664
Johannes Berg074ac8d2010-09-16 14:58:22 +0200665 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
666 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200667 return;
668
Johannes Bergf7969962009-08-21 12:23:49 +0200669 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200670 return;
671
672 if (wdev->current_bss) {
673 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200674 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200675 }
676
677 wdev->current_bss = NULL;
678 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200679 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200680
Johannes Berg6829c872009-07-02 09:13:27 +0200681 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200682 const u8 *bssid;
683 int ret;
684
Johannes Berg6829c872009-07-02 09:13:27 +0200685 kfree(wdev->conn->ie);
686 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200687 kfree(wdev->conn);
688 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200689
690 /*
691 * If this disconnect was due to a disassoc, we
692 * we might still have an auth BSS around. For
693 * the userspace SME that's currently expected,
694 * but for the kernel SME (nl80211 CONNECT or
695 * wireless extensions) we want to clear up all
696 * state.
697 */
698 for (i = 0; i < MAX_AUTH_BSSES; i++) {
699 if (!wdev->auth_bsses[i])
700 continue;
701 bssid = wdev->auth_bsses[i]->pub.bssid;
702 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300703 WLAN_REASON_DEAUTH_LEAVING,
704 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200705 WARN(ret, "deauth failed: %d\n", ret);
706 }
Johannes Berg6829c872009-07-02 09:13:27 +0200707 }
708
Johannes Bergfffd0932009-07-08 14:22:54 +0200709 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
710
711 /*
712 * Delete all the keys ... pairwise keys can't really
713 * exist any more anyway, but default keys might.
714 */
715 if (rdev->ops->del_key)
716 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200717 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200718
Johannes Berg3d23e342009-09-29 23:27:28 +0200719#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200720 memset(&wrqu, 0, sizeof(wrqu));
721 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
722 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800723 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200724#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500725
726 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200727}
728
729void cfg80211_disconnected(struct net_device *dev, u16 reason,
730 u8 *ie, size_t ie_len, gfp_t gfp)
731{
Johannes Berg667503dd2009-07-07 03:56:11 +0200732 struct wireless_dev *wdev = dev->ieee80211_ptr;
733 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
734 struct cfg80211_event *ev;
735 unsigned long flags;
736
Johannes Bergf7969962009-08-21 12:23:49 +0200737 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
738
Johannes Berg667503dd2009-07-07 03:56:11 +0200739 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
740 if (!ev)
741 return;
742
743 ev->type = EVENT_DISCONNECTED;
744 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
745 ev->dc.ie_len = ie_len;
746 memcpy((void *)ev->dc.ie, ie, ie_len);
747 ev->dc.reason = reason;
748
749 spin_lock_irqsave(&wdev->event_lock, flags);
750 list_add_tail(&ev->list, &wdev->event_list);
751 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100752 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200753}
754EXPORT_SYMBOL(cfg80211_disconnected);
755
Johannes Berg667503dd2009-07-07 03:56:11 +0200756int __cfg80211_connect(struct cfg80211_registered_device *rdev,
757 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200758 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200759 struct cfg80211_cached_keys *connkeys,
760 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200761{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700763 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200764 int err;
765
766 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200767
768 if (wdev->sme_state != CFG80211_SME_IDLE)
769 return -EALREADY;
770
Johannes Bergfffd0932009-07-08 14:22:54 +0200771 if (WARN_ON(wdev->connect_keys)) {
772 kfree(wdev->connect_keys);
773 wdev->connect_keys = NULL;
774 }
775
776 if (connkeys && connkeys->def >= 0) {
777 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200778 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200779
780 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200781 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200782 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200783 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
784 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200785 connect->key_idx = idx;
786 connect->key = connkeys->params[idx].key;
787 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200788
789 /*
790 * If ciphers are not set (e.g. when going through
791 * iwconfig), we have to set them appropriately here.
792 */
793 if (connect->crypto.cipher_group == 0)
794 connect->crypto.cipher_group = cipher;
795
796 if (connect->crypto.n_ciphers_pairwise == 0) {
797 connect->crypto.n_ciphers_pairwise = 1;
798 connect->crypto.ciphers_pairwise[0] = cipher;
799 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200800 }
801 }
802
Samuel Ortizb23aa672009-07-01 21:26:54 +0200803 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200804 if (!rdev->ops->auth || !rdev->ops->assoc)
805 return -EOPNOTSUPP;
806
Johannes Berg19957bb2009-07-02 17:20:43 +0200807 if (WARN_ON(wdev->conn))
808 return -EINPROGRESS;
809
810 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
811 if (!wdev->conn)
812 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200813
814 /*
815 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
816 */
817 memcpy(&wdev->conn->params, connect, sizeof(*connect));
818 if (connect->bssid) {
819 wdev->conn->params.bssid = wdev->conn->bssid;
820 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
821 }
822
823 if (connect->ie) {
824 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
825 GFP_KERNEL);
826 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200827 if (!wdev->conn->ie) {
828 kfree(wdev->conn);
829 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200830 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200831 }
Johannes Berg6829c872009-07-02 09:13:27 +0200832 }
833
834 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
835 wdev->conn->auto_auth = true;
836 /* start with open system ... should mostly work */
837 wdev->conn->params.auth_type =
838 NL80211_AUTHTYPE_OPEN_SYSTEM;
839 } else {
840 wdev->conn->auto_auth = false;
841 }
842
843 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
844 wdev->ssid_len = connect->ssid_len;
845 wdev->conn->params.ssid = wdev->ssid;
846 wdev->conn->params.ssid_len = connect->ssid_len;
847
Johannes Berg8bb89482009-09-26 14:42:53 +0200848 /* see if we have the bss already */
849 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200850
851 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200852 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200853
Johannes Bergf401a6f2009-08-07 14:51:05 +0200854 if (prev_bssid) {
855 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
856 wdev->conn->prev_bssid_valid = true;
857 }
858
Johannes Bergbbac31f2009-09-16 09:04:26 -0700859 /* we're good if we have a matching bss struct */
860 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200861 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
862 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700863 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200864 } else {
865 /* otherwise we'll need to scan for the AP first */
866 err = cfg80211_conn_scan(wdev);
867 /*
868 * If we can't scan right now, then we need to scan again
869 * after the current scan finished, since the parameters
870 * changed (unless we find a good AP anyway).
871 */
872 if (err == -EBUSY) {
873 err = 0;
874 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
875 }
876 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200877 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100878 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200879 kfree(wdev->conn);
880 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200881 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200882 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200883 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200884 }
Johannes Berg6829c872009-07-02 09:13:27 +0200885
886 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200887 } else {
888 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200889 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200890 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
891 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200892 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200893 wdev->sme_state = CFG80211_SME_IDLE;
894 return err;
895 }
Johannes Berg6829c872009-07-02 09:13:27 +0200896
897 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
898 wdev->ssid_len = connect->ssid_len;
899
900 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902}
903
Johannes Berg667503dd2009-07-07 03:56:11 +0200904int cfg80211_connect(struct cfg80211_registered_device *rdev,
905 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200906 struct cfg80211_connect_params *connect,
907 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200908{
909 int err;
910
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200911 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200912 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200913 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200914 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200915 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200916
917 return err;
918}
919
920int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
921 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200922{
Johannes Berg6829c872009-07-02 09:13:27 +0200923 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200924 int err;
925
Johannes Berg667503dd2009-07-07 03:56:11 +0200926 ASSERT_WDEV_LOCK(wdev);
927
Johannes Berg6829c872009-07-02 09:13:27 +0200928 if (wdev->sme_state == CFG80211_SME_IDLE)
929 return -EINVAL;
930
Johannes Bergfffd0932009-07-08 14:22:54 +0200931 kfree(wdev->connect_keys);
932 wdev->connect_keys = NULL;
933
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200935 if (!rdev->ops->deauth)
936 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200937
Johannes Berg19957bb2009-07-02 17:20:43 +0200938 /* was it connected by userspace SME? */
939 if (!wdev->conn) {
940 cfg80211_mlme_down(rdev, dev);
941 return 0;
942 }
Johannes Berg6829c872009-07-02 09:13:27 +0200943
944 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
945 (wdev->conn->state == CFG80211_CONN_SCANNING ||
946 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
947 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100948 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200949 kfree(wdev->conn);
950 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200951 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200952 return 0;
953 }
954
Johannes Berg6829c872009-07-02 09:13:27 +0200955 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200956 err = __cfg80211_mlme_deauth(rdev, dev,
957 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300958 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200959 if (err)
960 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200961 } else {
962 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
963 if (err)
964 return err;
965 }
966
Johannes Berg6829c872009-07-02 09:13:27 +0200967 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200968 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200969 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200970 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
971 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200972 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973
974 return 0;
975}
Johannes Berg19957bb2009-07-02 17:20:43 +0200976
Johannes Berg667503dd2009-07-07 03:56:11 +0200977int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
978 struct net_device *dev,
979 u16 reason, bool wextev)
980{
981 int err;
982
983 wdev_lock(dev->ieee80211_ptr);
984 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
985 wdev_unlock(dev->ieee80211_ptr);
986
987 return err;
988}
989
Johannes Berg19957bb2009-07-02 17:20:43 +0200990void cfg80211_sme_disassoc(struct net_device *dev, int idx)
991{
992 struct wireless_dev *wdev = dev->ieee80211_ptr;
993 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
994 u8 bssid[ETH_ALEN];
995
Johannes Berg667503dd2009-07-07 03:56:11 +0200996 ASSERT_WDEV_LOCK(wdev);
997
Johannes Berg19957bb2009-07-02 17:20:43 +0200998 if (!wdev->conn)
999 return;
1000
1001 if (wdev->conn->state == CFG80211_CONN_IDLE)
1002 return;
1003
1004 /*
1005 * Ok, so the association was made by this SME -- we don't
1006 * want it any more so deauthenticate too.
1007 */
1008
1009 if (!wdev->auth_bsses[idx])
1010 return;
1011
1012 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +02001013 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001014 NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
1015 false)) {
Johannes Berg19957bb2009-07-02 17:20:43 +02001016 /* whatever -- assume gone anyway */
1017 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
1018 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1019 wdev->auth_bsses[idx] = NULL;
1020 }
1021}