blob: 7fae7eee65def0b24d17aa1046c37a496bcf04b6 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
2 * SME code for cfg80211's connect emulation.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 */
7
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
10#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020011#include <linux/wireless.h>
12#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <net/cfg80211.h>
14#include <net/rtnetlink.h>
15#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070016#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020017
Johannes Berg6829c872009-07-02 09:13:27 +020018struct cfg80211_conn {
19 struct cfg80211_connect_params params;
20 /* these are sub-states of the _CONNECTING sme_state */
21 enum {
22 CFG80211_CONN_IDLE,
23 CFG80211_CONN_SCANNING,
24 CFG80211_CONN_SCAN_AGAIN,
25 CFG80211_CONN_AUTHENTICATE_NEXT,
26 CFG80211_CONN_AUTHENTICATING,
27 CFG80211_CONN_ASSOCIATE_NEXT,
28 CFG80211_CONN_ASSOCIATING,
29 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020030 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020031 u8 *ie;
32 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020033 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020034};
35
36
37static int cfg80211_conn_scan(struct wireless_dev *wdev)
38{
Johannes Berg79c97e92009-07-07 03:56:12 +020039 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020040 struct cfg80211_scan_request *request;
41 int n_channels, err;
42
43 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020044 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020045 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020046
Johannes Berg79c97e92009-07-07 03:56:12 +020047 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020048 return -EBUSY;
49
50 if (wdev->conn->params.channel) {
51 n_channels = 1;
52 } else {
53 enum ieee80211_band band;
54 n_channels = 0;
55
56 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
57 if (!wdev->wiphy->bands[band])
58 continue;
59 n_channels += wdev->wiphy->bands[band]->n_channels;
60 }
61 }
62 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
63 sizeof(request->channels[0]) * n_channels,
64 GFP_KERNEL);
65 if (!request)
66 return -ENOMEM;
67
Johannes Berg6829c872009-07-02 09:13:27 +020068 if (wdev->conn->params.channel)
69 request->channels[0] = wdev->conn->params.channel;
70 else {
71 int i = 0, j;
72 enum ieee80211_band band;
73
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 if (!wdev->wiphy->bands[band])
76 continue;
77 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
78 i++, j++)
79 request->channels[i] =
80 &wdev->wiphy->bands[band]->channels[j];
81 }
82 }
83 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +020084 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +020085 request->n_ssids = 1;
86
87 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
88 wdev->conn->params.ssid_len);
89 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
90
Johannes Berg463d0182009-07-14 00:33:35 +020091 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +020092 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +020093
Johannes Berg79c97e92009-07-07 03:56:12 +020094 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +020095
Johannes Berg79c97e92009-07-07 03:56:12 +020096 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +020097 if (!err) {
98 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +020099 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200100 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200101 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200102 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200103 kfree(request);
104 }
105 return err;
106}
107
108static int cfg80211_conn_do_work(struct wireless_dev *wdev)
109{
Johannes Berg79c97e92009-07-07 03:56:12 +0200110 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200111 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200112 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200113 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200114
Johannes Berg667503d2009-07-07 03:56:11 +0200115 ASSERT_WDEV_LOCK(wdev);
116
Johannes Berg6829c872009-07-02 09:13:27 +0200117 if (!wdev->conn)
118 return 0;
119
Johannes Berg19957bb2009-07-02 17:20:43 +0200120 params = &wdev->conn->params;
121
Johannes Berg6829c872009-07-02 09:13:27 +0200122 switch (wdev->conn->state) {
123 case CFG80211_CONN_SCAN_AGAIN:
124 return cfg80211_conn_scan(wdev);
125 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200127 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200128 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200129 params->channel, params->auth_type,
130 params->bssid,
131 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200132 NULL, 0,
133 params->key, params->key_len,
134 params->key_idx);
Johannes Berg6829c872009-07-02 09:13:27 +0200135 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200137 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200138 if (wdev->conn->prev_bssid_valid)
139 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200140 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200141 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200142 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200143 params->ssid, params->ssid_len,
144 params->ie, params->ie_len,
145 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200146 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200147 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200148 NULL, 0,
149 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200150 return err;
Johannes Berg6829c872009-07-02 09:13:27 +0200151 default:
152 return 0;
153 }
154}
155
156void cfg80211_conn_work(struct work_struct *work)
157{
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200159 container_of(work, struct cfg80211_registered_device, conn_work);
160 struct wireless_dev *wdev;
161
162 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200163 cfg80211_lock_rdev(rdev);
164 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200165
Johannes Berg79c97e92009-07-07 03:56:12 +0200166 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200167 wdev_lock(wdev);
168 if (!netif_running(wdev->netdev)) {
169 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200170 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200171 }
172 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
173 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200174 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200175 }
Johannes Berg6829c872009-07-02 09:13:27 +0200176 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200177 __cfg80211_connect_result(
178 wdev->netdev,
179 wdev->conn->params.bssid,
180 NULL, 0, NULL, 0,
181 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200182 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200183 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200184 }
185
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 mutex_unlock(&rdev->devlist_mtx);
187 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200188 rtnl_unlock();
189}
190
Johannes Bergbbac31f2009-09-16 09:04:26 -0700191static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200192{
Johannes Berg79c97e92009-07-07 03:56:12 +0200193 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200194 struct cfg80211_bss *bss;
195 u16 capa = WLAN_CAPABILITY_ESS;
196
Johannes Berg667503d2009-07-07 03:56:11 +0200197 ASSERT_WDEV_LOCK(wdev);
198
Johannes Berg6829c872009-07-02 09:13:27 +0200199 if (wdev->conn->params.privacy)
200 capa |= WLAN_CAPABILITY_PRIVACY;
201
202 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
203 wdev->conn->params.ssid,
204 wdev->conn->params.ssid_len,
205 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
206 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200207 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700208 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200209
210 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
211 wdev->conn->params.bssid = wdev->conn->bssid;
212 wdev->conn->params.channel = bss->channel;
213 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200214 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200215
Johannes Bergbbac31f2009-09-16 09:04:26 -0700216 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200217}
218
Johannes Berg667503d2009-07-07 03:56:11 +0200219static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200220{
221 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700223 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200224
Johannes Berg667503d2009-07-07 03:56:11 +0200225 ASSERT_WDEV_LOCK(wdev);
226
Johannes Berg6829c872009-07-02 09:13:27 +0200227 if (wdev->sme_state != CFG80211_SME_CONNECTING)
228 return;
229
Zhu Yid4b1a682009-07-16 17:34:14 +0800230 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200231 return;
232
233 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
234 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
235 return;
236
Johannes Bergbbac31f2009-09-16 09:04:26 -0700237 bss = cfg80211_get_conn_bss(wdev);
238 if (bss) {
239 cfg80211_put_bss(bss);
240 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200241 /* not found */
242 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200243 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200244 else
Johannes Berg667503d2009-07-07 03:56:11 +0200245 __cfg80211_connect_result(
246 wdev->netdev,
247 wdev->conn->params.bssid,
248 NULL, 0, NULL, 0,
249 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200250 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200251 }
252}
253
Johannes Berg667503d2009-07-07 03:56:11 +0200254void cfg80211_sme_scan_done(struct net_device *dev)
255{
256 struct wireless_dev *wdev = dev->ieee80211_ptr;
257
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200258 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200259 wdev_lock(wdev);
260 __cfg80211_sme_scan_done(dev);
261 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200262 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200263}
264
265void cfg80211_sme_rx_auth(struct net_device *dev,
266 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200267{
268 struct wireless_dev *wdev = dev->ieee80211_ptr;
269 struct wiphy *wiphy = wdev->wiphy;
270 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
271 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
272 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
273
Johannes Berg667503d2009-07-07 03:56:11 +0200274 ASSERT_WDEV_LOCK(wdev);
275
Johannes Berg6829c872009-07-02 09:13:27 +0200276 /* should only RX auth frames when connecting */
277 if (wdev->sme_state != CFG80211_SME_CONNECTING)
278 return;
279
280 if (WARN_ON(!wdev->conn))
281 return;
282
283 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
284 wdev->conn->auto_auth &&
285 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
286 /* select automatically between only open, shared, leap */
287 switch (wdev->conn->params.auth_type) {
288 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200289 if (wdev->connect_keys)
290 wdev->conn->params.auth_type =
291 NL80211_AUTHTYPE_SHARED_KEY;
292 else
293 wdev->conn->params.auth_type =
294 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200295 break;
296 case NL80211_AUTHTYPE_SHARED_KEY:
297 wdev->conn->params.auth_type =
298 NL80211_AUTHTYPE_NETWORK_EAP;
299 break;
300 default:
301 /* huh? */
302 wdev->conn->params.auth_type =
303 NL80211_AUTHTYPE_OPEN_SYSTEM;
304 break;
305 }
306 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
307 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200308 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200309 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200310 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200311 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200312 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
313 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
314 schedule_work(&rdev->conn_work);
315 }
316}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200317
Johannes Bergf401a6f2009-08-07 14:51:05 +0200318bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
319{
320 struct wiphy *wiphy = wdev->wiphy;
321 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
322
323 if (WARN_ON(!wdev->conn))
324 return false;
325
326 if (!wdev->conn->prev_bssid_valid)
327 return false;
328
329 /*
330 * Some stupid APs don't accept reassoc, so we
331 * need to fall back to trying regular assoc.
332 */
333 wdev->conn->prev_bssid_valid = false;
334 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
335 schedule_work(&rdev->conn_work);
336
337 return true;
338}
339
Johannes Berg667503d2009-07-07 03:56:11 +0200340void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
341 const u8 *req_ie, size_t req_ie_len,
342 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200343 u16 status, bool wextev,
344 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200345{
346 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700347 u8 *country_ie;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200348#ifdef CONFIG_WIRELESS_EXT
349 union iwreq_data wrqu;
350#endif
351
Johannes Berg667503d2009-07-07 03:56:11 +0200352 ASSERT_WDEV_LOCK(wdev);
353
Samuel Ortizb23aa672009-07-01 21:26:54 +0200354 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
355 return;
356
Johannes Bergf7969962009-08-21 12:23:49 +0200357 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200358 return;
359
360 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200361 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200362 resp_ie, resp_ie_len,
363 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200364
365#ifdef CONFIG_WIRELESS_EXT
366 if (wextev) {
367 if (req_ie && status == WLAN_STATUS_SUCCESS) {
368 memset(&wrqu, 0, sizeof(wrqu));
369 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800370 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200371 }
372
373 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
374 memset(&wrqu, 0, sizeof(wrqu));
375 wrqu.data.length = resp_ie_len;
376 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
377 }
378
379 memset(&wrqu, 0, sizeof(wrqu));
380 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200381 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200382 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200383 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
384 wdev->wext.prev_bssid_valid = true;
385 }
Johannes Berge45cd822009-07-02 09:58:04 +0200386 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
387 }
388#endif
389
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200390 if (wdev->current_bss) {
391 cfg80211_unhold_bss(wdev->current_bss);
392 cfg80211_put_bss(&wdev->current_bss->pub);
393 wdev->current_bss = NULL;
394 }
395
Johannes Berg19957bb2009-07-02 17:20:43 +0200396 if (wdev->conn)
397 wdev->conn->state = CFG80211_CONN_IDLE;
398
Johannes Bergfffd0932009-07-08 14:22:54 +0200399 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200400 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100401 if (wdev->conn)
402 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200403 kfree(wdev->conn);
404 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200405 kfree(wdev->connect_keys);
406 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200407 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200408 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200409 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200410
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200411 if (!bss)
412 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
413 wdev->ssid, wdev->ssid_len,
414 WLAN_CAPABILITY_ESS,
415 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200416
417 if (WARN_ON(!bss))
418 return;
419
420 cfg80211_hold_bss(bss_from_pub(bss));
421 wdev->current_bss = bss_from_pub(bss);
422
Johannes Bergfffd0932009-07-08 14:22:54 +0200423 wdev->sme_state = CFG80211_SME_CONNECTED;
424 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700425
426 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
427
428 if (!country_ie)
429 return;
430
431 /*
432 * ieee80211_bss_get_ie() ensures we can access:
433 * - country_ie + 2, the start of the country ie data, and
434 * - and country_ie[1] which is the IE length
435 */
436 regulatory_hint_11d(wdev->wiphy,
437 country_ie + 2,
438 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200439}
Johannes Bergf2129352009-07-01 21:26:56 +0200440
441void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
442 const u8 *req_ie, size_t req_ie_len,
443 const u8 *resp_ie, size_t resp_ie_len,
444 u16 status, gfp_t gfp)
445{
Johannes Berg667503d2009-07-07 03:56:11 +0200446 struct wireless_dev *wdev = dev->ieee80211_ptr;
447 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
448 struct cfg80211_event *ev;
449 unsigned long flags;
450
Johannes Bergf7969962009-08-21 12:23:49 +0200451 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
452
Johannes Berg667503d2009-07-07 03:56:11 +0200453 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
454 if (!ev)
455 return;
456
457 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800458 if (bssid)
459 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200460 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
461 ev->cr.req_ie_len = req_ie_len;
462 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
463 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
464 ev->cr.resp_ie_len = resp_ie_len;
465 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
466 ev->cr.status = status;
467
468 spin_lock_irqsave(&wdev->event_lock, flags);
469 list_add_tail(&ev->list, &wdev->event_list);
470 spin_unlock_irqrestore(&wdev->event_lock, flags);
471 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200472}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200473EXPORT_SYMBOL(cfg80211_connect_result);
474
Johannes Berg667503d2009-07-07 03:56:11 +0200475void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
476 const u8 *req_ie, size_t req_ie_len,
477 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200478{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200479 struct cfg80211_bss *bss;
480#ifdef CONFIG_WIRELESS_EXT
481 union iwreq_data wrqu;
482#endif
483
Johannes Berg667503d2009-07-07 03:56:11 +0200484 ASSERT_WDEV_LOCK(wdev);
485
Samuel Ortizb23aa672009-07-01 21:26:54 +0200486 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
487 return;
488
Johannes Bergf7969962009-08-21 12:23:49 +0200489 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200490 return;
491
492 /* internal error -- how did we get to CONNECTED w/o BSS? */
493 if (WARN_ON(!wdev->current_bss)) {
494 return;
495 }
496
497 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200498 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200499 wdev->current_bss = NULL;
500
501 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
502 wdev->ssid, wdev->ssid_len,
503 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
504
505 if (WARN_ON(!bss))
506 return;
507
Johannes Berg19957bb2009-07-02 17:20:43 +0200508 cfg80211_hold_bss(bss_from_pub(bss));
509 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200510
Johannes Berg667503d2009-07-07 03:56:11 +0200511 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
512 req_ie, req_ie_len, resp_ie, resp_ie_len,
513 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200514
515#ifdef CONFIG_WIRELESS_EXT
516 if (req_ie) {
517 memset(&wrqu, 0, sizeof(wrqu));
518 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800519 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200520 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200521 }
522
523 if (resp_ie) {
524 memset(&wrqu, 0, sizeof(wrqu));
525 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200526 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
527 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200528 }
529
530 memset(&wrqu, 0, sizeof(wrqu));
531 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
532 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200533 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
534 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200535 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200536#endif
537}
Johannes Berg667503d2009-07-07 03:56:11 +0200538
539void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
540 const u8 *req_ie, size_t req_ie_len,
541 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
542{
543 struct wireless_dev *wdev = dev->ieee80211_ptr;
544 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
545 struct cfg80211_event *ev;
546 unsigned long flags;
547
Johannes Bergf7969962009-08-21 12:23:49 +0200548 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
549
Johannes Berg667503d2009-07-07 03:56:11 +0200550 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
551 if (!ev)
552 return;
553
554 ev->type = EVENT_ROAMED;
555 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
556 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
557 ev->rm.req_ie_len = req_ie_len;
558 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
559 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
560 ev->rm.resp_ie_len = resp_ie_len;
561 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
562
563 spin_lock_irqsave(&wdev->event_lock, flags);
564 list_add_tail(&ev->list, &wdev->event_list);
565 spin_unlock_irqrestore(&wdev->event_lock, flags);
566 schedule_work(&rdev->event_work);
567}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200568EXPORT_SYMBOL(cfg80211_roamed);
569
Johannes Berg667503d2009-07-07 03:56:11 +0200570void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200571 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200572{
573 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200574 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
575 int i;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200576#ifdef CONFIG_WIRELESS_EXT
577 union iwreq_data wrqu;
578#endif
579
Johannes Berg667503d2009-07-07 03:56:11 +0200580 ASSERT_WDEV_LOCK(wdev);
581
Samuel Ortizb23aa672009-07-01 21:26:54 +0200582 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
583 return;
584
Johannes Bergf7969962009-08-21 12:23:49 +0200585 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586 return;
587
588 if (wdev->current_bss) {
589 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200590 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591 }
592
593 wdev->current_bss = NULL;
594 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200595 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200596
Johannes Berg6829c872009-07-02 09:13:27 +0200597 if (wdev->conn) {
Johannes Bergb6f0b632009-08-06 20:41:34 +0200598 const u8 *bssid;
599 int ret;
600
Johannes Berg6829c872009-07-02 09:13:27 +0200601 kfree(wdev->conn->ie);
602 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200603 kfree(wdev->conn);
604 wdev->conn = NULL;
Johannes Bergb6f0b632009-08-06 20:41:34 +0200605
606 /*
607 * If this disconnect was due to a disassoc, we
608 * we might still have an auth BSS around. For
609 * the userspace SME that's currently expected,
610 * but for the kernel SME (nl80211 CONNECT or
611 * wireless extensions) we want to clear up all
612 * state.
613 */
614 for (i = 0; i < MAX_AUTH_BSSES; i++) {
615 if (!wdev->auth_bsses[i])
616 continue;
617 bssid = wdev->auth_bsses[i]->pub.bssid;
618 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
619 WLAN_REASON_DEAUTH_LEAVING);
620 WARN(ret, "deauth failed: %d\n", ret);
621 }
Johannes Berg6829c872009-07-02 09:13:27 +0200622 }
623
Johannes Bergfffd0932009-07-08 14:22:54 +0200624 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
625
626 /*
627 * Delete all the keys ... pairwise keys can't really
628 * exist any more anyway, but default keys might.
629 */
630 if (rdev->ops->del_key)
631 for (i = 0; i < 6; i++)
632 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200633
634#ifdef CONFIG_WIRELESS_EXT
635 memset(&wrqu, 0, sizeof(wrqu));
636 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
637 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
638#endif
639}
640
641void cfg80211_disconnected(struct net_device *dev, u16 reason,
642 u8 *ie, size_t ie_len, gfp_t gfp)
643{
Johannes Berg667503d2009-07-07 03:56:11 +0200644 struct wireless_dev *wdev = dev->ieee80211_ptr;
645 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
646 struct cfg80211_event *ev;
647 unsigned long flags;
648
Johannes Bergf7969962009-08-21 12:23:49 +0200649 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
650
Johannes Berg667503d2009-07-07 03:56:11 +0200651 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
652 if (!ev)
653 return;
654
655 ev->type = EVENT_DISCONNECTED;
656 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
657 ev->dc.ie_len = ie_len;
658 memcpy((void *)ev->dc.ie, ie, ie_len);
659 ev->dc.reason = reason;
660
661 spin_lock_irqsave(&wdev->event_lock, flags);
662 list_add_tail(&ev->list, &wdev->event_list);
663 spin_unlock_irqrestore(&wdev->event_lock, flags);
664 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200665}
666EXPORT_SYMBOL(cfg80211_disconnected);
667
Johannes Berg667503d2009-07-07 03:56:11 +0200668int __cfg80211_connect(struct cfg80211_registered_device *rdev,
669 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200670 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200671 struct cfg80211_cached_keys *connkeys,
672 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200673{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200674 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200675 struct ieee80211_channel *chan;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700676 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200677 int err;
678
679 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200680
681 if (wdev->sme_state != CFG80211_SME_IDLE)
682 return -EALREADY;
683
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200684 chan = rdev_fixed_channel(rdev, wdev);
685 if (chan && chan != connect->channel)
686 return -EBUSY;
687
Johannes Bergfffd0932009-07-08 14:22:54 +0200688 if (WARN_ON(wdev->connect_keys)) {
689 kfree(wdev->connect_keys);
690 wdev->connect_keys = NULL;
691 }
692
693 if (connkeys && connkeys->def >= 0) {
694 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200695 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200696
697 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200698 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200699 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200700 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
701 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200702 connect->key_idx = idx;
703 connect->key = connkeys->params[idx].key;
704 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200705
706 /*
707 * If ciphers are not set (e.g. when going through
708 * iwconfig), we have to set them appropriately here.
709 */
710 if (connect->crypto.cipher_group == 0)
711 connect->crypto.cipher_group = cipher;
712
713 if (connect->crypto.n_ciphers_pairwise == 0) {
714 connect->crypto.n_ciphers_pairwise = 1;
715 connect->crypto.ciphers_pairwise[0] = cipher;
716 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200717 }
718 }
719
Samuel Ortizb23aa672009-07-01 21:26:54 +0200720 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200721 if (!rdev->ops->auth || !rdev->ops->assoc)
722 return -EOPNOTSUPP;
723
Johannes Berg19957bb2009-07-02 17:20:43 +0200724 if (WARN_ON(wdev->conn))
725 return -EINPROGRESS;
726
727 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
728 if (!wdev->conn)
729 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200730
731 /*
732 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
733 */
734 memcpy(&wdev->conn->params, connect, sizeof(*connect));
735 if (connect->bssid) {
736 wdev->conn->params.bssid = wdev->conn->bssid;
737 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
738 }
739
740 if (connect->ie) {
741 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
742 GFP_KERNEL);
743 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200744 if (!wdev->conn->ie) {
745 kfree(wdev->conn);
746 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200747 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200748 }
Johannes Berg6829c872009-07-02 09:13:27 +0200749 }
750
751 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
752 wdev->conn->auto_auth = true;
753 /* start with open system ... should mostly work */
754 wdev->conn->params.auth_type =
755 NL80211_AUTHTYPE_OPEN_SYSTEM;
756 } else {
757 wdev->conn->auto_auth = false;
758 }
759
760 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
761 wdev->ssid_len = connect->ssid_len;
762 wdev->conn->params.ssid = wdev->ssid;
763 wdev->conn->params.ssid_len = connect->ssid_len;
764
765 /* don't care about result -- but fill bssid & channel */
766 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700767 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200768
769 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200770 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200771
Johannes Bergf401a6f2009-08-07 14:51:05 +0200772 if (prev_bssid) {
773 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
774 wdev->conn->prev_bssid_valid = true;
775 }
776
Johannes Bergbbac31f2009-09-16 09:04:26 -0700777 /* we're good if we have a matching bss struct */
778 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200779 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
780 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700781 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200782 } else {
783 /* otherwise we'll need to scan for the AP first */
784 err = cfg80211_conn_scan(wdev);
785 /*
786 * If we can't scan right now, then we need to scan again
787 * after the current scan finished, since the parameters
788 * changed (unless we find a good AP anyway).
789 */
790 if (err == -EBUSY) {
791 err = 0;
792 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
793 }
794 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200795 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100796 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200797 kfree(wdev->conn);
798 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200799 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200800 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200801 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200802 }
Johannes Berg6829c872009-07-02 09:13:27 +0200803
804 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200805 } else {
806 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200807 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200808 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
809 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200810 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200811 wdev->sme_state = CFG80211_SME_IDLE;
812 return err;
813 }
Johannes Berg6829c872009-07-02 09:13:27 +0200814
815 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
816 wdev->ssid_len = connect->ssid_len;
817
818 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200819 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200820}
821
Johannes Berg667503d2009-07-07 03:56:11 +0200822int cfg80211_connect(struct cfg80211_registered_device *rdev,
823 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200824 struct cfg80211_connect_params *connect,
825 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200826{
827 int err;
828
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200829 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200830 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200831 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200832 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200833 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200834
835 return err;
836}
837
838int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
839 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200840{
Johannes Berg6829c872009-07-02 09:13:27 +0200841 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200842 int err;
843
Johannes Berg667503d2009-07-07 03:56:11 +0200844 ASSERT_WDEV_LOCK(wdev);
845
Johannes Berg6829c872009-07-02 09:13:27 +0200846 if (wdev->sme_state == CFG80211_SME_IDLE)
847 return -EINVAL;
848
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 kfree(wdev->connect_keys);
850 wdev->connect_keys = NULL;
851
Samuel Ortizb23aa672009-07-01 21:26:54 +0200852 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200853 if (!rdev->ops->deauth)
854 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200855
Johannes Berg19957bb2009-07-02 17:20:43 +0200856 /* was it connected by userspace SME? */
857 if (!wdev->conn) {
858 cfg80211_mlme_down(rdev, dev);
859 return 0;
860 }
Johannes Berg6829c872009-07-02 09:13:27 +0200861
862 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
863 (wdev->conn->state == CFG80211_CONN_SCANNING ||
864 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
865 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100866 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200867 kfree(wdev->conn);
868 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200869 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200870 return 0;
871 }
872
Johannes Berg6829c872009-07-02 09:13:27 +0200873 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200874 err = __cfg80211_mlme_deauth(rdev, dev,
875 wdev->conn->params.bssid,
876 NULL, 0, reason);
Johannes Berg6829c872009-07-02 09:13:27 +0200877 if (err)
878 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200879 } else {
880 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
881 if (err)
882 return err;
883 }
884
Johannes Berg6829c872009-07-02 09:13:27 +0200885 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200886 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200887 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200888 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
889 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200890 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200891
892 return 0;
893}
Johannes Berg19957bb2009-07-02 17:20:43 +0200894
Johannes Berg667503d2009-07-07 03:56:11 +0200895int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
896 struct net_device *dev,
897 u16 reason, bool wextev)
898{
899 int err;
900
901 wdev_lock(dev->ieee80211_ptr);
902 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
903 wdev_unlock(dev->ieee80211_ptr);
904
905 return err;
906}
907
Johannes Berg19957bb2009-07-02 17:20:43 +0200908void cfg80211_sme_disassoc(struct net_device *dev, int idx)
909{
910 struct wireless_dev *wdev = dev->ieee80211_ptr;
911 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
912 u8 bssid[ETH_ALEN];
913
Johannes Berg667503d2009-07-07 03:56:11 +0200914 ASSERT_WDEV_LOCK(wdev);
915
Johannes Berg19957bb2009-07-02 17:20:43 +0200916 if (!wdev->conn)
917 return;
918
919 if (wdev->conn->state == CFG80211_CONN_IDLE)
920 return;
921
922 /*
923 * Ok, so the association was made by this SME -- we don't
924 * want it any more so deauthenticate too.
925 */
926
927 if (!wdev->auth_bsses[idx])
928 return;
929
930 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200931 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
932 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200933 /* whatever -- assume gone anyway */
934 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
935 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
936 wdev->auth_bsses[idx] = NULL;
937 }
938}