blob: c854f1ce22db539aa1ba0d339712acef8a834f4d [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 Berg667503d2009-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
Ilan Peerbdfbec22014-01-09 11:37:23 +020073 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020074 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020075 else
76 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020077
Johannes Berg6829c872009-07-02 09:13:27 +020078 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
79 sizeof(request->channels[0]) * n_channels,
80 GFP_KERNEL);
81 if (!request)
82 return -ENOMEM;
83
Johannes Berg6829c872009-07-02 09:13:27 +020084 if (wdev->conn->params.channel)
85 request->channels[0] = wdev->conn->params.channel;
86 else {
87 int i = 0, j;
88 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053089 struct ieee80211_supported_band *bands;
90 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020091
92 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +053093 bands = wdev->wiphy->bands[band];
94 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +020095 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053096 for (j = 0; j < bands->n_channels; j++) {
97 channel = &bands->channels[j];
98 if (channel->flags & IEEE80211_CHAN_DISABLED)
99 continue;
100 request->channels[i++] = channel;
101 }
102 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200103 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530104 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200105 }
106 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200107 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200108 request->n_ssids = 1;
109
110 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
111 wdev->conn->params.ssid_len);
112 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
113
Johannes Bergfd014282012-06-18 19:17:03 +0200114 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200115 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700116 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200117
Johannes Berg79c97e92009-07-07 03:56:12 +0200118 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200119
Hila Gonene35e4d22012-06-27 17:19:42 +0300120 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200121 if (!err) {
122 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200123 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200124 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200125 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200126 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200127 kfree(request);
128 }
129 return err;
130}
131
132static int cfg80211_conn_do_work(struct wireless_dev *wdev)
133{
Johannes Berg79c97e92009-07-07 03:56:12 +0200134 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200135 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100136 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200137 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200138
Johannes Berg667503d2009-07-07 03:56:11 +0200139 ASSERT_WDEV_LOCK(wdev);
140
Johannes Berg6829c872009-07-02 09:13:27 +0200141 if (!wdev->conn)
142 return 0;
143
Johannes Berg19957bb2009-07-02 17:20:43 +0200144 params = &wdev->conn->params;
145
Johannes Berg6829c872009-07-02 09:13:27 +0200146 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200147 case CFG80211_CONN_SCANNING:
148 /* didn't find it during scan ... */
149 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200150 case CFG80211_CONN_SCAN_AGAIN:
151 return cfg80211_conn_scan(wdev);
152 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200153 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200154 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200155 return cfg80211_mlme_auth(rdev, wdev->netdev,
156 params->channel, params->auth_type,
157 params->bssid,
158 params->ssid, params->ssid_len,
159 NULL, 0,
160 params->key, params->key_len,
161 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200162 case CFG80211_CONN_AUTH_FAILED:
163 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200164 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200165 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200166 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200167 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100168 req.prev_bssid = wdev->conn->prev_bssid;
169 req.ie = params->ie;
170 req.ie_len = params->ie_len;
171 req.use_mfp = params->mfp != NL80211_MFP_NO;
172 req.crypto = params->crypto;
173 req.flags = params->flags;
174 req.ht_capa = params->ht_capa;
175 req.ht_capa_mask = params->ht_capa_mask;
176 req.vht_capa = params->vht_capa;
177 req.vht_capa_mask = params->vht_capa_mask;
178
Johannes Berg91bf9b22013-05-15 17:44:01 +0200179 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
180 params->bssid, params->ssid,
181 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200182 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200183 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
184 NULL, 0,
185 WLAN_REASON_DEAUTH_LEAVING,
186 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200187 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200188 case CFG80211_CONN_ASSOC_FAILED:
189 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
190 NULL, 0,
191 WLAN_REASON_DEAUTH_LEAVING, false);
192 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200193 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200194 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
195 NULL, 0,
196 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200197 /* free directly, disconnected event already sent */
198 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200199 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200200 default:
201 return 0;
202 }
203}
204
205void cfg80211_conn_work(struct work_struct *work)
206{
Johannes Berg79c97e92009-07-07 03:56:12 +0200207 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200208 container_of(work, struct cfg80211_registered_device, conn_work);
209 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100210 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200211
212 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200213
Johannes Berg89a54e42012-06-15 14:33:17 +0200214 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200215 if (!wdev->netdev)
216 continue;
217
Johannes Berg667503d2009-07-07 03:56:11 +0200218 wdev_lock(wdev);
219 if (!netif_running(wdev->netdev)) {
220 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200221 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200222 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200223 if (!wdev->conn ||
224 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503d2009-07-07 03:56:11 +0200225 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200226 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200227 }
Johannes Berg7400f422009-10-31 07:40:37 +0100228 if (wdev->conn->params.bssid) {
229 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
230 bssid = bssid_buf;
231 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200232 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503d2009-07-07 03:56:11 +0200233 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900234 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200235 NULL, 0, NULL, 0,
236 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200237 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200238 cfg80211_sme_free(wdev);
239 }
Johannes Berg667503d2009-07-07 03:56:11 +0200240 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200241 }
242
Johannes Berg6829c872009-07-02 09:13:27 +0200243 rtnl_unlock();
244}
245
Ben Greear0e3a39b2013-06-19 14:06:27 -0700246/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700247static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200248{
Johannes Berg79c97e92009-07-07 03:56:12 +0200249 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200250 struct cfg80211_bss *bss;
251 u16 capa = WLAN_CAPABILITY_ESS;
252
Johannes Berg667503d2009-07-07 03:56:11 +0200253 ASSERT_WDEV_LOCK(wdev);
254
Johannes Berg6829c872009-07-02 09:13:27 +0200255 if (wdev->conn->params.privacy)
256 capa |= WLAN_CAPABILITY_PRIVACY;
257
Jouni Malinened9d0102011-05-16 19:40:15 +0300258 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
259 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200260 wdev->conn->params.ssid,
261 wdev->conn->params.ssid_len,
262 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
263 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200264 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700265 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200266
267 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
268 wdev->conn->params.bssid = wdev->conn->bssid;
269 wdev->conn->params.channel = bss->channel;
270 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200271 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200272
Johannes Bergbbac31f2009-09-16 09:04:26 -0700273 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200274}
275
Johannes Berg667503d2009-07-07 03:56:11 +0200276static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200277{
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200279 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700280 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200281
Johannes Berg667503d2009-07-07 03:56:11 +0200282 ASSERT_WDEV_LOCK(wdev);
283
Zhu Yid4b1a682009-07-16 17:34:14 +0800284 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200285 return;
286
287 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
288 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
289 return;
290
Johannes Bergbbac31f2009-09-16 09:04:26 -0700291 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200292 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100293 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200294 else
295 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200296}
297
Johannes Berg667503d2009-07-07 03:56:11 +0200298void cfg80211_sme_scan_done(struct net_device *dev)
299{
300 struct wireless_dev *wdev = dev->ieee80211_ptr;
301
302 wdev_lock(wdev);
303 __cfg80211_sme_scan_done(dev);
304 wdev_unlock(wdev);
305}
306
Johannes Bergceca7b72013-05-16 00:55:45 +0200307void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200308{
Johannes Berg6829c872009-07-02 09:13:27 +0200309 struct wiphy *wiphy = wdev->wiphy;
310 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
311 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
312 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
313
Johannes Berg667503d2009-07-07 03:56:11 +0200314 ASSERT_WDEV_LOCK(wdev);
315
Johannes Bergceca7b72013-05-16 00:55:45 +0200316 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200317 return;
318
319 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
320 wdev->conn->auto_auth &&
321 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
322 /* select automatically between only open, shared, leap */
323 switch (wdev->conn->params.auth_type) {
324 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200325 if (wdev->connect_keys)
326 wdev->conn->params.auth_type =
327 NL80211_AUTHTYPE_SHARED_KEY;
328 else
329 wdev->conn->params.auth_type =
330 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200331 break;
332 case NL80211_AUTHTYPE_SHARED_KEY:
333 wdev->conn->params.auth_type =
334 NL80211_AUTHTYPE_NETWORK_EAP;
335 break;
336 default:
337 /* huh? */
338 wdev->conn->params.auth_type =
339 NL80211_AUTHTYPE_OPEN_SYSTEM;
340 break;
341 }
342 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
343 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200344 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200345 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
346 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200347 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200348 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200349 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
350 schedule_work(&rdev->conn_work);
351 }
352}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200353
Johannes Bergceca7b72013-05-16 00:55:45 +0200354bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200355{
Johannes Bergceca7b72013-05-16 00:55:45 +0200356 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200357
Johannes Bergceca7b72013-05-16 00:55:45 +0200358 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200359 return false;
360
Johannes Bergceca7b72013-05-16 00:55:45 +0200361 if (status == WLAN_STATUS_SUCCESS) {
362 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200363 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200364 }
365
366 if (wdev->conn->prev_bssid_valid) {
367 /*
368 * Some stupid APs don't accept reassoc, so we
369 * need to fall back to trying regular assoc;
370 * return true so no event is sent to userspace.
371 */
372 wdev->conn->prev_bssid_valid = false;
373 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
374 schedule_work(&rdev->conn_work);
375 return true;
376 }
377
Johannes Berg923a0e72013-06-28 11:38:54 +0200378 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200379 schedule_work(&rdev->conn_work);
380 return false;
381}
382
383void cfg80211_sme_deauth(struct wireless_dev *wdev)
384{
385 cfg80211_sme_free(wdev);
386}
387
388void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
389{
Johannes Berg923a0e72013-06-28 11:38:54 +0200390 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
391
392 if (!wdev->conn)
393 return;
394
395 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
396 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200397}
398
399void cfg80211_sme_disassoc(struct wireless_dev *wdev)
400{
401 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
402
403 if (!wdev->conn)
404 return;
405
406 wdev->conn->state = CFG80211_CONN_DEAUTH;
407 schedule_work(&rdev->conn_work);
408}
409
410void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
411{
Johannes Berg923a0e72013-06-28 11:38:54 +0200412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
413
414 if (!wdev->conn)
415 return;
416
417 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
418 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200419}
420
421static int cfg80211_sme_connect(struct wireless_dev *wdev,
422 struct cfg80211_connect_params *connect,
423 const u8 *prev_bssid)
424{
425 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
426 struct cfg80211_bss *bss;
427 int err;
428
429 if (!rdev->ops->auth || !rdev->ops->assoc)
430 return -EOPNOTSUPP;
431
432 if (wdev->current_bss)
433 return -EALREADY;
434
435 if (WARN_ON(wdev->conn))
436 return -EINPROGRESS;
437
438 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
439 if (!wdev->conn)
440 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200441
442 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200443 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200444 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200445 memcpy(&wdev->conn->params, connect, sizeof(*connect));
446 if (connect->bssid) {
447 wdev->conn->params.bssid = wdev->conn->bssid;
448 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
449 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200450
Johannes Bergceca7b72013-05-16 00:55:45 +0200451 if (connect->ie) {
452 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
453 GFP_KERNEL);
454 wdev->conn->params.ie = wdev->conn->ie;
455 if (!wdev->conn->ie) {
456 kfree(wdev->conn);
457 wdev->conn = NULL;
458 return -ENOMEM;
459 }
460 }
461
462 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
463 wdev->conn->auto_auth = true;
464 /* start with open system ... should mostly work */
465 wdev->conn->params.auth_type =
466 NL80211_AUTHTYPE_OPEN_SYSTEM;
467 } else {
468 wdev->conn->auto_auth = false;
469 }
470
471 wdev->conn->params.ssid = wdev->ssid;
472 wdev->conn->params.ssid_len = connect->ssid_len;
473
474 /* see if we have the bss already */
475 bss = cfg80211_get_conn_bss(wdev);
476
477 if (prev_bssid) {
478 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
479 wdev->conn->prev_bssid_valid = true;
480 }
481
482 /* we're good if we have a matching bss struct */
483 if (bss) {
484 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
485 err = cfg80211_conn_do_work(wdev);
486 cfg80211_put_bss(wdev->wiphy, bss);
487 } else {
488 /* otherwise we'll need to scan for the AP first */
489 err = cfg80211_conn_scan(wdev);
490
491 /*
492 * If we can't scan right now, then we need to scan again
493 * after the current scan finished, since the parameters
494 * changed (unless we find a good AP anyway).
495 */
496 if (err == -EBUSY) {
497 err = 0;
498 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
499 }
500 }
501
502 if (err)
503 cfg80211_sme_free(wdev);
504
505 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200506}
507
Johannes Bergceca7b72013-05-16 00:55:45 +0200508static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900509{
Johannes Bergceca7b72013-05-16 00:55:45 +0200510 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
511 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900512
Johannes Bergceca7b72013-05-16 00:55:45 +0200513 if (!wdev->conn)
514 return 0;
515
516 if (!rdev->ops->deauth)
517 return -EOPNOTSUPP;
518
519 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
520 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
521 err = 0;
522 goto out;
523 }
524
525 /* wdev->conn->params.bssid must be set if > SCANNING */
526 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
527 wdev->conn->params.bssid,
528 NULL, 0, reason, false);
529 out:
530 cfg80211_sme_free(wdev);
531 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900532}
533
Johannes Bergceca7b72013-05-16 00:55:45 +0200534/*
535 * code shared for in-device and software SME
536 */
537
538static bool cfg80211_is_all_idle(void)
539{
540 struct cfg80211_registered_device *rdev;
541 struct wireless_dev *wdev;
542 bool is_all_idle = true;
543
544 /*
545 * All devices must be idle as otherwise if you are actively
546 * scanning some new beacon hints could be learned and would
547 * count as new regulatory hints.
548 */
549 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
550 list_for_each_entry(wdev, &rdev->wdev_list, list) {
551 wdev_lock(wdev);
552 if (wdev->conn || wdev->current_bss)
553 is_all_idle = false;
554 wdev_unlock(wdev);
555 }
556 }
557
558 return is_all_idle;
559}
560
561static void disconnect_work(struct work_struct *work)
562{
563 rtnl_lock();
564 if (cfg80211_is_all_idle())
565 regulatory_hint_disconnect();
566 rtnl_unlock();
567}
568
569static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
570
571
572/*
573 * API calls for drivers implementing connect/disconnect and
574 * SME event handling
575 */
576
Ben Greear6f390902013-06-19 14:06:25 -0700577/* This method must consume bss one way or another */
Johannes Berg667503d2009-07-07 03:56:11 +0200578void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
579 const u8 *req_ie, size_t req_ie_len,
580 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200581 u16 status, bool wextev,
582 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200583{
584 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100585 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200586#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200587 union iwreq_data wrqu;
588#endif
589
Johannes Berg667503d2009-07-07 03:56:11 +0200590 ASSERT_WDEV_LOCK(wdev);
591
Johannes Berg074ac8d2010-09-16 14:58:22 +0200592 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700593 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
594 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595 return;
Ben Greear6f390902013-06-19 14:06:25 -0700596 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200597
Johannes Bergea416a72009-08-17 12:22:14 +0200598 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200599 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200600 resp_ie, resp_ie_len,
601 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200602
Johannes Berg3d23e342009-09-29 23:27:28 +0200603#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200604 if (wextev) {
605 if (req_ie && status == WLAN_STATUS_SUCCESS) {
606 memset(&wrqu, 0, sizeof(wrqu));
607 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800608 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200609 }
610
611 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
612 memset(&wrqu, 0, sizeof(wrqu));
613 wrqu.data.length = resp_ie_len;
614 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
615 }
616
617 memset(&wrqu, 0, sizeof(wrqu));
618 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200619 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200620 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200621 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
622 wdev->wext.prev_bssid_valid = true;
623 }
Johannes Berge45cd822009-07-02 09:58:04 +0200624 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
625 }
626#endif
627
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200628 if (wdev->current_bss) {
629 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100630 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200631 wdev->current_bss = NULL;
632 }
633
Johannes Bergfffd0932009-07-08 14:22:54 +0200634 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200635 kfree(wdev->connect_keys);
636 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200637 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200638 if (bss) {
639 cfg80211_unhold_bss(bss_from_pub(bss));
640 cfg80211_put_bss(wdev->wiphy, bss);
641 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200642 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200643 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200644
Johannes Bergf1940c52013-06-19 13:21:15 +0200645 if (!bss) {
646 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
Johannes Bergceca7b72013-05-16 00:55:45 +0200647 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200648 wdev->ssid, wdev->ssid_len,
649 WLAN_CAPABILITY_ESS,
650 WLAN_CAPABILITY_ESS);
Johannes Bergf1940c52013-06-19 13:21:15 +0200651 if (WARN_ON(!bss))
652 return;
653 cfg80211_hold_bss(bss_from_pub(bss));
654 }
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 Berg667503d2009-07-07 03:56:11 +0200688 struct wireless_dev *wdev = dev->ieee80211_ptr;
689 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
690 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 Berg667503d2009-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 Berg667503d2009-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 Berg667503d2009-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
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530744 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200745 req_ie, req_ie_len, resp_ie, resp_ie_len,
746 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200747
Johannes Berg3d23e342009-09-29 23:27:28 +0200748#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200749 if (req_ie) {
750 memset(&wrqu, 0, sizeof(wrqu));
751 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800752 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200753 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200754 }
755
756 if (resp_ie) {
757 memset(&wrqu, 0, sizeof(wrqu));
758 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200759 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
760 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200761 }
762
763 memset(&wrqu, 0, sizeof(wrqu));
764 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530765 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
766 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200767 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200768 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200769#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530770
771 return;
772out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100773 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200774}
Johannes Berg667503d2009-07-07 03:56:11 +0200775
Jouni Malinened9d0102011-05-16 19:40:15 +0300776void cfg80211_roamed(struct net_device *dev,
777 struct ieee80211_channel *channel,
778 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200779 const u8 *req_ie, size_t req_ie_len,
780 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
781{
782 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530783 struct cfg80211_bss *bss;
784
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530785 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
786 wdev->ssid_len, WLAN_CAPABILITY_ESS,
787 WLAN_CAPABILITY_ESS);
788 if (WARN_ON(!bss))
789 return;
790
791 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
792 resp_ie_len, gfp);
793}
794EXPORT_SYMBOL(cfg80211_roamed);
795
Ben Greear0e3a39b2013-06-19 14:06:27 -0700796/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530797void cfg80211_roamed_bss(struct net_device *dev,
798 struct cfg80211_bss *bss, const u8 *req_ie,
799 size_t req_ie_len, const u8 *resp_ie,
800 size_t resp_ie_len, gfp_t gfp)
801{
802 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200803 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
804 struct cfg80211_event *ev;
805 unsigned long flags;
806
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530807 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200808 return;
809
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530810 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
811 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100812 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530813 return;
814 }
815
Johannes Berg667503d2009-07-07 03:56:11 +0200816 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200817 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
818 ev->rm.req_ie_len = req_ie_len;
819 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
820 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
821 ev->rm.resp_ie_len = resp_ie_len;
822 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530823 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200824
825 spin_lock_irqsave(&wdev->event_lock, flags);
826 list_add_tail(&ev->list, &wdev->event_list);
827 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100828 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200829}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530830EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200831
Johannes Berg667503d2009-07-07 03:56:11 +0200832void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200833 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200834{
835 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200836 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
837 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200838#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200839 union iwreq_data wrqu;
840#endif
841
Johannes Berg667503d2009-07-07 03:56:11 +0200842 ASSERT_WDEV_LOCK(wdev);
843
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
845 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200846 return;
847
Samuel Ortizb23aa672009-07-01 21:26:54 +0200848 if (wdev->current_bss) {
849 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100850 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200851 }
852
853 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200854 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200855
Johannes Bergfffd0932009-07-08 14:22:54 +0200856 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
857
858 /*
859 * Delete all the keys ... pairwise keys can't really
860 * exist any more anyway, but default keys might.
861 */
862 if (rdev->ops->del_key)
863 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300864 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200865
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800866 rdev_set_qos_map(rdev, dev, NULL);
867
Johannes Berg3d23e342009-09-29 23:27:28 +0200868#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200869 memset(&wrqu, 0, sizeof(wrqu));
870 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
871 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800872 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200873#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500874
875 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200876}
877
878void cfg80211_disconnected(struct net_device *dev, u16 reason,
879 u8 *ie, size_t ie_len, gfp_t gfp)
880{
Johannes Berg667503d2009-07-07 03:56:11 +0200881 struct wireless_dev *wdev = dev->ieee80211_ptr;
882 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
883 struct cfg80211_event *ev;
884 unsigned long flags;
885
886 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
887 if (!ev)
888 return;
889
890 ev->type = EVENT_DISCONNECTED;
891 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
892 ev->dc.ie_len = ie_len;
893 memcpy((void *)ev->dc.ie, ie, ie_len);
894 ev->dc.reason = reason;
895
896 spin_lock_irqsave(&wdev->event_lock, flags);
897 list_add_tail(&ev->list, &wdev->event_list);
898 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100899 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200900}
901EXPORT_SYMBOL(cfg80211_disconnected);
902
Johannes Bergceca7b72013-05-16 00:55:45 +0200903/*
904 * API calls for nl80211/wext compatibility code
905 */
Johannes Berg83739b02013-05-15 17:44:01 +0200906int cfg80211_connect(struct cfg80211_registered_device *rdev,
907 struct net_device *dev,
908 struct cfg80211_connect_params *connect,
909 struct cfg80211_cached_keys *connkeys,
910 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200911{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200913 int err;
914
915 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916
Johannes Bergfffd0932009-07-08 14:22:54 +0200917 if (WARN_ON(wdev->connect_keys)) {
918 kfree(wdev->connect_keys);
919 wdev->connect_keys = NULL;
920 }
921
Ben Greear7e7c8922011-11-18 11:31:59 -0800922 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
923 rdev->wiphy.ht_capa_mod_mask);
924
Johannes Bergfffd0932009-07-08 14:22:54 +0200925 if (connkeys && connkeys->def >= 0) {
926 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200927 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200928
929 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200930 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200931 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200932 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
933 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200934 connect->key_idx = idx;
935 connect->key = connkeys->params[idx].key;
936 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200937
938 /*
939 * If ciphers are not set (e.g. when going through
940 * iwconfig), we have to set them appropriately here.
941 */
942 if (connect->crypto.cipher_group == 0)
943 connect->crypto.cipher_group = cipher;
944
945 if (connect->crypto.n_ciphers_pairwise == 0) {
946 connect->crypto.n_ciphers_pairwise = 1;
947 connect->crypto.ciphers_pairwise[0] = cipher;
948 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200949 }
950 }
951
Johannes Bergceca7b72013-05-16 00:55:45 +0200952 wdev->connect_keys = connkeys;
953 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
954 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200955
Johannes Bergceca7b72013-05-16 00:55:45 +0200956 if (!rdev->ops->connect)
957 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
958 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300959 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200960
Johannes Bergceca7b72013-05-16 00:55:45 +0200961 if (err) {
962 wdev->connect_keys = NULL;
963 wdev->ssid_len = 0;
964 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200965 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200966
967 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200968}
969
Johannes Berg83739b02013-05-15 17:44:01 +0200970int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
971 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200972{
Johannes Berg6829c872009-07-02 09:13:27 +0200973 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +0200974 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200975
Johannes Berg667503d2009-07-07 03:56:11 +0200976 ASSERT_WDEV_LOCK(wdev);
977
Johannes Bergfffd0932009-07-08 14:22:54 +0200978 kfree(wdev->connect_keys);
979 wdev->connect_keys = NULL;
980
Johannes Bergdee8a972013-08-13 09:23:57 +0200981 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +0200982 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +0200983 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +0200984 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +0200985 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +0300986 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200987
Johannes Bergceca7b72013-05-16 00:55:45 +0200988 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200989}