blob: e2923a3f2e5c633aedc44c97e450dc1f0a8eb5bd [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
Johannes Berg6829c872009-07-02 09:13:27 +020083 if (wdev->conn->params.channel)
84 request->channels[0] = wdev->conn->params.channel;
85 else {
86 int i = 0, j;
87 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053088 struct ieee80211_supported_band *bands;
89 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020090
91 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +053092 bands = wdev->wiphy->bands[band];
93 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +020094 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053095 for (j = 0; j < bands->n_channels; j++) {
96 channel = &bands->channels[j];
97 if (channel->flags & IEEE80211_CHAN_DISABLED)
98 continue;
99 request->channels[i++] = channel;
100 }
101 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200102 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530103 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200104 }
105 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200106 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200107 request->n_ssids = 1;
108
109 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
110 wdev->conn->params.ssid_len);
111 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
112
Johannes Bergfd014282012-06-18 19:17:03 +0200113 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200114 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700115 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200116
Johannes Berg79c97e92009-07-07 03:56:12 +0200117 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200118
Hila Gonene35e4d22012-06-27 17:19:42 +0300119 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200120 if (!err) {
121 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200122 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200123 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200124 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200125 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200126 kfree(request);
127 }
128 return err;
129}
130
131static int cfg80211_conn_do_work(struct wireless_dev *wdev)
132{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800133 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200134 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100135 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200136 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200137
Johannes Berg667503dd2009-07-07 03:56:11 +0200138 ASSERT_WDEV_LOCK(wdev);
139
Johannes Berg6829c872009-07-02 09:13:27 +0200140 if (!wdev->conn)
141 return 0;
142
Johannes Berg19957bb2009-07-02 17:20:43 +0200143 params = &wdev->conn->params;
144
Johannes Berg6829c872009-07-02 09:13:27 +0200145 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200146 case CFG80211_CONN_SCANNING:
147 /* didn't find it during scan ... */
148 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200149 case CFG80211_CONN_SCAN_AGAIN:
150 return cfg80211_conn_scan(wdev);
151 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200152 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200153 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200154 return cfg80211_mlme_auth(rdev, wdev->netdev,
155 params->channel, params->auth_type,
156 params->bssid,
157 params->ssid, params->ssid_len,
158 NULL, 0,
159 params->key, params->key_len,
160 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200161 case CFG80211_CONN_AUTH_FAILED:
162 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200163 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200164 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200165 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200166 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100167 req.prev_bssid = wdev->conn->prev_bssid;
168 req.ie = params->ie;
169 req.ie_len = params->ie_len;
170 req.use_mfp = params->mfp != NL80211_MFP_NO;
171 req.crypto = params->crypto;
172 req.flags = params->flags;
173 req.ht_capa = params->ht_capa;
174 req.ht_capa_mask = params->ht_capa_mask;
175 req.vht_capa = params->vht_capa;
176 req.vht_capa_mask = params->vht_capa_mask;
177
Johannes Berg91bf9b22013-05-15 17:44:01 +0200178 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
179 params->bssid, params->ssid,
180 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200181 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200182 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
183 NULL, 0,
184 WLAN_REASON_DEAUTH_LEAVING,
185 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200186 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200187 case CFG80211_CONN_ASSOC_FAILED:
188 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
189 NULL, 0,
190 WLAN_REASON_DEAUTH_LEAVING, false);
191 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200192 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200193 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
195 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200196 /* free directly, disconnected event already sent */
197 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200198 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200199 default:
200 return 0;
201 }
202}
203
204void cfg80211_conn_work(struct work_struct *work)
205{
Johannes Berg79c97e92009-07-07 03:56:12 +0200206 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200207 container_of(work, struct cfg80211_registered_device, conn_work);
208 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100209 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200210
211 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200212
Johannes Berg89a54e42012-06-15 14:33:17 +0200213 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200214 if (!wdev->netdev)
215 continue;
216
Johannes Berg667503dd2009-07-07 03:56:11 +0200217 wdev_lock(wdev);
218 if (!netif_running(wdev->netdev)) {
219 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200220 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200221 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200222 if (!wdev->conn ||
223 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200224 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200225 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200226 }
Johannes Berg7400f422009-10-31 07:40:37 +0100227 if (wdev->conn->params.bssid) {
228 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
229 bssid = bssid_buf;
230 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200231 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900233 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 NULL, 0, NULL, 0,
235 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200236 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200237 cfg80211_sme_free(wdev);
238 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200239 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200240 }
241
Johannes Berg6829c872009-07-02 09:13:27 +0200242 rtnl_unlock();
243}
244
Ben Greear0e3a39b2013-06-19 14:06:27 -0700245/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700246static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200247{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800248 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200249 struct cfg80211_bss *bss;
250 u16 capa = WLAN_CAPABILITY_ESS;
251
Johannes Berg667503dd2009-07-07 03:56:11 +0200252 ASSERT_WDEV_LOCK(wdev);
253
Johannes Berg6829c872009-07-02 09:13:27 +0200254 if (wdev->conn->params.privacy)
255 capa |= WLAN_CAPABILITY_PRIVACY;
256
Jouni Malinened9d0102011-05-16 19:40:15 +0300257 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
258 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200259 wdev->conn->params.ssid,
260 wdev->conn->params.ssid_len,
261 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
262 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200263 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700264 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200265
266 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
267 wdev->conn->params.bssid = wdev->conn->bssid;
268 wdev->conn->params.channel = bss->channel;
269 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200270 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200271
Johannes Bergbbac31f2009-09-16 09:04:26 -0700272 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200273}
274
Johannes Berg667503dd2009-07-07 03:56:11 +0200275static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200276{
277 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800278 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700279 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200280
Johannes Berg667503dd2009-07-07 03:56:11 +0200281 ASSERT_WDEV_LOCK(wdev);
282
Zhu Yid4b1a682009-07-16 17:34:14 +0800283 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200284 return;
285
286 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
287 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
288 return;
289
Johannes Bergbbac31f2009-09-16 09:04:26 -0700290 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200291 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100292 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200293 else
294 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200295}
296
Johannes Berg667503dd2009-07-07 03:56:11 +0200297void cfg80211_sme_scan_done(struct net_device *dev)
298{
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
300
301 wdev_lock(wdev);
302 __cfg80211_sme_scan_done(dev);
303 wdev_unlock(wdev);
304}
305
Johannes Bergceca7b72013-05-16 00:55:45 +0200306void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200307{
Johannes Berg6829c872009-07-02 09:13:27 +0200308 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800309 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200310 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
311 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
312
Johannes Berg667503dd2009-07-07 03:56:11 +0200313 ASSERT_WDEV_LOCK(wdev);
314
Johannes Bergceca7b72013-05-16 00:55:45 +0200315 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200316 return;
317
318 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
319 wdev->conn->auto_auth &&
320 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
321 /* select automatically between only open, shared, leap */
322 switch (wdev->conn->params.auth_type) {
323 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200324 if (wdev->connect_keys)
325 wdev->conn->params.auth_type =
326 NL80211_AUTHTYPE_SHARED_KEY;
327 else
328 wdev->conn->params.auth_type =
329 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200330 break;
331 case NL80211_AUTHTYPE_SHARED_KEY:
332 wdev->conn->params.auth_type =
333 NL80211_AUTHTYPE_NETWORK_EAP;
334 break;
335 default:
336 /* huh? */
337 wdev->conn->params.auth_type =
338 NL80211_AUTHTYPE_OPEN_SYSTEM;
339 break;
340 }
341 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
342 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200343 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200344 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
345 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200346 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200347 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200348 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
349 schedule_work(&rdev->conn_work);
350 }
351}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200352
Johannes Bergceca7b72013-05-16 00:55:45 +0200353bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200354{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800355 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200356
Johannes Bergceca7b72013-05-16 00:55:45 +0200357 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200358 return false;
359
Johannes Bergceca7b72013-05-16 00:55:45 +0200360 if (status == WLAN_STATUS_SUCCESS) {
361 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200362 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200363 }
364
365 if (wdev->conn->prev_bssid_valid) {
366 /*
367 * Some stupid APs don't accept reassoc, so we
368 * need to fall back to trying regular assoc;
369 * return true so no event is sent to userspace.
370 */
371 wdev->conn->prev_bssid_valid = false;
372 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
373 schedule_work(&rdev->conn_work);
374 return true;
375 }
376
Johannes Berg923a0e72013-06-28 11:38:54 +0200377 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200378 schedule_work(&rdev->conn_work);
379 return false;
380}
381
382void cfg80211_sme_deauth(struct wireless_dev *wdev)
383{
384 cfg80211_sme_free(wdev);
385}
386
387void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
388{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800389 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200390
391 if (!wdev->conn)
392 return;
393
394 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
395 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200396}
397
398void cfg80211_sme_disassoc(struct wireless_dev *wdev)
399{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800400 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200401
402 if (!wdev->conn)
403 return;
404
405 wdev->conn->state = CFG80211_CONN_DEAUTH;
406 schedule_work(&rdev->conn_work);
407}
408
409void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
410{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800411 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200412
413 if (!wdev->conn)
414 return;
415
416 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
417 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200418}
419
420static int cfg80211_sme_connect(struct wireless_dev *wdev,
421 struct cfg80211_connect_params *connect,
422 const u8 *prev_bssid)
423{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800424 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200425 struct cfg80211_bss *bss;
426 int err;
427
428 if (!rdev->ops->auth || !rdev->ops->assoc)
429 return -EOPNOTSUPP;
430
431 if (wdev->current_bss)
432 return -EALREADY;
433
434 if (WARN_ON(wdev->conn))
435 return -EINPROGRESS;
436
437 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
438 if (!wdev->conn)
439 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200440
441 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200442 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200443 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200444 memcpy(&wdev->conn->params, connect, sizeof(*connect));
445 if (connect->bssid) {
446 wdev->conn->params.bssid = wdev->conn->bssid;
447 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
448 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200449
Johannes Bergceca7b72013-05-16 00:55:45 +0200450 if (connect->ie) {
451 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
452 GFP_KERNEL);
453 wdev->conn->params.ie = wdev->conn->ie;
454 if (!wdev->conn->ie) {
455 kfree(wdev->conn);
456 wdev->conn = NULL;
457 return -ENOMEM;
458 }
459 }
460
461 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
462 wdev->conn->auto_auth = true;
463 /* start with open system ... should mostly work */
464 wdev->conn->params.auth_type =
465 NL80211_AUTHTYPE_OPEN_SYSTEM;
466 } else {
467 wdev->conn->auto_auth = false;
468 }
469
470 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800471 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200472
473 /* see if we have the bss already */
474 bss = cfg80211_get_conn_bss(wdev);
475
476 if (prev_bssid) {
477 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
478 wdev->conn->prev_bssid_valid = true;
479 }
480
481 /* we're good if we have a matching bss struct */
482 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200483 err = cfg80211_conn_do_work(wdev);
484 cfg80211_put_bss(wdev->wiphy, bss);
485 } else {
486 /* otherwise we'll need to scan for the AP first */
487 err = cfg80211_conn_scan(wdev);
488
489 /*
490 * If we can't scan right now, then we need to scan again
491 * after the current scan finished, since the parameters
492 * changed (unless we find a good AP anyway).
493 */
494 if (err == -EBUSY) {
495 err = 0;
496 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
497 }
498 }
499
500 if (err)
501 cfg80211_sme_free(wdev);
502
503 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200504}
505
Johannes Bergceca7b72013-05-16 00:55:45 +0200506static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900507{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800508 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200509 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900510
Johannes Bergceca7b72013-05-16 00:55:45 +0200511 if (!wdev->conn)
512 return 0;
513
514 if (!rdev->ops->deauth)
515 return -EOPNOTSUPP;
516
517 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
518 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
519 err = 0;
520 goto out;
521 }
522
523 /* wdev->conn->params.bssid must be set if > SCANNING */
524 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
525 wdev->conn->params.bssid,
526 NULL, 0, reason, false);
527 out:
528 cfg80211_sme_free(wdev);
529 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900530}
531
Johannes Bergceca7b72013-05-16 00:55:45 +0200532/*
533 * code shared for in-device and software SME
534 */
535
536static bool cfg80211_is_all_idle(void)
537{
538 struct cfg80211_registered_device *rdev;
539 struct wireless_dev *wdev;
540 bool is_all_idle = true;
541
542 /*
543 * All devices must be idle as otherwise if you are actively
544 * scanning some new beacon hints could be learned and would
545 * count as new regulatory hints.
546 */
547 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
548 list_for_each_entry(wdev, &rdev->wdev_list, list) {
549 wdev_lock(wdev);
550 if (wdev->conn || wdev->current_bss)
551 is_all_idle = false;
552 wdev_unlock(wdev);
553 }
554 }
555
556 return is_all_idle;
557}
558
559static void disconnect_work(struct work_struct *work)
560{
561 rtnl_lock();
562 if (cfg80211_is_all_idle())
563 regulatory_hint_disconnect();
564 rtnl_unlock();
565}
566
567static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
568
569
570/*
571 * API calls for drivers implementing connect/disconnect and
572 * SME event handling
573 */
574
Ben Greear6f390902013-06-19 14:06:25 -0700575/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200576void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
577 const u8 *req_ie, size_t req_ie_len,
578 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200579 u16 status, bool wextev,
580 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200581{
582 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100583 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200584#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200585 union iwreq_data wrqu;
586#endif
587
Johannes Berg667503dd2009-07-07 03:56:11 +0200588 ASSERT_WDEV_LOCK(wdev);
589
Johannes Berg074ac8d2010-09-16 14:58:22 +0200590 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700591 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
592 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593 return;
Ben Greear6f390902013-06-19 14:06:25 -0700594 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800596 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200597 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200598 resp_ie, resp_ie_len,
599 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200600
Johannes Berg3d23e342009-09-29 23:27:28 +0200601#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200602 if (wextev) {
603 if (req_ie && status == WLAN_STATUS_SUCCESS) {
604 memset(&wrqu, 0, sizeof(wrqu));
605 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800606 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200607 }
608
609 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
610 memset(&wrqu, 0, sizeof(wrqu));
611 wrqu.data.length = resp_ie_len;
612 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
613 }
614
615 memset(&wrqu, 0, sizeof(wrqu));
616 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200617 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200618 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200619 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
620 wdev->wext.prev_bssid_valid = true;
621 }
Johannes Berge45cd822009-07-02 09:58:04 +0200622 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
623 }
624#endif
625
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530626 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800627 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530628 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
629 wdev->ssid, wdev->ssid_len,
630 WLAN_CAPABILITY_ESS,
631 WLAN_CAPABILITY_ESS);
632 if (bss)
633 cfg80211_hold_bss(bss_from_pub(bss));
634 }
635
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200636 if (wdev->current_bss) {
637 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100638 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200639 wdev->current_bss = NULL;
640 }
641
Johannes Bergfffd0932009-07-08 14:22:54 +0200642 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200643 kfree(wdev->connect_keys);
644 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200645 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200646 if (bss) {
647 cfg80211_unhold_bss(bss_from_pub(bss));
648 cfg80211_put_bss(wdev->wiphy, bss);
649 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200650 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200651 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200652
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530653 if (WARN_ON(!bss))
654 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200655
Johannes Bergfffd0932009-07-08 14:22:54 +0200656 wdev->current_bss = bss_from_pub(bss);
657
Johannes Bergfffd0932009-07-08 14:22:54 +0200658 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700659
Johannes Berg9caf0362012-11-29 01:25:20 +0100660 rcu_read_lock();
661 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
662 if (!country_ie) {
663 rcu_read_unlock();
664 return;
665 }
666
667 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
668 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700669
670 if (!country_ie)
671 return;
672
673 /*
674 * ieee80211_bss_get_ie() ensures we can access:
675 * - country_ie + 2, the start of the country ie data, and
676 * - and country_ie[1] which is the IE length
677 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700678 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
679 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100680 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200681}
Johannes Bergf2129352009-07-01 21:26:56 +0200682
683void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
684 const u8 *req_ie, size_t req_ie_len,
685 const u8 *resp_ie, size_t resp_ie_len,
686 u16 status, gfp_t gfp)
687{
Johannes Berg667503dd2009-07-07 03:56:11 +0200688 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800689 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200690 struct cfg80211_event *ev;
691 unsigned long flags;
692
693 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
694 if (!ev)
695 return;
696
697 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800698 if (bssid)
699 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700700 if (req_ie_len) {
701 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
702 ev->cr.req_ie_len = req_ie_len;
703 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
704 }
705 if (resp_ie_len) {
706 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
707 ev->cr.resp_ie_len = resp_ie_len;
708 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
709 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200710 ev->cr.status = status;
711
712 spin_lock_irqsave(&wdev->event_lock, flags);
713 list_add_tail(&ev->list, &wdev->event_list);
714 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100715 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200716}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200717EXPORT_SYMBOL(cfg80211_connect_result);
718
Ben Greear0e3a39b2013-06-19 14:06:27 -0700719/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300720void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530721 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200722 const u8 *req_ie, size_t req_ie_len,
723 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200724{
Johannes Berg3d23e342009-09-29 23:27:28 +0200725#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200726 union iwreq_data wrqu;
727#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200728 ASSERT_WDEV_LOCK(wdev);
729
Johannes Berg074ac8d2010-09-16 14:58:22 +0200730 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
731 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530732 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200733
Johannes Bergceca7b72013-05-16 00:55:45 +0200734 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530735 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736
Samuel Ortizb23aa672009-07-01 21:26:54 +0200737 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100738 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200739 wdev->current_bss = NULL;
740
Johannes Berg19957bb2009-07-02 17:20:43 +0200741 cfg80211_hold_bss(bss_from_pub(bss));
742 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800744 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
745 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200746 req_ie, req_ie_len, resp_ie, resp_ie_len,
747 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748
Johannes Berg3d23e342009-09-29 23:27:28 +0200749#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200750 if (req_ie) {
751 memset(&wrqu, 0, sizeof(wrqu));
752 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800753 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200754 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200755 }
756
757 if (resp_ie) {
758 memset(&wrqu, 0, sizeof(wrqu));
759 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200760 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
761 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200762 }
763
764 memset(&wrqu, 0, sizeof(wrqu));
765 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530766 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
767 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200768 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200769 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200770#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530771
772 return;
773out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100774 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200775}
Johannes Berg667503dd2009-07-07 03:56:11 +0200776
Jouni Malinened9d0102011-05-16 19:40:15 +0300777void cfg80211_roamed(struct net_device *dev,
778 struct ieee80211_channel *channel,
779 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200780 const u8 *req_ie, size_t req_ie_len,
781 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
782{
783 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530784 struct cfg80211_bss *bss;
785
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530786 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
787 wdev->ssid_len, WLAN_CAPABILITY_ESS,
788 WLAN_CAPABILITY_ESS);
789 if (WARN_ON(!bss))
790 return;
791
792 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
793 resp_ie_len, gfp);
794}
795EXPORT_SYMBOL(cfg80211_roamed);
796
Ben Greear0e3a39b2013-06-19 14:06:27 -0700797/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530798void cfg80211_roamed_bss(struct net_device *dev,
799 struct cfg80211_bss *bss, const u8 *req_ie,
800 size_t req_ie_len, const u8 *resp_ie,
801 size_t resp_ie_len, gfp_t gfp)
802{
803 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800804 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200805 struct cfg80211_event *ev;
806 unsigned long flags;
807
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530808 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200809 return;
810
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530811 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
812 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100813 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530814 return;
815 }
816
Johannes Berg667503dd2009-07-07 03:56:11 +0200817 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200818 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
819 ev->rm.req_ie_len = req_ie_len;
820 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
821 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
822 ev->rm.resp_ie_len = resp_ie_len;
823 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530824 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200825
826 spin_lock_irqsave(&wdev->event_lock, flags);
827 list_add_tail(&ev->list, &wdev->event_list);
828 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100829 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200830}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530831EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200832
Johannes Berg667503dd2009-07-07 03:56:11 +0200833void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200834 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200835{
836 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800837 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200838 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200839#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200840 union iwreq_data wrqu;
841#endif
842
Johannes Berg667503dd2009-07-07 03:56:11 +0200843 ASSERT_WDEV_LOCK(wdev);
844
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
846 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200847 return;
848
Samuel Ortizb23aa672009-07-01 21:26:54 +0200849 if (wdev->current_bss) {
850 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100851 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200852 }
853
854 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200855 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200856
Johannes Bergfffd0932009-07-08 14:22:54 +0200857 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
858
859 /*
860 * Delete all the keys ... pairwise keys can't really
861 * exist any more anyway, but default keys might.
862 */
863 if (rdev->ops->del_key)
864 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300865 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200866
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800867 rdev_set_qos_map(rdev, dev, NULL);
868
Johannes Berg3d23e342009-09-29 23:27:28 +0200869#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200870 memset(&wrqu, 0, sizeof(wrqu));
871 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
872 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800873 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200874#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500875
876 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200877}
878
879void cfg80211_disconnected(struct net_device *dev, u16 reason,
880 u8 *ie, size_t ie_len, gfp_t gfp)
881{
Johannes Berg667503dd2009-07-07 03:56:11 +0200882 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800883 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200884 struct cfg80211_event *ev;
885 unsigned long flags;
886
887 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
888 if (!ev)
889 return;
890
891 ev->type = EVENT_DISCONNECTED;
892 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
893 ev->dc.ie_len = ie_len;
894 memcpy((void *)ev->dc.ie, ie, ie_len);
895 ev->dc.reason = reason;
896
897 spin_lock_irqsave(&wdev->event_lock, flags);
898 list_add_tail(&ev->list, &wdev->event_list);
899 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100900 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200901}
902EXPORT_SYMBOL(cfg80211_disconnected);
903
Johannes Bergceca7b72013-05-16 00:55:45 +0200904/*
905 * API calls for nl80211/wext compatibility code
906 */
Johannes Berg83739b02013-05-15 17:44:01 +0200907int cfg80211_connect(struct cfg80211_registered_device *rdev,
908 struct net_device *dev,
909 struct cfg80211_connect_params *connect,
910 struct cfg80211_cached_keys *connkeys,
911 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200913 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200914 int err;
915
916 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200917
Johannes Bergfffd0932009-07-08 14:22:54 +0200918 if (WARN_ON(wdev->connect_keys)) {
919 kfree(wdev->connect_keys);
920 wdev->connect_keys = NULL;
921 }
922
Ben Greear7e7c8922011-11-18 11:31:59 -0800923 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
924 rdev->wiphy.ht_capa_mod_mask);
925
Johannes Bergfffd0932009-07-08 14:22:54 +0200926 if (connkeys && connkeys->def >= 0) {
927 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200928 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200929
930 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200931 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200932 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200933 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
934 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200935 connect->key_idx = idx;
936 connect->key = connkeys->params[idx].key;
937 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200938
939 /*
940 * If ciphers are not set (e.g. when going through
941 * iwconfig), we have to set them appropriately here.
942 */
943 if (connect->crypto.cipher_group == 0)
944 connect->crypto.cipher_group = cipher;
945
946 if (connect->crypto.n_ciphers_pairwise == 0) {
947 connect->crypto.n_ciphers_pairwise = 1;
948 connect->crypto.ciphers_pairwise[0] = cipher;
949 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200950 }
951 }
952
Johannes Bergceca7b72013-05-16 00:55:45 +0200953 wdev->connect_keys = connkeys;
954 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
955 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200956
Johannes Bergceca7b72013-05-16 00:55:45 +0200957 if (!rdev->ops->connect)
958 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
959 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300960 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200961
Johannes Bergceca7b72013-05-16 00:55:45 +0200962 if (err) {
963 wdev->connect_keys = NULL;
964 wdev->ssid_len = 0;
965 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200966 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200967
968 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200969}
970
Johannes Berg83739b02013-05-15 17:44:01 +0200971int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
972 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973{
Johannes Berg6829c872009-07-02 09:13:27 +0200974 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +0200975 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200976
Johannes Berg667503dd2009-07-07 03:56:11 +0200977 ASSERT_WDEV_LOCK(wdev);
978
Johannes Bergfffd0932009-07-08 14:22:54 +0200979 kfree(wdev->connect_keys);
980 wdev->connect_keys = NULL;
981
Johannes Bergdee8a972013-08-13 09:23:57 +0200982 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +0200983 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +0200984 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +0200985 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +0200986 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300987 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200988
Johannes Bergceca7b72013-05-16 00:55:45 +0200989 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200990}