blob: 8a7dcbf9060233bbf9bb7f5ef7a3a2af11caa8d3 [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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +020046
Johannes Berg79c97e92009-07-07 03:56:12 +020047 if (rdev->scan_req)
Johannes Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +020094
Johannes Berg79c97e92009-07-07 03:56:12 +020095 rdev->scan_req = request;
Johannes Berg6829c8782009-07-02 09:13:27 +020096
Johannes Berg79c97e92009-07-07 03:56:12 +020097 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +0200102 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200103 rdev->scan_req = NULL;
Johannes Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +0200114
Johannes Berg667503d2009-07-07 03:56:11 +0200115 ASSERT_WDEV_LOCK(wdev);
116
Johannes Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +0200178 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200179 }
Johannes Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +0200228
Johannes Berg667503d2009-07-07 03:56:11 +0200229 ASSERT_WDEV_LOCK(wdev);
230
Johannes Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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 Berg6829c8782009-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;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200388 wdev->ssid_len = 0;
Johannes Bergfffd0932009-07-08 14:22:54 +0200389 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200390 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200391
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200392 success:
393 if (!bss)
394 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
395 wdev->ssid, wdev->ssid_len,
396 WLAN_CAPABILITY_ESS,
397 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200398
399 if (WARN_ON(!bss))
400 return;
401
402 cfg80211_hold_bss(bss_from_pub(bss));
403 wdev->current_bss = bss_from_pub(bss);
404
Johannes Bergfffd0932009-07-08 14:22:54 +0200405 wdev->sme_state = CFG80211_SME_CONNECTED;
406 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700407
408 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
409
410 if (!country_ie)
411 return;
412
413 /*
414 * ieee80211_bss_get_ie() ensures we can access:
415 * - country_ie + 2, the start of the country ie data, and
416 * - and country_ie[1] which is the IE length
417 */
418 regulatory_hint_11d(wdev->wiphy,
419 country_ie + 2,
420 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200421}
Johannes Bergf2129352009-07-01 21:26:56 +0200422
423void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
424 const u8 *req_ie, size_t req_ie_len,
425 const u8 *resp_ie, size_t resp_ie_len,
426 u16 status, gfp_t gfp)
427{
Johannes Berg667503d2009-07-07 03:56:11 +0200428 struct wireless_dev *wdev = dev->ieee80211_ptr;
429 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
430 struct cfg80211_event *ev;
431 unsigned long flags;
432
433 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
434 if (!ev)
435 return;
436
437 ev->type = EVENT_CONNECT_RESULT;
438 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
439 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
440 ev->cr.req_ie_len = req_ie_len;
441 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
442 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
443 ev->cr.resp_ie_len = resp_ie_len;
444 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
445 ev->cr.status = status;
446
447 spin_lock_irqsave(&wdev->event_lock, flags);
448 list_add_tail(&ev->list, &wdev->event_list);
449 spin_unlock_irqrestore(&wdev->event_lock, flags);
450 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200451}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200452EXPORT_SYMBOL(cfg80211_connect_result);
453
Johannes Berg667503d2009-07-07 03:56:11 +0200454void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
455 const u8 *req_ie, size_t req_ie_len,
456 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200457{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200458 struct cfg80211_bss *bss;
459#ifdef CONFIG_WIRELESS_EXT
460 union iwreq_data wrqu;
461#endif
462
Johannes Berg667503d2009-07-07 03:56:11 +0200463 ASSERT_WDEV_LOCK(wdev);
464
Samuel Ortizb23aa672009-07-01 21:26:54 +0200465 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
466 return;
467
468 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
469 return;
470
471 /* internal error -- how did we get to CONNECTED w/o BSS? */
472 if (WARN_ON(!wdev->current_bss)) {
473 return;
474 }
475
476 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200477 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200478 wdev->current_bss = NULL;
479
480 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
481 wdev->ssid, wdev->ssid_len,
482 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
483
484 if (WARN_ON(!bss))
485 return;
486
Johannes Berg19957bb2009-07-02 17:20:43 +0200487 cfg80211_hold_bss(bss_from_pub(bss));
488 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200489
Johannes Berg667503d2009-07-07 03:56:11 +0200490 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
491 req_ie, req_ie_len, resp_ie, resp_ie_len,
492 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200493
494#ifdef CONFIG_WIRELESS_EXT
495 if (req_ie) {
496 memset(&wrqu, 0, sizeof(wrqu));
497 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800498 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200499 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200500 }
501
502 if (resp_ie) {
503 memset(&wrqu, 0, sizeof(wrqu));
504 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200505 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
506 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200507 }
508
509 memset(&wrqu, 0, sizeof(wrqu));
510 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
511 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200512 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200513#endif
514}
Johannes Berg667503d2009-07-07 03:56:11 +0200515
516void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
517 const u8 *req_ie, size_t req_ie_len,
518 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
519{
520 struct wireless_dev *wdev = dev->ieee80211_ptr;
521 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
522 struct cfg80211_event *ev;
523 unsigned long flags;
524
525 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
526 if (!ev)
527 return;
528
529 ev->type = EVENT_ROAMED;
530 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
531 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
532 ev->rm.req_ie_len = req_ie_len;
533 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
534 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
535 ev->rm.resp_ie_len = resp_ie_len;
536 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
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);
541 schedule_work(&rdev->event_work);
542}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200543EXPORT_SYMBOL(cfg80211_roamed);
544
Johannes Berg667503d2009-07-07 03:56:11 +0200545void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c8782009-07-02 09:13:27 +0200546 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200547{
548 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200549 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
550 int i;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200551#ifdef CONFIG_WIRELESS_EXT
552 union iwreq_data wrqu;
553#endif
554
Johannes Berg667503d2009-07-07 03:56:11 +0200555 ASSERT_WDEV_LOCK(wdev);
556
Samuel Ortizb23aa672009-07-01 21:26:54 +0200557 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
558 return;
559
560 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
561 return;
562
563 if (wdev->current_bss) {
564 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200565 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200566 }
567
568 wdev->current_bss = NULL;
569 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200570 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200571
Johannes Berg6829c8782009-07-02 09:13:27 +0200572 if (wdev->conn) {
573 kfree(wdev->conn->ie);
574 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200575 kfree(wdev->conn);
576 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200577 }
578
Johannes Bergfffd0932009-07-08 14:22:54 +0200579 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
580
581 /*
582 * Delete all the keys ... pairwise keys can't really
583 * exist any more anyway, but default keys might.
584 */
585 if (rdev->ops->del_key)
586 for (i = 0; i < 6; i++)
587 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200588
589#ifdef CONFIG_WIRELESS_EXT
590 memset(&wrqu, 0, sizeof(wrqu));
591 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
592 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
593#endif
594}
595
596void cfg80211_disconnected(struct net_device *dev, u16 reason,
597 u8 *ie, size_t ie_len, gfp_t gfp)
598{
Johannes Berg667503d2009-07-07 03:56:11 +0200599 struct wireless_dev *wdev = dev->ieee80211_ptr;
600 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
601 struct cfg80211_event *ev;
602 unsigned long flags;
603
604 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
605 if (!ev)
606 return;
607
608 ev->type = EVENT_DISCONNECTED;
609 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
610 ev->dc.ie_len = ie_len;
611 memcpy((void *)ev->dc.ie, ie, ie_len);
612 ev->dc.reason = reason;
613
614 spin_lock_irqsave(&wdev->event_lock, flags);
615 list_add_tail(&ev->list, &wdev->event_list);
616 spin_unlock_irqrestore(&wdev->event_lock, flags);
617 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200618}
619EXPORT_SYMBOL(cfg80211_disconnected);
620
Johannes Berg667503d2009-07-07 03:56:11 +0200621int __cfg80211_connect(struct cfg80211_registered_device *rdev,
622 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200623 struct cfg80211_connect_params *connect,
624 struct cfg80211_cached_keys *connkeys)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200625{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200626 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200627 int err;
628
629 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200630
631 if (wdev->sme_state != CFG80211_SME_IDLE)
632 return -EALREADY;
633
Johannes Bergfffd0932009-07-08 14:22:54 +0200634 if (WARN_ON(wdev->connect_keys)) {
635 kfree(wdev->connect_keys);
636 wdev->connect_keys = NULL;
637 }
638
639 if (connkeys && connkeys->def >= 0) {
640 int idx;
641
642 idx = connkeys->def;
643 /* If given a WEP key we may need it for shared key auth */
644 if (connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP40 ||
645 connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP104) {
646 connect->key_idx = idx;
647 connect->key = connkeys->params[idx].key;
648 connect->key_len = connkeys->params[idx].key_len;
649 }
650 }
651
Samuel Ortizb23aa672009-07-01 21:26:54 +0200652 if (!rdev->ops->connect) {
Johannes Berg6829c8782009-07-02 09:13:27 +0200653 if (!rdev->ops->auth || !rdev->ops->assoc)
654 return -EOPNOTSUPP;
655
Johannes Berg19957bb2009-07-02 17:20:43 +0200656 if (WARN_ON(wdev->conn))
657 return -EINPROGRESS;
658
659 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
660 if (!wdev->conn)
661 return -ENOMEM;
Johannes Berg6829c8782009-07-02 09:13:27 +0200662
663 /*
664 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
665 */
666 memcpy(&wdev->conn->params, connect, sizeof(*connect));
667 if (connect->bssid) {
668 wdev->conn->params.bssid = wdev->conn->bssid;
669 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
670 }
671
672 if (connect->ie) {
673 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
674 GFP_KERNEL);
675 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200676 if (!wdev->conn->ie) {
677 kfree(wdev->conn);
678 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200679 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200680 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200681 }
682
683 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
684 wdev->conn->auto_auth = true;
685 /* start with open system ... should mostly work */
686 wdev->conn->params.auth_type =
687 NL80211_AUTHTYPE_OPEN_SYSTEM;
688 } else {
689 wdev->conn->auto_auth = false;
690 }
691
692 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
693 wdev->ssid_len = connect->ssid_len;
694 wdev->conn->params.ssid = wdev->ssid;
695 wdev->conn->params.ssid_len = connect->ssid_len;
696
697 /* don't care about result -- but fill bssid & channel */
698 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
699 cfg80211_get_conn_bss(wdev);
700
701 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200702 wdev->connect_keys = connkeys;
Johannes Berg6829c8782009-07-02 09:13:27 +0200703
704 /* we're good if we have both BSSID and channel */
705 if (wdev->conn->params.bssid && wdev->conn->params.channel) {
706 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
707 err = cfg80211_conn_do_work(wdev);
708 } else {
709 /* otherwise we'll need to scan for the AP first */
710 err = cfg80211_conn_scan(wdev);
711 /*
712 * If we can't scan right now, then we need to scan again
713 * after the current scan finished, since the parameters
714 * changed (unless we find a good AP anyway).
715 */
716 if (err == -EBUSY) {
717 err = 0;
718 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
719 }
720 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200721 if (err) {
722 kfree(wdev->conn);
723 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200724 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200726 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200727 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200728
729 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200730 } else {
731 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200732 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200733 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
734 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200735 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736 wdev->sme_state = CFG80211_SME_IDLE;
737 return err;
738 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200739
740 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
741 wdev->ssid_len = connect->ssid_len;
742
743 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200745}
746
Johannes Berg667503d2009-07-07 03:56:11 +0200747int cfg80211_connect(struct cfg80211_registered_device *rdev,
748 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200749 struct cfg80211_connect_params *connect,
750 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200751{
752 int err;
753
754 wdev_lock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +0200755 err = __cfg80211_connect(rdev, dev, connect, connkeys);
Johannes Berg667503d2009-07-07 03:56:11 +0200756 wdev_unlock(dev->ieee80211_ptr);
757
758 return err;
759}
760
761int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
762 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200763{
Johannes Berg6829c8782009-07-02 09:13:27 +0200764 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200765 int err;
766
Johannes Berg667503d2009-07-07 03:56:11 +0200767 ASSERT_WDEV_LOCK(wdev);
768
Johannes Berg6829c8782009-07-02 09:13:27 +0200769 if (wdev->sme_state == CFG80211_SME_IDLE)
770 return -EINVAL;
771
Johannes Bergfffd0932009-07-08 14:22:54 +0200772 kfree(wdev->connect_keys);
773 wdev->connect_keys = NULL;
774
Samuel Ortizb23aa672009-07-01 21:26:54 +0200775 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200776 if (!rdev->ops->deauth)
777 return -EOPNOTSUPP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200778
Johannes Berg19957bb2009-07-02 17:20:43 +0200779 /* was it connected by userspace SME? */
780 if (!wdev->conn) {
781 cfg80211_mlme_down(rdev, dev);
782 return 0;
783 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200784
785 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
786 (wdev->conn->state == CFG80211_CONN_SCANNING ||
787 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
788 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg19957bb2009-07-02 17:20:43 +0200789 kfree(wdev->conn);
790 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200791 wdev->ssid_len = 0;
Johannes Berg6829c8782009-07-02 09:13:27 +0200792 return 0;
793 }
794
Johannes Berg6829c8782009-07-02 09:13:27 +0200795 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200796 err = __cfg80211_mlme_deauth(rdev, dev,
797 wdev->conn->params.bssid,
798 NULL, 0, reason);
Johannes Berg6829c8782009-07-02 09:13:27 +0200799 if (err)
800 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200801 } else {
802 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
803 if (err)
804 return err;
805 }
806
Johannes Berg6829c8782009-07-02 09:13:27 +0200807 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200808 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200809 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200810 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
811 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200812 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200813
814 return 0;
815}
Johannes Berg19957bb2009-07-02 17:20:43 +0200816
Johannes Berg667503d2009-07-07 03:56:11 +0200817int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
818 struct net_device *dev,
819 u16 reason, bool wextev)
820{
821 int err;
822
823 wdev_lock(dev->ieee80211_ptr);
824 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
825 wdev_unlock(dev->ieee80211_ptr);
826
827 return err;
828}
829
Johannes Berg19957bb2009-07-02 17:20:43 +0200830void cfg80211_sme_disassoc(struct net_device *dev, int idx)
831{
832 struct wireless_dev *wdev = dev->ieee80211_ptr;
833 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
834 u8 bssid[ETH_ALEN];
835
Johannes Berg667503d2009-07-07 03:56:11 +0200836 ASSERT_WDEV_LOCK(wdev);
837
Johannes Berg19957bb2009-07-02 17:20:43 +0200838 if (!wdev->conn)
839 return;
840
841 if (wdev->conn->state == CFG80211_CONN_IDLE)
842 return;
843
844 /*
845 * Ok, so the association was made by this SME -- we don't
846 * want it any more so deauthenticate too.
847 */
848
849 if (!wdev->auth_bsses[idx])
850 return;
851
852 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200853 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
854 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200855 /* whatever -- assume gone anyway */
856 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
857 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
858 wdev->auth_bsses[idx] = NULL;
859 }
860}