blob: af91192eedf5b2fc5c1fb3083aaa3fbbacf733b8 [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;
30 u8 bssid[ETH_ALEN];
31 u8 *ie;
32 size_t ie_len;
33 bool auto_auth;
34};
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
68 request->channels = (void *)((char *)request + sizeof(*request));
69 if (wdev->conn->params.channel)
70 request->channels[0] = wdev->conn->params.channel;
71 else {
72 int i = 0, j;
73 enum ieee80211_band band;
74
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 if (!wdev->wiphy->bands[band])
77 continue;
78 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
79 i++, j++)
80 request->channels[i] =
81 &wdev->wiphy->bands[band]->channels[j];
82 }
83 }
84 request->n_channels = n_channels;
85 request->ssids = (void *)(request->channels + n_channels);
86 request->n_ssids = 1;
87
88 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
89 wdev->conn->params.ssid_len);
90 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
91
Johannes Berg463d0182009-07-14 00:33:35 +020092 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +020093 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +020094
Johannes Berg79c97e92009-07-07 03:56:12 +020095 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +020096
Johannes Berg79c97e92009-07-07 03:56:12 +020097 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +020098 if (!err) {
99 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200100 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200101 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200102 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200103 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200104 kfree(request);
105 }
106 return err;
107}
108
109static int cfg80211_conn_do_work(struct wireless_dev *wdev)
110{
Johannes Berg79c97e92009-07-07 03:56:12 +0200111 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200112 struct cfg80211_connect_params *params;
113 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 Berg3e5d7642009-07-07 14:37:26 +0200138 /*
139 * We could, later, implement roaming here and then actually
140 * set prev_bssid to non-NULL. But then we need to be aware
141 * that some APs don't like that -- so we'd need to retry
142 * the association.
143 */
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200145 params->channel, params->bssid,
146 NULL,
147 params->ssid, params->ssid_len,
148 params->ie, params->ie_len,
149 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200150 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200151 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200152 NULL, 0,
153 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200154 return err;
Johannes Berg6829c872009-07-02 09:13:27 +0200155 default:
156 return 0;
157 }
158}
159
160void cfg80211_conn_work(struct work_struct *work)
161{
Johannes Berg79c97e92009-07-07 03:56:12 +0200162 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200163 container_of(work, struct cfg80211_registered_device, conn_work);
164 struct wireless_dev *wdev;
165
166 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200167 cfg80211_lock_rdev(rdev);
168 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200169
Johannes Berg79c97e92009-07-07 03:56:12 +0200170 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200171 wdev_lock(wdev);
172 if (!netif_running(wdev->netdev)) {
173 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200174 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200175 }
176 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
177 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200178 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200179 }
Johannes Berg6829c872009-07-02 09:13:27 +0200180 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200181 __cfg80211_connect_result(
182 wdev->netdev,
183 wdev->conn->params.bssid,
184 NULL, 0, NULL, 0,
185 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200186 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200187 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200188 }
189
Johannes Berg79c97e92009-07-07 03:56:12 +0200190 mutex_unlock(&rdev->devlist_mtx);
191 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200192 rtnl_unlock();
193}
194
195static bool cfg80211_get_conn_bss(struct wireless_dev *wdev)
196{
Johannes Berg79c97e92009-07-07 03:56:12 +0200197 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200198 struct cfg80211_bss *bss;
199 u16 capa = WLAN_CAPABILITY_ESS;
200
Johannes Berg667503d2009-07-07 03:56:11 +0200201 ASSERT_WDEV_LOCK(wdev);
202
Johannes Berg6829c872009-07-02 09:13:27 +0200203 if (wdev->conn->params.privacy)
204 capa |= WLAN_CAPABILITY_PRIVACY;
205
206 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
207 wdev->conn->params.ssid,
208 wdev->conn->params.ssid_len,
209 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
210 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200211 if (!bss)
212 return false;
213
214 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
215 wdev->conn->params.bssid = wdev->conn->bssid;
216 wdev->conn->params.channel = bss->channel;
217 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200218 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200219
220 cfg80211_put_bss(bss);
221 return true;
222}
223
Johannes Berg667503d2009-07-07 03:56:11 +0200224static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200225{
226 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200227 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200228
Johannes Berg667503d2009-07-07 03:56:11 +0200229 ASSERT_WDEV_LOCK(wdev);
230
Johannes Berg6829c872009-07-02 09:13:27 +0200231 if (wdev->sme_state != CFG80211_SME_CONNECTING)
232 return;
233
Zhu Yid4b1a682009-07-16 17:34:14 +0800234 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200235 return;
236
237 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
238 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
239 return;
240
241 if (!cfg80211_get_conn_bss(wdev)) {
242 /* not found */
243 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200244 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200245 else
Johannes Berg667503d2009-07-07 03:56:11 +0200246 __cfg80211_connect_result(
247 wdev->netdev,
248 wdev->conn->params.bssid,
249 NULL, 0, NULL, 0,
250 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200251 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200252 }
253}
254
Johannes Berg667503d2009-07-07 03:56:11 +0200255void cfg80211_sme_scan_done(struct net_device *dev)
256{
257 struct wireless_dev *wdev = dev->ieee80211_ptr;
258
259 wdev_lock(wdev);
260 __cfg80211_sme_scan_done(dev);
261 wdev_unlock(wdev);
262}
263
264void cfg80211_sme_rx_auth(struct net_device *dev,
265 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200266{
267 struct wireless_dev *wdev = dev->ieee80211_ptr;
268 struct wiphy *wiphy = wdev->wiphy;
269 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
270 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
271 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
272
Johannes Berg667503d2009-07-07 03:56:11 +0200273 ASSERT_WDEV_LOCK(wdev);
274
Johannes Berg6829c872009-07-02 09:13:27 +0200275 /* should only RX auth frames when connecting */
276 if (wdev->sme_state != CFG80211_SME_CONNECTING)
277 return;
278
279 if (WARN_ON(!wdev->conn))
280 return;
281
282 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
283 wdev->conn->auto_auth &&
284 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
285 /* select automatically between only open, shared, leap */
286 switch (wdev->conn->params.auth_type) {
287 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200288 if (wdev->connect_keys)
289 wdev->conn->params.auth_type =
290 NL80211_AUTHTYPE_SHARED_KEY;
291 else
292 wdev->conn->params.auth_type =
293 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200294 break;
295 case NL80211_AUTHTYPE_SHARED_KEY:
296 wdev->conn->params.auth_type =
297 NL80211_AUTHTYPE_NETWORK_EAP;
298 break;
299 default:
300 /* huh? */
301 wdev->conn->params.auth_type =
302 NL80211_AUTHTYPE_OPEN_SYSTEM;
303 break;
304 }
305 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
306 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200307 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200308 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200309 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200310 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200311 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
312 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
313 schedule_work(&rdev->conn_work);
314 }
315}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200316
Johannes Berg667503d2009-07-07 03:56:11 +0200317void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
318 const u8 *req_ie, size_t req_ie_len,
319 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200320 u16 status, bool wextev,
321 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200322{
323 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700324 u8 *country_ie;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200325#ifdef CONFIG_WIRELESS_EXT
326 union iwreq_data wrqu;
327#endif
328
Johannes Berg667503d2009-07-07 03:56:11 +0200329 ASSERT_WDEV_LOCK(wdev);
330
Samuel Ortizb23aa672009-07-01 21:26:54 +0200331 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
332 return;
333
Johannes Berge45cd822009-07-02 09:58:04 +0200334 if (wdev->sme_state == CFG80211_SME_CONNECTED)
335 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), dev,
336 bssid, req_ie, req_ie_len,
Johannes Berg667503d2009-07-07 03:56:11 +0200337 resp_ie, resp_ie_len, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200338 else
339 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
340 bssid, req_ie, req_ie_len,
341 resp_ie, resp_ie_len,
Johannes Berg667503d2009-07-07 03:56:11 +0200342 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200343
344#ifdef CONFIG_WIRELESS_EXT
345 if (wextev) {
346 if (req_ie && status == WLAN_STATUS_SUCCESS) {
347 memset(&wrqu, 0, sizeof(wrqu));
348 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800349 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200350 }
351
352 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
353 memset(&wrqu, 0, sizeof(wrqu));
354 wrqu.data.length = resp_ie_len;
355 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
356 }
357
358 memset(&wrqu, 0, sizeof(wrqu));
359 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
360 if (bssid && status == WLAN_STATUS_SUCCESS)
361 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
362 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
363 }
364#endif
365
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200366 if (wdev->current_bss) {
367 cfg80211_unhold_bss(wdev->current_bss);
368 cfg80211_put_bss(&wdev->current_bss->pub);
369 wdev->current_bss = NULL;
370 }
371
Johannes Berge45cd822009-07-02 09:58:04 +0200372 if (status == WLAN_STATUS_SUCCESS &&
Johannes Bergfffd0932009-07-08 14:22:54 +0200373 wdev->sme_state == CFG80211_SME_IDLE)
374 goto success;
Johannes Berge45cd822009-07-02 09:58:04 +0200375
Johannes Berg6829c872009-07-02 09:13:27 +0200376 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200377 return;
378
Johannes Berg19957bb2009-07-02 17:20:43 +0200379 if (wdev->conn)
380 wdev->conn->state = CFG80211_CONN_IDLE;
381
Johannes Bergfffd0932009-07-08 14:22:54 +0200382 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200383 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg19957bb2009-07-02 17:20:43 +0200384 kfree(wdev->conn);
385 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200386 kfree(wdev->connect_keys);
387 wdev->connect_keys = NULL;
388 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200389 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200390
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200391 success:
392 if (!bss)
393 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
394 wdev->ssid, wdev->ssid_len,
395 WLAN_CAPABILITY_ESS,
396 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200397
398 if (WARN_ON(!bss))
399 return;
400
401 cfg80211_hold_bss(bss_from_pub(bss));
402 wdev->current_bss = bss_from_pub(bss);
403
Johannes Bergfffd0932009-07-08 14:22:54 +0200404 wdev->sme_state = CFG80211_SME_CONNECTED;
405 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700406
407 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
408
409 if (!country_ie)
410 return;
411
412 /*
413 * ieee80211_bss_get_ie() ensures we can access:
414 * - country_ie + 2, the start of the country ie data, and
415 * - and country_ie[1] which is the IE length
416 */
417 regulatory_hint_11d(wdev->wiphy,
418 country_ie + 2,
419 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200420}
Johannes Bergf2129352009-07-01 21:26:56 +0200421
422void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
423 const u8 *req_ie, size_t req_ie_len,
424 const u8 *resp_ie, size_t resp_ie_len,
425 u16 status, gfp_t gfp)
426{
Johannes Berg667503d2009-07-07 03:56:11 +0200427 struct wireless_dev *wdev = dev->ieee80211_ptr;
428 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
429 struct cfg80211_event *ev;
430 unsigned long flags;
431
432 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
433 if (!ev)
434 return;
435
436 ev->type = EVENT_CONNECT_RESULT;
437 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
438 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
439 ev->cr.req_ie_len = req_ie_len;
440 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
441 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
442 ev->cr.resp_ie_len = resp_ie_len;
443 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
444 ev->cr.status = status;
445
446 spin_lock_irqsave(&wdev->event_lock, flags);
447 list_add_tail(&ev->list, &wdev->event_list);
448 spin_unlock_irqrestore(&wdev->event_lock, flags);
449 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200450}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200451EXPORT_SYMBOL(cfg80211_connect_result);
452
Johannes Berg667503d2009-07-07 03:56:11 +0200453void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
454 const u8 *req_ie, size_t req_ie_len,
455 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200456{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200457 struct cfg80211_bss *bss;
458#ifdef CONFIG_WIRELESS_EXT
459 union iwreq_data wrqu;
460#endif
461
Johannes Berg667503d2009-07-07 03:56:11 +0200462 ASSERT_WDEV_LOCK(wdev);
463
Samuel Ortizb23aa672009-07-01 21:26:54 +0200464 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
465 return;
466
467 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
468 return;
469
470 /* internal error -- how did we get to CONNECTED w/o BSS? */
471 if (WARN_ON(!wdev->current_bss)) {
472 return;
473 }
474
475 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200476 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200477 wdev->current_bss = NULL;
478
479 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
480 wdev->ssid, wdev->ssid_len,
481 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
482
483 if (WARN_ON(!bss))
484 return;
485
Johannes Berg19957bb2009-07-02 17:20:43 +0200486 cfg80211_hold_bss(bss_from_pub(bss));
487 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200488
Johannes Berg667503d2009-07-07 03:56:11 +0200489 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
490 req_ie, req_ie_len, resp_ie, resp_ie_len,
491 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200492
493#ifdef CONFIG_WIRELESS_EXT
494 if (req_ie) {
495 memset(&wrqu, 0, sizeof(wrqu));
496 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800497 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200498 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200499 }
500
501 if (resp_ie) {
502 memset(&wrqu, 0, sizeof(wrqu));
503 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200504 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
505 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200506 }
507
508 memset(&wrqu, 0, sizeof(wrqu));
509 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
510 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200511 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200512#endif
513}
Johannes Berg667503d2009-07-07 03:56:11 +0200514
515void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
516 const u8 *req_ie, size_t req_ie_len,
517 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
518{
519 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
524 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
525 if (!ev)
526 return;
527
528 ev->type = EVENT_ROAMED;
529 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
530 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
531 ev->rm.req_ie_len = req_ie_len;
532 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
533 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
534 ev->rm.resp_ie_len = resp_ie_len;
535 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
536
537 spin_lock_irqsave(&wdev->event_lock, flags);
538 list_add_tail(&ev->list, &wdev->event_list);
539 spin_unlock_irqrestore(&wdev->event_lock, flags);
540 schedule_work(&rdev->event_work);
541}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200542EXPORT_SYMBOL(cfg80211_roamed);
543
Johannes Berg667503d2009-07-07 03:56:11 +0200544void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200545 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200546{
547 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200548 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
549 int i;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200550#ifdef CONFIG_WIRELESS_EXT
551 union iwreq_data wrqu;
552#endif
553
Johannes Berg667503d2009-07-07 03:56:11 +0200554 ASSERT_WDEV_LOCK(wdev);
555
Samuel Ortizb23aa672009-07-01 21:26:54 +0200556 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
557 return;
558
559 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
560 return;
561
562 if (wdev->current_bss) {
563 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200564 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200565 }
566
567 wdev->current_bss = NULL;
568 wdev->sme_state = CFG80211_SME_IDLE;
569
Johannes Berg6829c872009-07-02 09:13:27 +0200570 if (wdev->conn) {
571 kfree(wdev->conn->ie);
572 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200573 kfree(wdev->conn);
574 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200575 }
576
Johannes Bergfffd0932009-07-08 14:22:54 +0200577 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
578
579 /*
580 * Delete all the keys ... pairwise keys can't really
581 * exist any more anyway, but default keys might.
582 */
583 if (rdev->ops->del_key)
584 for (i = 0; i < 6; i++)
585 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586
587#ifdef CONFIG_WIRELESS_EXT
588 memset(&wrqu, 0, sizeof(wrqu));
589 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
590 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
591#endif
592}
593
594void cfg80211_disconnected(struct net_device *dev, u16 reason,
595 u8 *ie, size_t ie_len, gfp_t gfp)
596{
Johannes Berg667503d2009-07-07 03:56:11 +0200597 struct wireless_dev *wdev = dev->ieee80211_ptr;
598 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
599 struct cfg80211_event *ev;
600 unsigned long flags;
601
602 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
603 if (!ev)
604 return;
605
606 ev->type = EVENT_DISCONNECTED;
607 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
608 ev->dc.ie_len = ie_len;
609 memcpy((void *)ev->dc.ie, ie, ie_len);
610 ev->dc.reason = reason;
611
612 spin_lock_irqsave(&wdev->event_lock, flags);
613 list_add_tail(&ev->list, &wdev->event_list);
614 spin_unlock_irqrestore(&wdev->event_lock, flags);
615 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200616}
617EXPORT_SYMBOL(cfg80211_disconnected);
618
Johannes Berg667503d2009-07-07 03:56:11 +0200619int __cfg80211_connect(struct cfg80211_registered_device *rdev,
620 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200621 struct cfg80211_connect_params *connect,
622 struct cfg80211_cached_keys *connkeys)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200623{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200624 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200625 int err;
626
627 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200628
629 if (wdev->sme_state != CFG80211_SME_IDLE)
630 return -EALREADY;
631
Johannes Bergfffd0932009-07-08 14:22:54 +0200632 if (WARN_ON(wdev->connect_keys)) {
633 kfree(wdev->connect_keys);
634 wdev->connect_keys = NULL;
635 }
636
637 if (connkeys && connkeys->def >= 0) {
638 int idx;
639
640 idx = connkeys->def;
641 /* If given a WEP key we may need it for shared key auth */
642 if (connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP40 ||
643 connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP104) {
644 connect->key_idx = idx;
645 connect->key = connkeys->params[idx].key;
646 connect->key_len = connkeys->params[idx].key_len;
647 }
648 }
649
Samuel Ortizb23aa672009-07-01 21:26:54 +0200650 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200651 if (!rdev->ops->auth || !rdev->ops->assoc)
652 return -EOPNOTSUPP;
653
Johannes Berg19957bb2009-07-02 17:20:43 +0200654 if (WARN_ON(wdev->conn))
655 return -EINPROGRESS;
656
657 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
658 if (!wdev->conn)
659 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200660
661 /*
662 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
663 */
664 memcpy(&wdev->conn->params, connect, sizeof(*connect));
665 if (connect->bssid) {
666 wdev->conn->params.bssid = wdev->conn->bssid;
667 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
668 }
669
670 if (connect->ie) {
671 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
672 GFP_KERNEL);
673 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200674 if (!wdev->conn->ie) {
675 kfree(wdev->conn);
676 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200677 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200678 }
Johannes Berg6829c872009-07-02 09:13:27 +0200679 }
680
681 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
682 wdev->conn->auto_auth = true;
683 /* start with open system ... should mostly work */
684 wdev->conn->params.auth_type =
685 NL80211_AUTHTYPE_OPEN_SYSTEM;
686 } else {
687 wdev->conn->auto_auth = false;
688 }
689
690 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
691 wdev->ssid_len = connect->ssid_len;
692 wdev->conn->params.ssid = wdev->ssid;
693 wdev->conn->params.ssid_len = connect->ssid_len;
694
695 /* don't care about result -- but fill bssid & channel */
696 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
697 cfg80211_get_conn_bss(wdev);
698
699 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200700 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200701
702 /* we're good if we have both BSSID and channel */
703 if (wdev->conn->params.bssid && wdev->conn->params.channel) {
704 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
705 err = cfg80211_conn_do_work(wdev);
706 } else {
707 /* otherwise we'll need to scan for the AP first */
708 err = cfg80211_conn_scan(wdev);
709 /*
710 * If we can't scan right now, then we need to scan again
711 * after the current scan finished, since the parameters
712 * changed (unless we find a good AP anyway).
713 */
714 if (err == -EBUSY) {
715 err = 0;
716 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
717 }
718 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200719 if (err) {
720 kfree(wdev->conn);
721 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200722 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200723 wdev->connect_keys = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200724 }
Johannes Berg6829c872009-07-02 09:13:27 +0200725
726 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200727 } else {
728 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200730 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
731 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200732 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200733 wdev->sme_state = CFG80211_SME_IDLE;
734 return err;
735 }
Johannes Berg6829c872009-07-02 09:13:27 +0200736
737 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
738 wdev->ssid_len = connect->ssid_len;
739
740 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200741 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200742}
743
Johannes Berg667503d2009-07-07 03:56:11 +0200744int cfg80211_connect(struct cfg80211_registered_device *rdev,
745 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200746 struct cfg80211_connect_params *connect,
747 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200748{
749 int err;
750
751 wdev_lock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 err = __cfg80211_connect(rdev, dev, connect, connkeys);
Johannes Berg667503d2009-07-07 03:56:11 +0200753 wdev_unlock(dev->ieee80211_ptr);
754
755 return err;
756}
757
758int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
759 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200760{
Johannes Berg6829c872009-07-02 09:13:27 +0200761 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762 int err;
763
Johannes Berg667503d2009-07-07 03:56:11 +0200764 ASSERT_WDEV_LOCK(wdev);
765
Johannes Berg6829c872009-07-02 09:13:27 +0200766 if (wdev->sme_state == CFG80211_SME_IDLE)
767 return -EINVAL;
768
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 kfree(wdev->connect_keys);
770 wdev->connect_keys = NULL;
771
Samuel Ortizb23aa672009-07-01 21:26:54 +0200772 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200773 if (!rdev->ops->deauth)
774 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200775
Johannes Berg19957bb2009-07-02 17:20:43 +0200776 /* was it connected by userspace SME? */
777 if (!wdev->conn) {
778 cfg80211_mlme_down(rdev, dev);
779 return 0;
780 }
Johannes Berg6829c872009-07-02 09:13:27 +0200781
782 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
783 (wdev->conn->state == CFG80211_CONN_SCANNING ||
784 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
785 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg19957bb2009-07-02 17:20:43 +0200786 kfree(wdev->conn);
787 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200788 return 0;
789 }
790
Johannes Berg6829c872009-07-02 09:13:27 +0200791 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200792 err = __cfg80211_mlme_deauth(rdev, dev,
793 wdev->conn->params.bssid,
794 NULL, 0, reason);
Johannes Berg6829c872009-07-02 09:13:27 +0200795 if (err)
796 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200797 } else {
798 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
799 if (err)
800 return err;
801 }
802
Johannes Berg6829c872009-07-02 09:13:27 +0200803 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200804 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200805 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200806 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
807 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200808 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200809
810 return 0;
811}
Johannes Berg19957bb2009-07-02 17:20:43 +0200812
Johannes Berg667503d2009-07-07 03:56:11 +0200813int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
814 struct net_device *dev,
815 u16 reason, bool wextev)
816{
817 int err;
818
819 wdev_lock(dev->ieee80211_ptr);
820 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
821 wdev_unlock(dev->ieee80211_ptr);
822
823 return err;
824}
825
Johannes Berg19957bb2009-07-02 17:20:43 +0200826void cfg80211_sme_disassoc(struct net_device *dev, int idx)
827{
828 struct wireless_dev *wdev = dev->ieee80211_ptr;
829 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
830 u8 bssid[ETH_ALEN];
831
Johannes Berg667503d2009-07-07 03:56:11 +0200832 ASSERT_WDEV_LOCK(wdev);
833
Johannes Berg19957bb2009-07-02 17:20:43 +0200834 if (!wdev->conn)
835 return;
836
837 if (wdev->conn->state == CFG80211_CONN_IDLE)
838 return;
839
840 /*
841 * Ok, so the association was made by this SME -- we don't
842 * want it any more so deauthenticate too.
843 */
844
845 if (!wdev->auth_bsses[idx])
846 return;
847
848 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200849 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
850 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200851 /* whatever -- assume gone anyway */
852 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
853 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
854 wdev->auth_bsses[idx] = NULL;
855 }
856}