blob: 3728d2b88b25b0c8e651f1b31681d0659187fd26 [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"
16
Johannes Berg6829c8782009-07-02 09:13:27 +020017struct cfg80211_conn {
18 struct cfg80211_connect_params params;
19 /* these are sub-states of the _CONNECTING sme_state */
20 enum {
21 CFG80211_CONN_IDLE,
22 CFG80211_CONN_SCANNING,
23 CFG80211_CONN_SCAN_AGAIN,
24 CFG80211_CONN_AUTHENTICATE_NEXT,
25 CFG80211_CONN_AUTHENTICATING,
26 CFG80211_CONN_ASSOCIATE_NEXT,
27 CFG80211_CONN_ASSOCIATING,
28 } state;
29 u8 bssid[ETH_ALEN];
30 u8 *ie;
31 size_t ie_len;
32 bool auto_auth;
33};
34
35
36static int cfg80211_conn_scan(struct wireless_dev *wdev)
37{
Johannes Berg79c97e92009-07-07 03:56:12 +020038 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +020039 struct cfg80211_scan_request *request;
40 int n_channels, err;
41
42 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020043 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020044 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +020045
Johannes Berg79c97e92009-07-07 03:56:12 +020046 if (rdev->scan_req)
Johannes Berg6829c8782009-07-02 09:13:27 +020047 return -EBUSY;
48
49 if (wdev->conn->params.channel) {
50 n_channels = 1;
51 } else {
52 enum ieee80211_band band;
53 n_channels = 0;
54
55 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
56 if (!wdev->wiphy->bands[band])
57 continue;
58 n_channels += wdev->wiphy->bands[band]->n_channels;
59 }
60 }
61 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
62 sizeof(request->channels[0]) * n_channels,
63 GFP_KERNEL);
64 if (!request)
65 return -ENOMEM;
66
67 request->channels = (void *)((char *)request + sizeof(*request));
68 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;
84 request->ssids = (void *)(request->channels + n_channels);
85 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 Berg6829c8782009-07-02 09:13:27 +020093
Johannes Berg79c97e92009-07-07 03:56:12 +020094 rdev->scan_req = request;
Johannes Berg6829c8782009-07-02 09:13:27 +020095
Johannes Berg79c97e92009-07-07 03:56:12 +020096 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c8782009-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 Berg6829c8782009-07-02 09:13:27 +0200101 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200102 rdev->scan_req = NULL;
Johannes Berg6829c8782009-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;
112 int err;
Johannes Berg6829c8782009-07-02 09:13:27 +0200113
Johannes Berg667503d2009-07-07 03:56:11 +0200114 ASSERT_WDEV_LOCK(wdev);
115
Johannes Berg6829c8782009-07-02 09:13:27 +0200116 if (!wdev->conn)
117 return 0;
118
Johannes Berg19957bb2009-07-02 17:20:43 +0200119 params = &wdev->conn->params;
120
Johannes Berg6829c8782009-07-02 09:13:27 +0200121 switch (wdev->conn->state) {
122 case CFG80211_CONN_SCAN_AGAIN:
123 return cfg80211_conn_scan(wdev);
124 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200125 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200126 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200127 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200128 params->channel, params->auth_type,
129 params->bssid,
130 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200131 NULL, 0,
132 params->key, params->key_len,
133 params->key_idx);
Johannes Berg6829c8782009-07-02 09:13:27 +0200134 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200135 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200136 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Berg3e5d7642009-07-07 14:37:26 +0200137 /*
138 * We could, later, implement roaming here and then actually
139 * set prev_bssid to non-NULL. But then we need to be aware
140 * that some APs don't like that -- so we'd need to retry
141 * the association.
142 */
Johannes Berg79c97e92009-07-07 03:56:12 +0200143 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200144 params->channel, params->bssid,
145 NULL,
146 params->ssid, params->ssid_len,
147 params->ie, params->ie_len,
148 false, &params->crypto);
Johannes Berg19957bb2009-07-02 17:20:43 +0200149 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200151 NULL, 0,
152 WLAN_REASON_DEAUTH_LEAVING);
Johannes Berg19957bb2009-07-02 17:20:43 +0200153 return err;
Johannes Berg6829c8782009-07-02 09:13:27 +0200154 default:
155 return 0;
156 }
157}
158
159void cfg80211_conn_work(struct work_struct *work)
160{
Johannes Berg79c97e92009-07-07 03:56:12 +0200161 struct cfg80211_registered_device *rdev =
Johannes Berg6829c8782009-07-02 09:13:27 +0200162 container_of(work, struct cfg80211_registered_device, conn_work);
163 struct wireless_dev *wdev;
164
165 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200166 cfg80211_lock_rdev(rdev);
167 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c8782009-07-02 09:13:27 +0200168
Johannes Berg79c97e92009-07-07 03:56:12 +0200169 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200170 wdev_lock(wdev);
171 if (!netif_running(wdev->netdev)) {
172 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200173 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200174 }
175 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
176 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200177 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200178 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200179 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200180 __cfg80211_connect_result(
181 wdev->netdev,
182 wdev->conn->params.bssid,
183 NULL, 0, NULL, 0,
184 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200185 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200186 wdev_unlock(wdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200187 }
188
Johannes Berg79c97e92009-07-07 03:56:12 +0200189 mutex_unlock(&rdev->devlist_mtx);
190 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c8782009-07-02 09:13:27 +0200191 rtnl_unlock();
192}
193
194static bool cfg80211_get_conn_bss(struct wireless_dev *wdev)
195{
Johannes Berg79c97e92009-07-07 03:56:12 +0200196 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +0200197 struct cfg80211_bss *bss;
198 u16 capa = WLAN_CAPABILITY_ESS;
199
Johannes Berg667503d2009-07-07 03:56:11 +0200200 ASSERT_WDEV_LOCK(wdev);
201
Johannes Berg6829c8782009-07-02 09:13:27 +0200202 if (wdev->conn->params.privacy)
203 capa |= WLAN_CAPABILITY_PRIVACY;
204
205 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
206 wdev->conn->params.ssid,
207 wdev->conn->params.ssid_len,
208 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
209 capa);
Johannes Berg6829c8782009-07-02 09:13:27 +0200210 if (!bss)
211 return false;
212
213 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
214 wdev->conn->params.bssid = wdev->conn->bssid;
215 wdev->conn->params.channel = bss->channel;
216 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200217 schedule_work(&rdev->conn_work);
Johannes Berg6829c8782009-07-02 09:13:27 +0200218
219 cfg80211_put_bss(bss);
220 return true;
221}
222
Johannes Berg667503d2009-07-07 03:56:11 +0200223static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c8782009-07-02 09:13:27 +0200224{
225 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200226 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c8782009-07-02 09:13:27 +0200227
Johannes Berg667503d2009-07-07 03:56:11 +0200228 ASSERT_WDEV_LOCK(wdev);
229
Johannes Berg6829c8782009-07-02 09:13:27 +0200230 if (wdev->sme_state != CFG80211_SME_CONNECTING)
231 return;
232
Zhu Yid4b1a682009-07-16 17:34:14 +0800233 if (!wdev->conn)
Johannes Berg6829c8782009-07-02 09:13:27 +0200234 return;
235
236 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
237 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
238 return;
239
240 if (!cfg80211_get_conn_bss(wdev)) {
241 /* 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 Berg6829c8782009-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 Berg6829c8782009-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
258 wdev_lock(wdev);
259 __cfg80211_sme_scan_done(dev);
260 wdev_unlock(wdev);
261}
262
263void cfg80211_sme_rx_auth(struct net_device *dev,
264 const u8 *buf, size_t len)
Johannes Berg6829c8782009-07-02 09:13:27 +0200265{
266 struct wireless_dev *wdev = dev->ieee80211_ptr;
267 struct wiphy *wiphy = wdev->wiphy;
268 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
269 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
270 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
271
Johannes Berg667503d2009-07-07 03:56:11 +0200272 ASSERT_WDEV_LOCK(wdev);
273
Johannes Berg6829c8782009-07-02 09:13:27 +0200274 /* should only RX auth frames when connecting */
275 if (wdev->sme_state != CFG80211_SME_CONNECTING)
276 return;
277
278 if (WARN_ON(!wdev->conn))
279 return;
280
281 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
282 wdev->conn->auto_auth &&
283 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
284 /* select automatically between only open, shared, leap */
285 switch (wdev->conn->params.auth_type) {
286 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200287 if (wdev->connect_keys)
288 wdev->conn->params.auth_type =
289 NL80211_AUTHTYPE_SHARED_KEY;
290 else
291 wdev->conn->params.auth_type =
292 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200293 break;
294 case NL80211_AUTHTYPE_SHARED_KEY:
295 wdev->conn->params.auth_type =
296 NL80211_AUTHTYPE_NETWORK_EAP;
297 break;
298 default:
299 /* huh? */
300 wdev->conn->params.auth_type =
301 NL80211_AUTHTYPE_OPEN_SYSTEM;
302 break;
303 }
304 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
305 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200306 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200307 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200308 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200309 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c8782009-07-02 09:13:27 +0200310 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
311 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
312 schedule_work(&rdev->conn_work);
313 }
314}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200315
Johannes Berg667503d2009-07-07 03:56:11 +0200316void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
317 const u8 *req_ie, size_t req_ie_len,
318 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200319 u16 status, bool wextev,
320 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200321{
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200323#ifdef CONFIG_WIRELESS_EXT
324 union iwreq_data wrqu;
325#endif
326
Johannes Berg667503d2009-07-07 03:56:11 +0200327 ASSERT_WDEV_LOCK(wdev);
328
Samuel Ortizb23aa672009-07-01 21:26:54 +0200329 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
330 return;
331
Johannes Berge45cd822009-07-02 09:58:04 +0200332 if (wdev->sme_state == CFG80211_SME_CONNECTED)
333 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), dev,
334 bssid, req_ie, req_ie_len,
Johannes Berg667503d2009-07-07 03:56:11 +0200335 resp_ie, resp_ie_len, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200336 else
337 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
338 bssid, req_ie, req_ie_len,
339 resp_ie, resp_ie_len,
Johannes Berg667503d2009-07-07 03:56:11 +0200340 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200341
342#ifdef CONFIG_WIRELESS_EXT
343 if (wextev) {
344 if (req_ie && status == WLAN_STATUS_SUCCESS) {
345 memset(&wrqu, 0, sizeof(wrqu));
346 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800347 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200348 }
349
350 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
351 memset(&wrqu, 0, sizeof(wrqu));
352 wrqu.data.length = resp_ie_len;
353 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
354 }
355
356 memset(&wrqu, 0, sizeof(wrqu));
357 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
358 if (bssid && status == WLAN_STATUS_SUCCESS)
359 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
360 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
361 }
362#endif
363
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200364 if (wdev->current_bss) {
365 cfg80211_unhold_bss(wdev->current_bss);
366 cfg80211_put_bss(&wdev->current_bss->pub);
367 wdev->current_bss = NULL;
368 }
369
Johannes Berge45cd822009-07-02 09:58:04 +0200370 if (status == WLAN_STATUS_SUCCESS &&
Johannes Bergfffd0932009-07-08 14:22:54 +0200371 wdev->sme_state == CFG80211_SME_IDLE)
372 goto success;
Johannes Berge45cd822009-07-02 09:58:04 +0200373
Johannes Berg6829c8782009-07-02 09:13:27 +0200374 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200375 return;
376
Johannes Berg19957bb2009-07-02 17:20:43 +0200377 if (wdev->conn)
378 wdev->conn->state = CFG80211_CONN_IDLE;
379
Johannes Bergfffd0932009-07-08 14:22:54 +0200380 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200381 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg19957bb2009-07-02 17:20:43 +0200382 kfree(wdev->conn);
383 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200384 kfree(wdev->connect_keys);
385 wdev->connect_keys = NULL;
386 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200387 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200388
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200389 success:
390 if (!bss)
391 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
392 wdev->ssid, wdev->ssid_len,
393 WLAN_CAPABILITY_ESS,
394 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200395
396 if (WARN_ON(!bss))
397 return;
398
399 cfg80211_hold_bss(bss_from_pub(bss));
400 wdev->current_bss = bss_from_pub(bss);
401
Johannes Bergfffd0932009-07-08 14:22:54 +0200402 wdev->sme_state = CFG80211_SME_CONNECTED;
403 cfg80211_upload_connect_keys(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200404}
Johannes Bergf2129352009-07-01 21:26:56 +0200405
406void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
407 const u8 *req_ie, size_t req_ie_len,
408 const u8 *resp_ie, size_t resp_ie_len,
409 u16 status, gfp_t gfp)
410{
Johannes Berg667503d2009-07-07 03:56:11 +0200411 struct wireless_dev *wdev = dev->ieee80211_ptr;
412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
413 struct cfg80211_event *ev;
414 unsigned long flags;
415
416 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
417 if (!ev)
418 return;
419
420 ev->type = EVENT_CONNECT_RESULT;
421 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
422 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
423 ev->cr.req_ie_len = req_ie_len;
424 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
425 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
426 ev->cr.resp_ie_len = resp_ie_len;
427 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
428 ev->cr.status = status;
429
430 spin_lock_irqsave(&wdev->event_lock, flags);
431 list_add_tail(&ev->list, &wdev->event_list);
432 spin_unlock_irqrestore(&wdev->event_lock, flags);
433 schedule_work(&rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200434}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200435EXPORT_SYMBOL(cfg80211_connect_result);
436
Johannes Berg667503d2009-07-07 03:56:11 +0200437void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
438 const u8 *req_ie, size_t req_ie_len,
439 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200440{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200441 struct cfg80211_bss *bss;
442#ifdef CONFIG_WIRELESS_EXT
443 union iwreq_data wrqu;
444#endif
445
Johannes Berg667503d2009-07-07 03:56:11 +0200446 ASSERT_WDEV_LOCK(wdev);
447
Samuel Ortizb23aa672009-07-01 21:26:54 +0200448 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
449 return;
450
451 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
452 return;
453
454 /* internal error -- how did we get to CONNECTED w/o BSS? */
455 if (WARN_ON(!wdev->current_bss)) {
456 return;
457 }
458
459 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200460 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200461 wdev->current_bss = NULL;
462
463 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
464 wdev->ssid, wdev->ssid_len,
465 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
466
467 if (WARN_ON(!bss))
468 return;
469
Johannes Berg19957bb2009-07-02 17:20:43 +0200470 cfg80211_hold_bss(bss_from_pub(bss));
471 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200472
Johannes Berg667503d2009-07-07 03:56:11 +0200473 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
474 req_ie, req_ie_len, resp_ie, resp_ie_len,
475 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200476
477#ifdef CONFIG_WIRELESS_EXT
478 if (req_ie) {
479 memset(&wrqu, 0, sizeof(wrqu));
480 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800481 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200482 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200483 }
484
485 if (resp_ie) {
486 memset(&wrqu, 0, sizeof(wrqu));
487 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200488 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
489 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200490 }
491
492 memset(&wrqu, 0, sizeof(wrqu));
493 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
494 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200495 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200496#endif
497}
Johannes Berg667503d2009-07-07 03:56:11 +0200498
499void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
500 const u8 *req_ie, size_t req_ie_len,
501 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
502{
503 struct wireless_dev *wdev = dev->ieee80211_ptr;
504 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
505 struct cfg80211_event *ev;
506 unsigned long flags;
507
508 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
509 if (!ev)
510 return;
511
512 ev->type = EVENT_ROAMED;
513 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
514 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
515 ev->rm.req_ie_len = req_ie_len;
516 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
517 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
518 ev->rm.resp_ie_len = resp_ie_len;
519 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
520
521 spin_lock_irqsave(&wdev->event_lock, flags);
522 list_add_tail(&ev->list, &wdev->event_list);
523 spin_unlock_irqrestore(&wdev->event_lock, flags);
524 schedule_work(&rdev->event_work);
525}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200526EXPORT_SYMBOL(cfg80211_roamed);
527
Johannes Berg667503d2009-07-07 03:56:11 +0200528void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c8782009-07-02 09:13:27 +0200529 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200530{
531 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200532 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
533 int i;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200534#ifdef CONFIG_WIRELESS_EXT
535 union iwreq_data wrqu;
536#endif
537
Johannes Berg667503d2009-07-07 03:56:11 +0200538 ASSERT_WDEV_LOCK(wdev);
539
Samuel Ortizb23aa672009-07-01 21:26:54 +0200540 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
541 return;
542
543 if (WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED))
544 return;
545
546 if (wdev->current_bss) {
547 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200548 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200549 }
550
551 wdev->current_bss = NULL;
552 wdev->sme_state = CFG80211_SME_IDLE;
553
Johannes Berg6829c8782009-07-02 09:13:27 +0200554 if (wdev->conn) {
555 kfree(wdev->conn->ie);
556 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200557 kfree(wdev->conn);
558 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200559 }
560
Johannes Bergfffd0932009-07-08 14:22:54 +0200561 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
562
563 /*
564 * Delete all the keys ... pairwise keys can't really
565 * exist any more anyway, but default keys might.
566 */
567 if (rdev->ops->del_key)
568 for (i = 0; i < 6; i++)
569 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570
571#ifdef CONFIG_WIRELESS_EXT
572 memset(&wrqu, 0, sizeof(wrqu));
573 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
574 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
575#endif
576}
577
578void cfg80211_disconnected(struct net_device *dev, u16 reason,
579 u8 *ie, size_t ie_len, gfp_t gfp)
580{
Johannes Berg667503d2009-07-07 03:56:11 +0200581 struct wireless_dev *wdev = dev->ieee80211_ptr;
582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
583 struct cfg80211_event *ev;
584 unsigned long flags;
585
586 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
587 if (!ev)
588 return;
589
590 ev->type = EVENT_DISCONNECTED;
591 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
592 ev->dc.ie_len = ie_len;
593 memcpy((void *)ev->dc.ie, ie, ie_len);
594 ev->dc.reason = reason;
595
596 spin_lock_irqsave(&wdev->event_lock, flags);
597 list_add_tail(&ev->list, &wdev->event_list);
598 spin_unlock_irqrestore(&wdev->event_lock, flags);
599 schedule_work(&rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200600}
601EXPORT_SYMBOL(cfg80211_disconnected);
602
Johannes Berg667503d2009-07-07 03:56:11 +0200603int __cfg80211_connect(struct cfg80211_registered_device *rdev,
604 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200605 struct cfg80211_connect_params *connect,
606 struct cfg80211_cached_keys *connkeys)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200607{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200608 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200609 int err;
610
611 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200612
613 if (wdev->sme_state != CFG80211_SME_IDLE)
614 return -EALREADY;
615
Johannes Bergfffd0932009-07-08 14:22:54 +0200616 if (WARN_ON(wdev->connect_keys)) {
617 kfree(wdev->connect_keys);
618 wdev->connect_keys = NULL;
619 }
620
621 if (connkeys && connkeys->def >= 0) {
622 int idx;
623
624 idx = connkeys->def;
625 /* If given a WEP key we may need it for shared key auth */
626 if (connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP40 ||
627 connkeys->params[idx].cipher == WLAN_CIPHER_SUITE_WEP104) {
628 connect->key_idx = idx;
629 connect->key = connkeys->params[idx].key;
630 connect->key_len = connkeys->params[idx].key_len;
631 }
632 }
633
Samuel Ortizb23aa672009-07-01 21:26:54 +0200634 if (!rdev->ops->connect) {
Johannes Berg6829c8782009-07-02 09:13:27 +0200635 if (!rdev->ops->auth || !rdev->ops->assoc)
636 return -EOPNOTSUPP;
637
Johannes Berg19957bb2009-07-02 17:20:43 +0200638 if (WARN_ON(wdev->conn))
639 return -EINPROGRESS;
640
641 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
642 if (!wdev->conn)
643 return -ENOMEM;
Johannes Berg6829c8782009-07-02 09:13:27 +0200644
645 /*
646 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
647 */
648 memcpy(&wdev->conn->params, connect, sizeof(*connect));
649 if (connect->bssid) {
650 wdev->conn->params.bssid = wdev->conn->bssid;
651 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
652 }
653
654 if (connect->ie) {
655 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
656 GFP_KERNEL);
657 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200658 if (!wdev->conn->ie) {
659 kfree(wdev->conn);
660 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200661 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200662 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200663 }
664
665 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
666 wdev->conn->auto_auth = true;
667 /* start with open system ... should mostly work */
668 wdev->conn->params.auth_type =
669 NL80211_AUTHTYPE_OPEN_SYSTEM;
670 } else {
671 wdev->conn->auto_auth = false;
672 }
673
674 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
675 wdev->ssid_len = connect->ssid_len;
676 wdev->conn->params.ssid = wdev->ssid;
677 wdev->conn->params.ssid_len = connect->ssid_len;
678
679 /* don't care about result -- but fill bssid & channel */
680 if (!wdev->conn->params.bssid || !wdev->conn->params.channel)
681 cfg80211_get_conn_bss(wdev);
682
683 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200684 wdev->connect_keys = connkeys;
Johannes Berg6829c8782009-07-02 09:13:27 +0200685
686 /* we're good if we have both BSSID and channel */
687 if (wdev->conn->params.bssid && wdev->conn->params.channel) {
688 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
689 err = cfg80211_conn_do_work(wdev);
690 } else {
691 /* otherwise we'll need to scan for the AP first */
692 err = cfg80211_conn_scan(wdev);
693 /*
694 * If we can't scan right now, then we need to scan again
695 * after the current scan finished, since the parameters
696 * changed (unless we find a good AP anyway).
697 */
698 if (err == -EBUSY) {
699 err = 0;
700 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
701 }
702 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200703 if (err) {
704 kfree(wdev->conn);
705 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200706 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200707 wdev->connect_keys = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200708 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200709
710 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200711 } else {
712 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200713 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200714 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
715 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200716 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200717 wdev->sme_state = CFG80211_SME_IDLE;
718 return err;
719 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200720
721 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
722 wdev->ssid_len = connect->ssid_len;
723
724 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200725 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200726}
727
Johannes Berg667503d2009-07-07 03:56:11 +0200728int cfg80211_connect(struct cfg80211_registered_device *rdev,
729 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200730 struct cfg80211_connect_params *connect,
731 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200732{
733 int err;
734
735 wdev_lock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 err = __cfg80211_connect(rdev, dev, connect, connkeys);
Johannes Berg667503d2009-07-07 03:56:11 +0200737 wdev_unlock(dev->ieee80211_ptr);
738
739 return err;
740}
741
742int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
743 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744{
Johannes Berg6829c8782009-07-02 09:13:27 +0200745 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200746 int err;
747
Johannes Berg667503d2009-07-07 03:56:11 +0200748 ASSERT_WDEV_LOCK(wdev);
749
Johannes Berg6829c8782009-07-02 09:13:27 +0200750 if (wdev->sme_state == CFG80211_SME_IDLE)
751 return -EINVAL;
752
Johannes Bergfffd0932009-07-08 14:22:54 +0200753 kfree(wdev->connect_keys);
754 wdev->connect_keys = NULL;
755
Samuel Ortizb23aa672009-07-01 21:26:54 +0200756 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200757 if (!rdev->ops->deauth)
758 return -EOPNOTSUPP;
Johannes Berg6829c8782009-07-02 09:13:27 +0200759
Johannes Berg19957bb2009-07-02 17:20:43 +0200760 /* was it connected by userspace SME? */
761 if (!wdev->conn) {
762 cfg80211_mlme_down(rdev, dev);
763 return 0;
764 }
Johannes Berg6829c8782009-07-02 09:13:27 +0200765
766 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
767 (wdev->conn->state == CFG80211_CONN_SCANNING ||
768 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
769 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg19957bb2009-07-02 17:20:43 +0200770 kfree(wdev->conn);
771 wdev->conn = NULL;
Johannes Berg6829c8782009-07-02 09:13:27 +0200772 return 0;
773 }
774
Johannes Berg6829c8782009-07-02 09:13:27 +0200775 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200776 err = __cfg80211_mlme_deauth(rdev, dev,
777 wdev->conn->params.bssid,
778 NULL, 0, reason);
Johannes Berg6829c8782009-07-02 09:13:27 +0200779 if (err)
780 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200781 } else {
782 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
783 if (err)
784 return err;
785 }
786
Johannes Berg6829c8782009-07-02 09:13:27 +0200787 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200788 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c8782009-07-02 09:13:27 +0200789 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200790 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
791 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200792 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200793
794 return 0;
795}
Johannes Berg19957bb2009-07-02 17:20:43 +0200796
Johannes Berg667503d2009-07-07 03:56:11 +0200797int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
798 struct net_device *dev,
799 u16 reason, bool wextev)
800{
801 int err;
802
803 wdev_lock(dev->ieee80211_ptr);
804 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
805 wdev_unlock(dev->ieee80211_ptr);
806
807 return err;
808}
809
Johannes Berg19957bb2009-07-02 17:20:43 +0200810void cfg80211_sme_disassoc(struct net_device *dev, int idx)
811{
812 struct wireless_dev *wdev = dev->ieee80211_ptr;
813 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
814 u8 bssid[ETH_ALEN];
815
Johannes Berg667503d2009-07-07 03:56:11 +0200816 ASSERT_WDEV_LOCK(wdev);
817
Johannes Berg19957bb2009-07-02 17:20:43 +0200818 if (!wdev->conn)
819 return;
820
821 if (wdev->conn->state == CFG80211_CONN_IDLE)
822 return;
823
824 /*
825 * Ok, so the association was made by this SME -- we don't
826 * want it any more so deauthenticate too.
827 */
828
829 if (!wdev->auth_bsses[idx])
830 return;
831
832 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
Johannes Bergec3f1492009-07-10 02:45:38 +0200833 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
834 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200835 /* whatever -- assume gone anyway */
836 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
837 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
838 wdev->auth_bsses[idx] = NULL;
839 }
840}