blob: 20e86a95dc4e0ed358485f04208c670297ee6517 [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{
Johannes Berg79c97e92009-07-07 03:56:12 +020062 struct cfg80211_registered_device *rdev = wiphy_to_dev(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 Berg79c97e92009-07-07 03:56:12 +020067 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +020068 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020069
Johannes Berg79c97e92009-07-07 03:56:12 +020070 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020071 return -EBUSY;
72
73 if (wdev->conn->params.channel) {
74 n_channels = 1;
75 } else {
76 enum ieee80211_band band;
77 n_channels = 0;
78
79 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
80 if (!wdev->wiphy->bands[band])
81 continue;
82 n_channels += wdev->wiphy->bands[band]->n_channels;
83 }
84 }
85 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
86 sizeof(request->channels[0]) * n_channels,
87 GFP_KERNEL);
88 if (!request)
89 return -ENOMEM;
90
Johannes Berg6829c872009-07-02 09:13:27 +020091 if (wdev->conn->params.channel)
92 request->channels[0] = wdev->conn->params.channel;
93 else {
94 int i = 0, j;
95 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053096 struct ieee80211_supported_band *bands;
97 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020098
99 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530100 bands = wdev->wiphy->bands[band];
101 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200102 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530103 for (j = 0; j < bands->n_channels; j++) {
104 channel = &bands->channels[j];
105 if (channel->flags & IEEE80211_CHAN_DISABLED)
106 continue;
107 request->channels[i++] = channel;
108 }
109 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200110 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530111 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200112 }
113 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200114 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200115 request->n_ssids = 1;
116
117 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
118 wdev->conn->params.ssid_len);
119 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
120
Johannes Bergfd014282012-06-18 19:17:03 +0200121 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200122 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700123 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200124
Johannes Berg79c97e92009-07-07 03:56:12 +0200125 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200126
Hila Gonene35e4d22012-06-27 17:19:42 +0300127 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200128 if (!err) {
129 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200130 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200131 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200132 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200133 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200134 kfree(request);
135 }
136 return err;
137}
138
139static int cfg80211_conn_do_work(struct wireless_dev *wdev)
140{
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200142 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100143 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200144 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200145
Johannes Berg667503dd2009-07-07 03:56:11 +0200146 ASSERT_WDEV_LOCK(wdev);
147
Johannes Berg6829c872009-07-02 09:13:27 +0200148 if (!wdev->conn)
149 return 0;
150
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 params = &wdev->conn->params;
152
Johannes Berg6829c872009-07-02 09:13:27 +0200153 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200154 case CFG80211_CONN_SCANNING:
155 /* didn't find it during scan ... */
156 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200157 case CFG80211_CONN_SCAN_AGAIN:
158 return cfg80211_conn_scan(wdev);
159 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200160 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200162 return cfg80211_mlme_auth(rdev, wdev->netdev,
163 params->channel, params->auth_type,
164 params->bssid,
165 params->ssid, params->ssid_len,
166 NULL, 0,
167 params->key, params->key_len,
168 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200169 case CFG80211_CONN_AUTH_FAILED:
170 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200171 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200172 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200173 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200174 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100175 req.prev_bssid = wdev->conn->prev_bssid;
176 req.ie = params->ie;
177 req.ie_len = params->ie_len;
178 req.use_mfp = params->mfp != NL80211_MFP_NO;
179 req.crypto = params->crypto;
180 req.flags = params->flags;
181 req.ht_capa = params->ht_capa;
182 req.ht_capa_mask = params->ht_capa_mask;
183 req.vht_capa = params->vht_capa;
184 req.vht_capa_mask = params->vht_capa_mask;
185
Johannes Berg91bf9b22013-05-15 17:44:01 +0200186 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
187 params->bssid, params->ssid,
188 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200189 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200190 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
191 NULL, 0,
192 WLAN_REASON_DEAUTH_LEAVING,
193 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200194 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200195 case CFG80211_CONN_ASSOC_FAILED:
196 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
197 NULL, 0,
198 WLAN_REASON_DEAUTH_LEAVING, false);
199 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200200 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200204 /* free directly, disconnected event already sent */
205 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200206 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200207 default:
208 return 0;
209 }
210}
211
212void cfg80211_conn_work(struct work_struct *work)
213{
Johannes Berg79c97e92009-07-07 03:56:12 +0200214 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200215 container_of(work, struct cfg80211_registered_device, conn_work);
216 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100217 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200218
219 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200220
Johannes Berg89a54e42012-06-15 14:33:17 +0200221 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200222 if (!wdev->netdev)
223 continue;
224
Johannes Berg667503dd2009-07-07 03:56:11 +0200225 wdev_lock(wdev);
226 if (!netif_running(wdev->netdev)) {
227 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200228 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200229 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200230 if (!wdev->conn ||
231 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 }
Johannes Berg7400f422009-10-31 07:40:37 +0100235 if (wdev->conn->params.bssid) {
236 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
237 bssid = bssid_buf;
238 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200239 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200240 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900241 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200242 NULL, 0, NULL, 0,
243 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200244 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200245 cfg80211_sme_free(wdev);
246 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200247 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200248 }
249
Johannes Berg6829c872009-07-02 09:13:27 +0200250 rtnl_unlock();
251}
252
Ben Greear0e3a39b2013-06-19 14:06:27 -0700253/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700254static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200255{
Johannes Berg79c97e92009-07-07 03:56:12 +0200256 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200257 struct cfg80211_bss *bss;
258 u16 capa = WLAN_CAPABILITY_ESS;
259
Johannes Berg667503dd2009-07-07 03:56:11 +0200260 ASSERT_WDEV_LOCK(wdev);
261
Johannes Berg6829c872009-07-02 09:13:27 +0200262 if (wdev->conn->params.privacy)
263 capa |= WLAN_CAPABILITY_PRIVACY;
264
Jouni Malinened9d0102011-05-16 19:40:15 +0300265 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
266 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200267 wdev->conn->params.ssid,
268 wdev->conn->params.ssid_len,
269 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
270 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200271 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700272 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200273
274 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
275 wdev->conn->params.bssid = wdev->conn->bssid;
276 wdev->conn->params.channel = bss->channel;
277 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200278 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200279
Johannes Bergbbac31f2009-09-16 09:04:26 -0700280 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200281}
282
Johannes Berg667503dd2009-07-07 03:56:11 +0200283static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200284{
285 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200286 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700287 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200288
Johannes Berg667503dd2009-07-07 03:56:11 +0200289 ASSERT_WDEV_LOCK(wdev);
290
Zhu Yid4b1a682009-07-16 17:34:14 +0800291 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200292 return;
293
294 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
295 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
296 return;
297
Johannes Bergbbac31f2009-09-16 09:04:26 -0700298 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200299 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100300 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200301 else
302 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200303}
304
Johannes Berg667503dd2009-07-07 03:56:11 +0200305void cfg80211_sme_scan_done(struct net_device *dev)
306{
307 struct wireless_dev *wdev = dev->ieee80211_ptr;
308
309 wdev_lock(wdev);
310 __cfg80211_sme_scan_done(dev);
311 wdev_unlock(wdev);
312}
313
Johannes Bergceca7b72013-05-16 00:55:45 +0200314void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200315{
Johannes Berg6829c872009-07-02 09:13:27 +0200316 struct wiphy *wiphy = wdev->wiphy;
317 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
318 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
319 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
320
Johannes Berg667503dd2009-07-07 03:56:11 +0200321 ASSERT_WDEV_LOCK(wdev);
322
Johannes Bergceca7b72013-05-16 00:55:45 +0200323 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200324 return;
325
326 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
327 wdev->conn->auto_auth &&
328 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
329 /* select automatically between only open, shared, leap */
330 switch (wdev->conn->params.auth_type) {
331 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200332 if (wdev->connect_keys)
333 wdev->conn->params.auth_type =
334 NL80211_AUTHTYPE_SHARED_KEY;
335 else
336 wdev->conn->params.auth_type =
337 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200338 break;
339 case NL80211_AUTHTYPE_SHARED_KEY:
340 wdev->conn->params.auth_type =
341 NL80211_AUTHTYPE_NETWORK_EAP;
342 break;
343 default:
344 /* huh? */
345 wdev->conn->params.auth_type =
346 NL80211_AUTHTYPE_OPEN_SYSTEM;
347 break;
348 }
349 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
350 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200351 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200352 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
353 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200354 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200355 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200356 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
357 schedule_work(&rdev->conn_work);
358 }
359}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200360
Johannes Bergceca7b72013-05-16 00:55:45 +0200361bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200362{
Johannes Bergceca7b72013-05-16 00:55:45 +0200363 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200364
Johannes Bergceca7b72013-05-16 00:55:45 +0200365 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200366 return false;
367
Johannes Bergceca7b72013-05-16 00:55:45 +0200368 if (status == WLAN_STATUS_SUCCESS) {
369 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200370 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200371 }
372
373 if (wdev->conn->prev_bssid_valid) {
374 /*
375 * Some stupid APs don't accept reassoc, so we
376 * need to fall back to trying regular assoc;
377 * return true so no event is sent to userspace.
378 */
379 wdev->conn->prev_bssid_valid = false;
380 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
381 schedule_work(&rdev->conn_work);
382 return true;
383 }
384
Johannes Berg923a0e72013-06-28 11:38:54 +0200385 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200386 schedule_work(&rdev->conn_work);
387 return false;
388}
389
390void cfg80211_sme_deauth(struct wireless_dev *wdev)
391{
392 cfg80211_sme_free(wdev);
393}
394
395void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
396{
Johannes Berg923a0e72013-06-28 11:38:54 +0200397 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
398
399 if (!wdev->conn)
400 return;
401
402 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
403 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200404}
405
406void cfg80211_sme_disassoc(struct wireless_dev *wdev)
407{
408 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
409
410 if (!wdev->conn)
411 return;
412
413 wdev->conn->state = CFG80211_CONN_DEAUTH;
414 schedule_work(&rdev->conn_work);
415}
416
417void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
418{
Johannes Berg923a0e72013-06-28 11:38:54 +0200419 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
420
421 if (!wdev->conn)
422 return;
423
424 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
425 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200426}
427
428static int cfg80211_sme_connect(struct wireless_dev *wdev,
429 struct cfg80211_connect_params *connect,
430 const u8 *prev_bssid)
431{
432 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
433 struct cfg80211_bss *bss;
434 int err;
435
436 if (!rdev->ops->auth || !rdev->ops->assoc)
437 return -EOPNOTSUPP;
438
439 if (wdev->current_bss)
440 return -EALREADY;
441
442 if (WARN_ON(wdev->conn))
443 return -EINPROGRESS;
444
445 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
446 if (!wdev->conn)
447 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200448
449 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200450 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200451 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200452 memcpy(&wdev->conn->params, connect, sizeof(*connect));
453 if (connect->bssid) {
454 wdev->conn->params.bssid = wdev->conn->bssid;
455 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
456 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200457
Johannes Bergceca7b72013-05-16 00:55:45 +0200458 if (connect->ie) {
459 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
460 GFP_KERNEL);
461 wdev->conn->params.ie = wdev->conn->ie;
462 if (!wdev->conn->ie) {
463 kfree(wdev->conn);
464 wdev->conn = NULL;
465 return -ENOMEM;
466 }
467 }
468
469 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
470 wdev->conn->auto_auth = true;
471 /* start with open system ... should mostly work */
472 wdev->conn->params.auth_type =
473 NL80211_AUTHTYPE_OPEN_SYSTEM;
474 } else {
475 wdev->conn->auto_auth = false;
476 }
477
478 wdev->conn->params.ssid = wdev->ssid;
479 wdev->conn->params.ssid_len = connect->ssid_len;
480
481 /* see if we have the bss already */
482 bss = cfg80211_get_conn_bss(wdev);
483
484 if (prev_bssid) {
485 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
486 wdev->conn->prev_bssid_valid = true;
487 }
488
489 /* we're good if we have a matching bss struct */
490 if (bss) {
491 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
492 err = cfg80211_conn_do_work(wdev);
493 cfg80211_put_bss(wdev->wiphy, bss);
494 } else {
495 /* otherwise we'll need to scan for the AP first */
496 err = cfg80211_conn_scan(wdev);
497
498 /*
499 * If we can't scan right now, then we need to scan again
500 * after the current scan finished, since the parameters
501 * changed (unless we find a good AP anyway).
502 */
503 if (err == -EBUSY) {
504 err = 0;
505 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
506 }
507 }
508
509 if (err)
510 cfg80211_sme_free(wdev);
511
512 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200513}
514
Johannes Bergceca7b72013-05-16 00:55:45 +0200515static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900516{
Johannes Bergceca7b72013-05-16 00:55:45 +0200517 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
518 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900519
Johannes Bergceca7b72013-05-16 00:55:45 +0200520 if (!wdev->conn)
521 return 0;
522
523 if (!rdev->ops->deauth)
524 return -EOPNOTSUPP;
525
526 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
527 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
528 err = 0;
529 goto out;
530 }
531
532 /* wdev->conn->params.bssid must be set if > SCANNING */
533 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
534 wdev->conn->params.bssid,
535 NULL, 0, reason, false);
536 out:
537 cfg80211_sme_free(wdev);
538 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900539}
540
Johannes Bergceca7b72013-05-16 00:55:45 +0200541/*
542 * code shared for in-device and software SME
543 */
544
545static bool cfg80211_is_all_idle(void)
546{
547 struct cfg80211_registered_device *rdev;
548 struct wireless_dev *wdev;
549 bool is_all_idle = true;
550
551 /*
552 * All devices must be idle as otherwise if you are actively
553 * scanning some new beacon hints could be learned and would
554 * count as new regulatory hints.
555 */
556 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
557 list_for_each_entry(wdev, &rdev->wdev_list, list) {
558 wdev_lock(wdev);
559 if (wdev->conn || wdev->current_bss)
560 is_all_idle = false;
561 wdev_unlock(wdev);
562 }
563 }
564
565 return is_all_idle;
566}
567
568static void disconnect_work(struct work_struct *work)
569{
570 rtnl_lock();
571 if (cfg80211_is_all_idle())
572 regulatory_hint_disconnect();
573 rtnl_unlock();
574}
575
576static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
577
578
579/*
580 * API calls for drivers implementing connect/disconnect and
581 * SME event handling
582 */
583
Ben Greear6f390902013-06-19 14:06:25 -0700584/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200585void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
586 const u8 *req_ie, size_t req_ie_len,
587 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200588 u16 status, bool wextev,
589 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200590{
591 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100592 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200593#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200594 union iwreq_data wrqu;
595#endif
596
Johannes Berg667503dd2009-07-07 03:56:11 +0200597 ASSERT_WDEV_LOCK(wdev);
598
Johannes Berg074ac8d2010-09-16 14:58:22 +0200599 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700600 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
601 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200602 return;
Ben Greear6f390902013-06-19 14:06:25 -0700603 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200604
Johannes Bergea416a72009-08-17 12:22:14 +0200605 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200606 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200607 resp_ie, resp_ie_len,
608 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200609
Johannes Berg3d23e342009-09-29 23:27:28 +0200610#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200611 if (wextev) {
612 if (req_ie && status == WLAN_STATUS_SUCCESS) {
613 memset(&wrqu, 0, sizeof(wrqu));
614 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800615 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200616 }
617
618 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
619 memset(&wrqu, 0, sizeof(wrqu));
620 wrqu.data.length = resp_ie_len;
621 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
622 }
623
624 memset(&wrqu, 0, sizeof(wrqu));
625 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200626 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200627 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200628 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
629 wdev->wext.prev_bssid_valid = true;
630 }
Johannes Berge45cd822009-07-02 09:58:04 +0200631 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
632 }
633#endif
634
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200635 if (wdev->current_bss) {
636 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100637 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200638 wdev->current_bss = NULL;
639 }
640
Johannes Bergfffd0932009-07-08 14:22:54 +0200641 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200642 kfree(wdev->connect_keys);
643 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200644 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200645 if (bss) {
646 cfg80211_unhold_bss(bss_from_pub(bss));
647 cfg80211_put_bss(wdev->wiphy, bss);
648 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200649 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200650 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200651
Johannes Bergf1940c52013-06-19 13:21:15 +0200652 if (!bss) {
653 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
Johannes Bergceca7b72013-05-16 00:55:45 +0200654 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200655 wdev->ssid, wdev->ssid_len,
656 WLAN_CAPABILITY_ESS,
657 WLAN_CAPABILITY_ESS);
Johannes Bergf1940c52013-06-19 13:21:15 +0200658 if (WARN_ON(!bss))
659 return;
660 cfg80211_hold_bss(bss_from_pub(bss));
661 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200662
Johannes Bergfffd0932009-07-08 14:22:54 +0200663 wdev->current_bss = bss_from_pub(bss);
664
Johannes Bergfffd0932009-07-08 14:22:54 +0200665 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700666
Johannes Berg9caf0362012-11-29 01:25:20 +0100667 rcu_read_lock();
668 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
669 if (!country_ie) {
670 rcu_read_unlock();
671 return;
672 }
673
674 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
675 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700676
677 if (!country_ie)
678 return;
679
680 /*
681 * ieee80211_bss_get_ie() ensures we can access:
682 * - country_ie + 2, the start of the country ie data, and
683 * - and country_ie[1] which is the IE length
684 */
Johannes Berg1a919312012-12-03 17:21:11 +0100685 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
686 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100687 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688}
Johannes Bergf2129352009-07-01 21:26:56 +0200689
690void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
691 const u8 *req_ie, size_t req_ie_len,
692 const u8 *resp_ie, size_t resp_ie_len,
693 u16 status, gfp_t gfp)
694{
Johannes Berg667503dd2009-07-07 03:56:11 +0200695 struct wireless_dev *wdev = dev->ieee80211_ptr;
696 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
697 struct cfg80211_event *ev;
698 unsigned long flags;
699
700 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
701 if (!ev)
702 return;
703
704 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800705 if (bssid)
706 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700707 if (req_ie_len) {
708 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
709 ev->cr.req_ie_len = req_ie_len;
710 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
711 }
712 if (resp_ie_len) {
713 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
714 ev->cr.resp_ie_len = resp_ie_len;
715 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
716 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200717 ev->cr.status = status;
718
719 spin_lock_irqsave(&wdev->event_lock, flags);
720 list_add_tail(&ev->list, &wdev->event_list);
721 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100722 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200723}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200724EXPORT_SYMBOL(cfg80211_connect_result);
725
Ben Greear0e3a39b2013-06-19 14:06:27 -0700726/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300727void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530728 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200729 const u8 *req_ie, size_t req_ie_len,
730 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200731{
Johannes Berg3d23e342009-09-29 23:27:28 +0200732#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200733 union iwreq_data wrqu;
734#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200735 ASSERT_WDEV_LOCK(wdev);
736
Johannes Berg074ac8d2010-09-16 14:58:22 +0200737 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
738 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530739 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200740
Johannes Bergceca7b72013-05-16 00:55:45 +0200741 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530742 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100745 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200746 wdev->current_bss = NULL;
747
Johannes Berg19957bb2009-07-02 17:20:43 +0200748 cfg80211_hold_bss(bss_from_pub(bss));
749 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200750
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530751 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200752 req_ie, req_ie_len, resp_ie, resp_ie_len,
753 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200754
Johannes Berg3d23e342009-09-29 23:27:28 +0200755#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200756 if (req_ie) {
757 memset(&wrqu, 0, sizeof(wrqu));
758 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800759 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200760 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200761 }
762
763 if (resp_ie) {
764 memset(&wrqu, 0, sizeof(wrqu));
765 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200766 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
767 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200768 }
769
770 memset(&wrqu, 0, sizeof(wrqu));
771 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530772 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
773 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200774 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200775 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200776#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530777
778 return;
779out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100780 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200781}
Johannes Berg667503dd2009-07-07 03:56:11 +0200782
Jouni Malinened9d0102011-05-16 19:40:15 +0300783void cfg80211_roamed(struct net_device *dev,
784 struct ieee80211_channel *channel,
785 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200786 const u8 *req_ie, size_t req_ie_len,
787 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
788{
789 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530790 struct cfg80211_bss *bss;
791
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530792 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
793 wdev->ssid_len, WLAN_CAPABILITY_ESS,
794 WLAN_CAPABILITY_ESS);
795 if (WARN_ON(!bss))
796 return;
797
798 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
799 resp_ie_len, gfp);
800}
801EXPORT_SYMBOL(cfg80211_roamed);
802
Ben Greear0e3a39b2013-06-19 14:06:27 -0700803/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530804void cfg80211_roamed_bss(struct net_device *dev,
805 struct cfg80211_bss *bss, const u8 *req_ie,
806 size_t req_ie_len, const u8 *resp_ie,
807 size_t resp_ie_len, gfp_t gfp)
808{
809 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200810 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
811 struct cfg80211_event *ev;
812 unsigned long flags;
813
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530814 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200815 return;
816
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530817 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
818 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100819 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530820 return;
821 }
822
Johannes Berg667503dd2009-07-07 03:56:11 +0200823 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200824 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
825 ev->rm.req_ie_len = req_ie_len;
826 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
827 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
828 ev->rm.resp_ie_len = resp_ie_len;
829 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530830 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200831
832 spin_lock_irqsave(&wdev->event_lock, flags);
833 list_add_tail(&ev->list, &wdev->event_list);
834 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100835 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200836}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530837EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838
Johannes Berg667503dd2009-07-07 03:56:11 +0200839void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200840 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200841{
842 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200843 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
844 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200845#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200846 union iwreq_data wrqu;
847#endif
848
Johannes Berg667503dd2009-07-07 03:56:11 +0200849 ASSERT_WDEV_LOCK(wdev);
850
Johannes Berg074ac8d2010-09-16 14:58:22 +0200851 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
852 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200853 return;
854
Samuel Ortizb23aa672009-07-01 21:26:54 +0200855 if (wdev->current_bss) {
856 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100857 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200858 }
859
860 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200861 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200862
Johannes Bergfffd0932009-07-08 14:22:54 +0200863 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
864
865 /*
866 * Delete all the keys ... pairwise keys can't really
867 * exist any more anyway, but default keys might.
868 */
869 if (rdev->ops->del_key)
870 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300871 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200872
Johannes Berg3d23e342009-09-29 23:27:28 +0200873#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200874 memset(&wrqu, 0, sizeof(wrqu));
875 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
876 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800877 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200878#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500879
880 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200881}
882
883void cfg80211_disconnected(struct net_device *dev, u16 reason,
884 u8 *ie, size_t ie_len, gfp_t gfp)
885{
Johannes Berg667503dd2009-07-07 03:56:11 +0200886 struct wireless_dev *wdev = dev->ieee80211_ptr;
887 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
888 struct cfg80211_event *ev;
889 unsigned long flags;
890
891 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
892 if (!ev)
893 return;
894
895 ev->type = EVENT_DISCONNECTED;
896 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
897 ev->dc.ie_len = ie_len;
898 memcpy((void *)ev->dc.ie, ie, ie_len);
899 ev->dc.reason = reason;
900
901 spin_lock_irqsave(&wdev->event_lock, flags);
902 list_add_tail(&ev->list, &wdev->event_list);
903 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100904 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200905}
906EXPORT_SYMBOL(cfg80211_disconnected);
907
Johannes Bergceca7b72013-05-16 00:55:45 +0200908/*
909 * API calls for nl80211/wext compatibility code
910 */
Johannes Berg83739b02013-05-15 17:44:01 +0200911int cfg80211_connect(struct cfg80211_registered_device *rdev,
912 struct net_device *dev,
913 struct cfg80211_connect_params *connect,
914 struct cfg80211_cached_keys *connkeys,
915 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200917 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200918 int err;
919
920 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200921
Johannes Bergfffd0932009-07-08 14:22:54 +0200922 if (WARN_ON(wdev->connect_keys)) {
923 kfree(wdev->connect_keys);
924 wdev->connect_keys = NULL;
925 }
926
Ben Greear7e7c8922011-11-18 11:31:59 -0800927 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
928 rdev->wiphy.ht_capa_mod_mask);
929
Johannes Bergfffd0932009-07-08 14:22:54 +0200930 if (connkeys && connkeys->def >= 0) {
931 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200932 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200933
934 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200935 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200936 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200937 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
938 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200939 connect->key_idx = idx;
940 connect->key = connkeys->params[idx].key;
941 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200942
943 /*
944 * If ciphers are not set (e.g. when going through
945 * iwconfig), we have to set them appropriately here.
946 */
947 if (connect->crypto.cipher_group == 0)
948 connect->crypto.cipher_group = cipher;
949
950 if (connect->crypto.n_ciphers_pairwise == 0) {
951 connect->crypto.n_ciphers_pairwise = 1;
952 connect->crypto.ciphers_pairwise[0] = cipher;
953 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200954 }
955 }
956
Johannes Bergceca7b72013-05-16 00:55:45 +0200957 wdev->connect_keys = connkeys;
958 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
959 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200960
Johannes Bergceca7b72013-05-16 00:55:45 +0200961 if (!rdev->ops->connect)
962 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
963 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300964 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200965
Johannes Bergceca7b72013-05-16 00:55:45 +0200966 if (err) {
967 wdev->connect_keys = NULL;
968 wdev->ssid_len = 0;
969 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200970 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200971
972 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200973}
974
Johannes Berg83739b02013-05-15 17:44:01 +0200975int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
976 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200977{
Johannes Berg6829c872009-07-02 09:13:27 +0200978 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +0200979 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200980
Johannes Berg667503dd2009-07-07 03:56:11 +0200981 ASSERT_WDEV_LOCK(wdev);
982
Johannes Bergfffd0932009-07-08 14:22:54 +0200983 kfree(wdev->connect_keys);
984 wdev->connect_keys = NULL;
985
Johannes Bergdee8a972013-08-13 09:23:57 +0200986 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +0200987 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +0200988 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +0200989 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +0200990 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300991 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200992
Johannes Bergceca7b72013-05-16 00:55:45 +0200993 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200994}