blob: 81e6747003914ce73c3896078cfba0774ea55691 [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 Berg667503d2009-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;
113
114 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
115 if (!wdev->wiphy->bands[band])
116 continue;
117 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
118 i++, j++)
119 request->channels[i] =
120 &wdev->wiphy->bands[band]->channels[j];
121 }
122 }
123 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200124 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200125 request->n_ssids = 1;
126
127 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
128 wdev->conn->params.ssid_len);
129 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
130
Johannes Berg463d0182009-07-14 00:33:35 +0200131 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200132 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200133
Johannes Berg79c97e92009-07-07 03:56:12 +0200134 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200135
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200137 if (!err) {
138 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200139 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200140 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200141 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200143 kfree(request);
144 }
145 return err;
146}
147
148static int cfg80211_conn_do_work(struct wireless_dev *wdev)
149{
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200152 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200153 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200154
Johannes Berg667503d2009-07-07 03:56:11 +0200155 ASSERT_WDEV_LOCK(wdev);
156
Johannes Berg6829c872009-07-02 09:13:27 +0200157 if (!wdev->conn)
158 return 0;
159
Johannes Berg19957bb2009-07-02 17:20:43 +0200160 params = &wdev->conn->params;
161
Johannes Berg6829c872009-07-02 09:13:27 +0200162 switch (wdev->conn->state) {
163 case CFG80211_CONN_SCAN_AGAIN:
164 return cfg80211_conn_scan(wdev);
165 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200166 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200167 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200168 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200169 params->channel, params->auth_type,
170 params->bssid,
171 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200172 NULL, 0,
173 params->key, params->key_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300174 params->key_idx, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200175 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200177 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200178 if (wdev->conn->prev_bssid_valid)
179 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200180 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200181 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200182 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200183 params->ssid, params->ssid_len,
184 params->ie, params->ie_len,
185 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200186 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200187 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200188 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300189 WLAN_REASON_DEAUTH_LEAVING,
190 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200191 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900192 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
193 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300195 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900196 /* return an error so that we call __cfg80211_connect_result() */
197 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200198 default:
199 return 0;
200 }
201}
202
203void cfg80211_conn_work(struct work_struct *work)
204{
Johannes Berg79c97e92009-07-07 03:56:12 +0200205 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200206 container_of(work, struct cfg80211_registered_device, conn_work);
207 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100208 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200209
210 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200211 cfg80211_lock_rdev(rdev);
212 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200213
Johannes Berg79c97e92009-07-07 03:56:12 +0200214 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200215 wdev_lock(wdev);
216 if (!netif_running(wdev->netdev)) {
217 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200218 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200219 }
220 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
221 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200222 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200223 }
Johannes Berg7400f422009-10-31 07:40:37 +0100224 if (wdev->conn->params.bssid) {
225 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
226 bssid = bssid_buf;
227 }
Johannes Berg6829c872009-07-02 09:13:27 +0200228 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200229 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900230 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200231 NULL, 0, NULL, 0,
232 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200233 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200234 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200235 }
236
Johannes Berg79c97e92009-07-07 03:56:12 +0200237 mutex_unlock(&rdev->devlist_mtx);
238 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200239 rtnl_unlock();
240}
241
Johannes Bergbbac31f2009-09-16 09:04:26 -0700242static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200243{
Johannes Berg79c97e92009-07-07 03:56:12 +0200244 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200245 struct cfg80211_bss *bss;
246 u16 capa = WLAN_CAPABILITY_ESS;
247
Johannes Berg667503d2009-07-07 03:56:11 +0200248 ASSERT_WDEV_LOCK(wdev);
249
Johannes Berg6829c872009-07-02 09:13:27 +0200250 if (wdev->conn->params.privacy)
251 capa |= WLAN_CAPABILITY_PRIVACY;
252
Jouni Malinened9d0102011-05-16 19:40:15 +0300253 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
254 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200255 wdev->conn->params.ssid,
256 wdev->conn->params.ssid_len,
257 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
258 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700260 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200261
262 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
263 wdev->conn->params.bssid = wdev->conn->bssid;
264 wdev->conn->params.channel = bss->channel;
265 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200266 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200267
Johannes Bergbbac31f2009-09-16 09:04:26 -0700268 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200269}
270
Johannes Berg667503d2009-07-07 03:56:11 +0200271static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200272{
273 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700275 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200276
Johannes Berg667503d2009-07-07 03:56:11 +0200277 ASSERT_WDEV_LOCK(wdev);
278
Johannes Berg6829c872009-07-02 09:13:27 +0200279 if (wdev->sme_state != CFG80211_SME_CONNECTING)
280 return;
281
Zhu Yid4b1a682009-07-16 17:34:14 +0800282 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200283 return;
284
285 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
286 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
287 return;
288
Johannes Bergbbac31f2009-09-16 09:04:26 -0700289 bss = cfg80211_get_conn_bss(wdev);
290 if (bss) {
291 cfg80211_put_bss(bss);
292 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200293 /* not found */
294 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200295 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200296 else
Johannes Berg667503d2009-07-07 03:56:11 +0200297 __cfg80211_connect_result(
298 wdev->netdev,
299 wdev->conn->params.bssid,
300 NULL, 0, NULL, 0,
301 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200302 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200303 }
304}
305
Johannes Berg667503d2009-07-07 03:56:11 +0200306void cfg80211_sme_scan_done(struct net_device *dev)
307{
308 struct wireless_dev *wdev = dev->ieee80211_ptr;
309
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200310 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200311 wdev_lock(wdev);
312 __cfg80211_sme_scan_done(dev);
313 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200314 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200315}
316
317void cfg80211_sme_rx_auth(struct net_device *dev,
318 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200319{
320 struct wireless_dev *wdev = dev->ieee80211_ptr;
321 struct wiphy *wiphy = wdev->wiphy;
322 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
323 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
324 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
325
Johannes Berg667503d2009-07-07 03:56:11 +0200326 ASSERT_WDEV_LOCK(wdev);
327
Johannes Berg6829c872009-07-02 09:13:27 +0200328 /* should only RX auth frames when connecting */
329 if (wdev->sme_state != CFG80211_SME_CONNECTING)
330 return;
331
332 if (WARN_ON(!wdev->conn))
333 return;
334
335 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
336 wdev->conn->auto_auth &&
337 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
338 /* select automatically between only open, shared, leap */
339 switch (wdev->conn->params.auth_type) {
340 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200341 if (wdev->connect_keys)
342 wdev->conn->params.auth_type =
343 NL80211_AUTHTYPE_SHARED_KEY;
344 else
345 wdev->conn->params.auth_type =
346 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200347 break;
348 case NL80211_AUTHTYPE_SHARED_KEY:
349 wdev->conn->params.auth_type =
350 NL80211_AUTHTYPE_NETWORK_EAP;
351 break;
352 default:
353 /* huh? */
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_OPEN_SYSTEM;
356 break;
357 }
358 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
359 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200360 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200361 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200362 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200363 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200364 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
365 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
366 schedule_work(&rdev->conn_work);
367 }
368}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200369
Johannes Bergf401a6f2009-08-07 14:51:05 +0200370bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
371{
372 struct wiphy *wiphy = wdev->wiphy;
373 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
374
375 if (WARN_ON(!wdev->conn))
376 return false;
377
378 if (!wdev->conn->prev_bssid_valid)
379 return false;
380
381 /*
382 * Some stupid APs don't accept reassoc, so we
383 * need to fall back to trying regular assoc.
384 */
385 wdev->conn->prev_bssid_valid = false;
386 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
387 schedule_work(&rdev->conn_work);
388
389 return true;
390}
391
Johannes Berg7d930bc2009-10-20 15:08:53 +0900392void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
393{
394 struct wiphy *wiphy = wdev->wiphy;
395 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
396
397 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
398 schedule_work(&rdev->conn_work);
399}
400
Johannes Berg667503d2009-07-07 03:56:11 +0200401void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
402 const u8 *req_ie, size_t req_ie_len,
403 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200404 u16 status, bool wextev,
405 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200406{
407 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700408 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200409#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200410 union iwreq_data wrqu;
411#endif
412
Johannes Berg667503d2009-07-07 03:56:11 +0200413 ASSERT_WDEV_LOCK(wdev);
414
Johannes Berg074ac8d2010-09-16 14:58:22 +0200415 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
416 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200417 return;
418
Johannes Bergf7969962009-08-21 12:23:49 +0200419 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200420 return;
421
422 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200423 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200424 resp_ie, resp_ie_len,
425 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200426
Johannes Berg3d23e342009-09-29 23:27:28 +0200427#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200428 if (wextev) {
429 if (req_ie && status == WLAN_STATUS_SUCCESS) {
430 memset(&wrqu, 0, sizeof(wrqu));
431 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800432 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200433 }
434
435 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
436 memset(&wrqu, 0, sizeof(wrqu));
437 wrqu.data.length = resp_ie_len;
438 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
439 }
440
441 memset(&wrqu, 0, sizeof(wrqu));
442 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200443 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200444 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200445 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
446 wdev->wext.prev_bssid_valid = true;
447 }
Johannes Berge45cd822009-07-02 09:58:04 +0200448 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
449 }
450#endif
451
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200452 if (wdev->current_bss) {
453 cfg80211_unhold_bss(wdev->current_bss);
454 cfg80211_put_bss(&wdev->current_bss->pub);
455 wdev->current_bss = NULL;
456 }
457
Johannes Berg19957bb2009-07-02 17:20:43 +0200458 if (wdev->conn)
459 wdev->conn->state = CFG80211_CONN_IDLE;
460
Johannes Bergfffd0932009-07-08 14:22:54 +0200461 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200462 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100463 if (wdev->conn)
464 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200465 kfree(wdev->conn);
466 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200467 kfree(wdev->connect_keys);
468 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200469 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200470 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200471 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200472
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200473 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300474 bss = cfg80211_get_bss(wdev->wiphy,
475 wdev->conn ? wdev->conn->params.channel :
476 NULL,
477 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200478 wdev->ssid, wdev->ssid_len,
479 WLAN_CAPABILITY_ESS,
480 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200481
482 if (WARN_ON(!bss))
483 return;
484
485 cfg80211_hold_bss(bss_from_pub(bss));
486 wdev->current_bss = bss_from_pub(bss);
487
Johannes Bergfffd0932009-07-08 14:22:54 +0200488 wdev->sme_state = CFG80211_SME_CONNECTED;
489 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700490
491 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
492
493 if (!country_ie)
494 return;
495
496 /*
497 * ieee80211_bss_get_ie() ensures we can access:
498 * - country_ie + 2, the start of the country ie data, and
499 * - and country_ie[1] which is the IE length
500 */
501 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500502 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700503 country_ie + 2,
504 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200505}
Johannes Bergf2129352009-07-01 21:26:56 +0200506
507void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
508 const u8 *req_ie, size_t req_ie_len,
509 const u8 *resp_ie, size_t resp_ie_len,
510 u16 status, gfp_t gfp)
511{
Johannes Berg667503d2009-07-07 03:56:11 +0200512 struct wireless_dev *wdev = dev->ieee80211_ptr;
513 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
514 struct cfg80211_event *ev;
515 unsigned long flags;
516
Johannes Bergf7969962009-08-21 12:23:49 +0200517 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
518
Johannes Berg667503d2009-07-07 03:56:11 +0200519 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
520 if (!ev)
521 return;
522
523 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800524 if (bssid)
525 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700526 if (req_ie_len) {
527 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
528 ev->cr.req_ie_len = req_ie_len;
529 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
530 }
531 if (resp_ie_len) {
532 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
533 ev->cr.resp_ie_len = resp_ie_len;
534 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
535 }
Johannes Berg667503d2009-07-07 03:56:11 +0200536 ev->cr.status = status;
537
538 spin_lock_irqsave(&wdev->event_lock, flags);
539 list_add_tail(&ev->list, &wdev->event_list);
540 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100541 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200542}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200543EXPORT_SYMBOL(cfg80211_connect_result);
544
Jouni Malinened9d0102011-05-16 19:40:15 +0300545void __cfg80211_roamed(struct wireless_dev *wdev,
546 struct ieee80211_channel *channel,
547 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200548 const u8 *req_ie, size_t req_ie_len,
549 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200550{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200551 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +0200552#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200553 union iwreq_data wrqu;
554#endif
555
Johannes Berg667503d2009-07-07 03:56:11 +0200556 ASSERT_WDEV_LOCK(wdev);
557
Johannes Berg074ac8d2010-09-16 14:58:22 +0200558 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
559 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200560 return;
561
Johannes Bergf7969962009-08-21 12:23:49 +0200562 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200563 return;
564
565 /* internal error -- how did we get to CONNECTED w/o BSS? */
566 if (WARN_ON(!wdev->current_bss)) {
567 return;
568 }
569
570 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200571 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200572 wdev->current_bss = NULL;
573
Jouni Malinened9d0102011-05-16 19:40:15 +0300574 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
Samuel Ortizb23aa672009-07-01 21:26:54 +0200575 wdev->ssid, wdev->ssid_len,
576 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
577
578 if (WARN_ON(!bss))
579 return;
580
Johannes Berg19957bb2009-07-02 17:20:43 +0200581 cfg80211_hold_bss(bss_from_pub(bss));
582 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200583
Johannes Berg667503d2009-07-07 03:56:11 +0200584 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
585 req_ie, req_ie_len, resp_ie, resp_ie_len,
586 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200587
Johannes Berg3d23e342009-09-29 23:27:28 +0200588#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200589 if (req_ie) {
590 memset(&wrqu, 0, sizeof(wrqu));
591 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800592 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200593 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200594 }
595
596 if (resp_ie) {
597 memset(&wrqu, 0, sizeof(wrqu));
598 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200599 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
600 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200601 }
602
603 memset(&wrqu, 0, sizeof(wrqu));
604 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
605 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200606 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
607 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200608 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200609#endif
610}
Johannes Berg667503d2009-07-07 03:56:11 +0200611
Jouni Malinened9d0102011-05-16 19:40:15 +0300612void cfg80211_roamed(struct net_device *dev,
613 struct ieee80211_channel *channel,
614 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200615 const u8 *req_ie, size_t req_ie_len,
616 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
617{
618 struct wireless_dev *wdev = dev->ieee80211_ptr;
619 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
620 struct cfg80211_event *ev;
621 unsigned long flags;
622
Johannes Bergf7969962009-08-21 12:23:49 +0200623 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
624
Johannes Berg667503d2009-07-07 03:56:11 +0200625 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
626 if (!ev)
627 return;
628
629 ev->type = EVENT_ROAMED;
Jouni Malinened9d0102011-05-16 19:40:15 +0300630 ev->rm.channel = channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200631 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
632 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
633 ev->rm.req_ie_len = req_ie_len;
634 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
635 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
636 ev->rm.resp_ie_len = resp_ie_len;
637 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
638
639 spin_lock_irqsave(&wdev->event_lock, flags);
640 list_add_tail(&ev->list, &wdev->event_list);
641 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100642 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200643}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200644EXPORT_SYMBOL(cfg80211_roamed);
645
Johannes Berg667503d2009-07-07 03:56:11 +0200646void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200647 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200648{
649 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200650 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
651 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200652#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200653 union iwreq_data wrqu;
654#endif
655
Johannes Berg667503d2009-07-07 03:56:11 +0200656 ASSERT_WDEV_LOCK(wdev);
657
Johannes Berg074ac8d2010-09-16 14:58:22 +0200658 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
659 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200660 return;
661
Dmitry Shmidtfdfcbc62011-10-18 12:30:02 -0700662#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Johannes Bergf7969962009-08-21 12:23:49 +0200663 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200664 return;
Dmitry Shmidtfdfcbc62011-10-18 12:30:02 -0700665#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +0200666
667 if (wdev->current_bss) {
668 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200669 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200670 }
671
672 wdev->current_bss = NULL;
673 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200674 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200675
Johannes Berg6829c872009-07-02 09:13:27 +0200676 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200677 const u8 *bssid;
678 int ret;
679
Johannes Berg6829c872009-07-02 09:13:27 +0200680 kfree(wdev->conn->ie);
681 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200682 kfree(wdev->conn);
683 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200684
685 /*
686 * If this disconnect was due to a disassoc, we
687 * we might still have an auth BSS around. For
688 * the userspace SME that's currently expected,
689 * but for the kernel SME (nl80211 CONNECT or
690 * wireless extensions) we want to clear up all
691 * state.
692 */
693 for (i = 0; i < MAX_AUTH_BSSES; i++) {
694 if (!wdev->auth_bsses[i])
695 continue;
696 bssid = wdev->auth_bsses[i]->pub.bssid;
697 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300698 WLAN_REASON_DEAUTH_LEAVING,
699 false);
Johannes Bergb6f0b632009-08-06 20:41:34 +0200700 WARN(ret, "deauth failed: %d\n", ret);
701 }
Johannes Berg6829c872009-07-02 09:13:27 +0200702 }
703
Johannes Bergfffd0932009-07-08 14:22:54 +0200704 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
705
706 /*
707 * Delete all the keys ... pairwise keys can't really
708 * exist any more anyway, but default keys might.
709 */
710 if (rdev->ops->del_key)
711 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200712 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713
Johannes Berg3d23e342009-09-29 23:27:28 +0200714#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200715 memset(&wrqu, 0, sizeof(wrqu));
716 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
717 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800718 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200719#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500720
721 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200722}
723
724void cfg80211_disconnected(struct net_device *dev, u16 reason,
725 u8 *ie, size_t ie_len, gfp_t gfp)
726{
Johannes Berg667503d2009-07-07 03:56:11 +0200727 struct wireless_dev *wdev = dev->ieee80211_ptr;
728 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
729 struct cfg80211_event *ev;
730 unsigned long flags;
731
Johannes Bergf7969962009-08-21 12:23:49 +0200732 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
733
Johannes Berg667503d2009-07-07 03:56:11 +0200734 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
735 if (!ev)
736 return;
737
738 ev->type = EVENT_DISCONNECTED;
739 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
740 ev->dc.ie_len = ie_len;
741 memcpy((void *)ev->dc.ie, ie, ie_len);
742 ev->dc.reason = reason;
743
744 spin_lock_irqsave(&wdev->event_lock, flags);
745 list_add_tail(&ev->list, &wdev->event_list);
746 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100747 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748}
749EXPORT_SYMBOL(cfg80211_disconnected);
750
Johannes Berg667503d2009-07-07 03:56:11 +0200751int __cfg80211_connect(struct cfg80211_registered_device *rdev,
752 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200753 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200754 struct cfg80211_cached_keys *connkeys,
755 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200756{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200757 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700758 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200759 int err;
760
761 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762
Dmitry Shmidtf6ec5232011-09-15 09:22:35 -0700763#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200764 if (wdev->sme_state != CFG80211_SME_IDLE)
765 return -EALREADY;
Dmitry Shmidtf6ec5232011-09-15 09:22:35 -0700766#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +0200767
Johannes Bergfffd0932009-07-08 14:22:54 +0200768 if (WARN_ON(wdev->connect_keys)) {
769 kfree(wdev->connect_keys);
770 wdev->connect_keys = NULL;
771 }
772
773 if (connkeys && connkeys->def >= 0) {
774 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200775 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200776
777 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200778 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200779 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200780 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
781 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200782 connect->key_idx = idx;
783 connect->key = connkeys->params[idx].key;
784 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200785
786 /*
787 * If ciphers are not set (e.g. when going through
788 * iwconfig), we have to set them appropriately here.
789 */
790 if (connect->crypto.cipher_group == 0)
791 connect->crypto.cipher_group = cipher;
792
793 if (connect->crypto.n_ciphers_pairwise == 0) {
794 connect->crypto.n_ciphers_pairwise = 1;
795 connect->crypto.ciphers_pairwise[0] = cipher;
796 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200797 }
798 }
799
Samuel Ortizb23aa672009-07-01 21:26:54 +0200800 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200801 if (!rdev->ops->auth || !rdev->ops->assoc)
802 return -EOPNOTSUPP;
803
Johannes Berg19957bb2009-07-02 17:20:43 +0200804 if (WARN_ON(wdev->conn))
805 return -EINPROGRESS;
806
807 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
808 if (!wdev->conn)
809 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200810
811 /*
812 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
813 */
814 memcpy(&wdev->conn->params, connect, sizeof(*connect));
815 if (connect->bssid) {
816 wdev->conn->params.bssid = wdev->conn->bssid;
817 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
818 }
819
820 if (connect->ie) {
821 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
822 GFP_KERNEL);
823 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200824 if (!wdev->conn->ie) {
825 kfree(wdev->conn);
826 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200827 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200828 }
Johannes Berg6829c872009-07-02 09:13:27 +0200829 }
830
831 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
832 wdev->conn->auto_auth = true;
833 /* start with open system ... should mostly work */
834 wdev->conn->params.auth_type =
835 NL80211_AUTHTYPE_OPEN_SYSTEM;
836 } else {
837 wdev->conn->auto_auth = false;
838 }
839
840 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
841 wdev->ssid_len = connect->ssid_len;
842 wdev->conn->params.ssid = wdev->ssid;
843 wdev->conn->params.ssid_len = connect->ssid_len;
844
Johannes Berg8bb89482009-09-26 14:42:53 +0200845 /* see if we have the bss already */
846 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200847
848 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200850
Johannes Bergf401a6f2009-08-07 14:51:05 +0200851 if (prev_bssid) {
852 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
853 wdev->conn->prev_bssid_valid = true;
854 }
855
Johannes Bergbbac31f2009-09-16 09:04:26 -0700856 /* we're good if we have a matching bss struct */
857 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200858 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
859 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700860 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200861 } else {
862 /* otherwise we'll need to scan for the AP first */
863 err = cfg80211_conn_scan(wdev);
864 /*
865 * If we can't scan right now, then we need to scan again
866 * after the current scan finished, since the parameters
867 * changed (unless we find a good AP anyway).
868 */
869 if (err == -EBUSY) {
870 err = 0;
871 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
872 }
873 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200874 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100875 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200876 kfree(wdev->conn);
877 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200878 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200879 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200880 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200881 }
Johannes Berg6829c872009-07-02 09:13:27 +0200882
883 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200884 } else {
885 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200886 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200887 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
888 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200889 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200890 wdev->sme_state = CFG80211_SME_IDLE;
891 return err;
892 }
Johannes Berg6829c872009-07-02 09:13:27 +0200893
894 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
895 wdev->ssid_len = connect->ssid_len;
896
897 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200898 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200899}
900
Johannes Berg667503d2009-07-07 03:56:11 +0200901int cfg80211_connect(struct cfg80211_registered_device *rdev,
902 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200903 struct cfg80211_connect_params *connect,
904 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200905{
906 int err;
907
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200908 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200909 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200910 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200911 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200912 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200913
914 return err;
915}
916
917int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
918 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919{
Johannes Berg6829c872009-07-02 09:13:27 +0200920 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921 int err;
922
Johannes Berg667503d2009-07-07 03:56:11 +0200923 ASSERT_WDEV_LOCK(wdev);
924
Johannes Berg6829c872009-07-02 09:13:27 +0200925 if (wdev->sme_state == CFG80211_SME_IDLE)
926 return -EINVAL;
927
Johannes Bergfffd0932009-07-08 14:22:54 +0200928 kfree(wdev->connect_keys);
929 wdev->connect_keys = NULL;
930
Samuel Ortizb23aa672009-07-01 21:26:54 +0200931 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200932 if (!rdev->ops->deauth)
933 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200934
Johannes Berg19957bb2009-07-02 17:20:43 +0200935 /* was it connected by userspace SME? */
936 if (!wdev->conn) {
937 cfg80211_mlme_down(rdev, dev);
938 return 0;
939 }
Johannes Berg6829c872009-07-02 09:13:27 +0200940
941 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
942 (wdev->conn->state == CFG80211_CONN_SCANNING ||
943 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
944 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100945 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200946 kfree(wdev->conn);
947 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200948 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200949 return 0;
950 }
951
Johannes Berg6829c872009-07-02 09:13:27 +0200952 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200953 err = __cfg80211_mlme_deauth(rdev, dev,
954 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300955 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200956 if (err)
957 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200958 } else {
959 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
960 if (err)
961 return err;
962 }
963
Johannes Berg6829c872009-07-02 09:13:27 +0200964 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200965 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200966 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200967 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
968 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200969 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200970
971 return 0;
972}
Johannes Berg19957bb2009-07-02 17:20:43 +0200973
Johannes Berg667503d2009-07-07 03:56:11 +0200974int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
975 struct net_device *dev,
976 u16 reason, bool wextev)
977{
978 int err;
979
980 wdev_lock(dev->ieee80211_ptr);
981 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
982 wdev_unlock(dev->ieee80211_ptr);
983
984 return err;
985}
986
Johannes Berg19957bb2009-07-02 17:20:43 +0200987void cfg80211_sme_disassoc(struct net_device *dev, int idx)
988{
989 struct wireless_dev *wdev = dev->ieee80211_ptr;
990 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
991 u8 bssid[ETH_ALEN];
992
Johannes Berg667503d2009-07-07 03:56:11 +0200993 ASSERT_WDEV_LOCK(wdev);
994
Johannes Berg19957bb2009-07-02 17:20:43 +0200995 if (!wdev->conn)
996 return;
997
998 if (wdev->conn->state == CFG80211_CONN_IDLE)
999 return;
1000
1001 /*
1002 * Ok, so the association was made by this SME -- we don't
1003 * want it any more so deauthenticate too.
1004 */
1005
1006 if (!wdev->auth_bsses[idx])
1007 return;
1008
1009 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +02001010 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001011 NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
1012 false)) {
Johannes Berg19957bb2009-07-02 17:20:43 +02001013 /* whatever -- assume gone anyway */
1014 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
1015 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1016 wdev->auth_bsses[idx] = NULL;
1017 }
1018}