blob: 6fd24f6435c3a7dbb5995fd84722c431009727f4 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
Johannes Bergceca7b72013-05-16 00:55:45 +02002 * SME code for cfg80211
3 * both driver SME event handling and the SME implementation
4 * (for nl80211's connect() and wext)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005 *
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020016#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020017#include <net/cfg80211.h>
18#include <net/rtnetlink.h>
19#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070020#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030021#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020022
Johannes Bergceca7b72013-05-16 00:55:45 +020023/*
24 * Software SME in cfg80211, using auth/assoc/deauth calls to the
25 * driver. This is is for implementing nl80211's connect/disconnect
26 * and wireless extensions (if configured.)
27 */
28
Johannes Berg6829c872009-07-02 09:13:27 +020029struct cfg80211_conn {
30 struct cfg80211_connect_params params;
31 /* these are sub-states of the _CONNECTING sme_state */
32 enum {
Johannes Berg6829c872009-07-02 09:13:27 +020033 CFG80211_CONN_SCANNING,
34 CFG80211_CONN_SCAN_AGAIN,
35 CFG80211_CONN_AUTHENTICATE_NEXT,
36 CFG80211_CONN_AUTHENTICATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020037 CFG80211_CONN_AUTH_FAILED,
Johannes Berg6829c872009-07-02 09:13:27 +020038 CFG80211_CONN_ASSOCIATE_NEXT,
39 CFG80211_CONN_ASSOCIATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020040 CFG80211_CONN_ASSOC_FAILED,
Johannes Bergceca7b72013-05-16 00:55:45 +020041 CFG80211_CONN_DEAUTH,
Johannes Berg1976c762016-12-08 17:22:09 +010042 CFG80211_CONN_ABANDON,
Johannes Bergceca7b72013-05-16 00:55:45 +020043 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020044 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020045 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg46b9d182015-03-31 16:09:13 +020046 const u8 *ie;
Johannes Berg6829c872009-07-02 09:13:27 +020047 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020048 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020049};
50
Johannes Bergceca7b72013-05-16 00:55:45 +020051static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050052{
Johannes Bergceca7b72013-05-16 00:55:45 +020053 if (!wdev->conn)
54 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050055
Johannes Bergceca7b72013-05-16 00:55:45 +020056 kfree(wdev->conn->ie);
57 kfree(wdev->conn);
58 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050059}
60
Johannes Berg6829c872009-07-02 09:13:27 +020061static int cfg80211_conn_scan(struct wireless_dev *wdev)
62{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080063 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020064 struct cfg80211_scan_request *request;
65 int n_channels, err;
66
67 ASSERT_RTNL();
Johannes Berg667503dd2009-07-07 03:56:11 +020068 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020069
Johannes Bergf9d15d12014-01-22 11:14:19 +020070 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020071 return -EBUSY;
72
Ilan Peerbdfbec22014-01-09 11:37:23 +020073 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020074 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020075 else
76 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020077
Johannes Berg6829c872009-07-02 09:13:27 +020078 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
79 sizeof(request->channels[0]) * n_channels,
80 GFP_KERNEL);
81 if (!request)
82 return -ENOMEM;
83
Karl Beldan2a84ee82014-10-07 11:42:18 +020084 if (wdev->conn->params.channel) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020085 enum nl80211_band band = wdev->conn->params.channel->band;
Karl Beldan2a84ee82014-10-07 11:42:18 +020086 struct ieee80211_supported_band *sband =
87 wdev->wiphy->bands[band];
88
89 if (!sband) {
90 kfree(request);
91 return -EINVAL;
92 }
Johannes Berg6829c872009-07-02 09:13:27 +020093 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +020094 request->rates[band] = (1 << sband->n_bitrates) - 1;
95 } else {
Johannes Berg6829c872009-07-02 09:13:27 +020096 int i = 0, j;
Johannes Berg57fbcce2016-04-12 15:56:15 +020097 enum nl80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053098 struct ieee80211_supported_band *bands;
99 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200100
Johannes Berg57fbcce2016-04-12 15:56:15 +0200101 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530102 bands = wdev->wiphy->bands[band];
103 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200104 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530105 for (j = 0; j < bands->n_channels; j++) {
106 channel = &bands->channels[j];
107 if (channel->flags & IEEE80211_CHAN_DISABLED)
108 continue;
109 request->channels[i++] = channel;
110 }
111 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200112 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530113 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200114 }
115 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200116 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200117 request->n_ssids = 1;
118
119 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
120 wdev->conn->params.ssid_len);
121 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
122
Jouni Malinen818965d2016-02-26 22:12:47 +0200123 eth_broadcast_addr(request->bssid);
124
Johannes Bergfd014282012-06-18 19:17:03 +0200125 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700127 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200128
Johannes Berg79c97e92009-07-07 03:56:12 +0200129 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200130
Hila Gonene35e4d22012-06-27 17:19:42 +0300131 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200132 if (!err) {
133 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200134 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200135 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200136 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200137 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200138 kfree(request);
139 }
140 return err;
141}
142
143static int cfg80211_conn_do_work(struct wireless_dev *wdev)
144{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800145 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200146 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100147 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200148 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200149
Johannes Berg667503dd2009-07-07 03:56:11 +0200150 ASSERT_WDEV_LOCK(wdev);
151
Johannes Berg6829c872009-07-02 09:13:27 +0200152 if (!wdev->conn)
153 return 0;
154
Johannes Berg19957bb2009-07-02 17:20:43 +0200155 params = &wdev->conn->params;
156
Johannes Berg6829c872009-07-02 09:13:27 +0200157 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200158 case CFG80211_CONN_SCANNING:
159 /* didn't find it during scan ... */
160 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200161 case CFG80211_CONN_SCAN_AGAIN:
162 return cfg80211_conn_scan(wdev);
163 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200164 if (WARN_ON(!rdev->ops->auth))
165 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200166 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200167 return cfg80211_mlme_auth(rdev, wdev->netdev,
168 params->channel, params->auth_type,
169 params->bssid,
170 params->ssid, params->ssid_len,
171 NULL, 0,
172 params->key, params->key_len,
173 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200174 case CFG80211_CONN_AUTH_FAILED:
175 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200176 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200177 if (WARN_ON(!rdev->ops->assoc))
178 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200179 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200180 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100181 req.prev_bssid = wdev->conn->prev_bssid;
182 req.ie = params->ie;
183 req.ie_len = params->ie_len;
184 req.use_mfp = params->mfp != NL80211_MFP_NO;
185 req.crypto = params->crypto;
186 req.flags = params->flags;
187 req.ht_capa = params->ht_capa;
188 req.ht_capa_mask = params->ht_capa_mask;
189 req.vht_capa = params->vht_capa;
190 req.vht_capa_mask = params->vht_capa_mask;
191
Johannes Berg91bf9b22013-05-15 17:44:01 +0200192 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
193 params->bssid, params->ssid,
194 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200195 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200196 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
197 NULL, 0,
198 WLAN_REASON_DEAUTH_LEAVING,
199 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200200 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200201 case CFG80211_CONN_ASSOC_FAILED:
202 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
203 NULL, 0,
204 WLAN_REASON_DEAUTH_LEAVING, false);
205 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200206 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200207 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
208 NULL, 0,
209 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg1976c762016-12-08 17:22:09 +0100210 /* fall through */
211 case CFG80211_CONN_ABANDON:
Johannes Berg923a0e72013-06-28 11:38:54 +0200212 /* free directly, disconnected event already sent */
213 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200214 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200215 default:
216 return 0;
217 }
218}
219
220void cfg80211_conn_work(struct work_struct *work)
221{
Johannes Berg79c97e92009-07-07 03:56:12 +0200222 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200223 container_of(work, struct cfg80211_registered_device, conn_work);
224 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100225 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200226
227 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200228
Johannes Berg53873f12016-05-03 16:52:04 +0300229 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200230 if (!wdev->netdev)
231 continue;
232
Johannes Berg667503dd2009-07-07 03:56:11 +0200233 wdev_lock(wdev);
234 if (!netif_running(wdev->netdev)) {
235 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200236 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200237 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200238 if (!wdev->conn ||
239 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200240 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200241 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200242 }
Johannes Berg7400f422009-10-31 07:40:37 +0100243 if (wdev->conn->params.bssid) {
244 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
245 bssid = bssid_buf;
246 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200247 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200248 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900249 wdev->netdev, bssid,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300250 NULL, 0, NULL, 0, -1, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200251 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200252 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200253 }
254
Johannes Berg6829c872009-07-02 09:13:27 +0200255 rtnl_unlock();
256}
257
Ben Greear0e3a39b2013-06-19 14:06:27 -0700258/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700259static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200260{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800261 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200262 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200263
Johannes Berg667503dd2009-07-07 03:56:11 +0200264 ASSERT_WDEV_LOCK(wdev);
265
Jouni Malinened9d0102011-05-16 19:40:15 +0300266 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
267 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200268 wdev->conn->params.ssid,
269 wdev->conn->params.ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200270 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200271 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200272 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700273 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200274
275 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
276 wdev->conn->params.bssid = wdev->conn->bssid;
277 wdev->conn->params.channel = bss->channel;
278 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200279 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200280
Johannes Bergbbac31f2009-09-16 09:04:26 -0700281 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200282}
283
Johannes Berg667503dd2009-07-07 03:56:11 +0200284static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200285{
286 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800287 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700288 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200289
Johannes Berg667503dd2009-07-07 03:56:11 +0200290 ASSERT_WDEV_LOCK(wdev);
291
Zhu Yid4b1a682009-07-16 17:34:14 +0800292 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200293 return;
294
295 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
296 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
297 return;
298
Johannes Bergbbac31f2009-09-16 09:04:26 -0700299 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200300 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100301 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200302 else
303 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200304}
305
Johannes Berg667503dd2009-07-07 03:56:11 +0200306void cfg80211_sme_scan_done(struct net_device *dev)
307{
308 struct wireless_dev *wdev = dev->ieee80211_ptr;
309
310 wdev_lock(wdev);
311 __cfg80211_sme_scan_done(dev);
312 wdev_unlock(wdev);
313}
314
Johannes Bergceca7b72013-05-16 00:55:45 +0200315void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200316{
Johannes Berg6829c872009-07-02 09:13:27 +0200317 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800318 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200319 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
320 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
321
Johannes Berg667503dd2009-07-07 03:56:11 +0200322 ASSERT_WDEV_LOCK(wdev);
323
Johannes Bergceca7b72013-05-16 00:55:45 +0200324 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200325 return;
326
327 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
328 wdev->conn->auto_auth &&
329 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
330 /* select automatically between only open, shared, leap */
331 switch (wdev->conn->params.auth_type) {
332 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200333 if (wdev->connect_keys)
334 wdev->conn->params.auth_type =
335 NL80211_AUTHTYPE_SHARED_KEY;
336 else
337 wdev->conn->params.auth_type =
338 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200339 break;
340 case NL80211_AUTHTYPE_SHARED_KEY:
341 wdev->conn->params.auth_type =
342 NL80211_AUTHTYPE_NETWORK_EAP;
343 break;
344 default:
345 /* huh? */
346 wdev->conn->params.auth_type =
347 NL80211_AUTHTYPE_OPEN_SYSTEM;
348 break;
349 }
350 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
351 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200352 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200353 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
354 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200355 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200356 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200357 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
358 schedule_work(&rdev->conn_work);
359 }
360}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200361
Johannes Bergceca7b72013-05-16 00:55:45 +0200362bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200363{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800364 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200365
Johannes Bergceca7b72013-05-16 00:55:45 +0200366 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200367 return false;
368
Johannes Bergceca7b72013-05-16 00:55:45 +0200369 if (status == WLAN_STATUS_SUCCESS) {
370 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200371 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200372 }
373
374 if (wdev->conn->prev_bssid_valid) {
375 /*
376 * Some stupid APs don't accept reassoc, so we
377 * need to fall back to trying regular assoc;
378 * return true so no event is sent to userspace.
379 */
380 wdev->conn->prev_bssid_valid = false;
381 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
382 schedule_work(&rdev->conn_work);
383 return true;
384 }
385
Johannes Berg923a0e72013-06-28 11:38:54 +0200386 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200387 schedule_work(&rdev->conn_work);
388 return false;
389}
390
391void cfg80211_sme_deauth(struct wireless_dev *wdev)
392{
393 cfg80211_sme_free(wdev);
394}
395
396void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
397{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800398 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200399
400 if (!wdev->conn)
401 return;
402
403 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
404 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200405}
406
407void cfg80211_sme_disassoc(struct wireless_dev *wdev)
408{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800409 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200410
411 if (!wdev->conn)
412 return;
413
414 wdev->conn->state = CFG80211_CONN_DEAUTH;
415 schedule_work(&rdev->conn_work);
416}
417
418void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
419{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800420 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200421
422 if (!wdev->conn)
423 return;
424
425 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
426 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200427}
428
Johannes Berg1976c762016-12-08 17:22:09 +0100429void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
430{
431 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
432
433 if (!wdev->conn)
434 return;
435
436 wdev->conn->state = CFG80211_CONN_ABANDON;
437 schedule_work(&rdev->conn_work);
438}
439
Johannes Berg46b9d182015-03-31 16:09:13 +0200440static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
441 const u8 *ies, size_t ies_len,
442 const u8 **out_ies, size_t *out_ies_len)
443{
444 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
445 u8 *buf;
446 size_t offs;
447
448 if (!rdev->wiphy.extended_capabilities_len ||
449 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
450 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
451 if (!*out_ies)
452 return -ENOMEM;
453 *out_ies_len = ies_len;
454 return 0;
455 }
456
457 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
458 GFP_KERNEL);
459 if (!buf)
460 return -ENOMEM;
461
462 if (ies_len) {
463 static const u8 before_extcapa[] = {
464 /* not listing IEs expected to be created by driver */
465 WLAN_EID_RSN,
466 WLAN_EID_QOS_CAPA,
467 WLAN_EID_RRM_ENABLED_CAPABILITIES,
468 WLAN_EID_MOBILITY_DOMAIN,
469 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
470 WLAN_EID_BSS_COEX_2040,
471 };
472
473 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
474 ARRAY_SIZE(before_extcapa), 0);
475 memcpy(buf, ies, offs);
476 /* leave a whole for extended capabilities IE */
477 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
478 ies + offs, ies_len - offs);
479 } else {
480 offs = 0;
481 }
482
483 /* place extended capabilities IE (with only driver capabilities) */
484 buf[offs] = WLAN_EID_EXT_CAPABILITY;
485 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
486 memcpy(buf + offs + 2,
487 rdev->wiphy.extended_capabilities,
488 rdev->wiphy.extended_capabilities_len);
489
490 *out_ies = buf;
491 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
492
493 return 0;
494}
495
Johannes Bergceca7b72013-05-16 00:55:45 +0200496static int cfg80211_sme_connect(struct wireless_dev *wdev,
497 struct cfg80211_connect_params *connect,
498 const u8 *prev_bssid)
499{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800500 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200501 struct cfg80211_bss *bss;
502 int err;
503
504 if (!rdev->ops->auth || !rdev->ops->assoc)
505 return -EOPNOTSUPP;
506
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300507 if (wdev->current_bss) {
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300508 cfg80211_unhold_bss(wdev->current_bss);
509 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
510 wdev->current_bss = NULL;
511
512 cfg80211_sme_free(wdev);
513 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200514
515 if (WARN_ON(wdev->conn))
516 return -EINPROGRESS;
517
518 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
519 if (!wdev->conn)
520 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200521
522 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200523 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200524 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200525 memcpy(&wdev->conn->params, connect, sizeof(*connect));
526 if (connect->bssid) {
527 wdev->conn->params.bssid = wdev->conn->bssid;
528 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
529 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200530
Johannes Berg46b9d182015-03-31 16:09:13 +0200531 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
532 &wdev->conn->ie,
533 &wdev->conn->params.ie_len)) {
534 kfree(wdev->conn);
535 wdev->conn = NULL;
536 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200537 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200538 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200539
540 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
541 wdev->conn->auto_auth = true;
542 /* start with open system ... should mostly work */
543 wdev->conn->params.auth_type =
544 NL80211_AUTHTYPE_OPEN_SYSTEM;
545 } else {
546 wdev->conn->auto_auth = false;
547 }
548
549 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800550 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200551
552 /* see if we have the bss already */
553 bss = cfg80211_get_conn_bss(wdev);
554
555 if (prev_bssid) {
556 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
557 wdev->conn->prev_bssid_valid = true;
558 }
559
560 /* we're good if we have a matching bss struct */
561 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200562 err = cfg80211_conn_do_work(wdev);
563 cfg80211_put_bss(wdev->wiphy, bss);
564 } else {
565 /* otherwise we'll need to scan for the AP first */
566 err = cfg80211_conn_scan(wdev);
567
568 /*
569 * If we can't scan right now, then we need to scan again
570 * after the current scan finished, since the parameters
571 * changed (unless we find a good AP anyway).
572 */
573 if (err == -EBUSY) {
574 err = 0;
575 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
576 }
577 }
578
579 if (err)
580 cfg80211_sme_free(wdev);
581
582 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200583}
584
Johannes Bergceca7b72013-05-16 00:55:45 +0200585static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900586{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800587 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200588 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900589
Johannes Bergceca7b72013-05-16 00:55:45 +0200590 if (!wdev->conn)
591 return 0;
592
593 if (!rdev->ops->deauth)
594 return -EOPNOTSUPP;
595
596 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
597 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
598 err = 0;
599 goto out;
600 }
601
602 /* wdev->conn->params.bssid must be set if > SCANNING */
603 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
604 wdev->conn->params.bssid,
605 NULL, 0, reason, false);
606 out:
607 cfg80211_sme_free(wdev);
608 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900609}
610
Johannes Bergceca7b72013-05-16 00:55:45 +0200611/*
612 * code shared for in-device and software SME
613 */
614
615static bool cfg80211_is_all_idle(void)
616{
617 struct cfg80211_registered_device *rdev;
618 struct wireless_dev *wdev;
619 bool is_all_idle = true;
620
621 /*
622 * All devices must be idle as otherwise if you are actively
623 * scanning some new beacon hints could be learned and would
624 * count as new regulatory hints.
625 */
626 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300627 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200628 wdev_lock(wdev);
629 if (wdev->conn || wdev->current_bss)
630 is_all_idle = false;
631 wdev_unlock(wdev);
632 }
633 }
634
635 return is_all_idle;
636}
637
638static void disconnect_work(struct work_struct *work)
639{
640 rtnl_lock();
641 if (cfg80211_is_all_idle())
642 regulatory_hint_disconnect();
643 rtnl_unlock();
644}
645
646static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
647
648
649/*
650 * API calls for drivers implementing connect/disconnect and
651 * SME event handling
652 */
653
Ben Greear6f390902013-06-19 14:06:25 -0700654/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200655void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
656 const u8 *req_ie, size_t req_ie_len,
657 const u8 *resp_ie, size_t resp_ie_len,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300658 int status, bool wextev,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200659 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200660{
661 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100662 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200663#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200664 union iwreq_data wrqu;
665#endif
666
Johannes Berg667503dd2009-07-07 03:56:11 +0200667 ASSERT_WDEV_LOCK(wdev);
668
Johannes Berg074ac8d2010-09-16 14:58:22 +0200669 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700670 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
671 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200672 return;
Ben Greear6f390902013-06-19 14:06:25 -0700673 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200674
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800675 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200676 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200677 resp_ie, resp_ie_len,
678 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200679
Johannes Berg3d23e342009-09-29 23:27:28 +0200680#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200681 if (wextev) {
682 if (req_ie && status == WLAN_STATUS_SUCCESS) {
683 memset(&wrqu, 0, sizeof(wrqu));
684 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800685 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200686 }
687
688 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
689 memset(&wrqu, 0, sizeof(wrqu));
690 wrqu.data.length = resp_ie_len;
691 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
692 }
693
694 memset(&wrqu, 0, sizeof(wrqu));
695 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200696 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200697 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200698 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
699 wdev->wext.prev_bssid_valid = true;
700 }
Johannes Berge45cd822009-07-02 09:58:04 +0200701 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
702 }
703#endif
704
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530705 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800706 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530707 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
708 wdev->ssid, wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200709 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200710 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530711 if (bss)
712 cfg80211_hold_bss(bss_from_pub(bss));
713 }
714
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200715 if (wdev->current_bss) {
716 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100717 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200718 wdev->current_bss = NULL;
719 }
720
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300722 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200723 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200724 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200725 if (bss) {
726 cfg80211_unhold_bss(bss_from_pub(bss));
727 cfg80211_put_bss(wdev->wiphy, bss);
728 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300729 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200730 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200731 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200732
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530733 if (WARN_ON(!bss))
734 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200735
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 wdev->current_bss = bss_from_pub(bss);
737
David Spinadelb8676222016-09-22 23:16:50 +0300738 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
739 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700740
Johannes Berg9caf0362012-11-29 01:25:20 +0100741 rcu_read_lock();
742 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
743 if (!country_ie) {
744 rcu_read_unlock();
745 return;
746 }
747
748 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
749 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700750
751 if (!country_ie)
752 return;
753
754 /*
755 * ieee80211_bss_get_ie() ensures we can access:
756 * - country_ie + 2, the start of the country ie data, and
757 * - and country_ie[1] which is the IE length
758 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700759 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
760 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100761 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762}
Johannes Bergf2129352009-07-01 21:26:56 +0200763
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530764/* Consumes bss object one way or another */
765void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
766 struct cfg80211_bss *bss, const u8 *req_ie,
767 size_t req_ie_len, const u8 *resp_ie,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300768 size_t resp_ie_len, int status, gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200769{
Johannes Berg667503dd2009-07-07 03:56:11 +0200770 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800771 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200772 struct cfg80211_event *ev;
773 unsigned long flags;
774
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530775 if (bss) {
776 /* Make sure the bss entry provided by the driver is valid. */
777 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
778
779 if (WARN_ON(list_empty(&ibss->list))) {
780 cfg80211_put_bss(wdev->wiphy, bss);
781 return;
782 }
783 }
784
Johannes Berg667503dd2009-07-07 03:56:11 +0200785 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530786 if (!ev) {
787 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg667503dd2009-07-07 03:56:11 +0200788 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530789 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200790
791 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800792 if (bssid)
793 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700794 if (req_ie_len) {
795 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
796 ev->cr.req_ie_len = req_ie_len;
797 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
798 }
799 if (resp_ie_len) {
800 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
801 ev->cr.resp_ie_len = resp_ie_len;
802 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
803 }
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530804 if (bss)
805 cfg80211_hold_bss(bss_from_pub(bss));
806 ev->cr.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200807 ev->cr.status = status;
808
809 spin_lock_irqsave(&wdev->event_lock, flags);
810 list_add_tail(&ev->list, &wdev->event_list);
811 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100812 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200813}
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530814EXPORT_SYMBOL(cfg80211_connect_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200815
Ben Greear0e3a39b2013-06-19 14:06:27 -0700816/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300817void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530818 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200819 const u8 *req_ie, size_t req_ie_len,
820 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200821{
Johannes Berg3d23e342009-09-29 23:27:28 +0200822#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200823 union iwreq_data wrqu;
824#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200825 ASSERT_WDEV_LOCK(wdev);
826
Johannes Berg074ac8d2010-09-16 14:58:22 +0200827 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
828 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530829 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200830
Johannes Bergceca7b72013-05-16 00:55:45 +0200831 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530832 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200833
Samuel Ortizb23aa672009-07-01 21:26:54 +0200834 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100835 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200836 wdev->current_bss = NULL;
837
Johannes Berg19957bb2009-07-02 17:20:43 +0200838 cfg80211_hold_bss(bss_from_pub(bss));
839 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200840
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800841 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
842 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200843 req_ie, req_ie_len, resp_ie, resp_ie_len,
844 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200845
Johannes Berg3d23e342009-09-29 23:27:28 +0200846#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200847 if (req_ie) {
848 memset(&wrqu, 0, sizeof(wrqu));
849 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800850 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200851 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200852 }
853
854 if (resp_ie) {
855 memset(&wrqu, 0, sizeof(wrqu));
856 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200857 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
858 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200859 }
860
861 memset(&wrqu, 0, sizeof(wrqu));
862 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530863 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
864 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200865 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200866 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200867#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530868
869 return;
870out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100871 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200872}
Johannes Berg667503dd2009-07-07 03:56:11 +0200873
Jouni Malinened9d0102011-05-16 19:40:15 +0300874void cfg80211_roamed(struct net_device *dev,
875 struct ieee80211_channel *channel,
876 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200877 const u8 *req_ie, size_t req_ie_len,
878 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
879{
880 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530881 struct cfg80211_bss *bss;
882
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530883 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200884 wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200885 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530886 if (WARN_ON(!bss))
887 return;
888
889 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
890 resp_ie_len, gfp);
891}
892EXPORT_SYMBOL(cfg80211_roamed);
893
Ben Greear0e3a39b2013-06-19 14:06:27 -0700894/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530895void cfg80211_roamed_bss(struct net_device *dev,
896 struct cfg80211_bss *bss, const u8 *req_ie,
897 size_t req_ie_len, const u8 *resp_ie,
898 size_t resp_ie_len, gfp_t gfp)
899{
900 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800901 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200902 struct cfg80211_event *ev;
903 unsigned long flags;
904
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530905 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200906 return;
907
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530908 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
909 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100910 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530911 return;
912 }
913
Johannes Berg667503dd2009-07-07 03:56:11 +0200914 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200915 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
916 ev->rm.req_ie_len = req_ie_len;
917 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
918 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
919 ev->rm.resp_ie_len = resp_ie_len;
920 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530921 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200922
923 spin_lock_irqsave(&wdev->event_lock, flags);
924 list_add_tail(&ev->list, &wdev->event_list);
925 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100926 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200927}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530928EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200929
Johannes Berg667503dd2009-07-07 03:56:11 +0200930void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200931 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200932{
933 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800934 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200935 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200936#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200937 union iwreq_data wrqu;
938#endif
939
Johannes Berg667503dd2009-07-07 03:56:11 +0200940 ASSERT_WDEV_LOCK(wdev);
941
Johannes Berg074ac8d2010-09-16 14:58:22 +0200942 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
943 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200944 return;
945
Samuel Ortizb23aa672009-07-01 21:26:54 +0200946 if (wdev->current_bss) {
947 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100948 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200949 }
950
951 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200952 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953
Johannes Bergfffd0932009-07-08 14:22:54 +0200954 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
955
Arend van Sprielb8607152016-02-15 14:35:53 +0100956 /* stop critical protocol if supported */
957 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
958 rdev->crit_proto_nlportid = 0;
959 rdev_crit_proto_stop(rdev, wdev);
960 }
961
Johannes Bergfffd0932009-07-08 14:22:54 +0200962 /*
963 * Delete all the keys ... pairwise keys can't really
964 * exist any more anyway, but default keys might.
965 */
966 if (rdev->ops->del_key)
967 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300968 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200969
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800970 rdev_set_qos_map(rdev, dev, NULL);
971
Johannes Berg3d23e342009-09-29 23:27:28 +0200972#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973 memset(&wrqu, 0, sizeof(wrqu));
974 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
975 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800976 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200977#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500978
979 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980}
981
982void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +0200983 const u8 *ie, size_t ie_len,
984 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200985{
Johannes Berg667503dd2009-07-07 03:56:11 +0200986 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800987 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200988 struct cfg80211_event *ev;
989 unsigned long flags;
990
991 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
992 if (!ev)
993 return;
994
995 ev->type = EVENT_DISCONNECTED;
996 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
997 ev->dc.ie_len = ie_len;
998 memcpy((void *)ev->dc.ie, ie, ie_len);
999 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +02001000 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +02001001
1002 spin_lock_irqsave(&wdev->event_lock, flags);
1003 list_add_tail(&ev->list, &wdev->event_list);
1004 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001005 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001006}
1007EXPORT_SYMBOL(cfg80211_disconnected);
1008
Johannes Bergceca7b72013-05-16 00:55:45 +02001009/*
1010 * API calls for nl80211/wext compatibility code
1011 */
Johannes Berg83739b02013-05-15 17:44:01 +02001012int cfg80211_connect(struct cfg80211_registered_device *rdev,
1013 struct net_device *dev,
1014 struct cfg80211_connect_params *connect,
1015 struct cfg80211_cached_keys *connkeys,
1016 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001017{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001018 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +02001019 int err;
1020
1021 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001022
Johannes Bergbb46f792017-10-17 21:56:20 +02001023 /*
1024 * If we have an ssid_len, we're trying to connect or are
1025 * already connected, so reject a new SSID unless it's the
1026 * same (which is the case for re-association.)
1027 */
1028 if (wdev->ssid_len &&
1029 (wdev->ssid_len != connect->ssid_len ||
1030 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1031 return -EALREADY;
1032
1033 /*
1034 * If connected, reject (re-)association unless prev_bssid
1035 * matches the current BSSID.
1036 */
1037 if (wdev->current_bss) {
1038 if (!prev_bssid)
1039 return -EALREADY;
1040 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1041 return -ENOTCONN;
Johannes Bergfffd0932009-07-08 14:22:54 +02001042 }
1043
Johannes Bergbb46f792017-10-17 21:56:20 +02001044 /*
1045 * Reject if we're in the process of connecting with WEP,
1046 * this case isn't very interesting and trying to handle
1047 * it would make the code much more complex.
1048 */
1049 if (wdev->connect_keys)
1050 return -EINPROGRESS;
1051
Ben Greear7e7c8922011-11-18 11:31:59 -08001052 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1053 rdev->wiphy.ht_capa_mod_mask);
1054
Johannes Bergfffd0932009-07-08 14:22:54 +02001055 if (connkeys && connkeys->def >= 0) {
1056 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001057 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001058
1059 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001060 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001061 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001062 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1063 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001064 connect->key_idx = idx;
1065 connect->key = connkeys->params[idx].key;
1066 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001067
1068 /*
1069 * If ciphers are not set (e.g. when going through
1070 * iwconfig), we have to set them appropriately here.
1071 */
1072 if (connect->crypto.cipher_group == 0)
1073 connect->crypto.cipher_group = cipher;
1074
1075 if (connect->crypto.n_ciphers_pairwise == 0) {
1076 connect->crypto.n_ciphers_pairwise = 1;
1077 connect->crypto.ciphers_pairwise[0] = cipher;
1078 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001079 }
David Spinadelb8676222016-09-22 23:16:50 +03001080
1081 connect->crypto.wep_keys = connkeys->params;
1082 connect->crypto.wep_tx_key = connkeys->def;
Johannes Bergf1c1f172016-09-13 17:08:23 +02001083 } else {
1084 if (WARN_ON(connkeys))
1085 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001086 }
1087
Johannes Bergceca7b72013-05-16 00:55:45 +02001088 wdev->connect_keys = connkeys;
1089 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1090 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001091
Lior David34d50512016-01-28 10:58:25 +02001092 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1093 IEEE80211_BSS_TYPE_ESS;
1094
Johannes Bergceca7b72013-05-16 00:55:45 +02001095 if (!rdev->ops->connect)
1096 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1097 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001098 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001099
Johannes Bergceca7b72013-05-16 00:55:45 +02001100 if (err) {
1101 wdev->connect_keys = NULL;
Johannes Bergbb46f792017-10-17 21:56:20 +02001102 /*
1103 * This could be reassoc getting refused, don't clear
1104 * ssid_len in that case.
1105 */
1106 if (!wdev->current_bss)
1107 wdev->ssid_len = 0;
Johannes Bergceca7b72013-05-16 00:55:45 +02001108 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001109 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001110
1111 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001112}
1113
Johannes Berg83739b02013-05-15 17:44:01 +02001114int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1115 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001116{
Johannes Berg6829c872009-07-02 09:13:27 +02001117 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001118 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001119
Johannes Berg667503dd2009-07-07 03:56:11 +02001120 ASSERT_WDEV_LOCK(wdev);
1121
Johannes Bergb47f6102014-09-10 13:39:54 +03001122 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001123 wdev->connect_keys = NULL;
1124
Johannes Bergdee8a972013-08-13 09:23:57 +02001125 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001126 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001127 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001128 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001129 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001130 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001131
Johannes Bergbb46f792017-10-17 21:56:20 +02001132 /*
1133 * Clear ssid_len unless we actually were fully connected,
1134 * in which case cfg80211_disconnected() will take care of
1135 * this later.
1136 */
1137 if (!wdev->current_bss)
1138 wdev->ssid_len = 0;
1139
Johannes Bergceca7b72013-05-16 00:55:45 +02001140 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001141}