blob: c08a3b57dca1268b0f57d5c20cd1e47a16676043 [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 Berg46b9d182015-03-31 16:09:13 +020045 const u8 *ie;
Johannes Berg6829c872009-07-02 09:13:27 +020046 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 Berg667503dd2009-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) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020084 enum nl80211_band band = wdev->conn->params.channel->band;
Karl Beldan2a84ee82014-10-07 11:42:18 +020085 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;
Johannes Berg57fbcce2016-04-12 15:56:15 +020096 enum nl80211_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
Johannes Berg57fbcce2016-04-12 15:56:15 +0200100 for (band = 0; band < NUM_NL80211_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
Jouni Malinen818965d2016-02-26 22:12:47 +0200122 eth_broadcast_addr(request->bssid);
123
Johannes Bergfd014282012-06-18 19:17:03 +0200124 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200125 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700126 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200127
Johannes Berg79c97e92009-07-07 03:56:12 +0200128 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200129
Hila Gonene35e4d22012-06-27 17:19:42 +0300130 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200131 if (!err) {
132 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200133 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200134 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200135 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200137 kfree(request);
138 }
139 return err;
140}
141
142static int cfg80211_conn_do_work(struct wireless_dev *wdev)
143{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800144 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200145 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100146 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200147 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200148
Johannes Berg667503dd2009-07-07 03:56:11 +0200149 ASSERT_WDEV_LOCK(wdev);
150
Johannes Berg6829c872009-07-02 09:13:27 +0200151 if (!wdev->conn)
152 return 0;
153
Johannes Berg19957bb2009-07-02 17:20:43 +0200154 params = &wdev->conn->params;
155
Johannes Berg6829c872009-07-02 09:13:27 +0200156 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200157 case CFG80211_CONN_SCANNING:
158 /* didn't find it during scan ... */
159 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200160 case CFG80211_CONN_SCAN_AGAIN:
161 return cfg80211_conn_scan(wdev);
162 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200163 if (WARN_ON(!rdev->ops->auth))
164 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200165 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200166 return cfg80211_mlme_auth(rdev, wdev->netdev,
167 params->channel, params->auth_type,
168 params->bssid,
169 params->ssid, params->ssid_len,
170 NULL, 0,
171 params->key, params->key_len,
172 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200173 case CFG80211_CONN_AUTH_FAILED:
174 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200175 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200176 if (WARN_ON(!rdev->ops->assoc))
177 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200178 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200179 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100180 req.prev_bssid = wdev->conn->prev_bssid;
181 req.ie = params->ie;
182 req.ie_len = params->ie_len;
183 req.use_mfp = params->mfp != NL80211_MFP_NO;
184 req.crypto = params->crypto;
185 req.flags = params->flags;
186 req.ht_capa = params->ht_capa;
187 req.ht_capa_mask = params->ht_capa_mask;
188 req.vht_capa = params->vht_capa;
189 req.vht_capa_mask = params->vht_capa_mask;
190
Johannes Berg91bf9b22013-05-15 17:44:01 +0200191 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
192 params->bssid, params->ssid,
193 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200194 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200195 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
196 NULL, 0,
197 WLAN_REASON_DEAUTH_LEAVING,
198 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200199 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200200 case CFG80211_CONN_ASSOC_FAILED:
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING, false);
204 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200205 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200206 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
207 NULL, 0,
208 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200209 /* free directly, disconnected event already sent */
210 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200211 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200212 default:
213 return 0;
214 }
215}
216
217void cfg80211_conn_work(struct work_struct *work)
218{
Johannes Berg79c97e92009-07-07 03:56:12 +0200219 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200220 container_of(work, struct cfg80211_registered_device, conn_work);
221 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100222 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200223
224 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200225
Johannes Berg53873f12016-05-03 16:52:04 +0300226 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200227 if (!wdev->netdev)
228 continue;
229
Johannes Berg667503dd2009-07-07 03:56:11 +0200230 wdev_lock(wdev);
231 if (!netif_running(wdev->netdev)) {
232 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200235 if (!wdev->conn ||
236 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200237 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200238 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200239 }
Johannes Berg7400f422009-10-31 07:40:37 +0100240 if (wdev->conn->params.bssid) {
241 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
242 bssid = bssid_buf;
243 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200244 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200245 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900246 wdev->netdev, bssid,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300247 NULL, 0, NULL, 0, -1, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200248 }
Johannes Berg667503dd2009-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 Berg667503dd2009-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,
Lior David34d50512016-01-28 10:58:25 +0200267 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200268 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 Berg667503dd2009-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 Berg667503dd2009-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 Berg667503dd2009-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 Berg667503dd2009-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
Johannes Berg46b9d182015-03-31 16:09:13 +0200426static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
427 const u8 *ies, size_t ies_len,
428 const u8 **out_ies, size_t *out_ies_len)
429{
430 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
431 u8 *buf;
432 size_t offs;
433
434 if (!rdev->wiphy.extended_capabilities_len ||
435 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
436 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
437 if (!*out_ies)
438 return -ENOMEM;
439 *out_ies_len = ies_len;
440 return 0;
441 }
442
443 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
444 GFP_KERNEL);
445 if (!buf)
446 return -ENOMEM;
447
448 if (ies_len) {
449 static const u8 before_extcapa[] = {
450 /* not listing IEs expected to be created by driver */
451 WLAN_EID_RSN,
452 WLAN_EID_QOS_CAPA,
453 WLAN_EID_RRM_ENABLED_CAPABILITIES,
454 WLAN_EID_MOBILITY_DOMAIN,
455 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
456 WLAN_EID_BSS_COEX_2040,
457 };
458
459 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
460 ARRAY_SIZE(before_extcapa), 0);
461 memcpy(buf, ies, offs);
462 /* leave a whole for extended capabilities IE */
463 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
464 ies + offs, ies_len - offs);
465 } else {
466 offs = 0;
467 }
468
469 /* place extended capabilities IE (with only driver capabilities) */
470 buf[offs] = WLAN_EID_EXT_CAPABILITY;
471 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
472 memcpy(buf + offs + 2,
473 rdev->wiphy.extended_capabilities,
474 rdev->wiphy.extended_capabilities_len);
475
476 *out_ies = buf;
477 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
478
479 return 0;
480}
481
Johannes Bergceca7b72013-05-16 00:55:45 +0200482static int cfg80211_sme_connect(struct wireless_dev *wdev,
483 struct cfg80211_connect_params *connect,
484 const u8 *prev_bssid)
485{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800486 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200487 struct cfg80211_bss *bss;
488 int err;
489
490 if (!rdev->ops->auth || !rdev->ops->assoc)
491 return -EOPNOTSUPP;
492
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300493 if (wdev->current_bss) {
494 if (!prev_bssid)
495 return -EALREADY;
496 if (prev_bssid &&
497 !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
498 return -ENOTCONN;
499 cfg80211_unhold_bss(wdev->current_bss);
500 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
501 wdev->current_bss = NULL;
502
503 cfg80211_sme_free(wdev);
504 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200505
506 if (WARN_ON(wdev->conn))
507 return -EINPROGRESS;
508
509 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
510 if (!wdev->conn)
511 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200512
513 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200514 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200515 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200516 memcpy(&wdev->conn->params, connect, sizeof(*connect));
517 if (connect->bssid) {
518 wdev->conn->params.bssid = wdev->conn->bssid;
519 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
520 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200521
Johannes Berg46b9d182015-03-31 16:09:13 +0200522 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
523 &wdev->conn->ie,
524 &wdev->conn->params.ie_len)) {
525 kfree(wdev->conn);
526 wdev->conn = NULL;
527 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200528 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200529 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200530
531 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
532 wdev->conn->auto_auth = true;
533 /* start with open system ... should mostly work */
534 wdev->conn->params.auth_type =
535 NL80211_AUTHTYPE_OPEN_SYSTEM;
536 } else {
537 wdev->conn->auto_auth = false;
538 }
539
540 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800541 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200542
543 /* see if we have the bss already */
544 bss = cfg80211_get_conn_bss(wdev);
545
546 if (prev_bssid) {
547 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
548 wdev->conn->prev_bssid_valid = true;
549 }
550
551 /* we're good if we have a matching bss struct */
552 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200553 err = cfg80211_conn_do_work(wdev);
554 cfg80211_put_bss(wdev->wiphy, bss);
555 } else {
556 /* otherwise we'll need to scan for the AP first */
557 err = cfg80211_conn_scan(wdev);
558
559 /*
560 * If we can't scan right now, then we need to scan again
561 * after the current scan finished, since the parameters
562 * changed (unless we find a good AP anyway).
563 */
564 if (err == -EBUSY) {
565 err = 0;
566 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
567 }
568 }
569
570 if (err)
571 cfg80211_sme_free(wdev);
572
573 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200574}
575
Johannes Bergceca7b72013-05-16 00:55:45 +0200576static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900577{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800578 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200579 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900580
Johannes Bergceca7b72013-05-16 00:55:45 +0200581 if (!wdev->conn)
582 return 0;
583
584 if (!rdev->ops->deauth)
585 return -EOPNOTSUPP;
586
587 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
588 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
589 err = 0;
590 goto out;
591 }
592
593 /* wdev->conn->params.bssid must be set if > SCANNING */
594 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
595 wdev->conn->params.bssid,
596 NULL, 0, reason, false);
597 out:
598 cfg80211_sme_free(wdev);
599 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900600}
601
Johannes Bergceca7b72013-05-16 00:55:45 +0200602/*
603 * code shared for in-device and software SME
604 */
605
606static bool cfg80211_is_all_idle(void)
607{
608 struct cfg80211_registered_device *rdev;
609 struct wireless_dev *wdev;
610 bool is_all_idle = true;
611
612 /*
613 * All devices must be idle as otherwise if you are actively
614 * scanning some new beacon hints could be learned and would
615 * count as new regulatory hints.
616 */
617 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300618 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200619 wdev_lock(wdev);
620 if (wdev->conn || wdev->current_bss)
621 is_all_idle = false;
622 wdev_unlock(wdev);
623 }
624 }
625
626 return is_all_idle;
627}
628
629static void disconnect_work(struct work_struct *work)
630{
631 rtnl_lock();
632 if (cfg80211_is_all_idle())
633 regulatory_hint_disconnect();
634 rtnl_unlock();
635}
636
637static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
638
639
640/*
641 * API calls for drivers implementing connect/disconnect and
642 * SME event handling
643 */
644
Ben Greear6f390902013-06-19 14:06:25 -0700645/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200646void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
647 const u8 *req_ie, size_t req_ie_len,
648 const u8 *resp_ie, size_t resp_ie_len,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300649 int status, bool wextev,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200650 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200651{
652 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100653 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200654#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655 union iwreq_data wrqu;
656#endif
657
Johannes Berg667503dd2009-07-07 03:56:11 +0200658 ASSERT_WDEV_LOCK(wdev);
659
Johannes Berg074ac8d2010-09-16 14:58:22 +0200660 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700661 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
662 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200663 return;
Ben Greear6f390902013-06-19 14:06:25 -0700664 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200665
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800666 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200667 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200668 resp_ie, resp_ie_len,
669 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200670
Johannes Berg3d23e342009-09-29 23:27:28 +0200671#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200672 if (wextev) {
673 if (req_ie && status == WLAN_STATUS_SUCCESS) {
674 memset(&wrqu, 0, sizeof(wrqu));
675 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800676 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200677 }
678
679 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
680 memset(&wrqu, 0, sizeof(wrqu));
681 wrqu.data.length = resp_ie_len;
682 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
683 }
684
685 memset(&wrqu, 0, sizeof(wrqu));
686 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200687 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200688 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200689 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
690 wdev->wext.prev_bssid_valid = true;
691 }
Johannes Berge45cd822009-07-02 09:58:04 +0200692 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
693 }
694#endif
695
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530696 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800697 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530698 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
699 wdev->ssid, wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200700 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200701 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530702 if (bss)
703 cfg80211_hold_bss(bss_from_pub(bss));
704 }
705
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200706 if (wdev->current_bss) {
707 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100708 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200709 wdev->current_bss = NULL;
710 }
711
Johannes Bergfffd0932009-07-08 14:22:54 +0200712 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300713 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200714 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200715 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200716 if (bss) {
717 cfg80211_unhold_bss(bss_from_pub(bss));
718 cfg80211_put_bss(wdev->wiphy, bss);
719 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300720 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200722 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200723
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530724 if (WARN_ON(!bss))
725 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200726
Johannes Bergfffd0932009-07-08 14:22:54 +0200727 wdev->current_bss = bss_from_pub(bss);
728
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700730
Johannes Berg9caf0362012-11-29 01:25:20 +0100731 rcu_read_lock();
732 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
733 if (!country_ie) {
734 rcu_read_unlock();
735 return;
736 }
737
738 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
739 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700740
741 if (!country_ie)
742 return;
743
744 /*
745 * ieee80211_bss_get_ie() ensures we can access:
746 * - country_ie + 2, the start of the country ie data, and
747 * - and country_ie[1] which is the IE length
748 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700749 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
750 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100751 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200752}
Johannes Bergf2129352009-07-01 21:26:56 +0200753
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530754/* Consumes bss object one way or another */
755void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
756 struct cfg80211_bss *bss, const u8 *req_ie,
757 size_t req_ie_len, const u8 *resp_ie,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300758 size_t resp_ie_len, int status, gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200759{
Johannes Berg667503dd2009-07-07 03:56:11 +0200760 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800761 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200762 struct cfg80211_event *ev;
763 unsigned long flags;
764
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530765 if (bss) {
766 /* Make sure the bss entry provided by the driver is valid. */
767 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
768
769 if (WARN_ON(list_empty(&ibss->list))) {
770 cfg80211_put_bss(wdev->wiphy, bss);
771 return;
772 }
773 }
774
Johannes Berg667503dd2009-07-07 03:56:11 +0200775 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530776 if (!ev) {
777 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg667503dd2009-07-07 03:56:11 +0200778 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530779 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200780
781 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800782 if (bssid)
783 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700784 if (req_ie_len) {
785 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
786 ev->cr.req_ie_len = req_ie_len;
787 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
788 }
789 if (resp_ie_len) {
790 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
791 ev->cr.resp_ie_len = resp_ie_len;
792 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
793 }
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530794 if (bss)
795 cfg80211_hold_bss(bss_from_pub(bss));
796 ev->cr.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200797 ev->cr.status = status;
798
799 spin_lock_irqsave(&wdev->event_lock, flags);
800 list_add_tail(&ev->list, &wdev->event_list);
801 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100802 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200803}
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530804EXPORT_SYMBOL(cfg80211_connect_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200805
Ben Greear0e3a39b2013-06-19 14:06:27 -0700806/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300807void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530808 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200809 const u8 *req_ie, size_t req_ie_len,
810 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200811{
Johannes Berg3d23e342009-09-29 23:27:28 +0200812#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200813 union iwreq_data wrqu;
814#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200815 ASSERT_WDEV_LOCK(wdev);
816
Johannes Berg074ac8d2010-09-16 14:58:22 +0200817 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
818 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530819 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200820
Johannes Bergceca7b72013-05-16 00:55:45 +0200821 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530822 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200823
Samuel Ortizb23aa672009-07-01 21:26:54 +0200824 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100825 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200826 wdev->current_bss = NULL;
827
Johannes Berg19957bb2009-07-02 17:20:43 +0200828 cfg80211_hold_bss(bss_from_pub(bss));
829 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200830
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800831 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
832 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200833 req_ie, req_ie_len, resp_ie, resp_ie_len,
834 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200835
Johannes Berg3d23e342009-09-29 23:27:28 +0200836#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200837 if (req_ie) {
838 memset(&wrqu, 0, sizeof(wrqu));
839 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800840 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200841 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200842 }
843
844 if (resp_ie) {
845 memset(&wrqu, 0, sizeof(wrqu));
846 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200847 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
848 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200849 }
850
851 memset(&wrqu, 0, sizeof(wrqu));
852 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530853 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
854 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200855 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200856 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200857#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530858
859 return;
860out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100861 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200862}
Johannes Berg667503dd2009-07-07 03:56:11 +0200863
Jouni Malinened9d0102011-05-16 19:40:15 +0300864void cfg80211_roamed(struct net_device *dev,
865 struct ieee80211_channel *channel,
866 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200867 const u8 *req_ie, size_t req_ie_len,
868 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
869{
870 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530871 struct cfg80211_bss *bss;
872
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530873 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200874 wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200875 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530876 if (WARN_ON(!bss))
877 return;
878
879 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
880 resp_ie_len, gfp);
881}
882EXPORT_SYMBOL(cfg80211_roamed);
883
Ben Greear0e3a39b2013-06-19 14:06:27 -0700884/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530885void cfg80211_roamed_bss(struct net_device *dev,
886 struct cfg80211_bss *bss, const u8 *req_ie,
887 size_t req_ie_len, const u8 *resp_ie,
888 size_t resp_ie_len, gfp_t gfp)
889{
890 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800891 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200892 struct cfg80211_event *ev;
893 unsigned long flags;
894
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530895 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200896 return;
897
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530898 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
899 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100900 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530901 return;
902 }
903
Johannes Berg667503dd2009-07-07 03:56:11 +0200904 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200905 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
906 ev->rm.req_ie_len = req_ie_len;
907 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
908 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
909 ev->rm.resp_ie_len = resp_ie_len;
910 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530911 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200912
913 spin_lock_irqsave(&wdev->event_lock, flags);
914 list_add_tail(&ev->list, &wdev->event_list);
915 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100916 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200917}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530918EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919
Johannes Berg667503dd2009-07-07 03:56:11 +0200920void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200921 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200922{
923 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800924 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200925 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200926#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200927 union iwreq_data wrqu;
928#endif
929
Johannes Berg667503dd2009-07-07 03:56:11 +0200930 ASSERT_WDEV_LOCK(wdev);
931
Johannes Berg074ac8d2010-09-16 14:58:22 +0200932 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
933 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934 return;
935
Samuel Ortizb23aa672009-07-01 21:26:54 +0200936 if (wdev->current_bss) {
937 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100938 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200939 }
940
941 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200942 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200943
Johannes Bergfffd0932009-07-08 14:22:54 +0200944 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
945
Arend van Sprielb8607152016-02-15 14:35:53 +0100946 /* stop critical protocol if supported */
947 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
948 rdev->crit_proto_nlportid = 0;
949 rdev_crit_proto_stop(rdev, wdev);
950 }
951
Johannes Bergfffd0932009-07-08 14:22:54 +0200952 /*
953 * Delete all the keys ... pairwise keys can't really
954 * exist any more anyway, but default keys might.
955 */
956 if (rdev->ops->del_key)
957 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300958 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200959
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800960 rdev_set_qos_map(rdev, dev, NULL);
961
Johannes Berg3d23e342009-09-29 23:27:28 +0200962#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200963 memset(&wrqu, 0, sizeof(wrqu));
964 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
965 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800966 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200967#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500968
969 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200970}
971
972void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +0200973 const u8 *ie, size_t ie_len,
974 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200975{
Johannes Berg667503dd2009-07-07 03:56:11 +0200976 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800977 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200978 struct cfg80211_event *ev;
979 unsigned long flags;
980
981 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
982 if (!ev)
983 return;
984
985 ev->type = EVENT_DISCONNECTED;
986 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
987 ev->dc.ie_len = ie_len;
988 memcpy((void *)ev->dc.ie, ie, ie_len);
989 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +0200990 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +0200991
992 spin_lock_irqsave(&wdev->event_lock, flags);
993 list_add_tail(&ev->list, &wdev->event_list);
994 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100995 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200996}
997EXPORT_SYMBOL(cfg80211_disconnected);
998
Johannes Bergceca7b72013-05-16 00:55:45 +0200999/*
1000 * API calls for nl80211/wext compatibility code
1001 */
Johannes Berg83739b02013-05-15 17:44:01 +02001002int cfg80211_connect(struct cfg80211_registered_device *rdev,
1003 struct net_device *dev,
1004 struct cfg80211_connect_params *connect,
1005 struct cfg80211_cached_keys *connkeys,
1006 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001007{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001008 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +02001009 int err;
1010
1011 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001012
Johannes Bergfffd0932009-07-08 14:22:54 +02001013 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +03001014 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001015 wdev->connect_keys = NULL;
1016 }
1017
Ben Greear7e7c8922011-11-18 11:31:59 -08001018 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1019 rdev->wiphy.ht_capa_mod_mask);
1020
Johannes Bergfffd0932009-07-08 14:22:54 +02001021 if (connkeys && connkeys->def >= 0) {
1022 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001023 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001024
1025 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001026 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001027 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001028 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1029 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001030 connect->key_idx = idx;
1031 connect->key = connkeys->params[idx].key;
1032 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001033
1034 /*
1035 * If ciphers are not set (e.g. when going through
1036 * iwconfig), we have to set them appropriately here.
1037 */
1038 if (connect->crypto.cipher_group == 0)
1039 connect->crypto.cipher_group = cipher;
1040
1041 if (connect->crypto.n_ciphers_pairwise == 0) {
1042 connect->crypto.n_ciphers_pairwise = 1;
1043 connect->crypto.ciphers_pairwise[0] = cipher;
1044 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001045 }
Johannes Bergf1c1f172016-09-13 17:08:23 +02001046 } else {
1047 if (WARN_ON(connkeys))
1048 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001049 }
1050
Johannes Bergceca7b72013-05-16 00:55:45 +02001051 wdev->connect_keys = connkeys;
1052 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1053 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001054
Lior David34d50512016-01-28 10:58:25 +02001055 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1056 IEEE80211_BSS_TYPE_ESS;
1057
Johannes Bergceca7b72013-05-16 00:55:45 +02001058 if (!rdev->ops->connect)
1059 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1060 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001061 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001062
Johannes Bergceca7b72013-05-16 00:55:45 +02001063 if (err) {
1064 wdev->connect_keys = NULL;
1065 wdev->ssid_len = 0;
1066 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001067 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001068
1069 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001070}
1071
Johannes Berg83739b02013-05-15 17:44:01 +02001072int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1073 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001074{
Johannes Berg6829c872009-07-02 09:13:27 +02001075 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001076 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001077
Johannes Berg667503dd2009-07-07 03:56:11 +02001078 ASSERT_WDEV_LOCK(wdev);
1079
Johannes Bergb47f6102014-09-10 13:39:54 +03001080 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001081 wdev->connect_keys = NULL;
1082
Johannes Bergdee8a972013-08-13 09:23:57 +02001083 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001084 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001085 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001086 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001087 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001088 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001089
Johannes Bergceca7b72013-05-16 00:55:45 +02001090 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001091}