blob: 35cc1de85dcc53ea18817a5a23a4745718277910 [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) {
508 if (!prev_bssid)
509 return -EALREADY;
510 if (prev_bssid &&
511 !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
512 return -ENOTCONN;
513 cfg80211_unhold_bss(wdev->current_bss);
514 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
515 wdev->current_bss = NULL;
516
517 cfg80211_sme_free(wdev);
518 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200519
520 if (WARN_ON(wdev->conn))
521 return -EINPROGRESS;
522
523 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
524 if (!wdev->conn)
525 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200526
527 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200528 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200529 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200530 memcpy(&wdev->conn->params, connect, sizeof(*connect));
531 if (connect->bssid) {
532 wdev->conn->params.bssid = wdev->conn->bssid;
533 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
534 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200535
Johannes Berg46b9d182015-03-31 16:09:13 +0200536 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
537 &wdev->conn->ie,
538 &wdev->conn->params.ie_len)) {
539 kfree(wdev->conn);
540 wdev->conn = NULL;
541 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200542 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200543 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200544
545 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
546 wdev->conn->auto_auth = true;
547 /* start with open system ... should mostly work */
548 wdev->conn->params.auth_type =
549 NL80211_AUTHTYPE_OPEN_SYSTEM;
550 } else {
551 wdev->conn->auto_auth = false;
552 }
553
554 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800555 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200556
557 /* see if we have the bss already */
558 bss = cfg80211_get_conn_bss(wdev);
559
560 if (prev_bssid) {
561 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
562 wdev->conn->prev_bssid_valid = true;
563 }
564
565 /* we're good if we have a matching bss struct */
566 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200567 err = cfg80211_conn_do_work(wdev);
568 cfg80211_put_bss(wdev->wiphy, bss);
569 } else {
570 /* otherwise we'll need to scan for the AP first */
571 err = cfg80211_conn_scan(wdev);
572
573 /*
574 * If we can't scan right now, then we need to scan again
575 * after the current scan finished, since the parameters
576 * changed (unless we find a good AP anyway).
577 */
578 if (err == -EBUSY) {
579 err = 0;
580 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
581 }
582 }
583
584 if (err)
585 cfg80211_sme_free(wdev);
586
587 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200588}
589
Johannes Bergceca7b72013-05-16 00:55:45 +0200590static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900591{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800592 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200593 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900594
Johannes Bergceca7b72013-05-16 00:55:45 +0200595 if (!wdev->conn)
596 return 0;
597
598 if (!rdev->ops->deauth)
599 return -EOPNOTSUPP;
600
601 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
602 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
603 err = 0;
604 goto out;
605 }
606
607 /* wdev->conn->params.bssid must be set if > SCANNING */
608 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
609 wdev->conn->params.bssid,
610 NULL, 0, reason, false);
611 out:
612 cfg80211_sme_free(wdev);
613 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900614}
615
Johannes Bergceca7b72013-05-16 00:55:45 +0200616/*
617 * code shared for in-device and software SME
618 */
619
620static bool cfg80211_is_all_idle(void)
621{
622 struct cfg80211_registered_device *rdev;
623 struct wireless_dev *wdev;
624 bool is_all_idle = true;
625
626 /*
627 * All devices must be idle as otherwise if you are actively
628 * scanning some new beacon hints could be learned and would
629 * count as new regulatory hints.
630 */
631 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300632 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200633 wdev_lock(wdev);
634 if (wdev->conn || wdev->current_bss)
635 is_all_idle = false;
636 wdev_unlock(wdev);
637 }
638 }
639
640 return is_all_idle;
641}
642
643static void disconnect_work(struct work_struct *work)
644{
645 rtnl_lock();
646 if (cfg80211_is_all_idle())
647 regulatory_hint_disconnect();
648 rtnl_unlock();
649}
650
651static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
652
653
654/*
655 * API calls for drivers implementing connect/disconnect and
656 * SME event handling
657 */
658
Ben Greear6f390902013-06-19 14:06:25 -0700659/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200660void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
661 const u8 *req_ie, size_t req_ie_len,
662 const u8 *resp_ie, size_t resp_ie_len,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300663 int status, bool wextev,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200664 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200665{
666 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100667 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200668#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200669 union iwreq_data wrqu;
670#endif
671
Johannes Berg667503dd2009-07-07 03:56:11 +0200672 ASSERT_WDEV_LOCK(wdev);
673
Johannes Berg074ac8d2010-09-16 14:58:22 +0200674 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700675 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
676 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200677 return;
Ben Greear6f390902013-06-19 14:06:25 -0700678 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200679
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800680 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200681 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200682 resp_ie, resp_ie_len,
683 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200684
Johannes Berg3d23e342009-09-29 23:27:28 +0200685#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200686 if (wextev) {
687 if (req_ie && status == WLAN_STATUS_SUCCESS) {
688 memset(&wrqu, 0, sizeof(wrqu));
689 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800690 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200691 }
692
693 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
694 memset(&wrqu, 0, sizeof(wrqu));
695 wrqu.data.length = resp_ie_len;
696 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
697 }
698
699 memset(&wrqu, 0, sizeof(wrqu));
700 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200701 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200702 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200703 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
704 wdev->wext.prev_bssid_valid = true;
705 }
Johannes Berge45cd822009-07-02 09:58:04 +0200706 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
707 }
708#endif
709
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530710 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800711 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530712 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
713 wdev->ssid, wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200714 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200715 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530716 if (bss)
717 cfg80211_hold_bss(bss_from_pub(bss));
718 }
719
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200720 if (wdev->current_bss) {
721 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100722 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200723 wdev->current_bss = NULL;
724 }
725
Johannes Bergfffd0932009-07-08 14:22:54 +0200726 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300727 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200728 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200729 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200730 if (bss) {
731 cfg80211_unhold_bss(bss_from_pub(bss));
732 cfg80211_put_bss(wdev->wiphy, bss);
733 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300734 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200735 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200737
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530738 if (WARN_ON(!bss))
739 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200740
Johannes Bergfffd0932009-07-08 14:22:54 +0200741 wdev->current_bss = bss_from_pub(bss);
742
David Spinadelb8676222016-09-22 23:16:50 +0300743 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
744 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700745
Johannes Berg9caf0362012-11-29 01:25:20 +0100746 rcu_read_lock();
747 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
748 if (!country_ie) {
749 rcu_read_unlock();
750 return;
751 }
752
753 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
754 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700755
756 if (!country_ie)
757 return;
758
759 /*
760 * ieee80211_bss_get_ie() ensures we can access:
761 * - country_ie + 2, the start of the country ie data, and
762 * - and country_ie[1] which is the IE length
763 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700764 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
765 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100766 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200767}
Johannes Bergf2129352009-07-01 21:26:56 +0200768
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530769/* Consumes bss object one way or another */
770void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
771 struct cfg80211_bss *bss, const u8 *req_ie,
772 size_t req_ie_len, const u8 *resp_ie,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300773 size_t resp_ie_len, int status, gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200774{
Johannes Berg667503dd2009-07-07 03:56:11 +0200775 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800776 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200777 struct cfg80211_event *ev;
778 unsigned long flags;
779
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530780 if (bss) {
781 /* Make sure the bss entry provided by the driver is valid. */
782 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
783
784 if (WARN_ON(list_empty(&ibss->list))) {
785 cfg80211_put_bss(wdev->wiphy, bss);
786 return;
787 }
788 }
789
Johannes Berg667503dd2009-07-07 03:56:11 +0200790 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530791 if (!ev) {
792 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg667503dd2009-07-07 03:56:11 +0200793 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530794 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200795
796 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800797 if (bssid)
798 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700799 if (req_ie_len) {
800 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
801 ev->cr.req_ie_len = req_ie_len;
802 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
803 }
804 if (resp_ie_len) {
805 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
806 ev->cr.resp_ie_len = resp_ie_len;
807 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
808 }
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530809 if (bss)
810 cfg80211_hold_bss(bss_from_pub(bss));
811 ev->cr.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200812 ev->cr.status = status;
813
814 spin_lock_irqsave(&wdev->event_lock, flags);
815 list_add_tail(&ev->list, &wdev->event_list);
816 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100817 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200818}
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530819EXPORT_SYMBOL(cfg80211_connect_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200820
Ben Greear0e3a39b2013-06-19 14:06:27 -0700821/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300822void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530823 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200824 const u8 *req_ie, size_t req_ie_len,
825 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200826{
Johannes Berg3d23e342009-09-29 23:27:28 +0200827#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200828 union iwreq_data wrqu;
829#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200830 ASSERT_WDEV_LOCK(wdev);
831
Johannes Berg074ac8d2010-09-16 14:58:22 +0200832 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
833 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530834 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200835
Johannes Bergceca7b72013-05-16 00:55:45 +0200836 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530837 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838
Samuel Ortizb23aa672009-07-01 21:26:54 +0200839 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100840 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200841 wdev->current_bss = NULL;
842
Johannes Berg19957bb2009-07-02 17:20:43 +0200843 cfg80211_hold_bss(bss_from_pub(bss));
844 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200845
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800846 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
847 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200848 req_ie, req_ie_len, resp_ie, resp_ie_len,
849 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200850
Johannes Berg3d23e342009-09-29 23:27:28 +0200851#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200852 if (req_ie) {
853 memset(&wrqu, 0, sizeof(wrqu));
854 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800855 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200856 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200857 }
858
859 if (resp_ie) {
860 memset(&wrqu, 0, sizeof(wrqu));
861 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200862 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
863 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200864 }
865
866 memset(&wrqu, 0, sizeof(wrqu));
867 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530868 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
869 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200870 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200871 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200872#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530873
874 return;
875out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100876 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200877}
Johannes Berg667503dd2009-07-07 03:56:11 +0200878
Jouni Malinened9d0102011-05-16 19:40:15 +0300879void cfg80211_roamed(struct net_device *dev,
880 struct ieee80211_channel *channel,
881 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200882 const u8 *req_ie, size_t req_ie_len,
883 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
884{
885 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530886 struct cfg80211_bss *bss;
887
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530888 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200889 wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200890 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530891 if (WARN_ON(!bss))
892 return;
893
894 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
895 resp_ie_len, gfp);
896}
897EXPORT_SYMBOL(cfg80211_roamed);
898
Ben Greear0e3a39b2013-06-19 14:06:27 -0700899/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530900void cfg80211_roamed_bss(struct net_device *dev,
901 struct cfg80211_bss *bss, const u8 *req_ie,
902 size_t req_ie_len, const u8 *resp_ie,
903 size_t resp_ie_len, gfp_t gfp)
904{
905 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800906 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200907 struct cfg80211_event *ev;
908 unsigned long flags;
909
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530910 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200911 return;
912
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530913 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
914 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100915 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530916 return;
917 }
918
Johannes Berg667503dd2009-07-07 03:56:11 +0200919 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200920 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
921 ev->rm.req_ie_len = req_ie_len;
922 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
923 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
924 ev->rm.resp_ie_len = resp_ie_len;
925 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530926 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200927
928 spin_lock_irqsave(&wdev->event_lock, flags);
929 list_add_tail(&ev->list, &wdev->event_list);
930 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100931 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200932}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530933EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200934
Johannes Berg667503dd2009-07-07 03:56:11 +0200935void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200936 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200937{
938 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800939 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200940 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200941#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200942 union iwreq_data wrqu;
943#endif
944
Johannes Berg667503dd2009-07-07 03:56:11 +0200945 ASSERT_WDEV_LOCK(wdev);
946
Johannes Berg074ac8d2010-09-16 14:58:22 +0200947 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
948 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200949 return;
950
Samuel Ortizb23aa672009-07-01 21:26:54 +0200951 if (wdev->current_bss) {
952 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100953 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200954 }
955
956 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200957 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200958
Johannes Bergfffd0932009-07-08 14:22:54 +0200959 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
960
Arend van Sprielb8607152016-02-15 14:35:53 +0100961 /* stop critical protocol if supported */
962 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
963 rdev->crit_proto_nlportid = 0;
964 rdev_crit_proto_stop(rdev, wdev);
965 }
966
Johannes Bergfffd0932009-07-08 14:22:54 +0200967 /*
968 * Delete all the keys ... pairwise keys can't really
969 * exist any more anyway, but default keys might.
970 */
971 if (rdev->ops->del_key)
972 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300973 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200974
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800975 rdev_set_qos_map(rdev, dev, NULL);
976
Johannes Berg3d23e342009-09-29 23:27:28 +0200977#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200978 memset(&wrqu, 0, sizeof(wrqu));
979 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
980 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800981 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200982#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500983
984 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200985}
986
987void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +0200988 const u8 *ie, size_t ie_len,
989 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200990{
Johannes Berg667503dd2009-07-07 03:56:11 +0200991 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800992 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200993 struct cfg80211_event *ev;
994 unsigned long flags;
995
996 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
997 if (!ev)
998 return;
999
1000 ev->type = EVENT_DISCONNECTED;
1001 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1002 ev->dc.ie_len = ie_len;
1003 memcpy((void *)ev->dc.ie, ie, ie_len);
1004 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +02001005 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +02001006
1007 spin_lock_irqsave(&wdev->event_lock, flags);
1008 list_add_tail(&ev->list, &wdev->event_list);
1009 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001010 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001011}
1012EXPORT_SYMBOL(cfg80211_disconnected);
1013
Johannes Bergceca7b72013-05-16 00:55:45 +02001014/*
1015 * API calls for nl80211/wext compatibility code
1016 */
Johannes Berg83739b02013-05-15 17:44:01 +02001017int cfg80211_connect(struct cfg80211_registered_device *rdev,
1018 struct net_device *dev,
1019 struct cfg80211_connect_params *connect,
1020 struct cfg80211_cached_keys *connkeys,
1021 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001022{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001023 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +02001024 int err;
1025
1026 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001027
Johannes Bergfffd0932009-07-08 14:22:54 +02001028 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +03001029 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001030 wdev->connect_keys = NULL;
1031 }
1032
Ben Greear7e7c8922011-11-18 11:31:59 -08001033 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1034 rdev->wiphy.ht_capa_mod_mask);
1035
Johannes Bergfffd0932009-07-08 14:22:54 +02001036 if (connkeys && connkeys->def >= 0) {
1037 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001038 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001039
1040 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001041 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001042 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001043 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1044 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001045 connect->key_idx = idx;
1046 connect->key = connkeys->params[idx].key;
1047 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001048
1049 /*
1050 * If ciphers are not set (e.g. when going through
1051 * iwconfig), we have to set them appropriately here.
1052 */
1053 if (connect->crypto.cipher_group == 0)
1054 connect->crypto.cipher_group = cipher;
1055
1056 if (connect->crypto.n_ciphers_pairwise == 0) {
1057 connect->crypto.n_ciphers_pairwise = 1;
1058 connect->crypto.ciphers_pairwise[0] = cipher;
1059 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001060 }
David Spinadelb8676222016-09-22 23:16:50 +03001061
1062 connect->crypto.wep_keys = connkeys->params;
1063 connect->crypto.wep_tx_key = connkeys->def;
Johannes Bergf1c1f172016-09-13 17:08:23 +02001064 } else {
1065 if (WARN_ON(connkeys))
1066 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001067 }
1068
Johannes Bergceca7b72013-05-16 00:55:45 +02001069 wdev->connect_keys = connkeys;
1070 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1071 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001072
Lior David34d50512016-01-28 10:58:25 +02001073 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1074 IEEE80211_BSS_TYPE_ESS;
1075
Johannes Bergceca7b72013-05-16 00:55:45 +02001076 if (!rdev->ops->connect)
1077 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1078 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001079 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001080
Johannes Bergceca7b72013-05-16 00:55:45 +02001081 if (err) {
1082 wdev->connect_keys = NULL;
1083 wdev->ssid_len = 0;
1084 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001085 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001086
1087 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001088}
1089
Johannes Berg83739b02013-05-15 17:44:01 +02001090int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1091 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001092{
Johannes Berg6829c872009-07-02 09:13:27 +02001093 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001094 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001095
Johannes Berg667503dd2009-07-07 03:56:11 +02001096 ASSERT_WDEV_LOCK(wdev);
1097
Johannes Bergb47f6102014-09-10 13:39:54 +03001098 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001099 wdev->connect_keys = NULL;
1100
Johannes Bergdee8a972013-08-13 09:23:57 +02001101 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001102 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001103 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001104 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001105 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001106 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001107
Johannes Bergceca7b72013-05-16 00:55:45 +02001108 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001109}