blob: ea1da6621ff051028970f50154af56d6f2e01b46 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
Johannes Bergceca7b72013-05-16 00:55:45 +02002 * SME code for cfg80211
3 * both driver SME event handling and the SME implementation
4 * (for nl80211's connect() and wext)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005 *
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020016#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020017#include <net/cfg80211.h>
18#include <net/rtnetlink.h>
19#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070020#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030021#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020022
Johannes Bergceca7b72013-05-16 00:55:45 +020023/*
24 * Software SME in cfg80211, using auth/assoc/deauth calls to the
25 * driver. This is is for implementing nl80211's connect/disconnect
26 * and wireless extensions (if configured.)
27 */
28
Johannes Berg6829c872009-07-02 09:13:27 +020029struct cfg80211_conn {
30 struct cfg80211_connect_params params;
31 /* these are sub-states of the _CONNECTING sme_state */
32 enum {
Johannes Berg6829c872009-07-02 09:13:27 +020033 CFG80211_CONN_SCANNING,
34 CFG80211_CONN_SCAN_AGAIN,
35 CFG80211_CONN_AUTHENTICATE_NEXT,
36 CFG80211_CONN_AUTHENTICATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020037 CFG80211_CONN_AUTH_FAILED,
Johannes Berg6829c872009-07-02 09:13:27 +020038 CFG80211_CONN_ASSOCIATE_NEXT,
39 CFG80211_CONN_ASSOCIATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020040 CFG80211_CONN_ASSOC_FAILED,
Johannes Bergceca7b72013-05-16 00:55:45 +020041 CFG80211_CONN_DEAUTH,
42 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020043 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020044 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020045 u8 *ie;
46 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020047 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020048};
49
Johannes Bergceca7b72013-05-16 00:55:45 +020050static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050051{
Johannes Bergceca7b72013-05-16 00:55:45 +020052 if (!wdev->conn)
53 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050054
Johannes Bergceca7b72013-05-16 00:55:45 +020055 kfree(wdev->conn->ie);
56 kfree(wdev->conn);
57 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050058}
59
Johannes Berg6829c872009-07-02 09:13:27 +020060static int cfg80211_conn_scan(struct wireless_dev *wdev)
61{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080062 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020063 struct cfg80211_scan_request *request;
64 int n_channels, err;
65
66 ASSERT_RTNL();
Johannes Berg667503d2009-07-07 03:56:11 +020067 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020068
Johannes Bergf9d15d12014-01-22 11:14:19 +020069 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020070 return -EBUSY;
71
Ilan Peerbdfbec22014-01-09 11:37:23 +020072 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020073 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020074 else
75 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020076
Johannes Berg6829c872009-07-02 09:13:27 +020077 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 sizeof(request->channels[0]) * n_channels,
79 GFP_KERNEL);
80 if (!request)
81 return -ENOMEM;
82
Karl Beldan2a84ee82014-10-07 11:42:18 +020083 if (wdev->conn->params.channel) {
84 enum ieee80211_band band = wdev->conn->params.channel->band;
85 struct ieee80211_supported_band *sband =
86 wdev->wiphy->bands[band];
87
88 if (!sband) {
89 kfree(request);
90 return -EINVAL;
91 }
Johannes Berg6829c872009-07-02 09:13:27 +020092 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +020093 request->rates[band] = (1 << sband->n_bitrates) - 1;
94 } else {
Johannes Berg6829c872009-07-02 09:13:27 +020095 int i = 0, j;
96 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053097 struct ieee80211_supported_band *bands;
98 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020099
100 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530101 bands = wdev->wiphy->bands[band];
102 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200103 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530104 for (j = 0; j < bands->n_channels; j++) {
105 channel = &bands->channels[j];
106 if (channel->flags & IEEE80211_CHAN_DISABLED)
107 continue;
108 request->channels[i++] = channel;
109 }
110 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200111 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530112 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200113 }
114 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200115 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200116 request->n_ssids = 1;
117
118 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
119 wdev->conn->params.ssid_len);
120 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
121
Johannes Bergfd014282012-06-18 19:17:03 +0200122 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200123 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700124 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200125
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200127
Hila Gonene35e4d22012-06-27 17:19:42 +0300128 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200129 if (!err) {
130 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200131 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200132 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200133 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200134 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200135 kfree(request);
136 }
137 return err;
138}
139
140static int cfg80211_conn_do_work(struct wireless_dev *wdev)
141{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800142 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200143 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100144 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200145 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200146
Johannes Berg667503d2009-07-07 03:56:11 +0200147 ASSERT_WDEV_LOCK(wdev);
148
Johannes Berg6829c872009-07-02 09:13:27 +0200149 if (!wdev->conn)
150 return 0;
151
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 params = &wdev->conn->params;
153
Johannes Berg6829c872009-07-02 09:13:27 +0200154 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200155 case CFG80211_CONN_SCANNING:
156 /* didn't find it during scan ... */
157 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200158 case CFG80211_CONN_SCAN_AGAIN:
159 return cfg80211_conn_scan(wdev);
160 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200161 if (WARN_ON(!rdev->ops->auth))
162 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200164 return cfg80211_mlme_auth(rdev, wdev->netdev,
165 params->channel, params->auth_type,
166 params->bssid,
167 params->ssid, params->ssid_len,
168 NULL, 0,
169 params->key, params->key_len,
170 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200171 case CFG80211_CONN_AUTH_FAILED:
172 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200173 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200174 if (WARN_ON(!rdev->ops->assoc))
175 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200176 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200177 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100178 req.prev_bssid = wdev->conn->prev_bssid;
179 req.ie = params->ie;
180 req.ie_len = params->ie_len;
181 req.use_mfp = params->mfp != NL80211_MFP_NO;
182 req.crypto = params->crypto;
183 req.flags = params->flags;
184 req.ht_capa = params->ht_capa;
185 req.ht_capa_mask = params->ht_capa_mask;
186 req.vht_capa = params->vht_capa;
187 req.vht_capa_mask = params->vht_capa_mask;
188
Johannes Berg91bf9b22013-05-15 17:44:01 +0200189 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
190 params->bssid, params->ssid,
191 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200192 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200193 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
195 WLAN_REASON_DEAUTH_LEAVING,
196 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200197 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200198 case CFG80211_CONN_ASSOC_FAILED:
199 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
200 NULL, 0,
201 WLAN_REASON_DEAUTH_LEAVING, false);
202 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200203 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200204 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
205 NULL, 0,
206 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200207 /* free directly, disconnected event already sent */
208 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200209 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200210 default:
211 return 0;
212 }
213}
214
215void cfg80211_conn_work(struct work_struct *work)
216{
Johannes Berg79c97e92009-07-07 03:56:12 +0200217 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200218 container_of(work, struct cfg80211_registered_device, conn_work);
219 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100220 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200221
222 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200223
Johannes Berg89a54e42012-06-15 14:33:17 +0200224 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200225 if (!wdev->netdev)
226 continue;
227
Johannes Berg667503d2009-07-07 03:56:11 +0200228 wdev_lock(wdev);
229 if (!netif_running(wdev->netdev)) {
230 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200231 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200232 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200233 if (!wdev->conn ||
234 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503d2009-07-07 03:56:11 +0200235 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200236 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200237 }
Johannes Berg7400f422009-10-31 07:40:37 +0100238 if (wdev->conn->params.bssid) {
239 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
240 bssid = bssid_buf;
241 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200242 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503d2009-07-07 03:56:11 +0200243 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900244 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200245 NULL, 0, NULL, 0,
246 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200247 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200248 }
Johannes Berg667503d2009-07-07 03:56:11 +0200249 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200250 }
251
Johannes Berg6829c872009-07-02 09:13:27 +0200252 rtnl_unlock();
253}
254
Ben Greear0e3a39b2013-06-19 14:06:27 -0700255/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700256static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200257{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800258 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200260
Johannes Berg667503d2009-07-07 03:56:11 +0200261 ASSERT_WDEV_LOCK(wdev);
262
Jouni Malinened9d0102011-05-16 19:40:15 +0300263 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
264 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200265 wdev->conn->params.ssid,
266 wdev->conn->params.ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200267 IEEE80211_BSS_TYPE_ESS,
268 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200269 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700270 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200271
272 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
273 wdev->conn->params.bssid = wdev->conn->bssid;
274 wdev->conn->params.channel = bss->channel;
275 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200276 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200277
Johannes Bergbbac31f2009-09-16 09:04:26 -0700278 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200279}
280
Johannes Berg667503d2009-07-07 03:56:11 +0200281static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200282{
283 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800284 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700285 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200286
Johannes Berg667503d2009-07-07 03:56:11 +0200287 ASSERT_WDEV_LOCK(wdev);
288
Zhu Yid4b1a682009-07-16 17:34:14 +0800289 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200290 return;
291
292 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
293 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
294 return;
295
Johannes Bergbbac31f2009-09-16 09:04:26 -0700296 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200297 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100298 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200299 else
300 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200301}
302
Johannes Berg667503d2009-07-07 03:56:11 +0200303void cfg80211_sme_scan_done(struct net_device *dev)
304{
305 struct wireless_dev *wdev = dev->ieee80211_ptr;
306
307 wdev_lock(wdev);
308 __cfg80211_sme_scan_done(dev);
309 wdev_unlock(wdev);
310}
311
Johannes Bergceca7b72013-05-16 00:55:45 +0200312void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200313{
Johannes Berg6829c872009-07-02 09:13:27 +0200314 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800315 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200316 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
317 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
318
Johannes Berg667503d2009-07-07 03:56:11 +0200319 ASSERT_WDEV_LOCK(wdev);
320
Johannes Bergceca7b72013-05-16 00:55:45 +0200321 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200322 return;
323
324 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
325 wdev->conn->auto_auth &&
326 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
327 /* select automatically between only open, shared, leap */
328 switch (wdev->conn->params.auth_type) {
329 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200330 if (wdev->connect_keys)
331 wdev->conn->params.auth_type =
332 NL80211_AUTHTYPE_SHARED_KEY;
333 else
334 wdev->conn->params.auth_type =
335 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200336 break;
337 case NL80211_AUTHTYPE_SHARED_KEY:
338 wdev->conn->params.auth_type =
339 NL80211_AUTHTYPE_NETWORK_EAP;
340 break;
341 default:
342 /* huh? */
343 wdev->conn->params.auth_type =
344 NL80211_AUTHTYPE_OPEN_SYSTEM;
345 break;
346 }
347 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
348 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200349 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200350 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
351 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200352 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200353 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200354 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
355 schedule_work(&rdev->conn_work);
356 }
357}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200358
Johannes Bergceca7b72013-05-16 00:55:45 +0200359bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200360{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800361 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200362
Johannes Bergceca7b72013-05-16 00:55:45 +0200363 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200364 return false;
365
Johannes Bergceca7b72013-05-16 00:55:45 +0200366 if (status == WLAN_STATUS_SUCCESS) {
367 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200368 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200369 }
370
371 if (wdev->conn->prev_bssid_valid) {
372 /*
373 * Some stupid APs don't accept reassoc, so we
374 * need to fall back to trying regular assoc;
375 * return true so no event is sent to userspace.
376 */
377 wdev->conn->prev_bssid_valid = false;
378 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
379 schedule_work(&rdev->conn_work);
380 return true;
381 }
382
Johannes Berg923a0e72013-06-28 11:38:54 +0200383 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200384 schedule_work(&rdev->conn_work);
385 return false;
386}
387
388void cfg80211_sme_deauth(struct wireless_dev *wdev)
389{
390 cfg80211_sme_free(wdev);
391}
392
393void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
394{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800395 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200396
397 if (!wdev->conn)
398 return;
399
400 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
401 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200402}
403
404void cfg80211_sme_disassoc(struct wireless_dev *wdev)
405{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800406 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200407
408 if (!wdev->conn)
409 return;
410
411 wdev->conn->state = CFG80211_CONN_DEAUTH;
412 schedule_work(&rdev->conn_work);
413}
414
415void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
416{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800417 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200418
419 if (!wdev->conn)
420 return;
421
422 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
423 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200424}
425
426static int cfg80211_sme_connect(struct wireless_dev *wdev,
427 struct cfg80211_connect_params *connect,
428 const u8 *prev_bssid)
429{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800430 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200431 struct cfg80211_bss *bss;
432 int err;
433
434 if (!rdev->ops->auth || !rdev->ops->assoc)
435 return -EOPNOTSUPP;
436
437 if (wdev->current_bss)
438 return -EALREADY;
439
440 if (WARN_ON(wdev->conn))
441 return -EINPROGRESS;
442
443 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
444 if (!wdev->conn)
445 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200446
447 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200448 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200449 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200450 memcpy(&wdev->conn->params, connect, sizeof(*connect));
451 if (connect->bssid) {
452 wdev->conn->params.bssid = wdev->conn->bssid;
453 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
454 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200455
Johannes Bergceca7b72013-05-16 00:55:45 +0200456 if (connect->ie) {
457 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
458 GFP_KERNEL);
459 wdev->conn->params.ie = wdev->conn->ie;
460 if (!wdev->conn->ie) {
461 kfree(wdev->conn);
462 wdev->conn = NULL;
463 return -ENOMEM;
464 }
465 }
466
467 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
468 wdev->conn->auto_auth = true;
469 /* start with open system ... should mostly work */
470 wdev->conn->params.auth_type =
471 NL80211_AUTHTYPE_OPEN_SYSTEM;
472 } else {
473 wdev->conn->auto_auth = false;
474 }
475
476 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800477 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200478
479 /* see if we have the bss already */
480 bss = cfg80211_get_conn_bss(wdev);
481
482 if (prev_bssid) {
483 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
484 wdev->conn->prev_bssid_valid = true;
485 }
486
487 /* we're good if we have a matching bss struct */
488 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200489 err = cfg80211_conn_do_work(wdev);
490 cfg80211_put_bss(wdev->wiphy, bss);
491 } else {
492 /* otherwise we'll need to scan for the AP first */
493 err = cfg80211_conn_scan(wdev);
494
495 /*
496 * If we can't scan right now, then we need to scan again
497 * after the current scan finished, since the parameters
498 * changed (unless we find a good AP anyway).
499 */
500 if (err == -EBUSY) {
501 err = 0;
502 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
503 }
504 }
505
506 if (err)
507 cfg80211_sme_free(wdev);
508
509 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200510}
511
Johannes Bergceca7b72013-05-16 00:55:45 +0200512static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900513{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800514 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200515 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900516
Johannes Bergceca7b72013-05-16 00:55:45 +0200517 if (!wdev->conn)
518 return 0;
519
520 if (!rdev->ops->deauth)
521 return -EOPNOTSUPP;
522
523 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
524 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
525 err = 0;
526 goto out;
527 }
528
529 /* wdev->conn->params.bssid must be set if > SCANNING */
530 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
531 wdev->conn->params.bssid,
532 NULL, 0, reason, false);
533 out:
534 cfg80211_sme_free(wdev);
535 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900536}
537
Johannes Bergceca7b72013-05-16 00:55:45 +0200538/*
539 * code shared for in-device and software SME
540 */
541
542static bool cfg80211_is_all_idle(void)
543{
544 struct cfg80211_registered_device *rdev;
545 struct wireless_dev *wdev;
546 bool is_all_idle = true;
547
548 /*
549 * All devices must be idle as otherwise if you are actively
550 * scanning some new beacon hints could be learned and would
551 * count as new regulatory hints.
552 */
553 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
554 list_for_each_entry(wdev, &rdev->wdev_list, list) {
555 wdev_lock(wdev);
556 if (wdev->conn || wdev->current_bss)
557 is_all_idle = false;
558 wdev_unlock(wdev);
559 }
560 }
561
562 return is_all_idle;
563}
564
565static void disconnect_work(struct work_struct *work)
566{
567 rtnl_lock();
568 if (cfg80211_is_all_idle())
569 regulatory_hint_disconnect();
570 rtnl_unlock();
571}
572
573static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
574
575
576/*
577 * API calls for drivers implementing connect/disconnect and
578 * SME event handling
579 */
580
Ben Greear6f390902013-06-19 14:06:25 -0700581/* This method must consume bss one way or another */
Johannes Berg667503d2009-07-07 03:56:11 +0200582void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
583 const u8 *req_ie, size_t req_ie_len,
584 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200585 u16 status, bool wextev,
586 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200587{
588 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100589 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200590#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591 union iwreq_data wrqu;
592#endif
593
Johannes Berg667503d2009-07-07 03:56:11 +0200594 ASSERT_WDEV_LOCK(wdev);
595
Johannes Berg074ac8d2010-09-16 14:58:22 +0200596 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700597 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
598 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200599 return;
Ben Greear6f390902013-06-19 14:06:25 -0700600 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200601
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800602 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200603 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200604 resp_ie, resp_ie_len,
605 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200606
Johannes Berg3d23e342009-09-29 23:27:28 +0200607#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200608 if (wextev) {
609 if (req_ie && status == WLAN_STATUS_SUCCESS) {
610 memset(&wrqu, 0, sizeof(wrqu));
611 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800612 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200613 }
614
615 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
616 memset(&wrqu, 0, sizeof(wrqu));
617 wrqu.data.length = resp_ie_len;
618 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
619 }
620
621 memset(&wrqu, 0, sizeof(wrqu));
622 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200623 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200624 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200625 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
626 wdev->wext.prev_bssid_valid = true;
627 }
Johannes Berge45cd822009-07-02 09:58:04 +0200628 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
629 }
630#endif
631
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530632 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800633 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530634 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
635 wdev->ssid, wdev->ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200636 IEEE80211_BSS_TYPE_ESS,
637 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530638 if (bss)
639 cfg80211_hold_bss(bss_from_pub(bss));
640 }
641
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200642 if (wdev->current_bss) {
643 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100644 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200645 wdev->current_bss = NULL;
646 }
647
Johannes Bergfffd0932009-07-08 14:22:54 +0200648 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300649 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200650 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200651 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200652 if (bss) {
653 cfg80211_unhold_bss(bss_from_pub(bss));
654 cfg80211_put_bss(wdev->wiphy, bss);
655 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300656 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200657 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200658 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200659
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530660 if (WARN_ON(!bss))
661 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200662
Johannes Bergfffd0932009-07-08 14:22:54 +0200663 wdev->current_bss = bss_from_pub(bss);
664
Johannes Bergfffd0932009-07-08 14:22:54 +0200665 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700666
Johannes Berg9caf0362012-11-29 01:25:20 +0100667 rcu_read_lock();
668 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
669 if (!country_ie) {
670 rcu_read_unlock();
671 return;
672 }
673
674 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
675 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700676
677 if (!country_ie)
678 return;
679
680 /*
681 * ieee80211_bss_get_ie() ensures we can access:
682 * - country_ie + 2, the start of the country ie data, and
683 * - and country_ie[1] which is the IE length
684 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700685 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
686 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100687 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688}
Johannes Bergf2129352009-07-01 21:26:56 +0200689
690void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
691 const u8 *req_ie, size_t req_ie_len,
692 const u8 *resp_ie, size_t resp_ie_len,
693 u16 status, gfp_t gfp)
694{
Johannes Berg667503d2009-07-07 03:56:11 +0200695 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800696 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +0200697 struct cfg80211_event *ev;
698 unsigned long flags;
699
700 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
701 if (!ev)
702 return;
703
704 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800705 if (bssid)
706 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700707 if (req_ie_len) {
708 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
709 ev->cr.req_ie_len = req_ie_len;
710 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
711 }
712 if (resp_ie_len) {
713 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
714 ev->cr.resp_ie_len = resp_ie_len;
715 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
716 }
Johannes Berg667503d2009-07-07 03:56:11 +0200717 ev->cr.status = status;
718
719 spin_lock_irqsave(&wdev->event_lock, flags);
720 list_add_tail(&ev->list, &wdev->event_list);
721 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100722 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200723}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200724EXPORT_SYMBOL(cfg80211_connect_result);
725
Ben Greear0e3a39b2013-06-19 14:06:27 -0700726/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300727void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530728 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200729 const u8 *req_ie, size_t req_ie_len,
730 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200731{
Johannes Berg3d23e342009-09-29 23:27:28 +0200732#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200733 union iwreq_data wrqu;
734#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200735 ASSERT_WDEV_LOCK(wdev);
736
Johannes Berg074ac8d2010-09-16 14:58:22 +0200737 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
738 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530739 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200740
Johannes Bergceca7b72013-05-16 00:55:45 +0200741 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530742 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100745 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200746 wdev->current_bss = NULL;
747
Johannes Berg19957bb2009-07-02 17:20:43 +0200748 cfg80211_hold_bss(bss_from_pub(bss));
749 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200750
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800751 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
752 wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200753 req_ie, req_ie_len, resp_ie, resp_ie_len,
754 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200755
Johannes Berg3d23e342009-09-29 23:27:28 +0200756#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200757 if (req_ie) {
758 memset(&wrqu, 0, sizeof(wrqu));
759 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800760 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200761 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762 }
763
764 if (resp_ie) {
765 memset(&wrqu, 0, sizeof(wrqu));
766 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200767 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
768 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200769 }
770
771 memset(&wrqu, 0, sizeof(wrqu));
772 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530773 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
774 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200775 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200776 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200777#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530778
779 return;
780out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100781 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200782}
Johannes Berg667503d2009-07-07 03:56:11 +0200783
Jouni Malinened9d0102011-05-16 19:40:15 +0300784void cfg80211_roamed(struct net_device *dev,
785 struct ieee80211_channel *channel,
786 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200787 const u8 *req_ie, size_t req_ie_len,
788 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
789{
790 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530791 struct cfg80211_bss *bss;
792
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530793 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200794 wdev->ssid_len,
795 IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530796 if (WARN_ON(!bss))
797 return;
798
799 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
800 resp_ie_len, gfp);
801}
802EXPORT_SYMBOL(cfg80211_roamed);
803
Ben Greear0e3a39b2013-06-19 14:06:27 -0700804/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530805void cfg80211_roamed_bss(struct net_device *dev,
806 struct cfg80211_bss *bss, const u8 *req_ie,
807 size_t req_ie_len, const u8 *resp_ie,
808 size_t resp_ie_len, gfp_t gfp)
809{
810 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800811 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +0200812 struct cfg80211_event *ev;
813 unsigned long flags;
814
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530815 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200816 return;
817
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530818 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
819 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100820 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530821 return;
822 }
823
Johannes Berg667503d2009-07-07 03:56:11 +0200824 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200825 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
826 ev->rm.req_ie_len = req_ie_len;
827 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
828 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
829 ev->rm.resp_ie_len = resp_ie_len;
830 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530831 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200832
833 spin_lock_irqsave(&wdev->event_lock, flags);
834 list_add_tail(&ev->list, &wdev->event_list);
835 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100836 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200837}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530838EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200839
Johannes Berg667503d2009-07-07 03:56:11 +0200840void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200841 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200842{
843 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800844 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200845 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200846#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200847 union iwreq_data wrqu;
848#endif
849
Johannes Berg667503d2009-07-07 03:56:11 +0200850 ASSERT_WDEV_LOCK(wdev);
851
Johannes Berg074ac8d2010-09-16 14:58:22 +0200852 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
853 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200854 return;
855
Samuel Ortizb23aa672009-07-01 21:26:54 +0200856 if (wdev->current_bss) {
857 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100858 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200859 }
860
861 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200862 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200863
Johannes Bergfffd0932009-07-08 14:22:54 +0200864 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
865
866 /*
867 * Delete all the keys ... pairwise keys can't really
868 * exist any more anyway, but default keys might.
869 */
870 if (rdev->ops->del_key)
871 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300872 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200873
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800874 rdev_set_qos_map(rdev, dev, NULL);
875
Johannes Berg3d23e342009-09-29 23:27:28 +0200876#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200877 memset(&wrqu, 0, sizeof(wrqu));
878 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
879 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800880 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200881#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500882
883 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200884}
885
886void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Bergc1e5f472014-05-19 17:53:16 +0200887 const u8 *ie, size_t ie_len, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200888{
Johannes Berg667503d2009-07-07 03:56:11 +0200889 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800890 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +0200891 struct cfg80211_event *ev;
892 unsigned long flags;
893
894 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
895 if (!ev)
896 return;
897
898 ev->type = EVENT_DISCONNECTED;
899 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
900 ev->dc.ie_len = ie_len;
901 memcpy((void *)ev->dc.ie, ie, ie_len);
902 ev->dc.reason = reason;
903
904 spin_lock_irqsave(&wdev->event_lock, flags);
905 list_add_tail(&ev->list, &wdev->event_list);
906 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100907 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200908}
909EXPORT_SYMBOL(cfg80211_disconnected);
910
Johannes Bergceca7b72013-05-16 00:55:45 +0200911/*
912 * API calls for nl80211/wext compatibility code
913 */
Johannes Berg83739b02013-05-15 17:44:01 +0200914int cfg80211_connect(struct cfg80211_registered_device *rdev,
915 struct net_device *dev,
916 struct cfg80211_connect_params *connect,
917 struct cfg80211_cached_keys *connkeys,
918 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200920 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200921 int err;
922
923 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200924
Johannes Bergfffd0932009-07-08 14:22:54 +0200925 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300926 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200927 wdev->connect_keys = NULL;
928 }
929
Ben Greear7e7c8922011-11-18 11:31:59 -0800930 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
931 rdev->wiphy.ht_capa_mod_mask);
932
Johannes Bergfffd0932009-07-08 14:22:54 +0200933 if (connkeys && connkeys->def >= 0) {
934 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200935 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200936
937 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200938 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200939 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200940 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
941 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200942 connect->key_idx = idx;
943 connect->key = connkeys->params[idx].key;
944 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200945
946 /*
947 * If ciphers are not set (e.g. when going through
948 * iwconfig), we have to set them appropriately here.
949 */
950 if (connect->crypto.cipher_group == 0)
951 connect->crypto.cipher_group = cipher;
952
953 if (connect->crypto.n_ciphers_pairwise == 0) {
954 connect->crypto.n_ciphers_pairwise = 1;
955 connect->crypto.ciphers_pairwise[0] = cipher;
956 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200957 }
958 }
959
Johannes Bergceca7b72013-05-16 00:55:45 +0200960 wdev->connect_keys = connkeys;
961 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
962 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200963
Johannes Bergceca7b72013-05-16 00:55:45 +0200964 if (!rdev->ops->connect)
965 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
966 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300967 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200968
Johannes Bergceca7b72013-05-16 00:55:45 +0200969 if (err) {
970 wdev->connect_keys = NULL;
971 wdev->ssid_len = 0;
972 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200974
975 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200976}
977
Johannes Berg83739b02013-05-15 17:44:01 +0200978int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
979 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980{
Johannes Berg6829c872009-07-02 09:13:27 +0200981 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +0200982 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200983
Johannes Berg667503d2009-07-07 03:56:11 +0200984 ASSERT_WDEV_LOCK(wdev);
985
Johannes Bergb47f6102014-09-10 13:39:54 +0300986 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200987 wdev->connect_keys = NULL;
988
Johannes Bergdee8a972013-08-13 09:23:57 +0200989 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +0200990 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +0200991 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +0200992 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +0200993 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300994 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200995
Johannes Bergceca7b72013-05-16 00:55:45 +0200996 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200997}