blob: 0ab3711c79a01ae98cdaaafa64bf12ac94e80271 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
Johannes Bergceca7b72013-05-16 00:55:45 +02002 * SME code for cfg80211
3 * both driver SME event handling and the SME implementation
4 * (for nl80211's connect() and wext)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005 *
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020013#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020016#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020017#include <net/cfg80211.h>
18#include <net/rtnetlink.h>
19#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070020#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030021#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020022
Johannes Bergceca7b72013-05-16 00:55:45 +020023/*
24 * Software SME in cfg80211, using auth/assoc/deauth calls to the
25 * driver. This is is for implementing nl80211's connect/disconnect
26 * and wireless extensions (if configured.)
27 */
28
Johannes Berg6829c872009-07-02 09:13:27 +020029struct cfg80211_conn {
30 struct cfg80211_connect_params params;
31 /* these are sub-states of the _CONNECTING sme_state */
32 enum {
Johannes Berg6829c872009-07-02 09:13:27 +020033 CFG80211_CONN_SCANNING,
34 CFG80211_CONN_SCAN_AGAIN,
35 CFG80211_CONN_AUTHENTICATE_NEXT,
36 CFG80211_CONN_AUTHENTICATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020037 CFG80211_CONN_AUTH_FAILED,
Johannes Berg6829c872009-07-02 09:13:27 +020038 CFG80211_CONN_ASSOCIATE_NEXT,
39 CFG80211_CONN_ASSOCIATING,
Johannes Berg923a0e72013-06-28 11:38:54 +020040 CFG80211_CONN_ASSOC_FAILED,
Johannes Bergceca7b72013-05-16 00:55:45 +020041 CFG80211_CONN_DEAUTH,
42 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020043 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020044 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020045 u8 *ie;
46 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020047 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020048};
49
Johannes Bergceca7b72013-05-16 00:55:45 +020050static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050051{
Johannes Bergceca7b72013-05-16 00:55:45 +020052 if (!wdev->conn)
53 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050054
Johannes Bergceca7b72013-05-16 00:55:45 +020055 kfree(wdev->conn->ie);
56 kfree(wdev->conn);
57 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050058}
59
Johannes Berg6829c872009-07-02 09:13:27 +020060static int cfg80211_conn_scan(struct wireless_dev *wdev)
61{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080062 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020063 struct cfg80211_scan_request *request;
64 int n_channels, err;
65
66 ASSERT_RTNL();
Johannes Berg667503dd2009-07-07 03:56:11 +020067 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020068
Johannes Bergf9d15d12014-01-22 11:14:19 +020069 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020070 return -EBUSY;
71
Ilan Peerbdfbec22014-01-09 11:37:23 +020072 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020073 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020074 else
75 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020076
Johannes Berg6829c872009-07-02 09:13:27 +020077 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 sizeof(request->channels[0]) * n_channels,
79 GFP_KERNEL);
80 if (!request)
81 return -ENOMEM;
82
Karl Beldan2a84ee82014-10-07 11:42:18 +020083 if (wdev->conn->params.channel) {
84 enum ieee80211_band band = wdev->conn->params.channel->band;
85 struct ieee80211_supported_band *sband =
86 wdev->wiphy->bands[band];
87
88 if (!sband) {
89 kfree(request);
90 return -EINVAL;
91 }
Johannes Berg6829c872009-07-02 09:13:27 +020092 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +020093 request->rates[band] = (1 << sband->n_bitrates) - 1;
94 } else {
Johannes Berg6829c872009-07-02 09:13:27 +020095 int i = 0, j;
96 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053097 struct ieee80211_supported_band *bands;
98 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020099
100 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530101 bands = wdev->wiphy->bands[band];
102 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200103 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530104 for (j = 0; j < bands->n_channels; j++) {
105 channel = &bands->channels[j];
106 if (channel->flags & IEEE80211_CHAN_DISABLED)
107 continue;
108 request->channels[i++] = channel;
109 }
110 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200111 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530112 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200113 }
114 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200115 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200116 request->n_ssids = 1;
117
118 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
119 wdev->conn->params.ssid_len);
120 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
121
Johannes Bergfd014282012-06-18 19:17:03 +0200122 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200123 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700124 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200125
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200127
Hila Gonene35e4d22012-06-27 17:19:42 +0300128 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200129 if (!err) {
130 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200131 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200132 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200133 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200134 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200135 kfree(request);
136 }
137 return err;
138}
139
140static int cfg80211_conn_do_work(struct wireless_dev *wdev)
141{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800142 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200143 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100144 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200145 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200146
Johannes Berg667503dd2009-07-07 03:56:11 +0200147 ASSERT_WDEV_LOCK(wdev);
148
Johannes Berg6829c872009-07-02 09:13:27 +0200149 if (!wdev->conn)
150 return 0;
151
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 params = &wdev->conn->params;
153
Johannes Berg6829c872009-07-02 09:13:27 +0200154 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200155 case CFG80211_CONN_SCANNING:
156 /* didn't find it during scan ... */
157 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200158 case CFG80211_CONN_SCAN_AGAIN:
159 return cfg80211_conn_scan(wdev);
160 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200161 if (WARN_ON(!rdev->ops->auth))
162 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200164 return cfg80211_mlme_auth(rdev, wdev->netdev,
165 params->channel, params->auth_type,
166 params->bssid,
167 params->ssid, params->ssid_len,
168 NULL, 0,
169 params->key, params->key_len,
170 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200171 case CFG80211_CONN_AUTH_FAILED:
172 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200173 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200174 if (WARN_ON(!rdev->ops->assoc))
175 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200176 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200177 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100178 req.prev_bssid = wdev->conn->prev_bssid;
179 req.ie = params->ie;
180 req.ie_len = params->ie_len;
181 req.use_mfp = params->mfp != NL80211_MFP_NO;
182 req.crypto = params->crypto;
183 req.flags = params->flags;
184 req.ht_capa = params->ht_capa;
185 req.ht_capa_mask = params->ht_capa_mask;
186 req.vht_capa = params->vht_capa;
187 req.vht_capa_mask = params->vht_capa_mask;
188
Johannes Berg91bf9b22013-05-15 17:44:01 +0200189 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
190 params->bssid, params->ssid,
191 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200192 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200193 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
195 WLAN_REASON_DEAUTH_LEAVING,
196 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200197 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200198 case CFG80211_CONN_ASSOC_FAILED:
199 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
200 NULL, 0,
201 WLAN_REASON_DEAUTH_LEAVING, false);
202 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200203 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200204 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
205 NULL, 0,
206 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200207 /* free directly, disconnected event already sent */
208 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200209 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200210 default:
211 return 0;
212 }
213}
214
215void cfg80211_conn_work(struct work_struct *work)
216{
Johannes Berg79c97e92009-07-07 03:56:12 +0200217 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200218 container_of(work, struct cfg80211_registered_device, conn_work);
219 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100220 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200221
222 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200223
Johannes Berg89a54e42012-06-15 14:33:17 +0200224 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200225 if (!wdev->netdev)
226 continue;
227
Johannes Berg667503dd2009-07-07 03:56:11 +0200228 wdev_lock(wdev);
229 if (!netif_running(wdev->netdev)) {
230 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200231 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200233 if (!wdev->conn ||
234 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200235 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200236 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200237 }
Johannes Berg7400f422009-10-31 07:40:37 +0100238 if (wdev->conn->params.bssid) {
239 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
240 bssid = bssid_buf;
241 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200242 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200243 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900244 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200245 NULL, 0, NULL, 0,
246 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200247 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200248 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200249 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200250 }
251
Johannes Berg6829c872009-07-02 09:13:27 +0200252 rtnl_unlock();
253}
254
Ben Greear0e3a39b2013-06-19 14:06:27 -0700255/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700256static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200257{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800258 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 struct cfg80211_bss *bss;
260 u16 capa = WLAN_CAPABILITY_ESS;
261
Johannes Berg667503dd2009-07-07 03:56:11 +0200262 ASSERT_WDEV_LOCK(wdev);
263
Johannes Berg6829c872009-07-02 09:13:27 +0200264 if (wdev->conn->params.privacy)
265 capa |= WLAN_CAPABILITY_PRIVACY;
266
Jouni Malinened9d0102011-05-16 19:40:15 +0300267 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
268 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200269 wdev->conn->params.ssid,
270 wdev->conn->params.ssid_len,
271 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
272 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200273 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700274 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200275
276 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
277 wdev->conn->params.bssid = wdev->conn->bssid;
278 wdev->conn->params.channel = bss->channel;
279 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200280 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200281
Johannes Bergbbac31f2009-09-16 09:04:26 -0700282 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200283}
284
Johannes Berg667503dd2009-07-07 03:56:11 +0200285static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200286{
287 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800288 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700289 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200290
Johannes Berg667503dd2009-07-07 03:56:11 +0200291 ASSERT_WDEV_LOCK(wdev);
292
Zhu Yid4b1a682009-07-16 17:34:14 +0800293 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200294 return;
295
296 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
297 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
298 return;
299
Johannes Bergbbac31f2009-09-16 09:04:26 -0700300 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200301 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100302 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200303 else
304 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200305}
306
Johannes Berg667503dd2009-07-07 03:56:11 +0200307void cfg80211_sme_scan_done(struct net_device *dev)
308{
309 struct wireless_dev *wdev = dev->ieee80211_ptr;
310
311 wdev_lock(wdev);
312 __cfg80211_sme_scan_done(dev);
313 wdev_unlock(wdev);
314}
315
Johannes Bergceca7b72013-05-16 00:55:45 +0200316void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200317{
Johannes Berg6829c872009-07-02 09:13:27 +0200318 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800319 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200320 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
321 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
322
Johannes Berg667503dd2009-07-07 03:56:11 +0200323 ASSERT_WDEV_LOCK(wdev);
324
Johannes Bergceca7b72013-05-16 00:55:45 +0200325 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200326 return;
327
328 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
329 wdev->conn->auto_auth &&
330 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
331 /* select automatically between only open, shared, leap */
332 switch (wdev->conn->params.auth_type) {
333 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200334 if (wdev->connect_keys)
335 wdev->conn->params.auth_type =
336 NL80211_AUTHTYPE_SHARED_KEY;
337 else
338 wdev->conn->params.auth_type =
339 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200340 break;
341 case NL80211_AUTHTYPE_SHARED_KEY:
342 wdev->conn->params.auth_type =
343 NL80211_AUTHTYPE_NETWORK_EAP;
344 break;
345 default:
346 /* huh? */
347 wdev->conn->params.auth_type =
348 NL80211_AUTHTYPE_OPEN_SYSTEM;
349 break;
350 }
351 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
352 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200353 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200354 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
355 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200356 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200357 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200358 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
359 schedule_work(&rdev->conn_work);
360 }
361}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200362
Johannes Bergceca7b72013-05-16 00:55:45 +0200363bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200364{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800365 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200366
Johannes Bergceca7b72013-05-16 00:55:45 +0200367 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200368 return false;
369
Johannes Bergceca7b72013-05-16 00:55:45 +0200370 if (status == WLAN_STATUS_SUCCESS) {
371 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200372 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200373 }
374
375 if (wdev->conn->prev_bssid_valid) {
376 /*
377 * Some stupid APs don't accept reassoc, so we
378 * need to fall back to trying regular assoc;
379 * return true so no event is sent to userspace.
380 */
381 wdev->conn->prev_bssid_valid = false;
382 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
383 schedule_work(&rdev->conn_work);
384 return true;
385 }
386
Johannes Berg923a0e72013-06-28 11:38:54 +0200387 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200388 schedule_work(&rdev->conn_work);
389 return false;
390}
391
392void cfg80211_sme_deauth(struct wireless_dev *wdev)
393{
394 cfg80211_sme_free(wdev);
395}
396
397void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
398{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800399 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200400
401 if (!wdev->conn)
402 return;
403
404 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
405 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200406}
407
408void cfg80211_sme_disassoc(struct wireless_dev *wdev)
409{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800410 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200411
412 if (!wdev->conn)
413 return;
414
415 wdev->conn->state = CFG80211_CONN_DEAUTH;
416 schedule_work(&rdev->conn_work);
417}
418
419void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
420{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800421 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200422
423 if (!wdev->conn)
424 return;
425
426 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
427 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200428}
429
430static int cfg80211_sme_connect(struct wireless_dev *wdev,
431 struct cfg80211_connect_params *connect,
432 const u8 *prev_bssid)
433{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800434 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200435 struct cfg80211_bss *bss;
436 int err;
437
438 if (!rdev->ops->auth || !rdev->ops->assoc)
439 return -EOPNOTSUPP;
440
441 if (wdev->current_bss)
442 return -EALREADY;
443
444 if (WARN_ON(wdev->conn))
445 return -EINPROGRESS;
446
447 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
448 if (!wdev->conn)
449 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200450
451 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200452 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200453 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200454 memcpy(&wdev->conn->params, connect, sizeof(*connect));
455 if (connect->bssid) {
456 wdev->conn->params.bssid = wdev->conn->bssid;
457 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
458 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200459
Johannes Bergceca7b72013-05-16 00:55:45 +0200460 if (connect->ie) {
461 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
462 GFP_KERNEL);
463 wdev->conn->params.ie = wdev->conn->ie;
464 if (!wdev->conn->ie) {
465 kfree(wdev->conn);
466 wdev->conn = NULL;
467 return -ENOMEM;
468 }
469 }
470
471 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
472 wdev->conn->auto_auth = true;
473 /* start with open system ... should mostly work */
474 wdev->conn->params.auth_type =
475 NL80211_AUTHTYPE_OPEN_SYSTEM;
476 } else {
477 wdev->conn->auto_auth = false;
478 }
479
480 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800481 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200482
483 /* see if we have the bss already */
484 bss = cfg80211_get_conn_bss(wdev);
485
486 if (prev_bssid) {
487 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
488 wdev->conn->prev_bssid_valid = true;
489 }
490
491 /* we're good if we have a matching bss struct */
492 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200493 err = cfg80211_conn_do_work(wdev);
494 cfg80211_put_bss(wdev->wiphy, bss);
495 } else {
496 /* otherwise we'll need to scan for the AP first */
497 err = cfg80211_conn_scan(wdev);
498
499 /*
500 * If we can't scan right now, then we need to scan again
501 * after the current scan finished, since the parameters
502 * changed (unless we find a good AP anyway).
503 */
504 if (err == -EBUSY) {
505 err = 0;
506 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
507 }
508 }
509
510 if (err)
511 cfg80211_sme_free(wdev);
512
513 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200514}
515
Johannes Bergceca7b72013-05-16 00:55:45 +0200516static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900517{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800518 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200519 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900520
Johannes Bergceca7b72013-05-16 00:55:45 +0200521 if (!wdev->conn)
522 return 0;
523
524 if (!rdev->ops->deauth)
525 return -EOPNOTSUPP;
526
527 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
528 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
529 err = 0;
530 goto out;
531 }
532
533 /* wdev->conn->params.bssid must be set if > SCANNING */
534 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
535 wdev->conn->params.bssid,
536 NULL, 0, reason, false);
537 out:
538 cfg80211_sme_free(wdev);
539 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900540}
541
Johannes Bergceca7b72013-05-16 00:55:45 +0200542/*
543 * code shared for in-device and software SME
544 */
545
546static bool cfg80211_is_all_idle(void)
547{
548 struct cfg80211_registered_device *rdev;
549 struct wireless_dev *wdev;
550 bool is_all_idle = true;
551
552 /*
553 * All devices must be idle as otherwise if you are actively
554 * scanning some new beacon hints could be learned and would
555 * count as new regulatory hints.
556 */
557 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
558 list_for_each_entry(wdev, &rdev->wdev_list, list) {
559 wdev_lock(wdev);
560 if (wdev->conn || wdev->current_bss)
561 is_all_idle = false;
562 wdev_unlock(wdev);
563 }
564 }
565
566 return is_all_idle;
567}
568
569static void disconnect_work(struct work_struct *work)
570{
571 rtnl_lock();
572 if (cfg80211_is_all_idle())
573 regulatory_hint_disconnect();
574 rtnl_unlock();
575}
576
577static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
578
579
580/*
581 * API calls for drivers implementing connect/disconnect and
582 * SME event handling
583 */
584
Ben Greear6f390902013-06-19 14:06:25 -0700585/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200586void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
587 const u8 *req_ie, size_t req_ie_len,
588 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200589 u16 status, bool wextev,
590 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591{
592 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100593 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200594#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595 union iwreq_data wrqu;
596#endif
597
Johannes Berg667503dd2009-07-07 03:56:11 +0200598 ASSERT_WDEV_LOCK(wdev);
599
Johannes Berg074ac8d2010-09-16 14:58:22 +0200600 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700601 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
602 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200603 return;
Ben Greear6f390902013-06-19 14:06:25 -0700604 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200605
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800606 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200607 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200608 resp_ie, resp_ie_len,
609 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200610
Johannes Berg3d23e342009-09-29 23:27:28 +0200611#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200612 if (wextev) {
613 if (req_ie && status == WLAN_STATUS_SUCCESS) {
614 memset(&wrqu, 0, sizeof(wrqu));
615 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800616 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200617 }
618
619 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
620 memset(&wrqu, 0, sizeof(wrqu));
621 wrqu.data.length = resp_ie_len;
622 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
623 }
624
625 memset(&wrqu, 0, sizeof(wrqu));
626 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200627 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200628 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200629 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
630 wdev->wext.prev_bssid_valid = true;
631 }
Johannes Berge45cd822009-07-02 09:58:04 +0200632 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
633 }
634#endif
635
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530636 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800637 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530638 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
639 wdev->ssid, wdev->ssid_len,
640 WLAN_CAPABILITY_ESS,
641 WLAN_CAPABILITY_ESS);
642 if (bss)
643 cfg80211_hold_bss(bss_from_pub(bss));
644 }
645
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200646 if (wdev->current_bss) {
647 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100648 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200649 wdev->current_bss = NULL;
650 }
651
Johannes Bergfffd0932009-07-08 14:22:54 +0200652 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300653 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200654 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200655 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200656 if (bss) {
657 cfg80211_unhold_bss(bss_from_pub(bss));
658 cfg80211_put_bss(wdev->wiphy, bss);
659 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300660 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200661 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200662 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200663
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530664 if (WARN_ON(!bss))
665 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200666
Johannes Bergfffd0932009-07-08 14:22:54 +0200667 wdev->current_bss = bss_from_pub(bss);
668
Johannes Bergfffd0932009-07-08 14:22:54 +0200669 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700670
Johannes Berg9caf0362012-11-29 01:25:20 +0100671 rcu_read_lock();
672 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
673 if (!country_ie) {
674 rcu_read_unlock();
675 return;
676 }
677
678 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
679 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700680
681 if (!country_ie)
682 return;
683
684 /*
685 * ieee80211_bss_get_ie() ensures we can access:
686 * - country_ie + 2, the start of the country ie data, and
687 * - and country_ie[1] which is the IE length
688 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700689 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
690 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100691 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200692}
Johannes Bergf2129352009-07-01 21:26:56 +0200693
694void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
695 const u8 *req_ie, size_t req_ie_len,
696 const u8 *resp_ie, size_t resp_ie_len,
697 u16 status, gfp_t gfp)
698{
Johannes Berg667503dd2009-07-07 03:56:11 +0200699 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800700 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200701 struct cfg80211_event *ev;
702 unsigned long flags;
703
704 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
705 if (!ev)
706 return;
707
708 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800709 if (bssid)
710 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700711 if (req_ie_len) {
712 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
713 ev->cr.req_ie_len = req_ie_len;
714 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
715 }
716 if (resp_ie_len) {
717 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
718 ev->cr.resp_ie_len = resp_ie_len;
719 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
720 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200721 ev->cr.status = status;
722
723 spin_lock_irqsave(&wdev->event_lock, flags);
724 list_add_tail(&ev->list, &wdev->event_list);
725 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100726 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200727}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200728EXPORT_SYMBOL(cfg80211_connect_result);
729
Ben Greear0e3a39b2013-06-19 14:06:27 -0700730/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300731void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530732 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200733 const u8 *req_ie, size_t req_ie_len,
734 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200735{
Johannes Berg3d23e342009-09-29 23:27:28 +0200736#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200737 union iwreq_data wrqu;
738#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200739 ASSERT_WDEV_LOCK(wdev);
740
Johannes Berg074ac8d2010-09-16 14:58:22 +0200741 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
742 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530743 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744
Johannes Bergceca7b72013-05-16 00:55:45 +0200745 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530746 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200747
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100749 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200750 wdev->current_bss = NULL;
751
Johannes Berg19957bb2009-07-02 17:20:43 +0200752 cfg80211_hold_bss(bss_from_pub(bss));
753 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200754
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800755 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
756 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200757 req_ie, req_ie_len, resp_ie, resp_ie_len,
758 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200759
Johannes Berg3d23e342009-09-29 23:27:28 +0200760#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200761 if (req_ie) {
762 memset(&wrqu, 0, sizeof(wrqu));
763 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800764 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200765 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200766 }
767
768 if (resp_ie) {
769 memset(&wrqu, 0, sizeof(wrqu));
770 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200771 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
772 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773 }
774
775 memset(&wrqu, 0, sizeof(wrqu));
776 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530777 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
778 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200779 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200780 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200781#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530782
783 return;
784out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100785 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200786}
Johannes Berg667503dd2009-07-07 03:56:11 +0200787
Jouni Malinened9d0102011-05-16 19:40:15 +0300788void cfg80211_roamed(struct net_device *dev,
789 struct ieee80211_channel *channel,
790 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200791 const u8 *req_ie, size_t req_ie_len,
792 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
793{
794 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530795 struct cfg80211_bss *bss;
796
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530797 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
798 wdev->ssid_len, WLAN_CAPABILITY_ESS,
799 WLAN_CAPABILITY_ESS);
800 if (WARN_ON(!bss))
801 return;
802
803 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
804 resp_ie_len, gfp);
805}
806EXPORT_SYMBOL(cfg80211_roamed);
807
Ben Greear0e3a39b2013-06-19 14:06:27 -0700808/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530809void cfg80211_roamed_bss(struct net_device *dev,
810 struct cfg80211_bss *bss, const u8 *req_ie,
811 size_t req_ie_len, const u8 *resp_ie,
812 size_t resp_ie_len, gfp_t gfp)
813{
814 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800815 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200816 struct cfg80211_event *ev;
817 unsigned long flags;
818
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530819 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200820 return;
821
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530822 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
823 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100824 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530825 return;
826 }
827
Johannes Berg667503dd2009-07-07 03:56:11 +0200828 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200829 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
830 ev->rm.req_ie_len = req_ie_len;
831 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
832 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
833 ev->rm.resp_ie_len = resp_ie_len;
834 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530835 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200836
837 spin_lock_irqsave(&wdev->event_lock, flags);
838 list_add_tail(&ev->list, &wdev->event_list);
839 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100840 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200841}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530842EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200843
Johannes Berg667503dd2009-07-07 03:56:11 +0200844void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200845 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200846{
847 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800848 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200850#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200851 union iwreq_data wrqu;
852#endif
853
Johannes Berg667503dd2009-07-07 03:56:11 +0200854 ASSERT_WDEV_LOCK(wdev);
855
Johannes Berg074ac8d2010-09-16 14:58:22 +0200856 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
857 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200858 return;
859
Samuel Ortizb23aa672009-07-01 21:26:54 +0200860 if (wdev->current_bss) {
861 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100862 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200863 }
864
865 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200866 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200867
Johannes Bergfffd0932009-07-08 14:22:54 +0200868 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
869
870 /*
871 * Delete all the keys ... pairwise keys can't really
872 * exist any more anyway, but default keys might.
873 */
874 if (rdev->ops->del_key)
875 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300876 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200877
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800878 rdev_set_qos_map(rdev, dev, NULL);
879
Johannes Berg3d23e342009-09-29 23:27:28 +0200880#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200881 memset(&wrqu, 0, sizeof(wrqu));
882 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
883 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800884 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200885#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500886
887 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200888}
889
890void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Bergc1e5f472014-05-19 17:53:16 +0200891 const u8 *ie, size_t ie_len, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200892{
Johannes Berg667503dd2009-07-07 03:56:11 +0200893 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800894 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200895 struct cfg80211_event *ev;
896 unsigned long flags;
897
898 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
899 if (!ev)
900 return;
901
902 ev->type = EVENT_DISCONNECTED;
903 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
904 ev->dc.ie_len = ie_len;
905 memcpy((void *)ev->dc.ie, ie, ie_len);
906 ev->dc.reason = reason;
907
908 spin_lock_irqsave(&wdev->event_lock, flags);
909 list_add_tail(&ev->list, &wdev->event_list);
910 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100911 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912}
913EXPORT_SYMBOL(cfg80211_disconnected);
914
Johannes Bergceca7b72013-05-16 00:55:45 +0200915/*
916 * API calls for nl80211/wext compatibility code
917 */
Johannes Berg83739b02013-05-15 17:44:01 +0200918int cfg80211_connect(struct cfg80211_registered_device *rdev,
919 struct net_device *dev,
920 struct cfg80211_connect_params *connect,
921 struct cfg80211_cached_keys *connkeys,
922 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200923{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200924 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200925 int err;
926
927 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200928
Johannes Bergfffd0932009-07-08 14:22:54 +0200929 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300930 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200931 wdev->connect_keys = NULL;
932 }
933
Ben Greear7e7c8922011-11-18 11:31:59 -0800934 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
935 rdev->wiphy.ht_capa_mod_mask);
936
Johannes Bergfffd0932009-07-08 14:22:54 +0200937 if (connkeys && connkeys->def >= 0) {
938 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200939 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200940
941 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200942 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200943 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200944 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
945 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200946 connect->key_idx = idx;
947 connect->key = connkeys->params[idx].key;
948 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200949
950 /*
951 * If ciphers are not set (e.g. when going through
952 * iwconfig), we have to set them appropriately here.
953 */
954 if (connect->crypto.cipher_group == 0)
955 connect->crypto.cipher_group = cipher;
956
957 if (connect->crypto.n_ciphers_pairwise == 0) {
958 connect->crypto.n_ciphers_pairwise = 1;
959 connect->crypto.ciphers_pairwise[0] = cipher;
960 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200961 }
962 }
963
Johannes Bergceca7b72013-05-16 00:55:45 +0200964 wdev->connect_keys = connkeys;
965 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
966 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200967
Johannes Bergceca7b72013-05-16 00:55:45 +0200968 if (!rdev->ops->connect)
969 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
970 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300971 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200972
Johannes Bergceca7b72013-05-16 00:55:45 +0200973 if (err) {
974 wdev->connect_keys = NULL;
975 wdev->ssid_len = 0;
976 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200977 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200978
979 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980}
981
Johannes Berg83739b02013-05-15 17:44:01 +0200982int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
983 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200984{
Johannes Berg6829c872009-07-02 09:13:27 +0200985 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +0200986 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200987
Johannes Berg667503dd2009-07-07 03:56:11 +0200988 ASSERT_WDEV_LOCK(wdev);
989
Johannes Bergb47f6102014-09-10 13:39:54 +0300990 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200991 wdev->connect_keys = NULL;
992
Johannes Bergdee8a972013-08-13 09:23:57 +0200993 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +0200994 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +0200995 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +0200996 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +0200997 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300998 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200999
Johannes Bergceca7b72013-05-16 00:55:45 +02001000 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001001}