blob: 07756ca5e3b5960198955db356741be90b9355c5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Samuel Ortizb23aa672009-07-01 21:26:54 +02002/*
Johannes Bergceca7b72013-05-16 00:55:45 +02003 * SME code for cfg80211
4 * both driver SME event handling and the SME implementation
5 * (for nl80211's connect() and wext)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006 *
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg0e479012020-05-28 21:34:24 +02008 * Copyright (C) 2009, 2020 Intel Corporation. All rights reserved.
Avraham Stern29ce6ec2017-04-26 10:58:49 +03009 * Copyright 2017 Intel Deutschland GmbH
Samuel Ortizb23aa672009-07-01 21:26:54 +020010 */
11
12#include <linux/etherdevice.h>
13#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020015#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020016#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040017#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020018#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020019#include <net/cfg80211.h>
20#include <net/rtnetlink.h>
21#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070022#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030023#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020024
Johannes Bergceca7b72013-05-16 00:55:45 +020025/*
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
Randy Dunlap54f65de2020-08-22 16:19:52 -070027 * driver. This is for implementing nl80211's connect/disconnect
Johannes Bergceca7b72013-05-16 00:55:45 +020028 * and wireless extensions (if configured.)
29 */
30
Johannes Berg6829c872009-07-02 09:13:27 +020031struct cfg80211_conn {
32 struct cfg80211_connect_params params;
33 /* these are sub-states of the _CONNECTING sme_state */
34 enum {
Johannes Berg6829c872009-07-02 09:13:27 +020035 CFG80211_CONN_SCANNING,
36 CFG80211_CONN_SCAN_AGAIN,
37 CFG80211_CONN_AUTHENTICATE_NEXT,
38 CFG80211_CONN_AUTHENTICATING,
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +020039 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
Johannes Berg6829c872009-07-02 09:13:27 +020040 CFG80211_CONN_ASSOCIATE_NEXT,
41 CFG80211_CONN_ASSOCIATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020042 CFG80211_CONN_ASSOC_FAILED,
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +020043 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
Johannes Bergceca7b72013-05-16 00:55:45 +020044 CFG80211_CONN_DEAUTH,
Johannes Berge6f462d2016-12-08 17:22:09 +010045 CFG80211_CONN_ABANDON,
Johannes Bergceca7b72013-05-16 00:55:45 +020046 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020047 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020048 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg46b9d182015-03-31 16:09:13 +020049 const u8 *ie;
Johannes Berg6829c872009-07-02 09:13:27 +020050 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020051 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020052};
53
Johannes Bergceca7b72013-05-16 00:55:45 +020054static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050055{
Johannes Bergceca7b72013-05-16 00:55:45 +020056 if (!wdev->conn)
57 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050058
Johannes Bergceca7b72013-05-16 00:55:45 +020059 kfree(wdev->conn->ie);
60 kfree(wdev->conn);
61 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050062}
63
Johannes Berg6829c872009-07-02 09:13:27 +020064static int cfg80211_conn_scan(struct wireless_dev *wdev)
65{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080066 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020067 struct cfg80211_scan_request *request;
68 int n_channels, err;
69
Johannes Berg667503d2009-07-07 03:56:11 +020070 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020071
Johannes Bergf9d15d12014-01-22 11:14:19 +020072 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020073 return -EBUSY;
74
Ilan Peerbdfbec22014-01-09 11:37:23 +020075 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020076 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020077 else
78 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020079
Johannes Berg6829c872009-07-02 09:13:27 +020080 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
81 sizeof(request->channels[0]) * n_channels,
82 GFP_KERNEL);
83 if (!request)
84 return -ENOMEM;
85
Karl Beldan2a84ee82014-10-07 11:42:18 +020086 if (wdev->conn->params.channel) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020087 enum nl80211_band band = wdev->conn->params.channel->band;
Karl Beldan2a84ee82014-10-07 11:42:18 +020088 struct ieee80211_supported_band *sband =
89 wdev->wiphy->bands[band];
90
91 if (!sband) {
92 kfree(request);
93 return -EINVAL;
94 }
Johannes Berg6829c872009-07-02 09:13:27 +020095 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +020096 request->rates[band] = (1 << sband->n_bitrates) - 1;
97 } else {
Johannes Berg6829c872009-07-02 09:13:27 +020098 int i = 0, j;
Johannes Berg57fbcce2016-04-12 15:56:15 +020099 enum nl80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530100 struct ieee80211_supported_band *bands;
101 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200102
Johannes Berg57fbcce2016-04-12 15:56:15 +0200103 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530104 bands = wdev->wiphy->bands[band];
105 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200106 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530107 for (j = 0; j < bands->n_channels; j++) {
108 channel = &bands->channels[j];
109 if (channel->flags & IEEE80211_CHAN_DISABLED)
110 continue;
111 request->channels[i++] = channel;
112 }
113 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200114 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530115 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200116 }
117 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200118 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200119 request->n_ssids = 1;
120
121 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
122 wdev->conn->params.ssid_len);
123 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
124
Jouni Malinen818965d2016-02-26 22:12:47 +0200125 eth_broadcast_addr(request->bssid);
126
Johannes Bergfd014282012-06-18 19:17:03 +0200127 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200128 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700129 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200130
Johannes Berg79c97e92009-07-07 03:56:12 +0200131 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200132
Hila Gonene35e4d22012-06-27 17:19:42 +0300133 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200134 if (!err) {
135 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200136 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200137 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200138 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200139 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200140 kfree(request);
141 }
142 return err;
143}
144
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200145static int cfg80211_conn_do_work(struct wireless_dev *wdev,
146 enum nl80211_timeout_reason *treason)
Johannes Berg6829c872009-07-02 09:13:27 +0200147{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200149 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100150 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200152
Johannes Berg667503d2009-07-07 03:56:11 +0200153 ASSERT_WDEV_LOCK(wdev);
154
Johannes Berg6829c872009-07-02 09:13:27 +0200155 if (!wdev->conn)
156 return 0;
157
Johannes Berg19957bb2009-07-02 17:20:43 +0200158 params = &wdev->conn->params;
159
Johannes Berg6829c872009-07-02 09:13:27 +0200160 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200161 case CFG80211_CONN_SCANNING:
162 /* didn't find it during scan ... */
163 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200164 case CFG80211_CONN_SCAN_AGAIN:
165 return cfg80211_conn_scan(wdev);
166 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200167 if (WARN_ON(!rdev->ops->auth))
168 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200169 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200170 return cfg80211_mlme_auth(rdev, wdev->netdev,
171 params->channel, params->auth_type,
172 params->bssid,
173 params->ssid, params->ssid_len,
174 NULL, 0,
175 params->key, params->key_len,
176 params->key_idx, NULL, 0);
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200177 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
178 *treason = NL80211_TIMEOUT_AUTH;
Johannes Berg923a0e72013-06-28 11:38:54 +0200179 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200180 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200181 if (WARN_ON(!rdev->ops->assoc))
182 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200183 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200184 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100185 req.prev_bssid = wdev->conn->prev_bssid;
186 req.ie = params->ie;
187 req.ie_len = params->ie_len;
188 req.use_mfp = params->mfp != NL80211_MFP_NO;
189 req.crypto = params->crypto;
190 req.flags = params->flags;
191 req.ht_capa = params->ht_capa;
192 req.ht_capa_mask = params->ht_capa_mask;
193 req.vht_capa = params->vht_capa;
194 req.vht_capa_mask = params->vht_capa_mask;
195
Johannes Berg91bf9b22013-05-15 17:44:01 +0200196 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
197 params->bssid, params->ssid,
198 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200199 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200200 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
201 NULL, 0,
202 WLAN_REASON_DEAUTH_LEAVING,
203 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200204 return err;
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200205 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
206 *treason = NL80211_TIMEOUT_ASSOC;
Miaohe Lin7b506ff2020-08-22 04:23:23 -0400207 fallthrough;
Johannes Berg923a0e72013-06-28 11:38:54 +0200208 case CFG80211_CONN_ASSOC_FAILED:
209 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
210 NULL, 0,
211 WLAN_REASON_DEAUTH_LEAVING, false);
212 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200213 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200214 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
215 NULL, 0,
216 WLAN_REASON_DEAUTH_LEAVING, false);
Miaohe Lin7b506ff2020-08-22 04:23:23 -0400217 fallthrough;
Johannes Berge6f462d2016-12-08 17:22:09 +0100218 case CFG80211_CONN_ABANDON:
Johannes Berg923a0e72013-06-28 11:38:54 +0200219 /* free directly, disconnected event already sent */
220 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200221 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200222 default:
223 return 0;
224 }
225}
226
227void cfg80211_conn_work(struct work_struct *work)
228{
Johannes Berg79c97e92009-07-07 03:56:12 +0200229 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200230 container_of(work, struct cfg80211_registered_device, conn_work);
231 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100232 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200233 enum nl80211_timeout_reason treason;
Johannes Berg6829c872009-07-02 09:13:27 +0200234
Johannes Berga05829a2021-01-22 16:19:43 +0100235 wiphy_lock(&rdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200236
Johannes Berg53873f12016-05-03 16:52:04 +0300237 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200238 if (!wdev->netdev)
239 continue;
240
Johannes Berg667503d2009-07-07 03:56:11 +0200241 wdev_lock(wdev);
242 if (!netif_running(wdev->netdev)) {
243 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200244 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200245 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200246 if (!wdev->conn ||
247 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503d2009-07-07 03:56:11 +0200248 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200249 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200250 }
Johannes Berg7400f422009-10-31 07:40:37 +0100251 if (wdev->conn->params.bssid) {
252 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
253 bssid = bssid_buf;
254 }
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200255 treason = NL80211_TIMEOUT_UNSPECIFIED;
256 if (cfg80211_conn_do_work(wdev, &treason)) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300257 struct cfg80211_connect_resp_params cr;
258
259 memset(&cr, 0, sizeof(cr));
260 cr.status = -1;
261 cr.bssid = bssid;
262 cr.timeout_reason = treason;
263 __cfg80211_connect_result(wdev->netdev, &cr, false);
Johannes Bergceca7b72013-05-16 00:55:45 +0200264 }
Johannes Berg667503d2009-07-07 03:56:11 +0200265 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200266 }
267
Johannes Berga05829a2021-01-22 16:19:43 +0100268 wiphy_unlock(&rdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200269}
270
Ben Greear0e3a39b2013-06-19 14:06:27 -0700271/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700272static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200273{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800274 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200275 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200276
Johannes Berg667503d2009-07-07 03:56:11 +0200277 ASSERT_WDEV_LOCK(wdev);
278
Jouni Malinened9d0102011-05-16 19:40:15 +0300279 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
280 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200281 wdev->conn->params.ssid,
282 wdev->conn->params.ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200283 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200284 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200285 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700286 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200287
288 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
289 wdev->conn->params.bssid = wdev->conn->bssid;
290 wdev->conn->params.channel = bss->channel;
291 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200292 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200293
Johannes Bergbbac31f2009-09-16 09:04:26 -0700294 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200295}
296
Johannes Berg667503d2009-07-07 03:56:11 +0200297static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200298{
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700301 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200302
Johannes Berg667503d2009-07-07 03:56:11 +0200303 ASSERT_WDEV_LOCK(wdev);
304
Zhu Yid4b1a682009-07-16 17:34:14 +0800305 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200306 return;
307
308 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
309 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
310 return;
311
Johannes Bergbbac31f2009-09-16 09:04:26 -0700312 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200313 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100314 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200315 else
316 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200317}
318
Johannes Berg667503d2009-07-07 03:56:11 +0200319void cfg80211_sme_scan_done(struct net_device *dev)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322
323 wdev_lock(wdev);
324 __cfg80211_sme_scan_done(dev);
325 wdev_unlock(wdev);
326}
327
Johannes Bergceca7b72013-05-16 00:55:45 +0200328void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200329{
Johannes Berg6829c872009-07-02 09:13:27 +0200330 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800331 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200332 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
333 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
334
Johannes Berg667503d2009-07-07 03:56:11 +0200335 ASSERT_WDEV_LOCK(wdev);
336
Johannes Bergceca7b72013-05-16 00:55:45 +0200337 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200338 return;
339
340 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
341 wdev->conn->auto_auth &&
342 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
343 /* select automatically between only open, shared, leap */
344 switch (wdev->conn->params.auth_type) {
345 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200346 if (wdev->connect_keys)
347 wdev->conn->params.auth_type =
348 NL80211_AUTHTYPE_SHARED_KEY;
349 else
350 wdev->conn->params.auth_type =
351 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200352 break;
353 case NL80211_AUTHTYPE_SHARED_KEY:
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_NETWORK_EAP;
356 break;
357 default:
358 /* huh? */
359 wdev->conn->params.auth_type =
360 NL80211_AUTHTYPE_OPEN_SYSTEM;
361 break;
362 }
363 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
364 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200365 } else if (status_code != WLAN_STATUS_SUCCESS) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300366 struct cfg80211_connect_resp_params cr;
367
368 memset(&cr, 0, sizeof(cr));
369 cr.status = status_code;
370 cr.bssid = mgmt->bssid;
371 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
372 __cfg80211_connect_result(wdev->netdev, &cr, false);
Johannes Bergceca7b72013-05-16 00:55:45 +0200373 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200374 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
375 schedule_work(&rdev->conn_work);
376 }
377}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200378
Johannes Bergceca7b72013-05-16 00:55:45 +0200379bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200380{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200382
Johannes Bergceca7b72013-05-16 00:55:45 +0200383 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200384 return false;
385
Johannes Bergceca7b72013-05-16 00:55:45 +0200386 if (status == WLAN_STATUS_SUCCESS) {
387 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200388 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200389 }
390
391 if (wdev->conn->prev_bssid_valid) {
392 /*
393 * Some stupid APs don't accept reassoc, so we
394 * need to fall back to trying regular assoc;
395 * return true so no event is sent to userspace.
396 */
397 wdev->conn->prev_bssid_valid = false;
398 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
399 schedule_work(&rdev->conn_work);
400 return true;
401 }
402
Johannes Berg923a0e72013-06-28 11:38:54 +0200403 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200404 schedule_work(&rdev->conn_work);
405 return false;
406}
407
408void cfg80211_sme_deauth(struct wireless_dev *wdev)
409{
410 cfg80211_sme_free(wdev);
411}
412
413void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
414{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800415 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200416
417 if (!wdev->conn)
418 return;
419
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200420 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
Johannes Berg923a0e72013-06-28 11:38:54 +0200421 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200422}
423
424void cfg80211_sme_disassoc(struct wireless_dev *wdev)
425{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800426 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200427
428 if (!wdev->conn)
429 return;
430
431 wdev->conn->state = CFG80211_CONN_DEAUTH;
432 schedule_work(&rdev->conn_work);
433}
434
435void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
436{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800437 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200438
439 if (!wdev->conn)
440 return;
441
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200442 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
Johannes Berg923a0e72013-06-28 11:38:54 +0200443 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200444}
445
Johannes Berge6f462d2016-12-08 17:22:09 +0100446void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
447{
448 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
449
450 if (!wdev->conn)
451 return;
452
453 wdev->conn->state = CFG80211_CONN_ABANDON;
454 schedule_work(&rdev->conn_work);
455}
456
Johannes Berg46b9d182015-03-31 16:09:13 +0200457static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
458 const u8 *ies, size_t ies_len,
459 const u8 **out_ies, size_t *out_ies_len)
460{
461 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
462 u8 *buf;
463 size_t offs;
464
465 if (!rdev->wiphy.extended_capabilities_len ||
466 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
467 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
468 if (!*out_ies)
469 return -ENOMEM;
470 *out_ies_len = ies_len;
471 return 0;
472 }
473
474 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
475 GFP_KERNEL);
476 if (!buf)
477 return -ENOMEM;
478
479 if (ies_len) {
480 static const u8 before_extcapa[] = {
481 /* not listing IEs expected to be created by driver */
482 WLAN_EID_RSN,
483 WLAN_EID_QOS_CAPA,
484 WLAN_EID_RRM_ENABLED_CAPABILITIES,
485 WLAN_EID_MOBILITY_DOMAIN,
486 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
487 WLAN_EID_BSS_COEX_2040,
488 };
489
490 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
491 ARRAY_SIZE(before_extcapa), 0);
492 memcpy(buf, ies, offs);
493 /* leave a whole for extended capabilities IE */
494 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
495 ies + offs, ies_len - offs);
496 } else {
497 offs = 0;
498 }
499
500 /* place extended capabilities IE (with only driver capabilities) */
501 buf[offs] = WLAN_EID_EXT_CAPABILITY;
502 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
503 memcpy(buf + offs + 2,
504 rdev->wiphy.extended_capabilities,
505 rdev->wiphy.extended_capabilities_len);
506
507 *out_ies = buf;
508 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
509
510 return 0;
511}
512
Johannes Bergceca7b72013-05-16 00:55:45 +0200513static int cfg80211_sme_connect(struct wireless_dev *wdev,
514 struct cfg80211_connect_params *connect,
515 const u8 *prev_bssid)
516{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200518 struct cfg80211_bss *bss;
519 int err;
520
521 if (!rdev->ops->auth || !rdev->ops->assoc)
522 return -EOPNOTSUPP;
523
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300524 if (wdev->current_bss) {
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300525 cfg80211_unhold_bss(wdev->current_bss);
526 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
527 wdev->current_bss = NULL;
528
529 cfg80211_sme_free(wdev);
530 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200531
532 if (WARN_ON(wdev->conn))
533 return -EINPROGRESS;
534
535 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
536 if (!wdev->conn)
537 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200538
539 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200540 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200541 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200542 memcpy(&wdev->conn->params, connect, sizeof(*connect));
543 if (connect->bssid) {
544 wdev->conn->params.bssid = wdev->conn->bssid;
545 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
546 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200547
Johannes Berg46b9d182015-03-31 16:09:13 +0200548 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
549 &wdev->conn->ie,
550 &wdev->conn->params.ie_len)) {
551 kfree(wdev->conn);
552 wdev->conn = NULL;
553 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200554 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200555 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200556
557 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
558 wdev->conn->auto_auth = true;
559 /* start with open system ... should mostly work */
560 wdev->conn->params.auth_type =
561 NL80211_AUTHTYPE_OPEN_SYSTEM;
562 } else {
563 wdev->conn->auto_auth = false;
564 }
565
566 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800567 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200568
569 /* see if we have the bss already */
570 bss = cfg80211_get_conn_bss(wdev);
571
572 if (prev_bssid) {
573 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
574 wdev->conn->prev_bssid_valid = true;
575 }
576
577 /* we're good if we have a matching bss struct */
578 if (bss) {
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200579 enum nl80211_timeout_reason treason;
580
581 err = cfg80211_conn_do_work(wdev, &treason);
Johannes Bergceca7b72013-05-16 00:55:45 +0200582 cfg80211_put_bss(wdev->wiphy, bss);
583 } else {
584 /* otherwise we'll need to scan for the AP first */
585 err = cfg80211_conn_scan(wdev);
586
587 /*
588 * If we can't scan right now, then we need to scan again
589 * after the current scan finished, since the parameters
590 * changed (unless we find a good AP anyway).
591 */
592 if (err == -EBUSY) {
593 err = 0;
594 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
595 }
596 }
597
598 if (err)
599 cfg80211_sme_free(wdev);
600
601 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200602}
603
Johannes Bergceca7b72013-05-16 00:55:45 +0200604static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900605{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800606 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200607 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900608
Johannes Bergceca7b72013-05-16 00:55:45 +0200609 if (!wdev->conn)
610 return 0;
611
612 if (!rdev->ops->deauth)
613 return -EOPNOTSUPP;
614
615 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
616 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
617 err = 0;
618 goto out;
619 }
620
621 /* wdev->conn->params.bssid must be set if > SCANNING */
622 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
623 wdev->conn->params.bssid,
624 NULL, 0, reason, false);
625 out:
626 cfg80211_sme_free(wdev);
627 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900628}
629
Johannes Bergceca7b72013-05-16 00:55:45 +0200630/*
631 * code shared for in-device and software SME
632 */
633
634static bool cfg80211_is_all_idle(void)
635{
636 struct cfg80211_registered_device *rdev;
637 struct wireless_dev *wdev;
638 bool is_all_idle = true;
639
640 /*
641 * All devices must be idle as otherwise if you are actively
642 * scanning some new beacon hints could be learned and would
643 * count as new regulatory hints.
Sriram R113f3aa2018-10-19 14:42:59 +0530644 * Also if there is any other active beaconing interface we
645 * need not issue a disconnect hint and reset any info such
646 * as chan dfs state, etc.
Johannes Bergceca7b72013-05-16 00:55:45 +0200647 */
648 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300649 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200650 wdev_lock(wdev);
Sriram R113f3aa2018-10-19 14:42:59 +0530651 if (wdev->conn || wdev->current_bss ||
652 cfg80211_beaconing_iface_active(wdev))
Johannes Bergceca7b72013-05-16 00:55:45 +0200653 is_all_idle = false;
654 wdev_unlock(wdev);
655 }
656 }
657
658 return is_all_idle;
659}
660
661static void disconnect_work(struct work_struct *work)
662{
663 rtnl_lock();
664 if (cfg80211_is_all_idle())
665 regulatory_hint_disconnect();
666 rtnl_unlock();
667}
668
Johannes Berge005bd7d2019-02-01 11:09:54 +0100669DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200670
671
672/*
673 * API calls for drivers implementing connect/disconnect and
674 * SME event handling
675 */
676
Ben Greear6f390902013-06-19 14:06:25 -0700677/* This method must consume bss one way or another */
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300678void __cfg80211_connect_result(struct net_device *dev,
679 struct cfg80211_connect_resp_params *cr,
680 bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200681{
682 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100683 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200684#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200685 union iwreq_data wrqu;
686#endif
687
Johannes Berg667503d2009-07-07 03:56:11 +0200688 ASSERT_WDEV_LOCK(wdev);
689
Johannes Berg074ac8d2010-09-16 14:58:22 +0200690 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700691 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300692 cfg80211_put_bss(wdev->wiphy, cr->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200693 return;
Ben Greear6f390902013-06-19 14:06:25 -0700694 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200695
Jouni Malinen4d797fc2020-04-01 17:25:47 +0300696 wdev->unprot_beacon_reported = 0;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300697 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
698 GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200699
Johannes Berg3d23e342009-09-29 23:27:28 +0200700#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200701 if (wextev) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300702 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200703 memset(&wrqu, 0, sizeof(wrqu));
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300704 wrqu.data.length = cr->req_ie_len;
705 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
706 cr->req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200707 }
708
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300709 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200710 memset(&wrqu, 0, sizeof(wrqu));
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300711 wrqu.data.length = cr->resp_ie_len;
712 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
713 cr->resp_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200714 }
715
716 memset(&wrqu, 0, sizeof(wrqu));
717 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300718 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
719 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
720 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200721 wdev->wext.prev_bssid_valid = true;
722 }
Johannes Berge45cd822009-07-02 09:58:04 +0200723 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
724 }
725#endif
726
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300727 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800728 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300729 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
730 wdev->ssid, wdev->ssid_len,
731 wdev->conn_bss_type,
732 IEEE80211_PRIVACY_ANY);
733 if (cr->bss)
734 cfg80211_hold_bss(bss_from_pub(cr->bss));
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530735 }
736
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200737 if (wdev->current_bss) {
738 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100739 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200740 wdev->current_bss = NULL;
741 }
742
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300743 if (cr->status != WLAN_STATUS_SUCCESS) {
Waiman Long453431a2020-08-06 23:18:13 -0700744 kfree_sensitive(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200745 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200746 wdev->ssid_len = 0;
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -0500747 wdev->conn_owner_nlportid = 0;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300748 if (cr->bss) {
749 cfg80211_unhold_bss(bss_from_pub(cr->bss));
750 cfg80211_put_bss(wdev->wiphy, cr->bss);
Johannes Bergf1940c52013-06-19 13:21:15 +0200751 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300752 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200753 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200754 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200755
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300756 if (WARN_ON(!cr->bss))
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530757 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200758
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300759 wdev->current_bss = bss_from_pub(cr->bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200760
David Spinadelb8676222016-09-22 23:16:50 +0300761 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
762 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700763
Johannes Berg9caf0362012-11-29 01:25:20 +0100764 rcu_read_lock();
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300765 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
Johannes Berg9caf0362012-11-29 01:25:20 +0100766 if (!country_ie) {
767 rcu_read_unlock();
768 return;
769 }
770
771 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
772 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700773
774 if (!country_ie)
775 return;
776
777 /*
778 * ieee80211_bss_get_ie() ensures we can access:
779 * - country_ie + 2, the start of the country ie data, and
780 * - and country_ie[1] which is the IE length
781 */
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300782 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700783 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100784 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200785}
Johannes Bergf2129352009-07-01 21:26:56 +0200786
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530787/* Consumes bss object one way or another */
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300788void cfg80211_connect_done(struct net_device *dev,
789 struct cfg80211_connect_resp_params *params,
790 gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200791{
Johannes Berg667503d2009-07-07 03:56:11 +0200792 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800793 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +0200794 struct cfg80211_event *ev;
795 unsigned long flags;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300796 u8 *next;
Johannes Berg667503d2009-07-07 03:56:11 +0200797
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300798 if (params->bss) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300799 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530800
Chaitanya Tataa3ce17d2019-05-01 18:25:24 +0530801 if (list_empty(&ibss->list)) {
802 struct cfg80211_bss *found = NULL, *tmp = params->bss;
803
804 found = cfg80211_get_bss(wdev->wiphy, NULL,
805 params->bss->bssid,
806 wdev->ssid, wdev->ssid_len,
807 wdev->conn_bss_type,
808 IEEE80211_PRIVACY_ANY);
809 if (found) {
810 /* The same BSS is already updated so use it
811 * instead, as it has latest info.
812 */
813 params->bss = found;
814 } else {
815 /* Update with BSS provided by driver, it will
816 * be freshly added and ref cnted, we can free
817 * the old one.
818 *
819 * signal_valid can be false, as we are not
820 * expecting the BSS to be found.
821 *
822 * keep the old timestamp to avoid confusion
823 */
824 cfg80211_bss_update(rdev, ibss, false,
825 ibss->ts);
826 }
827
828 cfg80211_put_bss(wdev->wiphy, tmp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530829 }
830 }
831
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300832 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +0300833 params->req_ie_len + params->resp_ie_len +
Arend Van Spriel76804d22018-05-22 10:19:06 +0200834 params->fils.kek_len + params->fils.pmk_len +
835 (params->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530836 if (!ev) {
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300837 cfg80211_put_bss(wdev->wiphy, params->bss);
Johannes Berg667503d2009-07-07 03:56:11 +0200838 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530839 }
Johannes Berg667503d2009-07-07 03:56:11 +0200840
841 ev->type = EVENT_CONNECT_RESULT;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300842 next = ((u8 *)ev) + sizeof(*ev);
843 if (params->bssid) {
844 ev->cr.bssid = next;
845 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
846 next += ETH_ALEN;
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700847 }
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300848 if (params->req_ie_len) {
849 ev->cr.req_ie = next;
850 ev->cr.req_ie_len = params->req_ie_len;
851 memcpy((void *)ev->cr.req_ie, params->req_ie,
852 params->req_ie_len);
853 next += params->req_ie_len;
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700854 }
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300855 if (params->resp_ie_len) {
856 ev->cr.resp_ie = next;
857 ev->cr.resp_ie_len = params->resp_ie_len;
858 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
859 params->resp_ie_len);
860 next += params->resp_ie_len;
861 }
Arend Van Spriel76804d22018-05-22 10:19:06 +0200862 if (params->fils.kek_len) {
863 ev->cr.fils.kek = next;
864 ev->cr.fils.kek_len = params->fils.kek_len;
865 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
866 params->fils.kek_len);
867 next += params->fils.kek_len;
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +0300868 }
Arend Van Spriel76804d22018-05-22 10:19:06 +0200869 if (params->fils.pmk_len) {
870 ev->cr.fils.pmk = next;
871 ev->cr.fils.pmk_len = params->fils.pmk_len;
872 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
873 params->fils.pmk_len);
874 next += params->fils.pmk_len;
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +0300875 }
Arend Van Spriel76804d22018-05-22 10:19:06 +0200876 if (params->fils.pmkid) {
877 ev->cr.fils.pmkid = next;
878 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
879 WLAN_PMKID_LEN);
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +0300880 next += WLAN_PMKID_LEN;
881 }
Arend Van Spriel76804d22018-05-22 10:19:06 +0200882 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
883 if (params->fils.update_erp_next_seq_num)
884 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300885 if (params->bss)
886 cfg80211_hold_bss(bss_from_pub(params->bss));
887 ev->cr.bss = params->bss;
888 ev->cr.status = params->status;
889 ev->cr.timeout_reason = params->timeout_reason;
Johannes Berg667503d2009-07-07 03:56:11 +0200890
891 spin_lock_irqsave(&wdev->event_lock, flags);
892 list_add_tail(&ev->list, &wdev->event_list);
893 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100894 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200895}
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300896EXPORT_SYMBOL(cfg80211_connect_done);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200897
Ben Greear0e3a39b2013-06-19 14:06:27 -0700898/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300899void __cfg80211_roamed(struct wireless_dev *wdev,
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300900 struct cfg80211_roam_info *info)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901{
Johannes Berg3d23e342009-09-29 23:27:28 +0200902#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903 union iwreq_data wrqu;
904#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200905 ASSERT_WDEV_LOCK(wdev);
906
Johannes Berg074ac8d2010-09-16 14:58:22 +0200907 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
908 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530909 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200910
Johannes Bergceca7b72013-05-16 00:55:45 +0200911 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530912 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913
Samuel Ortizb23aa672009-07-01 21:26:54 +0200914 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100915 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916 wdev->current_bss = NULL;
917
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300918 if (WARN_ON(!info->bss))
919 return;
920
921 cfg80211_hold_bss(bss_from_pub(info->bss));
922 wdev->current_bss = bss_from_pub(info->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200923
Jouni Malinen4d797fc2020-04-01 17:25:47 +0300924 wdev->unprot_beacon_reported = 0;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800925 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300926 wdev->netdev, info, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200927
Johannes Berg3d23e342009-09-29 23:27:28 +0200928#ifdef CONFIG_CFG80211_WEXT
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300929 if (info->req_ie) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200930 memset(&wrqu, 0, sizeof(wrqu));
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300931 wrqu.data.length = info->req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800932 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300933 &wrqu, info->req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934 }
935
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300936 if (info->resp_ie) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200937 memset(&wrqu, 0, sizeof(wrqu));
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300938 wrqu.data.length = info->resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200939 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300940 &wrqu, info->resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200941 }
942
943 memset(&wrqu, 0, sizeof(wrqu));
944 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300945 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
946 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200947 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200948 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200949#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530950
951 return;
952out:
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300953 cfg80211_put_bss(wdev->wiphy, info->bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200954}
Johannes Berg667503d2009-07-07 03:56:11 +0200955
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300956/* Consumes info->bss object one way or another */
957void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
958 gfp_t gfp)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530959{
960 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800961 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +0200962 struct cfg80211_event *ev;
963 unsigned long flags;
Arend Van Spriele841b7b2018-05-22 10:19:07 +0200964 u8 *next;
Johannes Berg667503d2009-07-07 03:56:11 +0200965
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300966 if (!info->bss) {
967 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
968 info->bssid, wdev->ssid,
969 wdev->ssid_len,
970 wdev->conn_bss_type,
971 IEEE80211_PRIVACY_ANY);
972 }
973
974 if (WARN_ON(!info->bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200975 return;
976
Arend Van Spriele841b7b2018-05-22 10:19:07 +0200977 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
978 info->fils.kek_len + info->fils.pmk_len +
979 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530980 if (!ev) {
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300981 cfg80211_put_bss(wdev->wiphy, info->bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530982 return;
983 }
984
Johannes Berg667503d2009-07-07 03:56:11 +0200985 ev->type = EVENT_ROAMED;
Arend Van Spriele841b7b2018-05-22 10:19:07 +0200986 next = ((u8 *)ev) + sizeof(*ev);
987 if (info->req_ie_len) {
988 ev->rm.req_ie = next;
989 ev->rm.req_ie_len = info->req_ie_len;
990 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
991 next += info->req_ie_len;
992 }
993 if (info->resp_ie_len) {
994 ev->rm.resp_ie = next;
995 ev->rm.resp_ie_len = info->resp_ie_len;
996 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
997 info->resp_ie_len);
998 next += info->resp_ie_len;
999 }
1000 if (info->fils.kek_len) {
1001 ev->rm.fils.kek = next;
1002 ev->rm.fils.kek_len = info->fils.kek_len;
1003 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1004 info->fils.kek_len);
1005 next += info->fils.kek_len;
1006 }
1007 if (info->fils.pmk_len) {
1008 ev->rm.fils.pmk = next;
1009 ev->rm.fils.pmk_len = info->fils.pmk_len;
1010 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1011 info->fils.pmk_len);
1012 next += info->fils.pmk_len;
1013 }
1014 if (info->fils.pmkid) {
1015 ev->rm.fils.pmkid = next;
1016 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1017 WLAN_PMKID_LEN);
1018 next += WLAN_PMKID_LEN;
1019 }
1020 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1021 if (info->fils.update_erp_next_seq_num)
1022 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
Avraham Stern29ce6ec2017-04-26 10:58:49 +03001023 ev->rm.bss = info->bss;
Johannes Berg667503d2009-07-07 03:56:11 +02001024
1025 spin_lock_irqsave(&wdev->event_lock, flags);
1026 list_add_tail(&ev->list, &wdev->event_list);
1027 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001028 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +02001029}
Avraham Stern29ce6ec2017-04-26 10:58:49 +03001030EXPORT_SYMBOL(cfg80211_roamed);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001031
Avraham Stern503c1fb2017-09-29 14:21:49 +02001032void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1033{
1034 ASSERT_WDEV_LOCK(wdev);
1035
1036 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1037 return;
1038
1039 if (WARN_ON(!wdev->current_bss) ||
1040 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1041 return;
1042
1043 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1044 bssid);
1045}
1046
1047void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1048 gfp_t gfp)
1049{
1050 struct wireless_dev *wdev = dev->ieee80211_ptr;
1051 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1052 struct cfg80211_event *ev;
1053 unsigned long flags;
1054
1055 if (WARN_ON(!bssid))
1056 return;
1057
1058 ev = kzalloc(sizeof(*ev), gfp);
1059 if (!ev)
1060 return;
1061
1062 ev->type = EVENT_PORT_AUTHORIZED;
1063 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1064
1065 /*
1066 * Use the wdev event list so that if there are pending
1067 * connected/roamed events, they will be reported first.
1068 */
1069 spin_lock_irqsave(&wdev->event_lock, flags);
1070 list_add_tail(&ev->list, &wdev->event_list);
1071 spin_unlock_irqrestore(&wdev->event_lock, flags);
1072 queue_work(cfg80211_wq, &rdev->event_work);
1073}
1074EXPORT_SYMBOL(cfg80211_port_authorized);
1075
Johannes Berg667503d2009-07-07 03:56:11 +02001076void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +02001077 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001078{
1079 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001080 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +02001081 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +02001082#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +02001083 union iwreq_data wrqu;
1084#endif
1085
Johannes Berg667503d2009-07-07 03:56:11 +02001086 ASSERT_WDEV_LOCK(wdev);
1087
Johannes Berg074ac8d2010-09-16 14:58:22 +02001088 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1089 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02001090 return;
1091
Samuel Ortizb23aa672009-07-01 21:26:54 +02001092 if (wdev->current_bss) {
1093 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +01001094 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001095 }
1096
1097 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +02001098 wdev->ssid_len = 0;
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05001099 wdev->conn_owner_nlportid = 0;
Waiman Long453431a2020-08-06 23:18:13 -07001100 kfree_sensitive(wdev->connect_keys);
Avraham Stern3027a8e2018-02-19 14:48:38 +02001101 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001102
Johannes Bergfffd0932009-07-08 14:22:54 +02001103 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1104
Arend van Sprielb8607152016-02-15 14:35:53 +01001105 /* stop critical protocol if supported */
1106 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1107 rdev->crit_proto_nlportid = 0;
1108 rdev_crit_proto_stop(rdev, wdev);
1109 }
1110
Johannes Bergfffd0932009-07-08 14:22:54 +02001111 /*
1112 * Delete all the keys ... pairwise keys can't really
1113 * exist any more anyway, but default keys might.
1114 */
Jouni Malinen56be3932020-02-22 15:25:43 +02001115 if (rdev->ops->del_key) {
1116 int max_key_idx = 5;
1117
1118 if (wiphy_ext_feature_isset(
1119 wdev->wiphy,
Johannes Berg0e479012020-05-28 21:34:24 +02001120 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1121 wiphy_ext_feature_isset(
1122 wdev->wiphy,
1123 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
Jouni Malinen56be3932020-02-22 15:25:43 +02001124 max_key_idx = 7;
1125 for (i = 0; i <= max_key_idx; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +03001126 rdev_del_key(rdev, dev, i, false, NULL);
Jouni Malinen56be3932020-02-22 15:25:43 +02001127 }
Samuel Ortizb23aa672009-07-01 21:26:54 +02001128
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001129 rdev_set_qos_map(rdev, dev, NULL);
1130
Johannes Berg3d23e342009-09-29 23:27:28 +02001131#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +02001132 memset(&wrqu, 0, sizeof(wrqu));
1133 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1134 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -08001135 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001136#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001137
1138 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001139}
1140
1141void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +02001142 const u8 *ie, size_t ie_len,
1143 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001144{
Johannes Berg667503d2009-07-07 03:56:11 +02001145 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001146 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +02001147 struct cfg80211_event *ev;
1148 unsigned long flags;
1149
1150 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1151 if (!ev)
1152 return;
1153
1154 ev->type = EVENT_DISCONNECTED;
1155 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1156 ev->dc.ie_len = ie_len;
1157 memcpy((void *)ev->dc.ie, ie, ie_len);
1158 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +02001159 ev->dc.locally_generated = locally_generated;
Johannes Berg667503d2009-07-07 03:56:11 +02001160
1161 spin_lock_irqsave(&wdev->event_lock, flags);
1162 list_add_tail(&ev->list, &wdev->event_list);
1163 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001164 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001165}
1166EXPORT_SYMBOL(cfg80211_disconnected);
1167
Johannes Bergceca7b72013-05-16 00:55:45 +02001168/*
1169 * API calls for nl80211/wext compatibility code
1170 */
Johannes Berg83739b02013-05-15 17:44:01 +02001171int cfg80211_connect(struct cfg80211_registered_device *rdev,
1172 struct net_device *dev,
1173 struct cfg80211_connect_params *connect,
1174 struct cfg80211_cached_keys *connkeys,
1175 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001176{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001177 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +02001178 int err;
1179
1180 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001181
Johannes Berg51e13352017-10-17 21:56:20 +02001182 /*
1183 * If we have an ssid_len, we're trying to connect or are
1184 * already connected, so reject a new SSID unless it's the
1185 * same (which is the case for re-association.)
1186 */
1187 if (wdev->ssid_len &&
1188 (wdev->ssid_len != connect->ssid_len ||
1189 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1190 return -EALREADY;
1191
1192 /*
1193 * If connected, reject (re-)association unless prev_bssid
1194 * matches the current BSSID.
1195 */
1196 if (wdev->current_bss) {
1197 if (!prev_bssid)
1198 return -EALREADY;
1199 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1200 return -ENOTCONN;
Johannes Bergfffd0932009-07-08 14:22:54 +02001201 }
1202
Johannes Berg51e13352017-10-17 21:56:20 +02001203 /*
1204 * Reject if we're in the process of connecting with WEP,
1205 * this case isn't very interesting and trying to handle
1206 * it would make the code much more complex.
1207 */
1208 if (wdev->connect_keys)
1209 return -EINPROGRESS;
1210
Ben Greear7e7c8922011-11-18 11:31:59 -08001211 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1212 rdev->wiphy.ht_capa_mod_mask);
Sergey Matyukevich81c5dce2018-10-19 15:40:13 +00001213 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1214 rdev->wiphy.vht_capa_mod_mask);
Ben Greear7e7c8922011-11-18 11:31:59 -08001215
Johannes Bergfffd0932009-07-08 14:22:54 +02001216 if (connkeys && connkeys->def >= 0) {
1217 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001218 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001219
1220 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001221 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001222 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001223 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1224 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001225 connect->key_idx = idx;
1226 connect->key = connkeys->params[idx].key;
1227 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001228
1229 /*
1230 * If ciphers are not set (e.g. when going through
1231 * iwconfig), we have to set them appropriately here.
1232 */
1233 if (connect->crypto.cipher_group == 0)
1234 connect->crypto.cipher_group = cipher;
1235
1236 if (connect->crypto.n_ciphers_pairwise == 0) {
1237 connect->crypto.n_ciphers_pairwise = 1;
1238 connect->crypto.ciphers_pairwise[0] = cipher;
1239 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001240 }
David Spinadelb8676222016-09-22 23:16:50 +03001241
1242 connect->crypto.wep_keys = connkeys->params;
1243 connect->crypto.wep_tx_key = connkeys->def;
Johannes Bergf1c1f172016-09-13 17:08:23 +02001244 } else {
1245 if (WARN_ON(connkeys))
1246 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001247 }
1248
Johannes Bergceca7b72013-05-16 00:55:45 +02001249 wdev->connect_keys = connkeys;
1250 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1251 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001252
Lior David34d50512016-01-28 10:58:25 +02001253 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1254 IEEE80211_BSS_TYPE_ESS;
1255
Johannes Bergceca7b72013-05-16 00:55:45 +02001256 if (!rdev->ops->connect)
1257 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1258 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001259 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001260
Johannes Bergceca7b72013-05-16 00:55:45 +02001261 if (err) {
1262 wdev->connect_keys = NULL;
Johannes Berg51e13352017-10-17 21:56:20 +02001263 /*
1264 * This could be reassoc getting refused, don't clear
1265 * ssid_len in that case.
1266 */
1267 if (!wdev->current_bss)
1268 wdev->ssid_len = 0;
Johannes Bergceca7b72013-05-16 00:55:45 +02001269 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001270 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001271
1272 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001273}
1274
Johannes Berg83739b02013-05-15 17:44:01 +02001275int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1276 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001277{
Johannes Berg6829c872009-07-02 09:13:27 +02001278 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001279 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001280
Johannes Berg667503d2009-07-07 03:56:11 +02001281 ASSERT_WDEV_LOCK(wdev);
1282
Waiman Long453431a2020-08-06 23:18:13 -07001283 kfree_sensitive(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001284 wdev->connect_keys = NULL;
1285
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05001286 wdev->conn_owner_nlportid = 0;
1287
Johannes Bergdee8a972013-08-13 09:23:57 +02001288 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001289 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001290 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001291 cfg80211_mlme_down(rdev, dev);
Ilan Peer0711d632016-10-18 23:12:13 +03001292 else if (wdev->ssid_len)
Hila Gonene35e4d22012-06-27 17:19:42 +03001293 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001294
Johannes Berg51e13352017-10-17 21:56:20 +02001295 /*
1296 * Clear ssid_len unless we actually were fully connected,
1297 * in which case cfg80211_disconnected() will take care of
1298 * this later.
1299 */
1300 if (!wdev->current_bss)
1301 wdev->ssid_len = 0;
1302
Johannes Bergceca7b72013-05-16 00:55:45 +02001303 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001304}
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05001305
1306/*
1307 * Used to clean up after the connection / connection attempt owner socket
1308 * disconnects
1309 */
1310void cfg80211_autodisconnect_wk(struct work_struct *work)
1311{
1312 struct wireless_dev *wdev =
1313 container_of(work, struct wireless_dev, disconnect_wk);
1314 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1315
1316 wdev_lock(wdev);
1317
1318 if (wdev->conn_owner_nlportid) {
Denis Kenzior37b1c002018-03-26 12:52:44 -05001319 switch (wdev->iftype) {
1320 case NL80211_IFTYPE_ADHOC:
Markus Theil5a128a02020-01-08 12:55:36 +01001321 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
Denis Kenzior37b1c002018-03-26 12:52:44 -05001322 break;
1323 case NL80211_IFTYPE_AP:
1324 case NL80211_IFTYPE_P2P_GO:
Markus Theil5a128a02020-01-08 12:55:36 +01001325 __cfg80211_stop_ap(rdev, wdev->netdev, false);
Denis Kenzior37b1c002018-03-26 12:52:44 -05001326 break;
1327 case NL80211_IFTYPE_MESH_POINT:
Markus Theil5a128a02020-01-08 12:55:36 +01001328 __cfg80211_leave_mesh(rdev, wdev->netdev);
Denis Kenzior37b1c002018-03-26 12:52:44 -05001329 break;
1330 case NL80211_IFTYPE_STATION:
1331 case NL80211_IFTYPE_P2P_CLIENT:
1332 /*
1333 * Use disconnect_bssid if still connecting and
1334 * ops->disconnect not implemented. Otherwise we can
1335 * use cfg80211_disconnect.
1336 */
1337 if (rdev->ops->disconnect || wdev->current_bss)
1338 cfg80211_disconnect(rdev, wdev->netdev,
1339 WLAN_REASON_DEAUTH_LEAVING,
1340 true);
1341 else
1342 cfg80211_mlme_deauth(rdev, wdev->netdev,
1343 wdev->disconnect_bssid,
1344 NULL, 0,
1345 WLAN_REASON_DEAUTH_LEAVING,
1346 false);
1347 break;
1348 default:
1349 break;
1350 }
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05001351 }
1352
1353 wdev_unlock(wdev);
1354}