blob: ab91446cf2a9cd38d43c52b1672a8ba35ee396d1 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
2 * SME code for cfg80211's connect emulation.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 */
7
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020011#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020012#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020015#include <net/cfg80211.h>
16#include <net/rtnetlink.h>
17#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070018#include "reg.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020019
Johannes Berg6829c872009-07-02 09:13:27 +020020struct cfg80211_conn {
21 struct cfg80211_connect_params params;
22 /* these are sub-states of the _CONNECTING sme_state */
23 enum {
24 CFG80211_CONN_IDLE,
25 CFG80211_CONN_SCANNING,
26 CFG80211_CONN_SCAN_AGAIN,
27 CFG80211_CONN_AUTHENTICATE_NEXT,
28 CFG80211_CONN_AUTHENTICATING,
29 CFG80211_CONN_ASSOCIATE_NEXT,
30 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090031 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020032 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020033 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020034 u8 *ie;
35 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020036 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020037};
38
John W. Linville20925fe2010-07-20 12:32:52 -040039static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050040{
41 struct cfg80211_registered_device *rdev;
42 struct wireless_dev *wdev;
43 bool is_all_idle = true;
44
45 mutex_lock(&cfg80211_mutex);
46
47 /*
48 * All devices must be idle as otherwise if you are actively
49 * scanning some new beacon hints could be learned and would
50 * count as new regulatory hints.
51 */
52 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53 cfg80211_lock_rdev(rdev);
54 list_for_each_entry(wdev, &rdev->netdev_list, list) {
55 wdev_lock(wdev);
56 if (wdev->sme_state != CFG80211_SME_IDLE)
57 is_all_idle = false;
58 wdev_unlock(wdev);
59 }
60 cfg80211_unlock_rdev(rdev);
61 }
62
63 mutex_unlock(&cfg80211_mutex);
64
65 return is_all_idle;
66}
67
68static void disconnect_work(struct work_struct *work)
69{
70 if (!cfg80211_is_all_idle())
71 return;
72
73 regulatory_hint_disconnect();
74}
75
76static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020077
78static int cfg80211_conn_scan(struct wireless_dev *wdev)
79{
Johannes Berg79c97e92009-07-07 03:56:12 +020080 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020081 struct cfg80211_scan_request *request;
82 int n_channels, err;
83
84 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020085 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020086 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020087
Johannes Berg79c97e92009-07-07 03:56:12 +020088 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020089 return -EBUSY;
90
91 if (wdev->conn->params.channel) {
92 n_channels = 1;
93 } else {
94 enum ieee80211_band band;
95 n_channels = 0;
96
97 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
98 if (!wdev->wiphy->bands[band])
99 continue;
100 n_channels += wdev->wiphy->bands[band]->n_channels;
101 }
102 }
103 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
104 sizeof(request->channels[0]) * n_channels,
105 GFP_KERNEL);
106 if (!request)
107 return -ENOMEM;
108
Johannes Berg6829c872009-07-02 09:13:27 +0200109 if (wdev->conn->params.channel)
110 request->channels[0] = wdev->conn->params.channel;
111 else {
112 int i = 0, j;
113 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530114 struct ieee80211_supported_band *bands;
115 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200116
117 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530118 bands = wdev->wiphy->bands[band];
119 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200120 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530121 for (j = 0; j < bands->n_channels; j++) {
122 channel = &bands->channels[j];
123 if (channel->flags & IEEE80211_CHAN_DISABLED)
124 continue;
125 request->channels[i++] = channel;
126 }
127 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200128 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530129 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200130 }
131 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200132 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200133 request->n_ssids = 1;
134
135 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
136 wdev->conn->params.ssid_len);
137 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
138
Johannes Berg463d0182009-07-14 00:33:35 +0200139 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200140 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200141
Johannes Berg79c97e92009-07-07 03:56:12 +0200142 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200145 if (!err) {
146 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200147 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200148 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200149 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200150 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200151 kfree(request);
152 }
153 return err;
154}
155
156static int cfg80211_conn_do_work(struct wireless_dev *wdev)
157{
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200160 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200162
Johannes Berg667503d2009-07-07 03:56:11 +0200163 ASSERT_WDEV_LOCK(wdev);
164
Johannes Berg6829c872009-07-02 09:13:27 +0200165 if (!wdev->conn)
166 return 0;
167
Johannes Berg19957bb2009-07-02 17:20:43 +0200168 params = &wdev->conn->params;
169
Johannes Berg6829c872009-07-02 09:13:27 +0200170 switch (wdev->conn->state) {
171 case CFG80211_CONN_SCAN_AGAIN:
172 return cfg80211_conn_scan(wdev);
173 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200174 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200175 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200177 params->channel, params->auth_type,
178 params->bssid,
179 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200180 NULL, 0,
181 params->key, params->key_len,
Johannes Berg95de8172012-01-20 13:55:25 +0100182 params->key_idx);
Johannes Berg6829c872009-07-02 09:13:27 +0200183 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200184 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200186 if (wdev->conn->prev_bssid_valid)
187 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200188 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200189 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200190 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200191 params->ssid, params->ssid_len,
192 params->ie, params->ie_len,
Jouni Malinen7e73ad52013-01-15 15:15:57 +0000193 params->mfp != NL80211_MFP_NO,
194 &params->crypto,
Ben Greear7e7c8922011-11-18 11:31:59 -0800195 params->flags, &params->ht_capa,
196 &params->ht_capa_mask);
Johannes Berg19957bb2009-07-02 17:20:43 +0200197 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200198 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200199 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300200 WLAN_REASON_DEAUTH_LEAVING,
201 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200202 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900203 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
204 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
205 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300206 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900207 /* return an error so that we call __cfg80211_connect_result() */
208 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200209 default:
210 return 0;
211 }
212}
213
214void cfg80211_conn_work(struct work_struct *work)
215{
Johannes Berg79c97e92009-07-07 03:56:12 +0200216 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200217 container_of(work, struct cfg80211_registered_device, conn_work);
218 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100219 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200220
221 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 cfg80211_lock_rdev(rdev);
223 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200224
Johannes Berg79c97e92009-07-07 03:56:12 +0200225 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg667503d2009-07-07 03:56:11 +0200226 wdev_lock(wdev);
227 if (!netif_running(wdev->netdev)) {
228 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200229 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200230 }
231 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
232 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200234 }
Johannes Berg7400f422009-10-31 07:40:37 +0100235 if (wdev->conn->params.bssid) {
236 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
237 bssid = bssid_buf;
238 }
Johannes Berg6829c872009-07-02 09:13:27 +0200239 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200240 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900241 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200242 NULL, 0, NULL, 0,
243 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200244 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200245 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200246 }
247
Johannes Berg79c97e92009-07-07 03:56:12 +0200248 mutex_unlock(&rdev->devlist_mtx);
249 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200250 rtnl_unlock();
251}
252
Johannes Bergbbac31f2009-09-16 09:04:26 -0700253static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200254{
Johannes Berg79c97e92009-07-07 03:56:12 +0200255 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200256 struct cfg80211_bss *bss;
257 u16 capa = WLAN_CAPABILITY_ESS;
258
Johannes Berg667503d2009-07-07 03:56:11 +0200259 ASSERT_WDEV_LOCK(wdev);
260
Johannes Berg6829c872009-07-02 09:13:27 +0200261 if (wdev->conn->params.privacy)
262 capa |= WLAN_CAPABILITY_PRIVACY;
263
Jouni Malinened9d0102011-05-16 19:40:15 +0300264 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
265 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200266 wdev->conn->params.ssid,
267 wdev->conn->params.ssid_len,
268 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
269 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200270 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700271 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200272
273 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
274 wdev->conn->params.bssid = wdev->conn->bssid;
275 wdev->conn->params.channel = bss->channel;
276 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200277 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200278
Johannes Bergbbac31f2009-09-16 09:04:26 -0700279 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200280}
281
Johannes Berg667503d2009-07-07 03:56:11 +0200282static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200283{
284 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200285 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700286 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200287
Johannes Berg667503d2009-07-07 03:56:11 +0200288 ASSERT_WDEV_LOCK(wdev);
289
Johannes Berg6829c872009-07-02 09:13:27 +0200290 if (wdev->sme_state != CFG80211_SME_CONNECTING)
291 return;
292
Zhu Yid4b1a682009-07-16 17:34:14 +0800293 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200294 return;
295
296 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
297 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
298 return;
299
Johannes Bergbbac31f2009-09-16 09:04:26 -0700300 bss = cfg80211_get_conn_bss(wdev);
301 if (bss) {
302 cfg80211_put_bss(bss);
303 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200304 /* not found */
305 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200306 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200307 else
Johannes Berg667503d2009-07-07 03:56:11 +0200308 __cfg80211_connect_result(
309 wdev->netdev,
310 wdev->conn->params.bssid,
311 NULL, 0, NULL, 0,
312 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200313 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200314 }
315}
316
Johannes Berg667503d2009-07-07 03:56:11 +0200317void cfg80211_sme_scan_done(struct net_device *dev)
318{
319 struct wireless_dev *wdev = dev->ieee80211_ptr;
320
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200321 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200322 wdev_lock(wdev);
323 __cfg80211_sme_scan_done(dev);
324 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200325 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200326}
327
328void cfg80211_sme_rx_auth(struct net_device *dev,
329 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200330{
331 struct wireless_dev *wdev = dev->ieee80211_ptr;
332 struct wiphy *wiphy = wdev->wiphy;
333 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
334 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
335 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
336
Johannes Berg667503d2009-07-07 03:56:11 +0200337 ASSERT_WDEV_LOCK(wdev);
338
Johannes Berg6829c872009-07-02 09:13:27 +0200339 /* should only RX auth frames when connecting */
340 if (wdev->sme_state != CFG80211_SME_CONNECTING)
341 return;
342
343 if (WARN_ON(!wdev->conn))
344 return;
345
346 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
347 wdev->conn->auto_auth &&
348 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
349 /* select automatically between only open, shared, leap */
350 switch (wdev->conn->params.auth_type) {
351 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200352 if (wdev->connect_keys)
353 wdev->conn->params.auth_type =
354 NL80211_AUTHTYPE_SHARED_KEY;
355 else
356 wdev->conn->params.auth_type =
357 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200358 break;
359 case NL80211_AUTHTYPE_SHARED_KEY:
360 wdev->conn->params.auth_type =
361 NL80211_AUTHTYPE_NETWORK_EAP;
362 break;
363 default:
364 /* huh? */
365 wdev->conn->params.auth_type =
366 NL80211_AUTHTYPE_OPEN_SYSTEM;
367 break;
368 }
369 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
370 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200371 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200372 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200373 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200374 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200375 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
376 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
377 schedule_work(&rdev->conn_work);
378 }
379}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200380
Johannes Bergf401a6f2009-08-07 14:51:05 +0200381bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
382{
383 struct wiphy *wiphy = wdev->wiphy;
384 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
385
386 if (WARN_ON(!wdev->conn))
387 return false;
388
389 if (!wdev->conn->prev_bssid_valid)
390 return false;
391
392 /*
393 * Some stupid APs don't accept reassoc, so we
394 * need to fall back to trying regular assoc.
395 */
396 wdev->conn->prev_bssid_valid = false;
397 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
398 schedule_work(&rdev->conn_work);
399
400 return true;
401}
402
Johannes Berg7d930bc2009-10-20 15:08:53 +0900403void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
404{
405 struct wiphy *wiphy = wdev->wiphy;
406 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
407
408 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
409 schedule_work(&rdev->conn_work);
410}
411
Johannes Berg667503d2009-07-07 03:56:11 +0200412void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
413 const u8 *req_ie, size_t req_ie_len,
414 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200415 u16 status, bool wextev,
416 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200417{
418 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700419 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200420#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200421 union iwreq_data wrqu;
422#endif
423
Johannes Berg667503d2009-07-07 03:56:11 +0200424 ASSERT_WDEV_LOCK(wdev);
425
Johannes Berg074ac8d2010-09-16 14:58:22 +0200426 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
427 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200428 return;
429
Johannes Bergf7969962009-08-21 12:23:49 +0200430 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200431 return;
432
433 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200434 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200435 resp_ie, resp_ie_len,
436 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200437
Johannes Berg3d23e342009-09-29 23:27:28 +0200438#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200439 if (wextev) {
440 if (req_ie && status == WLAN_STATUS_SUCCESS) {
441 memset(&wrqu, 0, sizeof(wrqu));
442 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800443 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200444 }
445
446 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
447 memset(&wrqu, 0, sizeof(wrqu));
448 wrqu.data.length = resp_ie_len;
449 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
450 }
451
452 memset(&wrqu, 0, sizeof(wrqu));
453 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200454 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200455 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200456 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
457 wdev->wext.prev_bssid_valid = true;
458 }
Johannes Berge45cd822009-07-02 09:58:04 +0200459 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
460 }
461#endif
462
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200463 if (wdev->current_bss) {
464 cfg80211_unhold_bss(wdev->current_bss);
465 cfg80211_put_bss(&wdev->current_bss->pub);
466 wdev->current_bss = NULL;
467 }
468
Johannes Berg19957bb2009-07-02 17:20:43 +0200469 if (wdev->conn)
470 wdev->conn->state = CFG80211_CONN_IDLE;
471
Johannes Bergfffd0932009-07-08 14:22:54 +0200472 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200473 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100474 if (wdev->conn)
475 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200476 kfree(wdev->conn);
477 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 kfree(wdev->connect_keys);
479 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200480 wdev->ssid_len = 0;
Johannes Berg95de8172012-01-20 13:55:25 +0100481 cfg80211_put_bss(bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200482 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200483 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200484
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200485 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300486 bss = cfg80211_get_bss(wdev->wiphy,
487 wdev->conn ? wdev->conn->params.channel :
488 NULL,
489 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200490 wdev->ssid, wdev->ssid_len,
491 WLAN_CAPABILITY_ESS,
492 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200493
494 if (WARN_ON(!bss))
495 return;
496
497 cfg80211_hold_bss(bss_from_pub(bss));
498 wdev->current_bss = bss_from_pub(bss);
499
Johannes Bergfffd0932009-07-08 14:22:54 +0200500 wdev->sme_state = CFG80211_SME_CONNECTED;
501 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700502
503 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
504
505 if (!country_ie)
506 return;
507
508 /*
509 * ieee80211_bss_get_ie() ensures we can access:
510 * - country_ie + 2, the start of the country ie data, and
511 * - and country_ie[1] which is the IE length
512 */
513 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500514 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700515 country_ie + 2,
516 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200517}
Johannes Bergf2129352009-07-01 21:26:56 +0200518
519void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
520 const u8 *req_ie, size_t req_ie_len,
521 const u8 *resp_ie, size_t resp_ie_len,
522 u16 status, gfp_t gfp)
523{
Johannes Berg667503d2009-07-07 03:56:11 +0200524 struct wireless_dev *wdev = dev->ieee80211_ptr;
525 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
526 struct cfg80211_event *ev;
527 unsigned long flags;
528
Johannes Bergf7969962009-08-21 12:23:49 +0200529 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
530
Johannes Berg667503d2009-07-07 03:56:11 +0200531 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
532 if (!ev)
533 return;
534
535 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800536 if (bssid)
537 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700538 if (req_ie_len) {
539 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
540 ev->cr.req_ie_len = req_ie_len;
541 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
542 }
543 if (resp_ie_len) {
544 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
545 ev->cr.resp_ie_len = resp_ie_len;
546 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
547 }
Johannes Berg667503d2009-07-07 03:56:11 +0200548 ev->cr.status = status;
549
550 spin_lock_irqsave(&wdev->event_lock, flags);
551 list_add_tail(&ev->list, &wdev->event_list);
552 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100553 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200554}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200555EXPORT_SYMBOL(cfg80211_connect_result);
556
Jouni Malinened9d0102011-05-16 19:40:15 +0300557void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530558 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200559 const u8 *req_ie, size_t req_ie_len,
560 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200561{
Johannes Berg3d23e342009-09-29 23:27:28 +0200562#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200563 union iwreq_data wrqu;
564#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200565 ASSERT_WDEV_LOCK(wdev);
566
Johannes Berg074ac8d2010-09-16 14:58:22 +0200567 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
568 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530569 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570
Johannes Bergf7969962009-08-21 12:23:49 +0200571 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530572 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200573
574 /* internal error -- how did we get to CONNECTED w/o BSS? */
575 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530576 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200577 }
578
579 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200580 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200581 wdev->current_bss = NULL;
582
Johannes Berg19957bb2009-07-02 17:20:43 +0200583 cfg80211_hold_bss(bss_from_pub(bss));
584 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200585
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530586 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200587 req_ie, req_ie_len, resp_ie, resp_ie_len,
588 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200589
Johannes Berg3d23e342009-09-29 23:27:28 +0200590#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591 if (req_ie) {
592 memset(&wrqu, 0, sizeof(wrqu));
593 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800594 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200595 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200596 }
597
598 if (resp_ie) {
599 memset(&wrqu, 0, sizeof(wrqu));
600 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200601 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
602 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200603 }
604
605 memset(&wrqu, 0, sizeof(wrqu));
606 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530607 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
608 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200609 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200610 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200611#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530612
613 return;
614out:
615 cfg80211_put_bss(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200616}
Johannes Berg667503d2009-07-07 03:56:11 +0200617
Jouni Malinened9d0102011-05-16 19:40:15 +0300618void cfg80211_roamed(struct net_device *dev,
619 struct ieee80211_channel *channel,
620 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200621 const u8 *req_ie, size_t req_ie_len,
622 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
623{
624 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530625 struct cfg80211_bss *bss;
626
627 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
628
629 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
630 wdev->ssid_len, WLAN_CAPABILITY_ESS,
631 WLAN_CAPABILITY_ESS);
632 if (WARN_ON(!bss))
633 return;
634
635 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
636 resp_ie_len, gfp);
637}
638EXPORT_SYMBOL(cfg80211_roamed);
639
640void cfg80211_roamed_bss(struct net_device *dev,
641 struct cfg80211_bss *bss, const u8 *req_ie,
642 size_t req_ie_len, const u8 *resp_ie,
643 size_t resp_ie_len, gfp_t gfp)
644{
645 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200646 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
647 struct cfg80211_event *ev;
648 unsigned long flags;
649
Johannes Bergf7969962009-08-21 12:23:49 +0200650 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
651
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530652 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200653 return;
654
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530655 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
656 if (!ev) {
657 cfg80211_put_bss(bss);
658 return;
659 }
660
Johannes Berg667503d2009-07-07 03:56:11 +0200661 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200662 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
663 ev->rm.req_ie_len = req_ie_len;
664 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
665 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
666 ev->rm.resp_ie_len = resp_ie_len;
667 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530668 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200669
670 spin_lock_irqsave(&wdev->event_lock, flags);
671 list_add_tail(&ev->list, &wdev->event_list);
672 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100673 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200674}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530675EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200676
Johannes Berg667503d2009-07-07 03:56:11 +0200677void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200678 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200679{
680 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200681 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
682 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200683#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200684 union iwreq_data wrqu;
685#endif
686
Johannes Berg667503d2009-07-07 03:56:11 +0200687 ASSERT_WDEV_LOCK(wdev);
688
Johannes Berg074ac8d2010-09-16 14:58:22 +0200689 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
690 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200691 return;
692
Dmitry Shmidte7c926f2011-10-18 12:30:02 -0700693#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Johannes Bergf7969962009-08-21 12:23:49 +0200694 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200695 return;
Dmitry Shmidte7c926f2011-10-18 12:30:02 -0700696#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +0200697
698 if (wdev->current_bss) {
699 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200700 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200701 }
702
703 wdev->current_bss = NULL;
704 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200705 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200706
Johannes Berg6829c872009-07-02 09:13:27 +0200707 if (wdev->conn) {
708 kfree(wdev->conn->ie);
709 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200710 kfree(wdev->conn);
711 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200712 }
713
Johannes Bergfffd0932009-07-08 14:22:54 +0200714 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
715
716 /*
717 * Delete all the keys ... pairwise keys can't really
718 * exist any more anyway, but default keys might.
719 */
720 if (rdev->ops->del_key)
721 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200722 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200723
Johannes Berg3d23e342009-09-29 23:27:28 +0200724#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200725 memset(&wrqu, 0, sizeof(wrqu));
726 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
727 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800728 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200729#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500730
731 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200732}
733
734void cfg80211_disconnected(struct net_device *dev, u16 reason,
735 u8 *ie, size_t ie_len, gfp_t gfp)
736{
Johannes Berg667503d2009-07-07 03:56:11 +0200737 struct wireless_dev *wdev = dev->ieee80211_ptr;
738 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
739 struct cfg80211_event *ev;
740 unsigned long flags;
741
Johannes Bergf7969962009-08-21 12:23:49 +0200742 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
743
Johannes Berg667503d2009-07-07 03:56:11 +0200744 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
745 if (!ev)
746 return;
747
748 ev->type = EVENT_DISCONNECTED;
749 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
750 ev->dc.ie_len = ie_len;
751 memcpy((void *)ev->dc.ie, ie, ie_len);
752 ev->dc.reason = reason;
753
754 spin_lock_irqsave(&wdev->event_lock, flags);
755 list_add_tail(&ev->list, &wdev->event_list);
756 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100757 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200758}
759EXPORT_SYMBOL(cfg80211_disconnected);
760
Johannes Berg667503d2009-07-07 03:56:11 +0200761int __cfg80211_connect(struct cfg80211_registered_device *rdev,
762 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200763 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200764 struct cfg80211_cached_keys *connkeys,
765 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200766{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200767 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700768 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200769 int err;
770
771 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200772
Dmitry Shmidtc6a6a522011-09-15 09:22:35 -0700773#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200774 if (wdev->sme_state != CFG80211_SME_IDLE)
775 return -EALREADY;
776
Johannes Bergfffd0932009-07-08 14:22:54 +0200777 if (WARN_ON(wdev->connect_keys)) {
Dmitry Shmidtbd0ac3f2011-10-28 10:35:37 -0700778#else
779 if (wdev->connect_keys) {
780#endif
Johannes Bergfffd0932009-07-08 14:22:54 +0200781 kfree(wdev->connect_keys);
782 wdev->connect_keys = NULL;
783 }
784
Ben Greear7e7c8922011-11-18 11:31:59 -0800785 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
786 rdev->wiphy.ht_capa_mod_mask);
787
Johannes Bergfffd0932009-07-08 14:22:54 +0200788 if (connkeys && connkeys->def >= 0) {
789 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200790 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200791
792 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200793 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200795 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
796 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200797 connect->key_idx = idx;
798 connect->key = connkeys->params[idx].key;
799 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200800
801 /*
802 * If ciphers are not set (e.g. when going through
803 * iwconfig), we have to set them appropriately here.
804 */
805 if (connect->crypto.cipher_group == 0)
806 connect->crypto.cipher_group = cipher;
807
808 if (connect->crypto.n_ciphers_pairwise == 0) {
809 connect->crypto.n_ciphers_pairwise = 1;
810 connect->crypto.ciphers_pairwise[0] = cipher;
811 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200812 }
813 }
814
Samuel Ortizb23aa672009-07-01 21:26:54 +0200815 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200816 if (!rdev->ops->auth || !rdev->ops->assoc)
817 return -EOPNOTSUPP;
818
Johannes Berg19957bb2009-07-02 17:20:43 +0200819 if (WARN_ON(wdev->conn))
820 return -EINPROGRESS;
821
822 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
823 if (!wdev->conn)
824 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200825
826 /*
827 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
828 */
829 memcpy(&wdev->conn->params, connect, sizeof(*connect));
830 if (connect->bssid) {
831 wdev->conn->params.bssid = wdev->conn->bssid;
832 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
833 }
834
835 if (connect->ie) {
836 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
837 GFP_KERNEL);
838 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200839 if (!wdev->conn->ie) {
840 kfree(wdev->conn);
841 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200842 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200843 }
Johannes Berg6829c872009-07-02 09:13:27 +0200844 }
845
846 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
847 wdev->conn->auto_auth = true;
848 /* start with open system ... should mostly work */
849 wdev->conn->params.auth_type =
850 NL80211_AUTHTYPE_OPEN_SYSTEM;
851 } else {
852 wdev->conn->auto_auth = false;
853 }
854
855 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
856 wdev->ssid_len = connect->ssid_len;
857 wdev->conn->params.ssid = wdev->ssid;
858 wdev->conn->params.ssid_len = connect->ssid_len;
859
Johannes Berg8bb89482009-09-26 14:42:53 +0200860 /* see if we have the bss already */
861 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200862
863 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200864 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200865
Johannes Bergf401a6f2009-08-07 14:51:05 +0200866 if (prev_bssid) {
867 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
868 wdev->conn->prev_bssid_valid = true;
869 }
870
Johannes Bergbbac31f2009-09-16 09:04:26 -0700871 /* we're good if we have a matching bss struct */
872 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200873 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
874 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700875 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200876 } else {
877 /* otherwise we'll need to scan for the AP first */
878 err = cfg80211_conn_scan(wdev);
879 /*
880 * If we can't scan right now, then we need to scan again
881 * after the current scan finished, since the parameters
882 * changed (unless we find a good AP anyway).
883 */
884 if (err == -EBUSY) {
885 err = 0;
886 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
887 }
888 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200889 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100890 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200891 kfree(wdev->conn);
892 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200893 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200894 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200895 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200896 }
Johannes Berg6829c872009-07-02 09:13:27 +0200897
898 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200899 } else {
900 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200901 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
903 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200904 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200905 wdev->sme_state = CFG80211_SME_IDLE;
906 return err;
907 }
Johannes Berg6829c872009-07-02 09:13:27 +0200908
909 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
910 wdev->ssid_len = connect->ssid_len;
911
912 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200914}
915
Johannes Berg667503d2009-07-07 03:56:11 +0200916int cfg80211_connect(struct cfg80211_registered_device *rdev,
917 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200918 struct cfg80211_connect_params *connect,
919 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200920{
921 int err;
922
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200923 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200924 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200925 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200926 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200927 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200928
929 return err;
930}
931
932int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
933 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934{
Johannes Berg6829c872009-07-02 09:13:27 +0200935 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200936 int err;
937
Johannes Berg667503d2009-07-07 03:56:11 +0200938 ASSERT_WDEV_LOCK(wdev);
939
Johannes Berg6829c872009-07-02 09:13:27 +0200940 if (wdev->sme_state == CFG80211_SME_IDLE)
941 return -EINVAL;
942
Johannes Bergfffd0932009-07-08 14:22:54 +0200943 kfree(wdev->connect_keys);
944 wdev->connect_keys = NULL;
945
Samuel Ortizb23aa672009-07-01 21:26:54 +0200946 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200947 if (!rdev->ops->deauth)
948 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200949
Johannes Berg19957bb2009-07-02 17:20:43 +0200950 /* was it connected by userspace SME? */
951 if (!wdev->conn) {
952 cfg80211_mlme_down(rdev, dev);
953 return 0;
954 }
Johannes Berg6829c872009-07-02 09:13:27 +0200955
956 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
957 (wdev->conn->state == CFG80211_CONN_SCANNING ||
958 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
959 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100960 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200961 kfree(wdev->conn);
962 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200963 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200964 return 0;
965 }
966
Johannes Berg6829c872009-07-02 09:13:27 +0200967 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +0200968 err = __cfg80211_mlme_deauth(rdev, dev,
969 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300970 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200971 if (err)
972 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973 } else {
974 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
975 if (err)
976 return err;
977 }
978
Johannes Berg6829c872009-07-02 09:13:27 +0200979 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +0200980 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200981 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200982 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
983 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200984 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200985
986 return 0;
987}
Johannes Berg19957bb2009-07-02 17:20:43 +0200988
Johannes Berg667503d2009-07-07 03:56:11 +0200989int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
990 struct net_device *dev,
991 u16 reason, bool wextev)
992{
993 int err;
994
995 wdev_lock(dev->ieee80211_ptr);
996 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
997 wdev_unlock(dev->ieee80211_ptr);
998
999 return err;
1000}
1001
Johannes Berg95de8172012-01-20 13:55:25 +01001002void cfg80211_sme_disassoc(struct net_device *dev,
1003 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001004{
1005 struct wireless_dev *wdev = dev->ieee80211_ptr;
1006 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1007 u8 bssid[ETH_ALEN];
1008
Johannes Berg667503d2009-07-07 03:56:11 +02001009 ASSERT_WDEV_LOCK(wdev);
1010
Johannes Berg19957bb2009-07-02 17:20:43 +02001011 if (!wdev->conn)
1012 return;
1013
1014 if (wdev->conn->state == CFG80211_CONN_IDLE)
1015 return;
1016
1017 /*
1018 * Ok, so the association was made by this SME -- we don't
1019 * want it any more so deauthenticate too.
1020 */
1021
Johannes Berg95de8172012-01-20 13:55:25 +01001022 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001023
Johannes Berg95de8172012-01-20 13:55:25 +01001024 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1025 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001026}