blob: 32dac8cdd2e3744661f0e2babf9148ea781f2b24 [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,
37 CFG80211_CONN_ASSOCIATE_NEXT,
38 CFG80211_CONN_ASSOCIATING,
Johannes Bergceca7b72013-05-16 00:55:45 +020039 CFG80211_CONN_DEAUTH,
40 CFG80211_CONN_CONNECTED,
Johannes Berg6829c872009-07-02 09:13:27 +020041 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020042 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020043 u8 *ie;
44 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020045 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020046};
47
Johannes Bergceca7b72013-05-16 00:55:45 +020048static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050049{
Johannes Bergceca7b72013-05-16 00:55:45 +020050 if (!wdev->conn)
51 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050052
Johannes Bergceca7b72013-05-16 00:55:45 +020053 kfree(wdev->conn->ie);
54 kfree(wdev->conn);
55 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050056}
57
Johannes Berg6829c872009-07-02 09:13:27 +020058static int cfg80211_conn_scan(struct wireless_dev *wdev)
59{
Johannes Berg79c97e92009-07-07 03:56:12 +020060 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020061 struct cfg80211_scan_request *request;
62 int n_channels, err;
63
64 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020065 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503d2009-07-07 03:56:11 +020066 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020067
Johannes Berg79c97e92009-07-07 03:56:12 +020068 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020069 return -EBUSY;
70
71 if (wdev->conn->params.channel) {
72 n_channels = 1;
73 } else {
74 enum ieee80211_band band;
75 n_channels = 0;
76
77 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
78 if (!wdev->wiphy->bands[band])
79 continue;
80 n_channels += wdev->wiphy->bands[band]->n_channels;
81 }
82 }
83 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
84 sizeof(request->channels[0]) * n_channels,
85 GFP_KERNEL);
86 if (!request)
87 return -ENOMEM;
88
Johannes Berg6829c872009-07-02 09:13:27 +020089 if (wdev->conn->params.channel)
90 request->channels[0] = wdev->conn->params.channel;
91 else {
92 int i = 0, j;
93 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053094 struct ieee80211_supported_band *bands;
95 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020096
97 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +053098 bands = wdev->wiphy->bands[band];
99 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200100 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530101 for (j = 0; j < bands->n_channels; j++) {
102 channel = &bands->channels[j];
103 if (channel->flags & IEEE80211_CHAN_DISABLED)
104 continue;
105 request->channels[i++] = channel;
106 }
107 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200108 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530109 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200110 }
111 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200112 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200113 request->n_ssids = 1;
114
115 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
116 wdev->conn->params.ssid_len);
117 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
118
Johannes Bergfd014282012-06-18 19:17:03 +0200119 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200120 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700121 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200122
Johannes Berg79c97e92009-07-07 03:56:12 +0200123 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200124
Hila Gonene35e4d22012-06-27 17:19:42 +0300125 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200126 if (!err) {
127 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200128 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200129 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200130 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200131 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200132 kfree(request);
133 }
134 return err;
135}
136
137static int cfg80211_conn_do_work(struct wireless_dev *wdev)
138{
Johannes Berg79c97e92009-07-07 03:56:12 +0200139 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200140 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100141 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200142 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg667503d2009-07-07 03:56:11 +0200144 ASSERT_WDEV_LOCK(wdev);
145
Johannes Berg6829c872009-07-02 09:13:27 +0200146 if (!wdev->conn)
147 return 0;
148
Johannes Berg19957bb2009-07-02 17:20:43 +0200149 params = &wdev->conn->params;
150
Johannes Berg6829c872009-07-02 09:13:27 +0200151 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200152 case CFG80211_CONN_SCANNING:
153 /* didn't find it during scan ... */
154 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200155 case CFG80211_CONN_SCAN_AGAIN:
156 return cfg80211_conn_scan(wdev);
157 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200158 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200159 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200160 return cfg80211_mlme_auth(rdev, wdev->netdev,
161 params->channel, params->auth_type,
162 params->bssid,
163 params->ssid, params->ssid_len,
164 NULL, 0,
165 params->key, params->key_len,
166 params->key_idx, NULL, 0);
Johannes Berg6829c872009-07-02 09:13:27 +0200167 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200168 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200169 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200170 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100171 req.prev_bssid = wdev->conn->prev_bssid;
172 req.ie = params->ie;
173 req.ie_len = params->ie_len;
174 req.use_mfp = params->mfp != NL80211_MFP_NO;
175 req.crypto = params->crypto;
176 req.flags = params->flags;
177 req.ht_capa = params->ht_capa;
178 req.ht_capa_mask = params->ht_capa_mask;
179 req.vht_capa = params->vht_capa;
180 req.vht_capa_mask = params->vht_capa_mask;
181
Johannes Berg91bf9b22013-05-15 17:44:01 +0200182 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
183 params->bssid, params->ssid,
184 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200185 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200186 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
187 NULL, 0,
188 WLAN_REASON_DEAUTH_LEAVING,
189 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200190 return err;
Johannes Bergceca7b72013-05-16 00:55:45 +0200191 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200192 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
193 NULL, 0,
194 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergceca7b72013-05-16 00:55:45 +0200195 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200196 default:
197 return 0;
198 }
199}
200
201void cfg80211_conn_work(struct work_struct *work)
202{
Johannes Berg79c97e92009-07-07 03:56:12 +0200203 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200204 container_of(work, struct cfg80211_registered_device, conn_work);
205 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100206 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200207
208 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200209
Johannes Berg89a54e42012-06-15 14:33:17 +0200210 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200211 if (!wdev->netdev)
212 continue;
213
Johannes Berg667503d2009-07-07 03:56:11 +0200214 wdev_lock(wdev);
215 if (!netif_running(wdev->netdev)) {
216 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200217 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200218 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200219 if (!wdev->conn ||
220 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503d2009-07-07 03:56:11 +0200221 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200222 continue;
Johannes Berg667503d2009-07-07 03:56:11 +0200223 }
Johannes Berg7400f422009-10-31 07:40:37 +0100224 if (wdev->conn->params.bssid) {
225 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
226 bssid = bssid_buf;
227 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200228 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503d2009-07-07 03:56:11 +0200229 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900230 wdev->netdev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200231 NULL, 0, NULL, 0,
232 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200233 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200234 cfg80211_sme_free(wdev);
235 }
Johannes Berg667503d2009-07-07 03:56:11 +0200236 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200237 }
238
Johannes Berg6829c872009-07-02 09:13:27 +0200239 rtnl_unlock();
240}
241
Johannes Bergbbac31f2009-09-16 09:04:26 -0700242static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200243{
Johannes Berg79c97e92009-07-07 03:56:12 +0200244 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200245 struct cfg80211_bss *bss;
246 u16 capa = WLAN_CAPABILITY_ESS;
247
Johannes Berg667503d2009-07-07 03:56:11 +0200248 ASSERT_WDEV_LOCK(wdev);
249
Johannes Berg6829c872009-07-02 09:13:27 +0200250 if (wdev->conn->params.privacy)
251 capa |= WLAN_CAPABILITY_PRIVACY;
252
Jouni Malinened9d0102011-05-16 19:40:15 +0300253 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
254 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200255 wdev->conn->params.ssid,
256 wdev->conn->params.ssid_len,
257 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
258 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200259 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700260 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200261
262 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
263 wdev->conn->params.bssid = wdev->conn->bssid;
264 wdev->conn->params.channel = bss->channel;
265 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200266 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200267
Johannes Bergbbac31f2009-09-16 09:04:26 -0700268 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200269}
270
Johannes Berg667503d2009-07-07 03:56:11 +0200271static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200272{
273 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700275 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200276
Johannes Berg667503d2009-07-07 03:56:11 +0200277 ASSERT_WDEV_LOCK(wdev);
278
Zhu Yid4b1a682009-07-16 17:34:14 +0800279 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200280 return;
281
282 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
283 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
284 return;
285
Johannes Bergbbac31f2009-09-16 09:04:26 -0700286 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200287 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100288 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200289 else
290 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200291}
292
Johannes Berg667503d2009-07-07 03:56:11 +0200293void cfg80211_sme_scan_done(struct net_device *dev)
294{
295 struct wireless_dev *wdev = dev->ieee80211_ptr;
296
297 wdev_lock(wdev);
298 __cfg80211_sme_scan_done(dev);
299 wdev_unlock(wdev);
300}
301
Johannes Bergceca7b72013-05-16 00:55:45 +0200302void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200303{
Johannes Berg6829c872009-07-02 09:13:27 +0200304 struct wiphy *wiphy = wdev->wiphy;
305 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
306 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
307 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
308
Johannes Berg667503d2009-07-07 03:56:11 +0200309 ASSERT_WDEV_LOCK(wdev);
310
Johannes Bergceca7b72013-05-16 00:55:45 +0200311 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200312 return;
313
314 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
315 wdev->conn->auto_auth &&
316 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
317 /* select automatically between only open, shared, leap */
318 switch (wdev->conn->params.auth_type) {
319 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200320 if (wdev->connect_keys)
321 wdev->conn->params.auth_type =
322 NL80211_AUTHTYPE_SHARED_KEY;
323 else
324 wdev->conn->params.auth_type =
325 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200326 break;
327 case NL80211_AUTHTYPE_SHARED_KEY:
328 wdev->conn->params.auth_type =
329 NL80211_AUTHTYPE_NETWORK_EAP;
330 break;
331 default:
332 /* huh? */
333 wdev->conn->params.auth_type =
334 NL80211_AUTHTYPE_OPEN_SYSTEM;
335 break;
336 }
337 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
338 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200339 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200340 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
341 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200342 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200343 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200344 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
345 schedule_work(&rdev->conn_work);
346 }
347}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200348
Johannes Bergceca7b72013-05-16 00:55:45 +0200349bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200350{
Johannes Bergceca7b72013-05-16 00:55:45 +0200351 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200352
Johannes Bergceca7b72013-05-16 00:55:45 +0200353 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200354 return false;
355
Johannes Bergceca7b72013-05-16 00:55:45 +0200356 if (status == WLAN_STATUS_SUCCESS) {
357 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200358 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200359 }
360
361 if (wdev->conn->prev_bssid_valid) {
362 /*
363 * Some stupid APs don't accept reassoc, so we
364 * need to fall back to trying regular assoc;
365 * return true so no event is sent to userspace.
366 */
367 wdev->conn->prev_bssid_valid = false;
368 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
369 schedule_work(&rdev->conn_work);
370 return true;
371 }
372
373 wdev->conn->state = CFG80211_CONN_DEAUTH;
374 schedule_work(&rdev->conn_work);
375 return false;
376}
377
378void cfg80211_sme_deauth(struct wireless_dev *wdev)
379{
380 cfg80211_sme_free(wdev);
381}
382
383void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
384{
385 cfg80211_sme_free(wdev);
386}
387
388void cfg80211_sme_disassoc(struct wireless_dev *wdev)
389{
390 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
391
392 if (!wdev->conn)
393 return;
394
395 wdev->conn->state = CFG80211_CONN_DEAUTH;
396 schedule_work(&rdev->conn_work);
397}
398
399void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
400{
401 cfg80211_sme_disassoc(wdev);
402}
403
404static int cfg80211_sme_connect(struct wireless_dev *wdev,
405 struct cfg80211_connect_params *connect,
406 const u8 *prev_bssid)
407{
408 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
409 struct cfg80211_bss *bss;
410 int err;
411
412 if (!rdev->ops->auth || !rdev->ops->assoc)
413 return -EOPNOTSUPP;
414
415 if (wdev->current_bss)
416 return -EALREADY;
417
418 if (WARN_ON(wdev->conn))
419 return -EINPROGRESS;
420
421 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
422 if (!wdev->conn)
423 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200424
425 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200426 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200427 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200428 memcpy(&wdev->conn->params, connect, sizeof(*connect));
429 if (connect->bssid) {
430 wdev->conn->params.bssid = wdev->conn->bssid;
431 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
432 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200433
Johannes Bergceca7b72013-05-16 00:55:45 +0200434 if (connect->ie) {
435 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
436 GFP_KERNEL);
437 wdev->conn->params.ie = wdev->conn->ie;
438 if (!wdev->conn->ie) {
439 kfree(wdev->conn);
440 wdev->conn = NULL;
441 return -ENOMEM;
442 }
443 }
444
445 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
446 wdev->conn->auto_auth = true;
447 /* start with open system ... should mostly work */
448 wdev->conn->params.auth_type =
449 NL80211_AUTHTYPE_OPEN_SYSTEM;
450 } else {
451 wdev->conn->auto_auth = false;
452 }
453
454 wdev->conn->params.ssid = wdev->ssid;
455 wdev->conn->params.ssid_len = connect->ssid_len;
456
457 /* see if we have the bss already */
458 bss = cfg80211_get_conn_bss(wdev);
459
460 if (prev_bssid) {
461 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
462 wdev->conn->prev_bssid_valid = true;
463 }
464
465 /* we're good if we have a matching bss struct */
466 if (bss) {
467 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
468 err = cfg80211_conn_do_work(wdev);
469 cfg80211_put_bss(wdev->wiphy, bss);
470 } else {
471 /* otherwise we'll need to scan for the AP first */
472 err = cfg80211_conn_scan(wdev);
473
474 /*
475 * If we can't scan right now, then we need to scan again
476 * after the current scan finished, since the parameters
477 * changed (unless we find a good AP anyway).
478 */
479 if (err == -EBUSY) {
480 err = 0;
481 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
482 }
483 }
484
485 if (err)
486 cfg80211_sme_free(wdev);
487
488 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200489}
490
Johannes Bergceca7b72013-05-16 00:55:45 +0200491static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900492{
Johannes Bergceca7b72013-05-16 00:55:45 +0200493 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
494 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900495
Johannes Bergceca7b72013-05-16 00:55:45 +0200496 if (!wdev->conn)
497 return 0;
498
499 if (!rdev->ops->deauth)
500 return -EOPNOTSUPP;
501
502 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
503 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
504 err = 0;
505 goto out;
506 }
507
508 /* wdev->conn->params.bssid must be set if > SCANNING */
509 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
510 wdev->conn->params.bssid,
511 NULL, 0, reason, false);
512 out:
513 cfg80211_sme_free(wdev);
514 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900515}
516
Johannes Bergceca7b72013-05-16 00:55:45 +0200517/*
518 * code shared for in-device and software SME
519 */
520
521static bool cfg80211_is_all_idle(void)
522{
523 struct cfg80211_registered_device *rdev;
524 struct wireless_dev *wdev;
525 bool is_all_idle = true;
526
527 /*
528 * All devices must be idle as otherwise if you are actively
529 * scanning some new beacon hints could be learned and would
530 * count as new regulatory hints.
531 */
532 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
533 list_for_each_entry(wdev, &rdev->wdev_list, list) {
534 wdev_lock(wdev);
535 if (wdev->conn || wdev->current_bss)
536 is_all_idle = false;
537 wdev_unlock(wdev);
538 }
539 }
540
541 return is_all_idle;
542}
543
544static void disconnect_work(struct work_struct *work)
545{
546 rtnl_lock();
547 if (cfg80211_is_all_idle())
548 regulatory_hint_disconnect();
549 rtnl_unlock();
550}
551
552static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
553
554
555/*
556 * API calls for drivers implementing connect/disconnect and
557 * SME event handling
558 */
559
Ben Greear6f390902013-06-19 14:06:25 -0700560/* This method must consume bss one way or another */
Johannes Berg667503d2009-07-07 03:56:11 +0200561void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
562 const u8 *req_ie, size_t req_ie_len,
563 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200564 u16 status, bool wextev,
565 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200566{
567 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100568 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200569#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570 union iwreq_data wrqu;
571#endif
572
Johannes Berg667503d2009-07-07 03:56:11 +0200573 ASSERT_WDEV_LOCK(wdev);
574
Johannes Berg074ac8d2010-09-16 14:58:22 +0200575 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700576 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
577 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200578 return;
Ben Greear6f390902013-06-19 14:06:25 -0700579 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200580
Johannes Bergea416a72009-08-17 12:22:14 +0200581 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200582 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200583 resp_ie, resp_ie_len,
584 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200585
Johannes Berg3d23e342009-09-29 23:27:28 +0200586#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200587 if (wextev) {
588 if (req_ie && status == WLAN_STATUS_SUCCESS) {
589 memset(&wrqu, 0, sizeof(wrqu));
590 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800591 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200592 }
593
594 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
595 memset(&wrqu, 0, sizeof(wrqu));
596 wrqu.data.length = resp_ie_len;
597 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
598 }
599
600 memset(&wrqu, 0, sizeof(wrqu));
601 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200602 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200603 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200604 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
605 wdev->wext.prev_bssid_valid = true;
606 }
Johannes Berge45cd822009-07-02 09:58:04 +0200607 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
608 }
609#endif
610
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200611 if (wdev->current_bss) {
612 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100613 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200614 wdev->current_bss = NULL;
615 }
616
Johannes Bergfffd0932009-07-08 14:22:54 +0200617 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200618 kfree(wdev->connect_keys);
619 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200620 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200621 if (bss) {
622 cfg80211_unhold_bss(bss_from_pub(bss));
623 cfg80211_put_bss(wdev->wiphy, bss);
624 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200625 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200626 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200627
Johannes Bergf1940c52013-06-19 13:21:15 +0200628 if (!bss) {
629 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
Johannes Bergceca7b72013-05-16 00:55:45 +0200630 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200631 wdev->ssid, wdev->ssid_len,
632 WLAN_CAPABILITY_ESS,
633 WLAN_CAPABILITY_ESS);
Johannes Bergf1940c52013-06-19 13:21:15 +0200634 if (WARN_ON(!bss))
635 return;
636 cfg80211_hold_bss(bss_from_pub(bss));
637 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200638
Johannes Bergfffd0932009-07-08 14:22:54 +0200639 wdev->current_bss = bss_from_pub(bss);
640
Johannes Bergfffd0932009-07-08 14:22:54 +0200641 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700642
Johannes Berg9caf0362012-11-29 01:25:20 +0100643 rcu_read_lock();
644 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
645 if (!country_ie) {
646 rcu_read_unlock();
647 return;
648 }
649
650 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
651 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700652
653 if (!country_ie)
654 return;
655
656 /*
657 * ieee80211_bss_get_ie() ensures we can access:
658 * - country_ie + 2, the start of the country ie data, and
659 * - and country_ie[1] which is the IE length
660 */
Johannes Berg1a919312012-12-03 17:21:11 +0100661 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
662 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100663 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200664}
Johannes Bergf2129352009-07-01 21:26:56 +0200665
666void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
667 const u8 *req_ie, size_t req_ie_len,
668 const u8 *resp_ie, size_t resp_ie_len,
669 u16 status, gfp_t gfp)
670{
Johannes Berg667503d2009-07-07 03:56:11 +0200671 struct wireless_dev *wdev = dev->ieee80211_ptr;
672 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
673 struct cfg80211_event *ev;
674 unsigned long flags;
675
676 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
677 if (!ev)
678 return;
679
680 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800681 if (bssid)
682 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700683 if (req_ie_len) {
684 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
685 ev->cr.req_ie_len = req_ie_len;
686 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
687 }
688 if (resp_ie_len) {
689 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
690 ev->cr.resp_ie_len = resp_ie_len;
691 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
692 }
Johannes Berg667503d2009-07-07 03:56:11 +0200693 ev->cr.status = status;
694
695 spin_lock_irqsave(&wdev->event_lock, flags);
696 list_add_tail(&ev->list, &wdev->event_list);
697 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100698 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200699}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200700EXPORT_SYMBOL(cfg80211_connect_result);
701
Jouni Malinened9d0102011-05-16 19:40:15 +0300702void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530703 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200704 const u8 *req_ie, size_t req_ie_len,
705 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200706{
Johannes Berg3d23e342009-09-29 23:27:28 +0200707#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200708 union iwreq_data wrqu;
709#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200710 ASSERT_WDEV_LOCK(wdev);
711
Johannes Berg074ac8d2010-09-16 14:58:22 +0200712 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
713 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530714 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200715
Johannes Bergceca7b72013-05-16 00:55:45 +0200716 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530717 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200718
Samuel Ortizb23aa672009-07-01 21:26:54 +0200719 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100720 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200721 wdev->current_bss = NULL;
722
Johannes Berg19957bb2009-07-02 17:20:43 +0200723 cfg80211_hold_bss(bss_from_pub(bss));
724 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200725
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530726 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200727 req_ie, req_ie_len, resp_ie, resp_ie_len,
728 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200729
Johannes Berg3d23e342009-09-29 23:27:28 +0200730#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200731 if (req_ie) {
732 memset(&wrqu, 0, sizeof(wrqu));
733 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800734 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200735 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736 }
737
738 if (resp_ie) {
739 memset(&wrqu, 0, sizeof(wrqu));
740 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200741 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
742 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743 }
744
745 memset(&wrqu, 0, sizeof(wrqu));
746 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530747 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
748 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200749 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200750 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200751#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530752
753 return;
754out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100755 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200756}
Johannes Berg667503d2009-07-07 03:56:11 +0200757
Jouni Malinened9d0102011-05-16 19:40:15 +0300758void cfg80211_roamed(struct net_device *dev,
759 struct ieee80211_channel *channel,
760 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200761 const u8 *req_ie, size_t req_ie_len,
762 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
763{
764 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530765 struct cfg80211_bss *bss;
766
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530767 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
768 wdev->ssid_len, WLAN_CAPABILITY_ESS,
769 WLAN_CAPABILITY_ESS);
770 if (WARN_ON(!bss))
771 return;
772
773 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
774 resp_ie_len, gfp);
775}
776EXPORT_SYMBOL(cfg80211_roamed);
777
778void cfg80211_roamed_bss(struct net_device *dev,
779 struct cfg80211_bss *bss, const u8 *req_ie,
780 size_t req_ie_len, const u8 *resp_ie,
781 size_t resp_ie_len, gfp_t gfp)
782{
783 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200784 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
785 struct cfg80211_event *ev;
786 unsigned long flags;
787
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530788 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200789 return;
790
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530791 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
792 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100793 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530794 return;
795 }
796
Johannes Berg667503d2009-07-07 03:56:11 +0200797 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200798 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
799 ev->rm.req_ie_len = req_ie_len;
800 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
801 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
802 ev->rm.resp_ie_len = resp_ie_len;
803 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530804 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200805
806 spin_lock_irqsave(&wdev->event_lock, flags);
807 list_add_tail(&ev->list, &wdev->event_list);
808 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100809 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200810}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530811EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200812
Johannes Berg667503d2009-07-07 03:56:11 +0200813void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200814 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200815{
816 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200817 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
818 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200819#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200820 union iwreq_data wrqu;
821#endif
822
Johannes Berg667503d2009-07-07 03:56:11 +0200823 ASSERT_WDEV_LOCK(wdev);
824
Johannes Berg074ac8d2010-09-16 14:58:22 +0200825 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
826 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200827 return;
828
Samuel Ortizb23aa672009-07-01 21:26:54 +0200829 if (wdev->current_bss) {
830 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100831 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200832 }
833
834 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200835 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200836
Johannes Bergfffd0932009-07-08 14:22:54 +0200837 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
838
839 /*
840 * Delete all the keys ... pairwise keys can't really
841 * exist any more anyway, but default keys might.
842 */
843 if (rdev->ops->del_key)
844 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300845 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200846
Johannes Berg3d23e342009-09-29 23:27:28 +0200847#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200848 memset(&wrqu, 0, sizeof(wrqu));
849 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
850 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800851 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200852#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500853
854 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200855}
856
857void cfg80211_disconnected(struct net_device *dev, u16 reason,
858 u8 *ie, size_t ie_len, gfp_t gfp)
859{
Johannes Berg667503d2009-07-07 03:56:11 +0200860 struct wireless_dev *wdev = dev->ieee80211_ptr;
861 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
862 struct cfg80211_event *ev;
863 unsigned long flags;
864
865 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
866 if (!ev)
867 return;
868
869 ev->type = EVENT_DISCONNECTED;
870 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
871 ev->dc.ie_len = ie_len;
872 memcpy((void *)ev->dc.ie, ie, ie_len);
873 ev->dc.reason = reason;
874
875 spin_lock_irqsave(&wdev->event_lock, flags);
876 list_add_tail(&ev->list, &wdev->event_list);
877 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100878 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200879}
880EXPORT_SYMBOL(cfg80211_disconnected);
881
Johannes Bergceca7b72013-05-16 00:55:45 +0200882/*
883 * API calls for nl80211/wext compatibility code
884 */
Johannes Berg83739b02013-05-15 17:44:01 +0200885int cfg80211_connect(struct cfg80211_registered_device *rdev,
886 struct net_device *dev,
887 struct cfg80211_connect_params *connect,
888 struct cfg80211_cached_keys *connkeys,
889 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200890{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200891 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200892 int err;
893
894 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200895
Johannes Bergfffd0932009-07-08 14:22:54 +0200896 if (WARN_ON(wdev->connect_keys)) {
897 kfree(wdev->connect_keys);
898 wdev->connect_keys = NULL;
899 }
900
Ben Greear7e7c8922011-11-18 11:31:59 -0800901 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
902 rdev->wiphy.ht_capa_mod_mask);
903
Johannes Bergfffd0932009-07-08 14:22:54 +0200904 if (connkeys && connkeys->def >= 0) {
905 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200906 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200907
908 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200909 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200910 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200911 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
912 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200913 connect->key_idx = idx;
914 connect->key = connkeys->params[idx].key;
915 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200916
917 /*
918 * If ciphers are not set (e.g. when going through
919 * iwconfig), we have to set them appropriately here.
920 */
921 if (connect->crypto.cipher_group == 0)
922 connect->crypto.cipher_group = cipher;
923
924 if (connect->crypto.n_ciphers_pairwise == 0) {
925 connect->crypto.n_ciphers_pairwise = 1;
926 connect->crypto.ciphers_pairwise[0] = cipher;
927 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200928 }
929 }
930
Johannes Bergceca7b72013-05-16 00:55:45 +0200931 wdev->connect_keys = connkeys;
932 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
933 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200934
Johannes Bergceca7b72013-05-16 00:55:45 +0200935 if (!rdev->ops->connect)
936 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
937 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300938 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200939
Johannes Bergceca7b72013-05-16 00:55:45 +0200940 if (err) {
941 wdev->connect_keys = NULL;
942 wdev->ssid_len = 0;
943 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200944 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200945
946 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947}
948
Johannes Berg83739b02013-05-15 17:44:01 +0200949int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
950 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200951{
Johannes Berg6829c872009-07-02 09:13:27 +0200952 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200953 int err;
954
Johannes Berg667503d2009-07-07 03:56:11 +0200955 ASSERT_WDEV_LOCK(wdev);
956
Johannes Bergfffd0932009-07-08 14:22:54 +0200957 kfree(wdev->connect_keys);
958 wdev->connect_keys = NULL;
959
Johannes Bergceca7b72013-05-16 00:55:45 +0200960 if (wdev->conn) {
961 err = cfg80211_sme_disconnect(wdev, reason);
962 } else if (!rdev->ops->disconnect) {
963 cfg80211_mlme_down(rdev, dev);
964 err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200965 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300966 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200967 }
968
Johannes Bergceca7b72013-05-16 00:55:45 +0200969 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200970}