blob: 5d1c8ec911cd6d4c78862a5f16275eaacefec337 [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.
Avraham Stern9e841a62017-04-26 10:58:49 +03008 * Copyright 2017 Intel Deutschland GmbH
Samuel Ortizb23aa672009-07-01 21:26:54 +02009 */
10
11#include <linux/etherdevice.h>
12#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020014#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020015#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020017#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020018#include <net/cfg80211.h>
19#include <net/rtnetlink.h>
20#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070021#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030022#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020023
Johannes Bergceca7b72013-05-16 00:55:45 +020024/*
25 * Software SME in cfg80211, using auth/assoc/deauth calls to the
26 * driver. This is is for implementing nl80211's connect/disconnect
27 * and wireless extensions (if configured.)
28 */
29
Johannes Berg6829c872009-07-02 09:13:27 +020030struct cfg80211_conn {
31 struct cfg80211_connect_params params;
32 /* these are sub-states of the _CONNECTING sme_state */
33 enum {
Johannes Berg6829c872009-07-02 09:13:27 +020034 CFG80211_CONN_SCANNING,
35 CFG80211_CONN_SCAN_AGAIN,
36 CFG80211_CONN_AUTHENTICATE_NEXT,
37 CFG80211_CONN_AUTHENTICATING,
Purushottam Kushwahadf935062017-01-13 01:12:21 +020038 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
Johannes Berg6829c872009-07-02 09:13:27 +020039 CFG80211_CONN_ASSOCIATE_NEXT,
40 CFG80211_CONN_ASSOCIATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020041 CFG80211_CONN_ASSOC_FAILED,
Purushottam Kushwahadf935062017-01-13 01:12:21 +020042 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
Johannes Bergceca7b72013-05-16 00:55:45 +020043 CFG80211_CONN_DEAUTH,
Johannes Berg1976c762016-12-08 17:22:09 +010044 CFG80211_CONN_ABANDON,
Johannes Bergceca7b72013-05-16 00:55:45 +020045 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020046 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020047 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg46b9d182015-03-31 16:09:13 +020048 const u8 *ie;
Johannes Berg6829c872009-07-02 09:13:27 +020049 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020050 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020051};
52
Sachin Ahuja4216dfb2014-04-28 15:26:53 +053053static bool cfg80211_is_all_countryie_ignore(void)
54{
55 struct cfg80211_registered_device *rdev;
56 struct wireless_dev *wdev;
57 bool is_all_countryie_ignore = true;
58
59 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
60 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
61 wdev_lock(wdev);
62 if (!(wdev->wiphy->regulatory_flags &
63 REGULATORY_COUNTRY_IE_IGNORE)) {
64 is_all_countryie_ignore = false;
65 wdev_unlock(wdev);
66 goto out;
67 }
68 wdev_unlock(wdev);
69 }
70 }
71
72out:
73 return is_all_countryie_ignore;
74}
75
Johannes Bergceca7b72013-05-16 00:55:45 +020076static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050077{
Johannes Bergceca7b72013-05-16 00:55:45 +020078 if (!wdev->conn)
79 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050080
Johannes Bergceca7b72013-05-16 00:55:45 +020081 kfree(wdev->conn->ie);
82 kfree(wdev->conn);
83 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050084}
85
Johannes Berg6829c872009-07-02 09:13:27 +020086static int cfg80211_conn_scan(struct wireless_dev *wdev)
87{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080088 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020089 struct cfg80211_scan_request *request;
90 int n_channels, err;
91
92 ASSERT_RTNL();
Johannes Berg667503dd2009-07-07 03:56:11 +020093 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020094
Johannes Bergf9d15d12014-01-22 11:14:19 +020095 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020096 return -EBUSY;
97
Ilan Peerbdfbec22014-01-09 11:37:23 +020098 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020099 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +0200100 else
101 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200102
Johannes Berg6829c872009-07-02 09:13:27 +0200103 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
104 sizeof(request->channels[0]) * n_channels,
105 GFP_KERNEL);
106 if (!request)
107 return -ENOMEM;
108
Karl Beldan2a84ee82014-10-07 11:42:18 +0200109 if (wdev->conn->params.channel) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200110 enum nl80211_band band = wdev->conn->params.channel->band;
Karl Beldan2a84ee82014-10-07 11:42:18 +0200111 struct ieee80211_supported_band *sband =
112 wdev->wiphy->bands[band];
113
114 if (!sband) {
115 kfree(request);
116 return -EINVAL;
117 }
Johannes Berg6829c872009-07-02 09:13:27 +0200118 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +0200119 request->rates[band] = (1 << sband->n_bitrates) - 1;
120 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200121 int i = 0, j;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200122 enum nl80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530123 struct ieee80211_supported_band *bands;
124 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200125
Johannes Berg57fbcce2016-04-12 15:56:15 +0200126 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530127 bands = wdev->wiphy->bands[band];
128 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200129 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530130 for (j = 0; j < bands->n_channels; j++) {
131 channel = &bands->channels[j];
132 if (channel->flags & IEEE80211_CHAN_DISABLED)
133 continue;
134 request->channels[i++] = channel;
135 }
136 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200137 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530138 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200139 }
140 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200141 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200142 request->n_ssids = 1;
143
144 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
145 wdev->conn->params.ssid_len);
146 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
147
Jouni Malinen818965d2016-02-26 22:12:47 +0200148 eth_broadcast_addr(request->bssid);
149
Johannes Bergfd014282012-06-18 19:17:03 +0200150 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200151 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700152 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200153
Johannes Berg79c97e92009-07-07 03:56:12 +0200154 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200155
Hila Gonene35e4d22012-06-27 17:19:42 +0300156 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200157 if (!err) {
158 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200159 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200160 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200161 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200162 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200163 kfree(request);
164 }
165 return err;
166}
167
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200168static int cfg80211_conn_do_work(struct wireless_dev *wdev,
169 enum nl80211_timeout_reason *treason)
Johannes Berg6829c872009-07-02 09:13:27 +0200170{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800171 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200172 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100173 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200174 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200175
Johannes Berg667503dd2009-07-07 03:56:11 +0200176 ASSERT_WDEV_LOCK(wdev);
177
Johannes Berg6829c872009-07-02 09:13:27 +0200178 if (!wdev->conn)
179 return 0;
180
Johannes Berg19957bb2009-07-02 17:20:43 +0200181 params = &wdev->conn->params;
182
Johannes Berg6829c872009-07-02 09:13:27 +0200183 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200184 case CFG80211_CONN_SCANNING:
185 /* didn't find it during scan ... */
186 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200187 case CFG80211_CONN_SCAN_AGAIN:
188 return cfg80211_conn_scan(wdev);
189 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200190 if (WARN_ON(!rdev->ops->auth))
191 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200192 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200193 return cfg80211_mlme_auth(rdev, wdev->netdev,
194 params->channel, params->auth_type,
195 params->bssid,
196 params->ssid, params->ssid_len,
197 NULL, 0,
198 params->key, params->key_len,
199 params->key_idx, NULL, 0);
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200200 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
201 *treason = NL80211_TIMEOUT_AUTH;
Johannes Berg923a0e72013-06-28 11:38:54 +0200202 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200203 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200204 if (WARN_ON(!rdev->ops->assoc))
205 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200206 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200207 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100208 req.prev_bssid = wdev->conn->prev_bssid;
209 req.ie = params->ie;
210 req.ie_len = params->ie_len;
211 req.use_mfp = params->mfp != NL80211_MFP_NO;
212 req.crypto = params->crypto;
213 req.flags = params->flags;
214 req.ht_capa = params->ht_capa;
215 req.ht_capa_mask = params->ht_capa_mask;
216 req.vht_capa = params->vht_capa;
217 req.vht_capa_mask = params->vht_capa_mask;
218
Johannes Berg91bf9b22013-05-15 17:44:01 +0200219 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
220 params->bssid, params->ssid,
221 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200222 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200223 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
224 NULL, 0,
225 WLAN_REASON_DEAUTH_LEAVING,
226 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200227 return err;
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200228 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
229 *treason = NL80211_TIMEOUT_ASSOC;
230 /* fall through */
Johannes Berg923a0e72013-06-28 11:38:54 +0200231 case CFG80211_CONN_ASSOC_FAILED:
232 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
233 NULL, 0,
234 WLAN_REASON_DEAUTH_LEAVING, false);
235 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200236 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200237 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
238 NULL, 0,
239 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg1976c762016-12-08 17:22:09 +0100240 /* fall through */
241 case CFG80211_CONN_ABANDON:
Johannes Berg923a0e72013-06-28 11:38:54 +0200242 /* free directly, disconnected event already sent */
243 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200244 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200245 default:
246 return 0;
247 }
248}
249
250void cfg80211_conn_work(struct work_struct *work)
251{
Johannes Berg79c97e92009-07-07 03:56:12 +0200252 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200253 container_of(work, struct cfg80211_registered_device, conn_work);
254 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100255 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200256 enum nl80211_timeout_reason treason;
Johannes Berg6829c872009-07-02 09:13:27 +0200257
258 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200259
Johannes Berg53873f12016-05-03 16:52:04 +0300260 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200261 if (!wdev->netdev)
262 continue;
263
Johannes Berg667503dd2009-07-07 03:56:11 +0200264 wdev_lock(wdev);
265 if (!netif_running(wdev->netdev)) {
266 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200267 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200268 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200269 if (!wdev->conn ||
270 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200271 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200272 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200273 }
Johannes Berg7400f422009-10-31 07:40:37 +0100274 if (wdev->conn->params.bssid) {
275 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
276 bssid = bssid_buf;
277 }
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200278 treason = NL80211_TIMEOUT_UNSPECIFIED;
279 if (cfg80211_conn_do_work(wdev, &treason)) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300280 struct cfg80211_connect_resp_params cr;
281
282 memset(&cr, 0, sizeof(cr));
283 cr.status = -1;
284 cr.bssid = bssid;
285 cr.timeout_reason = treason;
286 __cfg80211_connect_result(wdev->netdev, &cr, false);
Johannes Bergceca7b72013-05-16 00:55:45 +0200287 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200288 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200289 }
290
Johannes Berg6829c872009-07-02 09:13:27 +0200291 rtnl_unlock();
292}
293
Ben Greear0e3a39b2013-06-19 14:06:27 -0700294/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700295static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200296{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800297 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200298 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200299
Johannes Berg667503dd2009-07-07 03:56:11 +0200300 ASSERT_WDEV_LOCK(wdev);
301
Jouni Malinened9d0102011-05-16 19:40:15 +0300302 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
303 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200304 wdev->conn->params.ssid,
305 wdev->conn->params.ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200306 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200307 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200308 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700309 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200310
311 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
312 wdev->conn->params.bssid = wdev->conn->bssid;
313 wdev->conn->params.channel = bss->channel;
314 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200315 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200316
Johannes Bergbbac31f2009-09-16 09:04:26 -0700317 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200318}
319
Johannes Berg667503dd2009-07-07 03:56:11 +0200320static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200321{
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800323 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700324 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200325
Johannes Berg667503dd2009-07-07 03:56:11 +0200326 ASSERT_WDEV_LOCK(wdev);
327
Zhu Yid4b1a682009-07-16 17:34:14 +0800328 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200329 return;
330
331 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
332 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
333 return;
334
Johannes Bergbbac31f2009-09-16 09:04:26 -0700335 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200336 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100337 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200338 else
339 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200340}
341
Johannes Berg667503dd2009-07-07 03:56:11 +0200342void cfg80211_sme_scan_done(struct net_device *dev)
343{
344 struct wireless_dev *wdev = dev->ieee80211_ptr;
345
346 wdev_lock(wdev);
347 __cfg80211_sme_scan_done(dev);
348 wdev_unlock(wdev);
349}
350
Johannes Bergceca7b72013-05-16 00:55:45 +0200351void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200352{
Johannes Berg6829c872009-07-02 09:13:27 +0200353 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800354 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200355 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
356 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
357
Johannes Berg667503dd2009-07-07 03:56:11 +0200358 ASSERT_WDEV_LOCK(wdev);
359
Johannes Bergceca7b72013-05-16 00:55:45 +0200360 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200361 return;
362
363 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
364 wdev->conn->auto_auth &&
365 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
366 /* select automatically between only open, shared, leap */
367 switch (wdev->conn->params.auth_type) {
368 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200369 if (wdev->connect_keys)
370 wdev->conn->params.auth_type =
371 NL80211_AUTHTYPE_SHARED_KEY;
372 else
373 wdev->conn->params.auth_type =
374 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200375 break;
376 case NL80211_AUTHTYPE_SHARED_KEY:
377 wdev->conn->params.auth_type =
378 NL80211_AUTHTYPE_NETWORK_EAP;
379 break;
380 default:
381 /* huh? */
382 wdev->conn->params.auth_type =
383 NL80211_AUTHTYPE_OPEN_SYSTEM;
384 break;
385 }
386 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
387 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200388 } else if (status_code != WLAN_STATUS_SUCCESS) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300389 struct cfg80211_connect_resp_params cr;
390
391 memset(&cr, 0, sizeof(cr));
392 cr.status = status_code;
393 cr.bssid = mgmt->bssid;
394 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
395 __cfg80211_connect_result(wdev->netdev, &cr, false);
Johannes Bergceca7b72013-05-16 00:55:45 +0200396 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200397 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
398 schedule_work(&rdev->conn_work);
399 }
400}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200401
Johannes Bergceca7b72013-05-16 00:55:45 +0200402bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200403{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800404 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200405
Johannes Bergceca7b72013-05-16 00:55:45 +0200406 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200407 return false;
408
Johannes Bergceca7b72013-05-16 00:55:45 +0200409 if (status == WLAN_STATUS_SUCCESS) {
410 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200411 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200412 }
413
414 if (wdev->conn->prev_bssid_valid) {
415 /*
416 * Some stupid APs don't accept reassoc, so we
417 * need to fall back to trying regular assoc;
418 * return true so no event is sent to userspace.
419 */
420 wdev->conn->prev_bssid_valid = false;
421 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
422 schedule_work(&rdev->conn_work);
423 return true;
424 }
425
Johannes Berg923a0e72013-06-28 11:38:54 +0200426 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200427 schedule_work(&rdev->conn_work);
428 return false;
429}
430
431void cfg80211_sme_deauth(struct wireless_dev *wdev)
432{
433 cfg80211_sme_free(wdev);
434}
435
436void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
437{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800438 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200439
440 if (!wdev->conn)
441 return;
442
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200443 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
Johannes Berg923a0e72013-06-28 11:38:54 +0200444 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200445}
446
447void cfg80211_sme_disassoc(struct wireless_dev *wdev)
448{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200450
451 if (!wdev->conn)
452 return;
453
454 wdev->conn->state = CFG80211_CONN_DEAUTH;
455 schedule_work(&rdev->conn_work);
456}
457
458void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
459{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800460 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200461
462 if (!wdev->conn)
463 return;
464
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200465 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
Johannes Berg923a0e72013-06-28 11:38:54 +0200466 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200467}
468
Johannes Berg1976c762016-12-08 17:22:09 +0100469void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
470{
471 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
472
473 if (!wdev->conn)
474 return;
475
476 wdev->conn->state = CFG80211_CONN_ABANDON;
477 schedule_work(&rdev->conn_work);
478}
479
Johannes Berg46b9d182015-03-31 16:09:13 +0200480static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
481 const u8 *ies, size_t ies_len,
482 const u8 **out_ies, size_t *out_ies_len)
483{
484 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
485 u8 *buf;
486 size_t offs;
487
488 if (!rdev->wiphy.extended_capabilities_len ||
489 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
490 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
491 if (!*out_ies)
492 return -ENOMEM;
493 *out_ies_len = ies_len;
494 return 0;
495 }
496
497 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
498 GFP_KERNEL);
499 if (!buf)
500 return -ENOMEM;
501
502 if (ies_len) {
503 static const u8 before_extcapa[] = {
504 /* not listing IEs expected to be created by driver */
505 WLAN_EID_RSN,
506 WLAN_EID_QOS_CAPA,
507 WLAN_EID_RRM_ENABLED_CAPABILITIES,
508 WLAN_EID_MOBILITY_DOMAIN,
509 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
510 WLAN_EID_BSS_COEX_2040,
511 };
512
513 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
514 ARRAY_SIZE(before_extcapa), 0);
515 memcpy(buf, ies, offs);
516 /* leave a whole for extended capabilities IE */
517 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
518 ies + offs, ies_len - offs);
519 } else {
520 offs = 0;
521 }
522
523 /* place extended capabilities IE (with only driver capabilities) */
524 buf[offs] = WLAN_EID_EXT_CAPABILITY;
525 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
526 memcpy(buf + offs + 2,
527 rdev->wiphy.extended_capabilities,
528 rdev->wiphy.extended_capabilities_len);
529
530 *out_ies = buf;
531 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
532
533 return 0;
534}
535
Johannes Bergceca7b72013-05-16 00:55:45 +0200536static int cfg80211_sme_connect(struct wireless_dev *wdev,
537 struct cfg80211_connect_params *connect,
538 const u8 *prev_bssid)
539{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800540 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200541 struct cfg80211_bss *bss;
542 int err;
543
544 if (!rdev->ops->auth || !rdev->ops->assoc)
545 return -EOPNOTSUPP;
546
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300547 if (wdev->current_bss) {
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300548 cfg80211_unhold_bss(wdev->current_bss);
549 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
550 wdev->current_bss = NULL;
551
552 cfg80211_sme_free(wdev);
553 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200554
555 if (WARN_ON(wdev->conn))
556 return -EINPROGRESS;
557
558 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
559 if (!wdev->conn)
560 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200561
562 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200563 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200564 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200565 memcpy(&wdev->conn->params, connect, sizeof(*connect));
566 if (connect->bssid) {
567 wdev->conn->params.bssid = wdev->conn->bssid;
568 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
569 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200570
Johannes Berg46b9d182015-03-31 16:09:13 +0200571 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
572 &wdev->conn->ie,
573 &wdev->conn->params.ie_len)) {
574 kfree(wdev->conn);
575 wdev->conn = NULL;
576 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200577 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200578 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200579
580 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
581 wdev->conn->auto_auth = true;
582 /* start with open system ... should mostly work */
583 wdev->conn->params.auth_type =
584 NL80211_AUTHTYPE_OPEN_SYSTEM;
585 } else {
586 wdev->conn->auto_auth = false;
587 }
588
589 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800590 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200591
592 /* see if we have the bss already */
593 bss = cfg80211_get_conn_bss(wdev);
594
595 if (prev_bssid) {
596 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
597 wdev->conn->prev_bssid_valid = true;
598 }
599
600 /* we're good if we have a matching bss struct */
601 if (bss) {
Purushottam Kushwahadf935062017-01-13 01:12:21 +0200602 enum nl80211_timeout_reason treason;
603
604 err = cfg80211_conn_do_work(wdev, &treason);
Johannes Bergceca7b72013-05-16 00:55:45 +0200605 cfg80211_put_bss(wdev->wiphy, bss);
606 } else {
607 /* otherwise we'll need to scan for the AP first */
608 err = cfg80211_conn_scan(wdev);
609
610 /*
611 * If we can't scan right now, then we need to scan again
612 * after the current scan finished, since the parameters
613 * changed (unless we find a good AP anyway).
614 */
615 if (err == -EBUSY) {
616 err = 0;
617 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
618 }
619 }
620
621 if (err)
622 cfg80211_sme_free(wdev);
623
624 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200625}
626
Johannes Bergceca7b72013-05-16 00:55:45 +0200627static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900628{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800629 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200630 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900631
Johannes Bergceca7b72013-05-16 00:55:45 +0200632 if (!wdev->conn)
633 return 0;
634
635 if (!rdev->ops->deauth)
636 return -EOPNOTSUPP;
637
638 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
639 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
640 err = 0;
641 goto out;
642 }
643
644 /* wdev->conn->params.bssid must be set if > SCANNING */
645 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
646 wdev->conn->params.bssid,
647 NULL, 0, reason, false);
648 out:
649 cfg80211_sme_free(wdev);
650 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900651}
652
Johannes Bergceca7b72013-05-16 00:55:45 +0200653/*
654 * code shared for in-device and software SME
655 */
656
657static bool cfg80211_is_all_idle(void)
658{
659 struct cfg80211_registered_device *rdev;
660 struct wireless_dev *wdev;
661 bool is_all_idle = true;
662
663 /*
664 * All devices must be idle as otherwise if you are actively
665 * scanning some new beacon hints could be learned and would
666 * count as new regulatory hints.
667 */
668 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300669 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200670 wdev_lock(wdev);
671 if (wdev->conn || wdev->current_bss)
672 is_all_idle = false;
673 wdev_unlock(wdev);
674 }
675 }
676
677 return is_all_idle;
678}
679
680static void disconnect_work(struct work_struct *work)
681{
682 rtnl_lock();
Sachin Ahuja4216dfb2014-04-28 15:26:53 +0530683 if (cfg80211_is_all_idle() &&
684 !cfg80211_is_all_countryie_ignore())
Johannes Bergceca7b72013-05-16 00:55:45 +0200685 regulatory_hint_disconnect();
686 rtnl_unlock();
687}
688
689static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
690
691
692/*
693 * API calls for drivers implementing connect/disconnect and
694 * SME event handling
695 */
696
Ben Greear6f390902013-06-19 14:06:25 -0700697/* This method must consume bss one way or another */
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300698void __cfg80211_connect_result(struct net_device *dev,
699 struct cfg80211_connect_resp_params *cr,
700 bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200701{
702 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100703 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200704#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200705 union iwreq_data wrqu;
706#endif
707
Johannes Berg667503dd2009-07-07 03:56:11 +0200708 ASSERT_WDEV_LOCK(wdev);
709
Johannes Berg074ac8d2010-09-16 14:58:22 +0200710 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700711 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300712 cfg80211_put_bss(wdev->wiphy, cr->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713 return;
Ben Greear6f390902013-06-19 14:06:25 -0700714 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200715
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300716 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
717 GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200718
Johannes Berg3d23e342009-09-29 23:27:28 +0200719#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200720 if (wextev) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300721 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200722 memset(&wrqu, 0, sizeof(wrqu));
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300723 wrqu.data.length = cr->req_ie_len;
724 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
725 cr->req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200726 }
727
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300728 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200729 memset(&wrqu, 0, sizeof(wrqu));
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300730 wrqu.data.length = cr->resp_ie_len;
731 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
732 cr->resp_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200733 }
734
735 memset(&wrqu, 0, sizeof(wrqu));
736 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300737 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
738 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
739 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200740 wdev->wext.prev_bssid_valid = true;
741 }
Johannes Berge45cd822009-07-02 09:58:04 +0200742 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
743 }
744#endif
745
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300746 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800747 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300748 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
749 wdev->ssid, wdev->ssid_len,
750 wdev->conn_bss_type,
751 IEEE80211_PRIVACY_ANY);
752 if (cr->bss)
753 cfg80211_hold_bss(bss_from_pub(cr->bss));
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530754 }
755
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200756 if (wdev->current_bss) {
757 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100758 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200759 wdev->current_bss = NULL;
760 }
761
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300762 if (cr->status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300763 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200764 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200765 wdev->ssid_len = 0;
Andrzej Zaborowski5f3f4d32018-02-04 21:57:28 +0530766 wdev->conn_owner_nlportid = 0;
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300767 if (cr->bss) {
768 cfg80211_unhold_bss(bss_from_pub(cr->bss));
769 cfg80211_put_bss(wdev->wiphy, cr->bss);
Johannes Bergf1940c52013-06-19 13:21:15 +0200770 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300771 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200772 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200774
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300775 if (WARN_ON(!cr->bss))
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530776 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200777
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300778 wdev->current_bss = bss_from_pub(cr->bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200779
David Spinadelb8676222016-09-22 23:16:50 +0300780 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
781 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700782
Johannes Berg9caf0362012-11-29 01:25:20 +0100783 rcu_read_lock();
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300784 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
Johannes Berg9caf0362012-11-29 01:25:20 +0100785 if (!country_ie) {
786 rcu_read_unlock();
787 return;
788 }
789
790 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
791 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700792
793 if (!country_ie)
794 return;
795
796 /*
797 * ieee80211_bss_get_ie() ensures we can access:
798 * - country_ie + 2, the start of the country ie data, and
799 * - and country_ie[1] which is the IE length
800 */
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300801 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700802 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100803 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200804}
Johannes Bergf2129352009-07-01 21:26:56 +0200805
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530806/* Consumes bss object one way or another */
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300807void cfg80211_connect_done(struct net_device *dev,
808 struct cfg80211_connect_resp_params *params,
809 gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200810{
Johannes Berg667503dd2009-07-07 03:56:11 +0200811 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800812 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200813 struct cfg80211_event *ev;
814 unsigned long flags;
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300815 u8 *next;
Johannes Berg667503dd2009-07-07 03:56:11 +0200816
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300817 if (params->bss) {
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530818 /* Make sure the bss entry provided by the driver is valid. */
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300819 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530820
821 if (WARN_ON(list_empty(&ibss->list))) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300822 cfg80211_put_bss(wdev->wiphy, params->bss);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530823 return;
824 }
825 }
826
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300827 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
Vidyullatha Kanchanapally36eabf62017-03-31 00:22:34 +0300828 params->req_ie_len + params->resp_ie_len +
829 params->fils_kek_len + params->pmk_len +
830 (params->pmkid ? WLAN_PMKID_LEN : 0), gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530831 if (!ev) {
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300832 cfg80211_put_bss(wdev->wiphy, params->bss);
Johannes Berg667503dd2009-07-07 03:56:11 +0200833 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530834 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200835
836 ev->type = EVENT_CONNECT_RESULT;
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300837 next = ((u8 *)ev) + sizeof(*ev);
838 if (params->bssid) {
839 ev->cr.bssid = next;
840 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
841 next += ETH_ALEN;
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700842 }
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300843 if (params->req_ie_len) {
844 ev->cr.req_ie = next;
845 ev->cr.req_ie_len = params->req_ie_len;
846 memcpy((void *)ev->cr.req_ie, params->req_ie,
847 params->req_ie_len);
848 next += params->req_ie_len;
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700849 }
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300850 if (params->resp_ie_len) {
851 ev->cr.resp_ie = next;
852 ev->cr.resp_ie_len = params->resp_ie_len;
853 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
854 params->resp_ie_len);
855 next += params->resp_ie_len;
856 }
Vidyullatha Kanchanapally36eabf62017-03-31 00:22:34 +0300857 if (params->fils_kek_len) {
858 ev->cr.fils_kek = next;
859 ev->cr.fils_kek_len = params->fils_kek_len;
860 memcpy((void *)ev->cr.fils_kek, params->fils_kek,
861 params->fils_kek_len);
862 next += params->fils_kek_len;
863 }
864 if (params->pmk_len) {
865 ev->cr.pmk = next;
866 ev->cr.pmk_len = params->pmk_len;
867 memcpy((void *)ev->cr.pmk, params->pmk, params->pmk_len);
868 next += params->pmk_len;
869 }
870 if (params->pmkid) {
871 ev->cr.pmkid = next;
872 memcpy((void *)ev->cr.pmkid, params->pmkid, WLAN_PMKID_LEN);
873 next += WLAN_PMKID_LEN;
874 }
875 ev->cr.update_erp_next_seq_num = params->update_erp_next_seq_num;
876 if (params->update_erp_next_seq_num)
877 ev->cr.fils_erp_next_seq_num = params->fils_erp_next_seq_num;
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300878 if (params->bss)
879 cfg80211_hold_bss(bss_from_pub(params->bss));
880 ev->cr.bss = params->bss;
881 ev->cr.status = params->status;
882 ev->cr.timeout_reason = params->timeout_reason;
Johannes Berg667503dd2009-07-07 03:56:11 +0200883
884 spin_lock_irqsave(&wdev->event_lock, flags);
885 list_add_tail(&ev->list, &wdev->event_list);
886 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100887 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200888}
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300889EXPORT_SYMBOL(cfg80211_connect_done);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200890
Ben Greear0e3a39b2013-06-19 14:06:27 -0700891/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300892void __cfg80211_roamed(struct wireless_dev *wdev,
Avraham Stern9e841a62017-04-26 10:58:49 +0300893 struct cfg80211_roam_info *info)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200894{
Johannes Berg3d23e342009-09-29 23:27:28 +0200895#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200896 union iwreq_data wrqu;
897#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200898 ASSERT_WDEV_LOCK(wdev);
899
Johannes Berg074ac8d2010-09-16 14:58:22 +0200900 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
901 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530902 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903
Johannes Bergceca7b72013-05-16 00:55:45 +0200904 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530905 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200906
Samuel Ortizb23aa672009-07-01 21:26:54 +0200907 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100908 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200909 wdev->current_bss = NULL;
910
Avraham Stern9e841a62017-04-26 10:58:49 +0300911 if (WARN_ON(!info->bss))
912 return;
913
914 cfg80211_hold_bss(bss_from_pub(info->bss));
915 wdev->current_bss = bss_from_pub(info->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800917 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
Avraham Stern9e841a62017-04-26 10:58:49 +0300918 wdev->netdev, info, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919
Johannes Berg3d23e342009-09-29 23:27:28 +0200920#ifdef CONFIG_CFG80211_WEXT
Avraham Stern9e841a62017-04-26 10:58:49 +0300921 if (info->req_ie) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200922 memset(&wrqu, 0, sizeof(wrqu));
Avraham Stern9e841a62017-04-26 10:58:49 +0300923 wrqu.data.length = info->req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800924 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Avraham Stern9e841a62017-04-26 10:58:49 +0300925 &wrqu, info->req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200926 }
927
Avraham Stern9e841a62017-04-26 10:58:49 +0300928 if (info->resp_ie) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200929 memset(&wrqu, 0, sizeof(wrqu));
Avraham Stern9e841a62017-04-26 10:58:49 +0300930 wrqu.data.length = info->resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200931 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
Avraham Stern9e841a62017-04-26 10:58:49 +0300932 &wrqu, info->resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200933 }
934
935 memset(&wrqu, 0, sizeof(wrqu));
936 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Avraham Stern9e841a62017-04-26 10:58:49 +0300937 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
938 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200939 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200940 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200941#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530942
943 return;
944out:
Avraham Stern9e841a62017-04-26 10:58:49 +0300945 cfg80211_put_bss(wdev->wiphy, info->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200946}
Johannes Berg667503dd2009-07-07 03:56:11 +0200947
Avraham Stern9e841a62017-04-26 10:58:49 +0300948/* Consumes info->bss object one way or another */
949void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
950 gfp_t gfp)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530951{
952 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800953 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200954 struct cfg80211_event *ev;
955 unsigned long flags;
956
Avraham Stern9e841a62017-04-26 10:58:49 +0300957 if (!info->bss) {
958 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
959 info->bssid, wdev->ssid,
960 wdev->ssid_len,
961 wdev->conn_bss_type,
962 IEEE80211_PRIVACY_ANY);
963 }
964
965 if (WARN_ON(!info->bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200966 return;
967
Avraham Stern9e841a62017-04-26 10:58:49 +0300968 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len, gfp);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530969 if (!ev) {
Avraham Stern9e841a62017-04-26 10:58:49 +0300970 cfg80211_put_bss(wdev->wiphy, info->bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530971 return;
972 }
973
Johannes Berg667503dd2009-07-07 03:56:11 +0200974 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200975 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
Avraham Stern9e841a62017-04-26 10:58:49 +0300976 ev->rm.req_ie_len = info->req_ie_len;
977 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
978 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + info->req_ie_len;
979 ev->rm.resp_ie_len = info->resp_ie_len;
980 memcpy((void *)ev->rm.resp_ie, info->resp_ie, info->resp_ie_len);
981 ev->rm.bss = info->bss;
Avraham Stern562be6e2017-06-09 13:08:45 +0100982 ev->rm.authorized = info->authorized;
Johannes Berg667503dd2009-07-07 03:56:11 +0200983
984 spin_lock_irqsave(&wdev->event_lock, flags);
985 list_add_tail(&ev->list, &wdev->event_list);
986 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100987 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200988}
Avraham Stern9e841a62017-04-26 10:58:49 +0300989EXPORT_SYMBOL(cfg80211_roamed);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200990
Johannes Berg667503dd2009-07-07 03:56:11 +0200991void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200992 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200993{
994 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800995 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200996 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200997#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200998 union iwreq_data wrqu;
999#endif
1000
Johannes Berg667503dd2009-07-07 03:56:11 +02001001 ASSERT_WDEV_LOCK(wdev);
1002
Johannes Berg074ac8d2010-09-16 14:58:22 +02001003 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1004 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02001005 return;
1006
Samuel Ortizb23aa672009-07-01 21:26:54 +02001007 if (wdev->current_bss) {
1008 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +01001009 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001010 }
1011
1012 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +02001013 wdev->ssid_len = 0;
Andrzej Zaborowski5f3f4d32018-02-04 21:57:28 +05301014 wdev->conn_owner_nlportid = 0;
Avraham Sternd016c072018-02-19 14:48:38 +02001015 kzfree(wdev->connect_keys);
1016 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001017
Johannes Bergfffd0932009-07-08 14:22:54 +02001018 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1019
Arend van Sprielb8607152016-02-15 14:35:53 +01001020 /* stop critical protocol if supported */
1021 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1022 rdev->crit_proto_nlportid = 0;
1023 rdev_crit_proto_stop(rdev, wdev);
1024 }
1025
Johannes Bergfffd0932009-07-08 14:22:54 +02001026 /*
1027 * Delete all the keys ... pairwise keys can't really
1028 * exist any more anyway, but default keys might.
1029 */
Jouni Malinena2be0702020-03-27 18:20:31 +05301030 if (rdev->ops->del_key) {
1031 int max_key_idx = 5;
1032
1033 if (wiphy_ext_feature_isset(
1034 wdev->wiphy,
1035 NL80211_EXT_FEATURE_BEACON_PROTECTION))
1036 max_key_idx = 7;
1037 for (i = 0; i <= max_key_idx; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +03001038 rdev_del_key(rdev, dev, i, false, NULL);
Jouni Malinena2be0702020-03-27 18:20:31 +05301039 }
Samuel Ortizb23aa672009-07-01 21:26:54 +02001040
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001041 rdev_set_qos_map(rdev, dev, NULL);
1042
Johannes Berg3d23e342009-09-29 23:27:28 +02001043#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +02001044 memset(&wrqu, 0, sizeof(wrqu));
1045 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1046 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -08001047 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001048#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001049
1050 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001051}
1052
1053void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +02001054 const u8 *ie, size_t ie_len,
1055 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001056{
Johannes Berg667503dd2009-07-07 03:56:11 +02001057 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001058 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +02001059 struct cfg80211_event *ev;
1060 unsigned long flags;
1061
1062 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1063 if (!ev)
1064 return;
1065
1066 ev->type = EVENT_DISCONNECTED;
1067 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1068 ev->dc.ie_len = ie_len;
1069 memcpy((void *)ev->dc.ie, ie, ie_len);
1070 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +02001071 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +02001072
1073 spin_lock_irqsave(&wdev->event_lock, flags);
1074 list_add_tail(&ev->list, &wdev->event_list);
1075 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001076 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001077}
1078EXPORT_SYMBOL(cfg80211_disconnected);
1079
Johannes Bergceca7b72013-05-16 00:55:45 +02001080/*
1081 * API calls for nl80211/wext compatibility code
1082 */
Johannes Berg83739b02013-05-15 17:44:01 +02001083int cfg80211_connect(struct cfg80211_registered_device *rdev,
1084 struct net_device *dev,
1085 struct cfg80211_connect_params *connect,
1086 struct cfg80211_cached_keys *connkeys,
1087 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001088{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001089 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +02001090 int err;
1091
1092 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001093
Johannes Bergbb46f792017-10-17 21:56:20 +02001094 /*
1095 * If we have an ssid_len, we're trying to connect or are
1096 * already connected, so reject a new SSID unless it's the
1097 * same (which is the case for re-association.)
1098 */
1099 if (wdev->ssid_len &&
1100 (wdev->ssid_len != connect->ssid_len ||
1101 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1102 return -EALREADY;
1103
1104 /*
1105 * If connected, reject (re-)association unless prev_bssid
1106 * matches the current BSSID.
1107 */
1108 if (wdev->current_bss) {
1109 if (!prev_bssid)
1110 return -EALREADY;
1111 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1112 return -ENOTCONN;
Johannes Bergfffd0932009-07-08 14:22:54 +02001113 }
1114
Johannes Bergbb46f792017-10-17 21:56:20 +02001115 /*
1116 * Reject if we're in the process of connecting with WEP,
1117 * this case isn't very interesting and trying to handle
1118 * it would make the code much more complex.
1119 */
1120 if (wdev->connect_keys)
1121 return -EINPROGRESS;
1122
Ben Greear7e7c8922011-11-18 11:31:59 -08001123 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1124 rdev->wiphy.ht_capa_mod_mask);
1125
Johannes Bergfffd0932009-07-08 14:22:54 +02001126 if (connkeys && connkeys->def >= 0) {
1127 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001128 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001129
1130 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001131 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001132 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001133 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1134 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001135 connect->key_idx = idx;
1136 connect->key = connkeys->params[idx].key;
1137 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001138
1139 /*
1140 * If ciphers are not set (e.g. when going through
1141 * iwconfig), we have to set them appropriately here.
1142 */
1143 if (connect->crypto.cipher_group == 0)
1144 connect->crypto.cipher_group = cipher;
1145
1146 if (connect->crypto.n_ciphers_pairwise == 0) {
1147 connect->crypto.n_ciphers_pairwise = 1;
1148 connect->crypto.ciphers_pairwise[0] = cipher;
1149 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001150 }
David Spinadelb8676222016-09-22 23:16:50 +03001151
1152 connect->crypto.wep_keys = connkeys->params;
1153 connect->crypto.wep_tx_key = connkeys->def;
Johannes Bergf1c1f172016-09-13 17:08:23 +02001154 } else {
1155 if (WARN_ON(connkeys))
1156 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001157 }
1158
Johannes Bergceca7b72013-05-16 00:55:45 +02001159 wdev->connect_keys = connkeys;
1160 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1161 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001162
Lior David34d50512016-01-28 10:58:25 +02001163 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1164 IEEE80211_BSS_TYPE_ESS;
1165
Johannes Bergceca7b72013-05-16 00:55:45 +02001166 if (!rdev->ops->connect)
1167 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1168 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001169 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001170
Johannes Bergceca7b72013-05-16 00:55:45 +02001171 if (err) {
1172 wdev->connect_keys = NULL;
Johannes Bergbb46f792017-10-17 21:56:20 +02001173 /*
1174 * This could be reassoc getting refused, don't clear
1175 * ssid_len in that case.
1176 */
1177 if (!wdev->current_bss)
1178 wdev->ssid_len = 0;
Johannes Bergceca7b72013-05-16 00:55:45 +02001179 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001180 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001181
1182 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001183}
1184
Johannes Berg83739b02013-05-15 17:44:01 +02001185int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1186 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001187{
Johannes Berg6829c872009-07-02 09:13:27 +02001188 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001189 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001190
Johannes Berg667503dd2009-07-07 03:56:11 +02001191 ASSERT_WDEV_LOCK(wdev);
1192
Johannes Bergb47f6102014-09-10 13:39:54 +03001193 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001194 wdev->connect_keys = NULL;
1195
Andrzej Zaborowski5f3f4d32018-02-04 21:57:28 +05301196 wdev->conn_owner_nlportid = 0;
1197
Johannes Bergdee8a972013-08-13 09:23:57 +02001198 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001199 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001200 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001201 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001202 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001203 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001204
Johannes Bergbb46f792017-10-17 21:56:20 +02001205 /*
1206 * Clear ssid_len unless we actually were fully connected,
1207 * in which case cfg80211_disconnected() will take care of
1208 * this later.
1209 */
1210 if (!wdev->current_bss)
1211 wdev->ssid_len = 0;
1212
Johannes Bergceca7b72013-05-16 00:55:45 +02001213 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001214}
Andrzej Zaborowski5f3f4d32018-02-04 21:57:28 +05301215
1216/*
1217 * Used to clean up after the connection / connection attempt owner socket
1218 * disconnects
1219 */
1220void cfg80211_autodisconnect_wk(struct work_struct *work)
1221{
1222 struct wireless_dev *wdev =
1223 container_of(work, struct wireless_dev, disconnect_wk);
1224 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1225
1226 wdev_lock(wdev);
1227
1228 if (wdev->conn_owner_nlportid) {
1229 /*
1230 * Use disconnect_bssid if still connecting and ops->disconnect
1231 * not implemented. Otherwise we can use cfg80211_disconnect.
1232 */
1233 if (rdev->ops->disconnect || wdev->current_bss)
1234 cfg80211_disconnect(rdev, wdev->netdev,
1235 WLAN_REASON_DEAUTH_LEAVING, true);
1236 else
1237 cfg80211_mlme_deauth(rdev, wdev->netdev,
1238 wdev->disconnect_bssid, NULL, 0,
1239 WLAN_REASON_DEAUTH_LEAVING, false);
1240 }
1241
1242 wdev_unlock(wdev);
1243}