blob: ae7e2cbf45cbfe2cc22ac52eff9262833eca5861 [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
Johannes Berg667503d2009-07-07 03:56:11 +0200560void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
561 const u8 *req_ie, size_t req_ie_len,
562 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200563 u16 status, bool wextev,
564 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200565{
566 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100567 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200568#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200569 union iwreq_data wrqu;
570#endif
571
Johannes Berg667503d2009-07-07 03:56:11 +0200572 ASSERT_WDEV_LOCK(wdev);
573
Johannes Berg074ac8d2010-09-16 14:58:22 +0200574 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
575 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200576 return;
577
Johannes Bergea416a72009-08-17 12:22:14 +0200578 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200579 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200580 resp_ie, resp_ie_len,
581 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200582
Johannes Berg3d23e342009-09-29 23:27:28 +0200583#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200584 if (wextev) {
585 if (req_ie && status == WLAN_STATUS_SUCCESS) {
586 memset(&wrqu, 0, sizeof(wrqu));
587 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800588 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200589 }
590
591 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
592 memset(&wrqu, 0, sizeof(wrqu));
593 wrqu.data.length = resp_ie_len;
594 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
595 }
596
597 memset(&wrqu, 0, sizeof(wrqu));
598 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200599 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200600 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200601 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
602 wdev->wext.prev_bssid_valid = true;
603 }
Johannes Berge45cd822009-07-02 09:58:04 +0200604 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
605 }
606#endif
607
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200608 if (wdev->current_bss) {
609 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100610 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200611 wdev->current_bss = NULL;
612 }
613
Johannes Bergfffd0932009-07-08 14:22:54 +0200614 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200615 kfree(wdev->connect_keys);
616 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200617 wdev->ssid_len = 0;
Johannes Berg5b112d32013-02-01 01:49:58 +0100618 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200619 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200620 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200621
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200622 if (!bss)
Johannes Bergceca7b72013-05-16 00:55:45 +0200623 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200624 wdev->ssid, wdev->ssid_len,
625 WLAN_CAPABILITY_ESS,
626 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200627 if (WARN_ON(!bss))
628 return;
629
630 cfg80211_hold_bss(bss_from_pub(bss));
631 wdev->current_bss = bss_from_pub(bss);
632
Johannes Bergfffd0932009-07-08 14:22:54 +0200633 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700634
Johannes Berg9caf0362012-11-29 01:25:20 +0100635 rcu_read_lock();
636 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
637 if (!country_ie) {
638 rcu_read_unlock();
639 return;
640 }
641
642 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
643 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700644
645 if (!country_ie)
646 return;
647
648 /*
649 * ieee80211_bss_get_ie() ensures we can access:
650 * - country_ie + 2, the start of the country ie data, and
651 * - and country_ie[1] which is the IE length
652 */
Johannes Berg1a919312012-12-03 17:21:11 +0100653 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
654 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100655 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200656}
Johannes Bergf2129352009-07-01 21:26:56 +0200657
658void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
659 const u8 *req_ie, size_t req_ie_len,
660 const u8 *resp_ie, size_t resp_ie_len,
661 u16 status, gfp_t gfp)
662{
Johannes Berg667503d2009-07-07 03:56:11 +0200663 struct wireless_dev *wdev = dev->ieee80211_ptr;
664 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
665 struct cfg80211_event *ev;
666 unsigned long flags;
667
668 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
669 if (!ev)
670 return;
671
672 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800673 if (bssid)
674 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700675 if (req_ie_len) {
676 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
677 ev->cr.req_ie_len = req_ie_len;
678 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
679 }
680 if (resp_ie_len) {
681 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
682 ev->cr.resp_ie_len = resp_ie_len;
683 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
684 }
Johannes Berg667503d2009-07-07 03:56:11 +0200685 ev->cr.status = status;
686
687 spin_lock_irqsave(&wdev->event_lock, flags);
688 list_add_tail(&ev->list, &wdev->event_list);
689 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100690 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200691}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200692EXPORT_SYMBOL(cfg80211_connect_result);
693
Jouni Malinened9d0102011-05-16 19:40:15 +0300694void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530695 struct cfg80211_bss *bss,
Johannes Berg667503d2009-07-07 03:56:11 +0200696 const u8 *req_ie, size_t req_ie_len,
697 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200698{
Johannes Berg3d23e342009-09-29 23:27:28 +0200699#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200700 union iwreq_data wrqu;
701#endif
Johannes Berg667503d2009-07-07 03:56:11 +0200702 ASSERT_WDEV_LOCK(wdev);
703
Johannes Berg074ac8d2010-09-16 14:58:22 +0200704 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
705 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530706 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200707
Johannes Bergceca7b72013-05-16 00:55:45 +0200708 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530709 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200710
Samuel Ortizb23aa672009-07-01 21:26:54 +0200711 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100712 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713 wdev->current_bss = NULL;
714
Johannes Berg19957bb2009-07-02 17:20:43 +0200715 cfg80211_hold_bss(bss_from_pub(bss));
716 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200717
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530718 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200719 req_ie, req_ie_len, resp_ie, resp_ie_len,
720 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200721
Johannes Berg3d23e342009-09-29 23:27:28 +0200722#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200723 if (req_ie) {
724 memset(&wrqu, 0, sizeof(wrqu));
725 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800726 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503d2009-07-07 03:56:11 +0200727 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200728 }
729
730 if (resp_ie) {
731 memset(&wrqu, 0, sizeof(wrqu));
732 wrqu.data.length = resp_ie_len;
Johannes Berg667503d2009-07-07 03:56:11 +0200733 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
734 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200735 }
736
737 memset(&wrqu, 0, sizeof(wrqu));
738 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530739 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
740 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200741 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503d2009-07-07 03:56:11 +0200742 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200743#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530744
745 return;
746out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100747 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748}
Johannes Berg667503d2009-07-07 03:56:11 +0200749
Jouni Malinened9d0102011-05-16 19:40:15 +0300750void cfg80211_roamed(struct net_device *dev,
751 struct ieee80211_channel *channel,
752 const u8 *bssid,
Johannes Berg667503d2009-07-07 03:56:11 +0200753 const u8 *req_ie, size_t req_ie_len,
754 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
755{
756 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530757 struct cfg80211_bss *bss;
758
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530759 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
760 wdev->ssid_len, WLAN_CAPABILITY_ESS,
761 WLAN_CAPABILITY_ESS);
762 if (WARN_ON(!bss))
763 return;
764
765 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
766 resp_ie_len, gfp);
767}
768EXPORT_SYMBOL(cfg80211_roamed);
769
770void cfg80211_roamed_bss(struct net_device *dev,
771 struct cfg80211_bss *bss, const u8 *req_ie,
772 size_t req_ie_len, const u8 *resp_ie,
773 size_t resp_ie_len, gfp_t gfp)
774{
775 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200776 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
777 struct cfg80211_event *ev;
778 unsigned long flags;
779
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530780 if (WARN_ON(!bss))
Johannes Berg667503d2009-07-07 03:56:11 +0200781 return;
782
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530783 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
784 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100785 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530786 return;
787 }
788
Johannes Berg667503d2009-07-07 03:56:11 +0200789 ev->type = EVENT_ROAMED;
Johannes Berg667503d2009-07-07 03:56:11 +0200790 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
791 ev->rm.req_ie_len = req_ie_len;
792 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
793 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
794 ev->rm.resp_ie_len = resp_ie_len;
795 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530796 ev->rm.bss = bss;
Johannes Berg667503d2009-07-07 03:56:11 +0200797
798 spin_lock_irqsave(&wdev->event_lock, flags);
799 list_add_tail(&ev->list, &wdev->event_list);
800 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100801 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +0200802}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530803EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200804
Johannes Berg667503d2009-07-07 03:56:11 +0200805void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200806 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200807{
808 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200809 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
810 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200811#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200812 union iwreq_data wrqu;
813#endif
814
Johannes Berg667503d2009-07-07 03:56:11 +0200815 ASSERT_WDEV_LOCK(wdev);
816
Johannes Berg074ac8d2010-09-16 14:58:22 +0200817 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
818 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200819 return;
820
Samuel Ortizb23aa672009-07-01 21:26:54 +0200821 if (wdev->current_bss) {
822 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100823 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200824 }
825
826 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200827 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200828
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
830
831 /*
832 * Delete all the keys ... pairwise keys can't really
833 * exist any more anyway, but default keys might.
834 */
835 if (rdev->ops->del_key)
836 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300837 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838
Johannes Berg3d23e342009-09-29 23:27:28 +0200839#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200840 memset(&wrqu, 0, sizeof(wrqu));
841 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
842 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800843 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200844#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500845
846 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200847}
848
849void cfg80211_disconnected(struct net_device *dev, u16 reason,
850 u8 *ie, size_t ie_len, gfp_t gfp)
851{
Johannes Berg667503d2009-07-07 03:56:11 +0200852 struct wireless_dev *wdev = dev->ieee80211_ptr;
853 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
854 struct cfg80211_event *ev;
855 unsigned long flags;
856
857 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
858 if (!ev)
859 return;
860
861 ev->type = EVENT_DISCONNECTED;
862 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
863 ev->dc.ie_len = ie_len;
864 memcpy((void *)ev->dc.ie, ie, ie_len);
865 ev->dc.reason = reason;
866
867 spin_lock_irqsave(&wdev->event_lock, flags);
868 list_add_tail(&ev->list, &wdev->event_list);
869 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100870 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200871}
872EXPORT_SYMBOL(cfg80211_disconnected);
873
Johannes Bergceca7b72013-05-16 00:55:45 +0200874/*
875 * API calls for nl80211/wext compatibility code
876 */
Johannes Berg83739b02013-05-15 17:44:01 +0200877int cfg80211_connect(struct cfg80211_registered_device *rdev,
878 struct net_device *dev,
879 struct cfg80211_connect_params *connect,
880 struct cfg80211_cached_keys *connkeys,
881 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200882{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200883 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200884 int err;
885
886 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200887
Johannes Bergfffd0932009-07-08 14:22:54 +0200888 if (WARN_ON(wdev->connect_keys)) {
889 kfree(wdev->connect_keys);
890 wdev->connect_keys = NULL;
891 }
892
Ben Greear7e7c8922011-11-18 11:31:59 -0800893 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
894 rdev->wiphy.ht_capa_mod_mask);
895
Johannes Bergfffd0932009-07-08 14:22:54 +0200896 if (connkeys && connkeys->def >= 0) {
897 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200898 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200899
900 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200901 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200902 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200903 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
904 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200905 connect->key_idx = idx;
906 connect->key = connkeys->params[idx].key;
907 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200908
909 /*
910 * If ciphers are not set (e.g. when going through
911 * iwconfig), we have to set them appropriately here.
912 */
913 if (connect->crypto.cipher_group == 0)
914 connect->crypto.cipher_group = cipher;
915
916 if (connect->crypto.n_ciphers_pairwise == 0) {
917 connect->crypto.n_ciphers_pairwise = 1;
918 connect->crypto.ciphers_pairwise[0] = cipher;
919 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200920 }
921 }
922
Johannes Bergceca7b72013-05-16 00:55:45 +0200923 wdev->connect_keys = connkeys;
924 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
925 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +0200926
Johannes Bergceca7b72013-05-16 00:55:45 +0200927 if (!rdev->ops->connect)
928 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
929 else
Hila Gonene35e4d22012-06-27 17:19:42 +0300930 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +0200931
Johannes Bergceca7b72013-05-16 00:55:45 +0200932 if (err) {
933 wdev->connect_keys = NULL;
934 wdev->ssid_len = 0;
935 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200936 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200937
938 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200939}
940
Johannes Berg83739b02013-05-15 17:44:01 +0200941int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
942 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200943{
Johannes Berg6829c872009-07-02 09:13:27 +0200944 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200945 int err;
946
Johannes Berg667503d2009-07-07 03:56:11 +0200947 ASSERT_WDEV_LOCK(wdev);
948
Johannes Bergfffd0932009-07-08 14:22:54 +0200949 kfree(wdev->connect_keys);
950 wdev->connect_keys = NULL;
951
Johannes Bergceca7b72013-05-16 00:55:45 +0200952 if (wdev->conn) {
953 err = cfg80211_sme_disconnect(wdev, reason);
954 } else if (!rdev->ops->disconnect) {
955 cfg80211_mlme_down(rdev, dev);
956 err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200957 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300958 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200959 }
960
Johannes Bergceca7b72013-05-16 00:55:45 +0200961 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200962}