blob: 53f9e72d19b571f8fc43b60d5e94da319ae77887 [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
Sachin Ahuja8d6e5ec2014-04-28 15:26:53 +053068
69static bool cfg80211_is_all_countryie_ignore(void)
70{
71 struct cfg80211_registered_device *rdev;
72 struct wireless_dev *wdev;
73 bool is_all_countryie_ignore = true;
74
75 mutex_lock(&cfg80211_mutex);
76
77 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
78 cfg80211_lock_rdev(rdev);
79 list_for_each_entry(wdev, &rdev->netdev_list, list) {
80 wdev_lock(wdev);
81 if (!(wdev->wiphy->country_ie_pref &
82 NL80211_COUNTRY_IE_IGNORE_CORE)) {
83 is_all_countryie_ignore = false;
84 wdev_unlock(wdev);
85 cfg80211_unlock_rdev(rdev);
86 goto out;
87 }
88 wdev_unlock(wdev);
89 }
90 cfg80211_unlock_rdev(rdev);
91 }
92out:
93 mutex_unlock(&cfg80211_mutex);
94
95 return is_all_countryie_ignore;
96}
97
98
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050099static void disconnect_work(struct work_struct *work)
100{
101 if (!cfg80211_is_all_idle())
102 return;
103
Sachin Ahuja8d6e5ec2014-04-28 15:26:53 +0530104 if (cfg80211_is_all_countryie_ignore())
105 return;
106
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500107 regulatory_hint_disconnect();
108}
109
110static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200111
112static int cfg80211_conn_scan(struct wireless_dev *wdev)
113{
Johannes Berg79c97e92009-07-07 03:56:12 +0200114 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200115 struct cfg80211_scan_request *request;
116 int n_channels, err;
117
118 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +0200119 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200120 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200121
Johannes Berg79c97e92009-07-07 03:56:12 +0200122 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +0200123 return -EBUSY;
124
125 if (wdev->conn->params.channel) {
126 n_channels = 1;
127 } else {
128 enum ieee80211_band band;
129 n_channels = 0;
130
131 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
132 if (!wdev->wiphy->bands[band])
133 continue;
134 n_channels += wdev->wiphy->bands[band]->n_channels;
135 }
136 }
137 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
138 sizeof(request->channels[0]) * n_channels,
139 GFP_KERNEL);
140 if (!request)
141 return -ENOMEM;
142
Johannes Berg6829c872009-07-02 09:13:27 +0200143 if (wdev->conn->params.channel)
144 request->channels[0] = wdev->conn->params.channel;
145 else {
146 int i = 0, j;
147 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530148 struct ieee80211_supported_band *bands;
149 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200150
151 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530152 bands = wdev->wiphy->bands[band];
153 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200154 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530155 for (j = 0; j < bands->n_channels; j++) {
156 channel = &bands->channels[j];
157 if (channel->flags & IEEE80211_CHAN_DISABLED)
158 continue;
159 request->channels[i++] = channel;
160 }
161 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200162 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530163 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200164 }
165 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200166 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200167 request->n_ssids = 1;
168
169 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
170 wdev->conn->params.ssid_len);
171 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
172
Johannes Berg463d0182009-07-14 00:33:35 +0200173 request->dev = wdev->netdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200174 request->wiphy = &rdev->wiphy;
Johannes Berg6829c872009-07-02 09:13:27 +0200175
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200177
Johannes Berg79c97e92009-07-07 03:56:12 +0200178 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200179 if (!err) {
180 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200181 nl80211_send_scan_start(rdev, wdev->netdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200182 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200183 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200184 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200185 kfree(request);
186 }
187 return err;
188}
189
190static int cfg80211_conn_do_work(struct wireless_dev *wdev)
191{
Johannes Berg79c97e92009-07-07 03:56:12 +0200192 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200193 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200194 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200195 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200196
Johannes Berg667503d2009-07-07 03:56:11 +0200197 ASSERT_WDEV_LOCK(wdev);
198
Johannes Berg6829c872009-07-02 09:13:27 +0200199 if (!wdev->conn)
200 return 0;
201
Johannes Berg19957bb2009-07-02 17:20:43 +0200202 params = &wdev->conn->params;
203
Johannes Berg6829c872009-07-02 09:13:27 +0200204 switch (wdev->conn->state) {
205 case CFG80211_CONN_SCAN_AGAIN:
206 return cfg80211_conn_scan(wdev);
207 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200208 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200209 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200210 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200211 params->channel, params->auth_type,
212 params->bssid,
213 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200214 NULL, 0,
215 params->key, params->key_len,
Johannes Berg95de8172012-01-20 13:55:25 +0100216 params->key_idx);
Johannes Berg6829c872009-07-02 09:13:27 +0200217 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200218 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200219 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200220 if (wdev->conn->prev_bssid_valid)
221 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503d2009-07-07 03:56:11 +0200223 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200224 prev_bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200225 params->ssid, params->ssid_len,
226 params->ie, params->ie_len,
Jouni Malinen7e73ad52013-01-15 15:15:57 +0000227 params->mfp != NL80211_MFP_NO,
228 &params->crypto,
Ben Greear7e7c8922011-11-18 11:31:59 -0800229 params->flags, &params->ht_capa,
230 &params->ht_capa_mask);
Johannes Berg19957bb2009-07-02 17:20:43 +0200231 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200232 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200233 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300234 WLAN_REASON_DEAUTH_LEAVING,
235 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200236 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900237 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
238 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
239 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300240 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900241 /* return an error so that we call __cfg80211_connect_result() */
242 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200243 default:
244 return 0;
245 }
246}
247
248void cfg80211_conn_work(struct work_struct *work)
249{
Johannes Berg79c97e92009-07-07 03:56:12 +0200250 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200251 container_of(work, struct cfg80211_registered_device, conn_work);
252 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100253 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200254
255 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200256 cfg80211_lock_rdev(rdev);
257 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200258
Johannes Berg79c97e92009-07-07 03:56:12 +0200259 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Berg4bba2582013-05-23 18:10:21 +0200260 if (!wdev->netdev)
261 continue;
262
Johannes Berg667503d2009-07-07 03:56:11 +0200263 wdev_lock(wdev);
264 if (!netif_running(wdev->netdev)) {
265 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200266 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200267 }
268 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
269 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200270 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200271 }
Johannes Berg7400f422009-10-31 07:40:37 +0100272 if (wdev->conn->params.bssid) {
273 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
274 bssid = bssid_buf;
275 }
Johannes Berg6829c872009-07-02 09:13:27 +0200276 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503d2009-07-07 03:56:11 +0200277 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900278 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200279 NULL, 0, NULL, 0,
280 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200281 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200282 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200283 }
284
Johannes Berg79c97e92009-07-07 03:56:12 +0200285 mutex_unlock(&rdev->devlist_mtx);
286 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200287 rtnl_unlock();
288}
289
Johannes Bergbbac31f2009-09-16 09:04:26 -0700290static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200291{
Johannes Berg79c97e92009-07-07 03:56:12 +0200292 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200293 struct cfg80211_bss *bss;
294 u16 capa = WLAN_CAPABILITY_ESS;
295
Johannes Berg667503d2009-07-07 03:56:11 +0200296 ASSERT_WDEV_LOCK(wdev);
297
Johannes Berg6829c872009-07-02 09:13:27 +0200298 if (wdev->conn->params.privacy)
299 capa |= WLAN_CAPABILITY_PRIVACY;
300
Jouni Malinened9d0102011-05-16 19:40:15 +0300301 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
302 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200303 wdev->conn->params.ssid,
304 wdev->conn->params.ssid_len,
305 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
306 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200307 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700308 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200309
310 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
311 wdev->conn->params.bssid = wdev->conn->bssid;
312 wdev->conn->params.channel = bss->channel;
313 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200314 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200315
Johannes Bergbbac31f2009-09-16 09:04:26 -0700316 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200317}
318
Johannes Berg667503d2009-07-07 03:56:11 +0200319static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200322 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700323 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200324
Johannes Berg667503d2009-07-07 03:56:11 +0200325 ASSERT_WDEV_LOCK(wdev);
326
Johannes Berg6829c872009-07-02 09:13:27 +0200327 if (wdev->sme_state != CFG80211_SME_CONNECTING)
328 return;
329
Zhu Yid4b1a682009-07-16 17:34:14 +0800330 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200331 return;
332
333 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
334 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
335 return;
336
Johannes Bergbbac31f2009-09-16 09:04:26 -0700337 bss = cfg80211_get_conn_bss(wdev);
338 if (bss) {
339 cfg80211_put_bss(bss);
340 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200341 /* not found */
342 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200343 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200344 else
Johannes Berg667503d2009-07-07 03:56:11 +0200345 __cfg80211_connect_result(
346 wdev->netdev,
347 wdev->conn->params.bssid,
348 NULL, 0, NULL, 0,
349 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200350 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200351 }
352}
353
Johannes Berg667503d2009-07-07 03:56:11 +0200354void cfg80211_sme_scan_done(struct net_device *dev)
355{
356 struct wireless_dev *wdev = dev->ieee80211_ptr;
357
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200358 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200359 wdev_lock(wdev);
360 __cfg80211_sme_scan_done(dev);
361 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200362 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200363}
364
365void cfg80211_sme_rx_auth(struct net_device *dev,
366 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200367{
368 struct wireless_dev *wdev = dev->ieee80211_ptr;
369 struct wiphy *wiphy = wdev->wiphy;
370 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
371 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
372 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
373
Johannes Berg667503d2009-07-07 03:56:11 +0200374 ASSERT_WDEV_LOCK(wdev);
375
Johannes Berg6829c872009-07-02 09:13:27 +0200376 /* should only RX auth frames when connecting */
377 if (wdev->sme_state != CFG80211_SME_CONNECTING)
378 return;
379
380 if (WARN_ON(!wdev->conn))
381 return;
382
383 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
384 wdev->conn->auto_auth &&
385 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
386 /* select automatically between only open, shared, leap */
387 switch (wdev->conn->params.auth_type) {
388 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200389 if (wdev->connect_keys)
390 wdev->conn->params.auth_type =
391 NL80211_AUTHTYPE_SHARED_KEY;
392 else
393 wdev->conn->params.auth_type =
394 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200395 break;
396 case NL80211_AUTHTYPE_SHARED_KEY:
397 wdev->conn->params.auth_type =
398 NL80211_AUTHTYPE_NETWORK_EAP;
399 break;
400 default:
401 /* huh? */
402 wdev->conn->params.auth_type =
403 NL80211_AUTHTYPE_OPEN_SYSTEM;
404 break;
405 }
406 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
407 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200408 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200409 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200410 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200411 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200412 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
413 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
414 schedule_work(&rdev->conn_work);
415 }
416}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200417
Johannes Bergf401a6f2009-08-07 14:51:05 +0200418bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
419{
420 struct wiphy *wiphy = wdev->wiphy;
421 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
422
423 if (WARN_ON(!wdev->conn))
424 return false;
425
426 if (!wdev->conn->prev_bssid_valid)
427 return false;
428
429 /*
430 * Some stupid APs don't accept reassoc, so we
431 * need to fall back to trying regular assoc.
432 */
433 wdev->conn->prev_bssid_valid = false;
434 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
435 schedule_work(&rdev->conn_work);
436
437 return true;
438}
439
Johannes Berg7d930bc2009-10-20 15:08:53 +0900440void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
441{
442 struct wiphy *wiphy = wdev->wiphy;
443 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
444
445 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
446 schedule_work(&rdev->conn_work);
447}
448
Johannes Berg667503d2009-07-07 03:56:11 +0200449void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
450 const u8 *req_ie, size_t req_ie_len,
451 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200452 u16 status, bool wextev,
453 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200454{
455 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700456 u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200457#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200458 union iwreq_data wrqu;
459#endif
460
Johannes Berg667503d2009-07-07 03:56:11 +0200461 ASSERT_WDEV_LOCK(wdev);
462
Johannes Berg074ac8d2010-09-16 14:58:22 +0200463 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
464 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200465 return;
466
Johannes Bergf7969962009-08-21 12:23:49 +0200467 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200468 return;
469
470 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200471 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200472 resp_ie, resp_ie_len,
473 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200474
Johannes Berg3d23e342009-09-29 23:27:28 +0200475#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200476 if (wextev) {
477 if (req_ie && status == WLAN_STATUS_SUCCESS) {
478 memset(&wrqu, 0, sizeof(wrqu));
479 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800480 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200481 }
482
483 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
484 memset(&wrqu, 0, sizeof(wrqu));
485 wrqu.data.length = resp_ie_len;
486 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
487 }
488
489 memset(&wrqu, 0, sizeof(wrqu));
490 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200491 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200492 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200493 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
494 wdev->wext.prev_bssid_valid = true;
495 }
Johannes Berge45cd822009-07-02 09:58:04 +0200496 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
497 }
498#endif
499
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200500 if (wdev->current_bss) {
501 cfg80211_unhold_bss(wdev->current_bss);
502 cfg80211_put_bss(&wdev->current_bss->pub);
503 wdev->current_bss = NULL;
504 }
505
Johannes Berg19957bb2009-07-02 17:20:43 +0200506 if (wdev->conn)
507 wdev->conn->state = CFG80211_CONN_IDLE;
508
Johannes Bergfffd0932009-07-08 14:22:54 +0200509 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200510 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100511 if (wdev->conn)
512 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200513 kfree(wdev->conn);
514 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200515 kfree(wdev->connect_keys);
516 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200517 wdev->ssid_len = 0;
Johannes Berg95de8172012-01-20 13:55:25 +0100518 cfg80211_put_bss(bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200519 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200520 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200521
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200522 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300523 bss = cfg80211_get_bss(wdev->wiphy,
524 wdev->conn ? wdev->conn->params.channel :
525 NULL,
526 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200527 wdev->ssid, wdev->ssid_len,
528 WLAN_CAPABILITY_ESS,
529 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200530
531 if (WARN_ON(!bss))
532 return;
533
534 cfg80211_hold_bss(bss_from_pub(bss));
535 wdev->current_bss = bss_from_pub(bss);
536
Johannes Bergfffd0932009-07-08 14:22:54 +0200537 wdev->sme_state = CFG80211_SME_CONNECTED;
538 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700539
540 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
541
542 if (!country_ie)
543 return;
544
545 /*
546 * ieee80211_bss_get_ie() ensures we can access:
547 * - country_ie + 2, the start of the country ie data, and
548 * - and country_ie[1] which is the IE length
549 */
550 regulatory_hint_11d(wdev->wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500551 bss->channel->band,
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700552 country_ie + 2,
553 country_ie[1]);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200554}
Johannes Bergf2129352009-07-01 21:26:56 +0200555
556void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
557 const u8 *req_ie, size_t req_ie_len,
558 const u8 *resp_ie, size_t resp_ie_len,
559 u16 status, gfp_t gfp)
560{
Johannes Berg667503d2009-07-07 03:56:11 +0200561 struct wireless_dev *wdev = dev->ieee80211_ptr;
562 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
563 struct cfg80211_event *ev;
564 unsigned long flags;
565
Johannes Bergf7969962009-08-21 12:23:49 +0200566 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
567
Johannes Berg667503d2009-07-07 03:56:11 +0200568 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
569 if (!ev)
570 return;
571
572 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800573 if (bssid)
574 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700575 if (req_ie_len) {
576 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
577 ev->cr.req_ie_len = req_ie_len;
578 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
579 }
580 if (resp_ie_len) {
581 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
582 ev->cr.resp_ie_len = resp_ie_len;
583 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
584 }
Johannes Berg667503d2009-07-07 03:56:11 +0200585 ev->cr.status = status;
586
587 spin_lock_irqsave(&wdev->event_lock, flags);
588 list_add_tail(&ev->list, &wdev->event_list);
589 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100590 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200591}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200592EXPORT_SYMBOL(cfg80211_connect_result);
593
Jouni Malinened9d0102011-05-16 19:40:15 +0300594void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530595 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200596 const u8 *req_ie, size_t req_ie_len,
597 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200598{
Johannes Berg3d23e342009-09-29 23:27:28 +0200599#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200600 union iwreq_data wrqu;
601#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200602 ASSERT_WDEV_LOCK(wdev);
603
Johannes Berg074ac8d2010-09-16 14:58:22 +0200604 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
605 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530606 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200607
Johannes Bergf7969962009-08-21 12:23:49 +0200608 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530609 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200610
611 /* internal error -- how did we get to CONNECTED w/o BSS? */
612 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530613 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200614 }
615
616 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200617 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200618 wdev->current_bss = NULL;
619
Johannes Berg19957bb2009-07-02 17:20:43 +0200620 cfg80211_hold_bss(bss_from_pub(bss));
621 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200622
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530623 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200624 req_ie, req_ie_len, resp_ie, resp_ie_len,
625 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200626
Johannes Berg3d23e342009-09-29 23:27:28 +0200627#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200628 if (req_ie) {
629 memset(&wrqu, 0, sizeof(wrqu));
630 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800631 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200632 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200633 }
634
635 if (resp_ie) {
636 memset(&wrqu, 0, sizeof(wrqu));
637 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200638 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
639 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200640 }
641
642 memset(&wrqu, 0, sizeof(wrqu));
643 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530644 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
645 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200646 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200647 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200648#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530649
650 return;
651out:
652 cfg80211_put_bss(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200653}
Johannes Berg667503d2009-07-07 03:56:11 +0200654
Jouni Malinened9d0102011-05-16 19:40:15 +0300655void cfg80211_roamed(struct net_device *dev,
656 struct ieee80211_channel *channel,
657 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200658 const u8 *req_ie, size_t req_ie_len,
659 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
660{
661 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530662 struct cfg80211_bss *bss;
663
664 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
665
666 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
667 wdev->ssid_len, WLAN_CAPABILITY_ESS,
668 WLAN_CAPABILITY_ESS);
669 if (WARN_ON(!bss))
670 return;
671
672 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
673 resp_ie_len, gfp);
674}
675EXPORT_SYMBOL(cfg80211_roamed);
676
677void cfg80211_roamed_bss(struct net_device *dev,
678 struct cfg80211_bss *bss, const u8 *req_ie,
679 size_t req_ie_len, const u8 *resp_ie,
680 size_t resp_ie_len, gfp_t gfp)
681{
682 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200683 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
684 struct cfg80211_event *ev;
685 unsigned long flags;
686
Johannes Bergf7969962009-08-21 12:23:49 +0200687 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
688
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530689 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200690 return;
691
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530692 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
693 if (!ev) {
694 cfg80211_put_bss(bss);
695 return;
696 }
697
Johannes Berg667503d2009-07-07 03:56:11 +0200698 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200699 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
700 ev->rm.req_ie_len = req_ie_len;
701 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
702 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
703 ev->rm.resp_ie_len = resp_ie_len;
704 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530705 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200706
707 spin_lock_irqsave(&wdev->event_lock, flags);
708 list_add_tail(&ev->list, &wdev->event_list);
709 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100710 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200711}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530712EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713
Johannes Berg667503d2009-07-07 03:56:11 +0200714void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200715 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200716{
717 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200718 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
719 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200720#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200721 union iwreq_data wrqu;
722#endif
723
Johannes Berg667503d2009-07-07 03:56:11 +0200724 ASSERT_WDEV_LOCK(wdev);
725
Johannes Berg074ac8d2010-09-16 14:58:22 +0200726 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
727 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200728 return;
729
Dmitry Shmidte7c926f2011-10-18 12:30:02 -0700730#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Johannes Bergf7969962009-08-21 12:23:49 +0200731 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200732 return;
Dmitry Shmidte7c926f2011-10-18 12:30:02 -0700733#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +0200734
735 if (wdev->current_bss) {
736 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200737 cfg80211_put_bss(&wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200738 }
739
740 wdev->current_bss = NULL;
741 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200742 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743
Johannes Berg6829c872009-07-02 09:13:27 +0200744 if (wdev->conn) {
745 kfree(wdev->conn->ie);
746 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200747 kfree(wdev->conn);
748 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200749 }
750
Johannes Bergfffd0932009-07-08 14:22:54 +0200751 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
752
753 /*
754 * Delete all the keys ... pairwise keys can't really
755 * exist any more anyway, but default keys might.
756 */
757 if (rdev->ops->del_key)
758 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200759 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200760
Kyeyoon Park1368a382014-04-17 19:57:34 +0530761 if (rdev->ops->set_qos_map) {
762 rdev->ops->set_qos_map(&rdev->wiphy, dev, NULL);
763 }
764
Johannes Berg3d23e342009-09-29 23:27:28 +0200765#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200766 memset(&wrqu, 0, sizeof(wrqu));
767 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
768 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800769 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200770#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500771
772 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773}
774
775void cfg80211_disconnected(struct net_device *dev, u16 reason,
776 u8 *ie, size_t ie_len, gfp_t gfp)
777{
Johannes Berg667503d2009-07-07 03:56:11 +0200778 struct wireless_dev *wdev = dev->ieee80211_ptr;
779 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
780 struct cfg80211_event *ev;
781 unsigned long flags;
782
Johannes Bergf7969962009-08-21 12:23:49 +0200783 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
784
Johannes Berg667503d2009-07-07 03:56:11 +0200785 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
786 if (!ev)
787 return;
788
789 ev->type = EVENT_DISCONNECTED;
790 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
791 ev->dc.ie_len = ie_len;
792 memcpy((void *)ev->dc.ie, ie, ie_len);
793 ev->dc.reason = reason;
794
795 spin_lock_irqsave(&wdev->event_lock, flags);
796 list_add_tail(&ev->list, &wdev->event_list);
797 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100798 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200799}
800EXPORT_SYMBOL(cfg80211_disconnected);
801
Johannes Berg667503d2009-07-07 03:56:11 +0200802int __cfg80211_connect(struct cfg80211_registered_device *rdev,
803 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200804 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200805 struct cfg80211_cached_keys *connkeys,
806 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200807{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200808 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700809 struct cfg80211_bss *bss = NULL;
Johannes Berg667503d2009-07-07 03:56:11 +0200810 int err;
811
812 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200813
Dmitry Shmidtc6a6a522011-09-15 09:22:35 -0700814#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200815 if (wdev->sme_state != CFG80211_SME_IDLE)
816 return -EALREADY;
817
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 if (WARN_ON(wdev->connect_keys)) {
Dmitry Shmidtbd0ac3f2011-10-28 10:35:37 -0700819#else
820 if (wdev->connect_keys) {
821#endif
Johannes Bergfffd0932009-07-08 14:22:54 +0200822 kfree(wdev->connect_keys);
823 wdev->connect_keys = NULL;
824 }
825
Ben Greear7e7c8922011-11-18 11:31:59 -0800826 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
827 rdev->wiphy.ht_capa_mod_mask);
828
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 if (connkeys && connkeys->def >= 0) {
830 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200831 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200832
833 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200834 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200835 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200836 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
837 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200838 connect->key_idx = idx;
839 connect->key = connkeys->params[idx].key;
840 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200841
842 /*
843 * If ciphers are not set (e.g. when going through
844 * iwconfig), we have to set them appropriately here.
845 */
846 if (connect->crypto.cipher_group == 0)
847 connect->crypto.cipher_group = cipher;
848
849 if (connect->crypto.n_ciphers_pairwise == 0) {
850 connect->crypto.n_ciphers_pairwise = 1;
851 connect->crypto.ciphers_pairwise[0] = cipher;
852 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 }
854 }
855
Samuel Ortizb23aa672009-07-01 21:26:54 +0200856 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200857 if (!rdev->ops->auth || !rdev->ops->assoc)
858 return -EOPNOTSUPP;
859
Johannes Berg19957bb2009-07-02 17:20:43 +0200860 if (WARN_ON(wdev->conn))
861 return -EINPROGRESS;
862
863 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
864 if (!wdev->conn)
865 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200866
867 /*
868 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
869 */
870 memcpy(&wdev->conn->params, connect, sizeof(*connect));
871 if (connect->bssid) {
872 wdev->conn->params.bssid = wdev->conn->bssid;
873 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
874 }
875
876 if (connect->ie) {
877 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
878 GFP_KERNEL);
879 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200880 if (!wdev->conn->ie) {
881 kfree(wdev->conn);
882 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200883 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200884 }
Johannes Berg6829c872009-07-02 09:13:27 +0200885 }
886
887 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
888 wdev->conn->auto_auth = true;
889 /* start with open system ... should mostly work */
890 wdev->conn->params.auth_type =
891 NL80211_AUTHTYPE_OPEN_SYSTEM;
892 } else {
893 wdev->conn->auto_auth = false;
894 }
895
896 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
897 wdev->ssid_len = connect->ssid_len;
898 wdev->conn->params.ssid = wdev->ssid;
899 wdev->conn->params.ssid_len = connect->ssid_len;
900
Johannes Berg8bb89482009-09-26 14:42:53 +0200901 /* see if we have the bss already */
902 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200903
904 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200905 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200906
Johannes Bergf401a6f2009-08-07 14:51:05 +0200907 if (prev_bssid) {
908 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
909 wdev->conn->prev_bssid_valid = true;
910 }
911
Johannes Bergbbac31f2009-09-16 09:04:26 -0700912 /* we're good if we have a matching bss struct */
913 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200914 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
915 err = cfg80211_conn_do_work(wdev);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700916 cfg80211_put_bss(bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200917 } else {
918 /* otherwise we'll need to scan for the AP first */
919 err = cfg80211_conn_scan(wdev);
920 /*
921 * If we can't scan right now, then we need to scan again
922 * after the current scan finished, since the parameters
923 * changed (unless we find a good AP anyway).
924 */
925 if (err == -EBUSY) {
926 err = 0;
927 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
928 }
929 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200930 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100931 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200932 kfree(wdev->conn);
933 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200934 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200935 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200936 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200937 }
Johannes Berg6829c872009-07-02 09:13:27 +0200938
939 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200940 } else {
941 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200942 wdev->connect_keys = connkeys;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200943 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
944 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200945 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200946 wdev->sme_state = CFG80211_SME_IDLE;
947 return err;
948 }
Johannes Berg6829c872009-07-02 09:13:27 +0200949
950 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
951 wdev->ssid_len = connect->ssid_len;
952
953 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200954 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200955}
956
Johannes Berg667503d2009-07-07 03:56:11 +0200957int cfg80211_connect(struct cfg80211_registered_device *rdev,
958 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200959 struct cfg80211_connect_params *connect,
960 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503d2009-07-07 03:56:11 +0200961{
962 int err;
963
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200964 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200965 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200966 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200967 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200968 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200969
970 return err;
971}
972
973int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
974 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200975{
Johannes Berg6829c872009-07-02 09:13:27 +0200976 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200977 int err;
978
Johannes Berg667503d2009-07-07 03:56:11 +0200979 ASSERT_WDEV_LOCK(wdev);
980
Johannes Berg6829c872009-07-02 09:13:27 +0200981 if (wdev->sme_state == CFG80211_SME_IDLE)
982 return -EINVAL;
983
Johannes Bergfffd0932009-07-08 14:22:54 +0200984 kfree(wdev->connect_keys);
985 wdev->connect_keys = NULL;
986
Samuel Ortizb23aa672009-07-01 21:26:54 +0200987 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200988 if (!rdev->ops->deauth)
989 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200990
Johannes Berg19957bb2009-07-02 17:20:43 +0200991 /* was it connected by userspace SME? */
992 if (!wdev->conn) {
993 cfg80211_mlme_down(rdev, dev);
994 return 0;
995 }
Johannes Berg6829c872009-07-02 09:13:27 +0200996
997 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
998 (wdev->conn->state == CFG80211_CONN_SCANNING ||
999 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
1000 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +01001001 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +02001002 kfree(wdev->conn);
1003 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +02001004 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +02001005 return 0;
1006 }
1007
Johannes Berg6829c872009-07-02 09:13:27 +02001008 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503d2009-07-07 03:56:11 +02001009 err = __cfg80211_mlme_deauth(rdev, dev,
1010 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03001011 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +02001012 if (err)
1013 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001014 } else {
1015 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
1016 if (err)
1017 return err;
1018 }
1019
Johannes Berg6829c872009-07-02 09:13:27 +02001020 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503d2009-07-07 03:56:11 +02001021 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +02001022 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +02001023 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
1024 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +02001025 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001026
1027 return 0;
1028}
Johannes Berg19957bb2009-07-02 17:20:43 +02001029
Johannes Berg667503d2009-07-07 03:56:11 +02001030int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1031 struct net_device *dev,
1032 u16 reason, bool wextev)
1033{
1034 int err;
1035
1036 wdev_lock(dev->ieee80211_ptr);
1037 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1038 wdev_unlock(dev->ieee80211_ptr);
1039
1040 return err;
1041}
1042
Johannes Berg95de8172012-01-20 13:55:25 +01001043void cfg80211_sme_disassoc(struct net_device *dev,
1044 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001045{
1046 struct wireless_dev *wdev = dev->ieee80211_ptr;
1047 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1048 u8 bssid[ETH_ALEN];
1049
Johannes Berg667503d2009-07-07 03:56:11 +02001050 ASSERT_WDEV_LOCK(wdev);
1051
Johannes Berg19957bb2009-07-02 17:20:43 +02001052 if (!wdev->conn)
1053 return;
1054
1055 if (wdev->conn->state == CFG80211_CONN_IDLE)
1056 return;
1057
1058 /*
1059 * Ok, so the association was made by this SME -- we don't
1060 * want it any more so deauthenticate too.
1061 */
1062
Johannes Berg95de8172012-01-20 13:55:25 +01001063 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001064
Johannes Berg95de8172012-01-20 13:55:25 +01001065 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1066 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001067}