blob: 65882d2777c0e9985122041d4674e5dd3dc1d067 [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 Berg46b9d182015-03-31 16:09:13 +020045 const u8 *ie;
Johannes Berg6829c872009-07-02 09:13:27 +020046 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020047 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020048};
49
Johannes Bergceca7b72013-05-16 00:55:45 +020050static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050051{
Johannes Bergceca7b72013-05-16 00:55:45 +020052 if (!wdev->conn)
53 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050054
Johannes Bergceca7b72013-05-16 00:55:45 +020055 kfree(wdev->conn->ie);
56 kfree(wdev->conn);
57 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050058}
59
Johannes Berg6829c872009-07-02 09:13:27 +020060static int cfg80211_conn_scan(struct wireless_dev *wdev)
61{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080062 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020063 struct cfg80211_scan_request *request;
64 int n_channels, err;
65
66 ASSERT_RTNL();
Johannes Berg667503dd2009-07-07 03:56:11 +020067 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020068
Johannes Bergf9d15d12014-01-22 11:14:19 +020069 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020070 return -EBUSY;
71
Ilan Peerbdfbec22014-01-09 11:37:23 +020072 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020073 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020074 else
75 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020076
Johannes Berg6829c872009-07-02 09:13:27 +020077 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 sizeof(request->channels[0]) * n_channels,
79 GFP_KERNEL);
80 if (!request)
81 return -ENOMEM;
82
Karl Beldan2a84ee82014-10-07 11:42:18 +020083 if (wdev->conn->params.channel) {
84 enum ieee80211_band band = wdev->conn->params.channel->band;
85 struct ieee80211_supported_band *sband =
86 wdev->wiphy->bands[band];
87
88 if (!sband) {
89 kfree(request);
90 return -EINVAL;
91 }
Johannes Berg6829c872009-07-02 09:13:27 +020092 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +020093 request->rates[band] = (1 << sband->n_bitrates) - 1;
94 } else {
Johannes Berg6829c872009-07-02 09:13:27 +020095 int i = 0, j;
96 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +053097 struct ieee80211_supported_band *bands;
98 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +020099
100 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530101 bands = wdev->wiphy->bands[band];
102 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200103 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530104 for (j = 0; j < bands->n_channels; j++) {
105 channel = &bands->channels[j];
106 if (channel->flags & IEEE80211_CHAN_DISABLED)
107 continue;
108 request->channels[i++] = channel;
109 }
110 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200111 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530112 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200113 }
114 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200115 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200116 request->n_ssids = 1;
117
118 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
119 wdev->conn->params.ssid_len);
120 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
121
Jouni Malinen818965d2016-02-26 22:12:47 +0200122 eth_broadcast_addr(request->bssid);
123
Johannes Bergfd014282012-06-18 19:17:03 +0200124 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200125 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700126 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200127
Johannes Berg79c97e92009-07-07 03:56:12 +0200128 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200129
Hila Gonene35e4d22012-06-27 17:19:42 +0300130 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200131 if (!err) {
132 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200133 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200134 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200135 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200136 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200137 kfree(request);
138 }
139 return err;
140}
141
142static int cfg80211_conn_do_work(struct wireless_dev *wdev)
143{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800144 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200145 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100146 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200147 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200148
Johannes Berg667503dd2009-07-07 03:56:11 +0200149 ASSERT_WDEV_LOCK(wdev);
150
Johannes Berg6829c872009-07-02 09:13:27 +0200151 if (!wdev->conn)
152 return 0;
153
Johannes Berg19957bb2009-07-02 17:20:43 +0200154 params = &wdev->conn->params;
155
Johannes Berg6829c872009-07-02 09:13:27 +0200156 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200157 case CFG80211_CONN_SCANNING:
158 /* didn't find it during scan ... */
159 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200160 case CFG80211_CONN_SCAN_AGAIN:
161 return cfg80211_conn_scan(wdev);
162 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200163 if (WARN_ON(!rdev->ops->auth))
164 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200165 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200166 return cfg80211_mlme_auth(rdev, wdev->netdev,
167 params->channel, params->auth_type,
168 params->bssid,
169 params->ssid, params->ssid_len,
170 NULL, 0,
171 params->key, params->key_len,
172 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200173 case CFG80211_CONN_AUTH_FAILED:
174 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200175 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200176 if (WARN_ON(!rdev->ops->assoc))
177 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200178 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200179 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100180 req.prev_bssid = wdev->conn->prev_bssid;
181 req.ie = params->ie;
182 req.ie_len = params->ie_len;
183 req.use_mfp = params->mfp != NL80211_MFP_NO;
184 req.crypto = params->crypto;
185 req.flags = params->flags;
186 req.ht_capa = params->ht_capa;
187 req.ht_capa_mask = params->ht_capa_mask;
188 req.vht_capa = params->vht_capa;
189 req.vht_capa_mask = params->vht_capa_mask;
190
Johannes Berg91bf9b22013-05-15 17:44:01 +0200191 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
192 params->bssid, params->ssid,
193 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200194 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200195 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
196 NULL, 0,
197 WLAN_REASON_DEAUTH_LEAVING,
198 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200199 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200200 case CFG80211_CONN_ASSOC_FAILED:
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING, false);
204 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200205 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200206 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
207 NULL, 0,
208 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200209 /* free directly, disconnected event already sent */
210 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200211 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200212 default:
213 return 0;
214 }
215}
216
217void cfg80211_conn_work(struct work_struct *work)
218{
Johannes Berg79c97e92009-07-07 03:56:12 +0200219 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200220 container_of(work, struct cfg80211_registered_device, conn_work);
221 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100222 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200223
224 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200225
Johannes Berg89a54e42012-06-15 14:33:17 +0200226 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200227 if (!wdev->netdev)
228 continue;
229
Johannes Berg667503dd2009-07-07 03:56:11 +0200230 wdev_lock(wdev);
231 if (!netif_running(wdev->netdev)) {
232 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200235 if (!wdev->conn ||
236 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200237 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200238 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200239 }
Johannes Berg7400f422009-10-31 07:40:37 +0100240 if (wdev->conn->params.bssid) {
241 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
242 bssid = bssid_buf;
243 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200244 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200245 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900246 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200247 NULL, 0, NULL, 0,
248 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200249 false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200250 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200251 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200252 }
253
Johannes Berg6829c872009-07-02 09:13:27 +0200254 rtnl_unlock();
255}
256
Ben Greear0e3a39b2013-06-19 14:06:27 -0700257/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700258static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200259{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800260 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200261 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200262
Johannes Berg667503dd2009-07-07 03:56:11 +0200263 ASSERT_WDEV_LOCK(wdev);
264
Jouni Malinened9d0102011-05-16 19:40:15 +0300265 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
266 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200267 wdev->conn->params.ssid,
268 wdev->conn->params.ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200269 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200270 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200271 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700272 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200273
274 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
275 wdev->conn->params.bssid = wdev->conn->bssid;
276 wdev->conn->params.channel = bss->channel;
277 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200278 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200279
Johannes Bergbbac31f2009-09-16 09:04:26 -0700280 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200281}
282
Johannes Berg667503dd2009-07-07 03:56:11 +0200283static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200284{
285 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800286 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700287 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200288
Johannes Berg667503dd2009-07-07 03:56:11 +0200289 ASSERT_WDEV_LOCK(wdev);
290
Zhu Yid4b1a682009-07-16 17:34:14 +0800291 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200292 return;
293
294 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
295 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
296 return;
297
Johannes Bergbbac31f2009-09-16 09:04:26 -0700298 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200299 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100300 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200301 else
302 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200303}
304
Johannes Berg667503dd2009-07-07 03:56:11 +0200305void cfg80211_sme_scan_done(struct net_device *dev)
306{
307 struct wireless_dev *wdev = dev->ieee80211_ptr;
308
309 wdev_lock(wdev);
310 __cfg80211_sme_scan_done(dev);
311 wdev_unlock(wdev);
312}
313
Johannes Bergceca7b72013-05-16 00:55:45 +0200314void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200315{
Johannes Berg6829c872009-07-02 09:13:27 +0200316 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800317 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200318 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
319 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
320
Johannes Berg667503dd2009-07-07 03:56:11 +0200321 ASSERT_WDEV_LOCK(wdev);
322
Johannes Bergceca7b72013-05-16 00:55:45 +0200323 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200324 return;
325
326 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
327 wdev->conn->auto_auth &&
328 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
329 /* select automatically between only open, shared, leap */
330 switch (wdev->conn->params.auth_type) {
331 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200332 if (wdev->connect_keys)
333 wdev->conn->params.auth_type =
334 NL80211_AUTHTYPE_SHARED_KEY;
335 else
336 wdev->conn->params.auth_type =
337 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200338 break;
339 case NL80211_AUTHTYPE_SHARED_KEY:
340 wdev->conn->params.auth_type =
341 NL80211_AUTHTYPE_NETWORK_EAP;
342 break;
343 default:
344 /* huh? */
345 wdev->conn->params.auth_type =
346 NL80211_AUTHTYPE_OPEN_SYSTEM;
347 break;
348 }
349 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
350 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200351 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200352 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
353 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200354 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200355 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200356 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
357 schedule_work(&rdev->conn_work);
358 }
359}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200360
Johannes Bergceca7b72013-05-16 00:55:45 +0200361bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200362{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800363 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200364
Johannes Bergceca7b72013-05-16 00:55:45 +0200365 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200366 return false;
367
Johannes Bergceca7b72013-05-16 00:55:45 +0200368 if (status == WLAN_STATUS_SUCCESS) {
369 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200370 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200371 }
372
373 if (wdev->conn->prev_bssid_valid) {
374 /*
375 * Some stupid APs don't accept reassoc, so we
376 * need to fall back to trying regular assoc;
377 * return true so no event is sent to userspace.
378 */
379 wdev->conn->prev_bssid_valid = false;
380 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
381 schedule_work(&rdev->conn_work);
382 return true;
383 }
384
Johannes Berg923a0e72013-06-28 11:38:54 +0200385 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200386 schedule_work(&rdev->conn_work);
387 return false;
388}
389
390void cfg80211_sme_deauth(struct wireless_dev *wdev)
391{
392 cfg80211_sme_free(wdev);
393}
394
395void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
396{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800397 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200398
399 if (!wdev->conn)
400 return;
401
402 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
403 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200404}
405
406void cfg80211_sme_disassoc(struct wireless_dev *wdev)
407{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800408 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200409
410 if (!wdev->conn)
411 return;
412
413 wdev->conn->state = CFG80211_CONN_DEAUTH;
414 schedule_work(&rdev->conn_work);
415}
416
417void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
418{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800419 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200420
421 if (!wdev->conn)
422 return;
423
424 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
425 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200426}
427
Johannes Berg46b9d182015-03-31 16:09:13 +0200428static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
429 const u8 *ies, size_t ies_len,
430 const u8 **out_ies, size_t *out_ies_len)
431{
432 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
433 u8 *buf;
434 size_t offs;
435
436 if (!rdev->wiphy.extended_capabilities_len ||
437 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
438 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
439 if (!*out_ies)
440 return -ENOMEM;
441 *out_ies_len = ies_len;
442 return 0;
443 }
444
445 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
446 GFP_KERNEL);
447 if (!buf)
448 return -ENOMEM;
449
450 if (ies_len) {
451 static const u8 before_extcapa[] = {
452 /* not listing IEs expected to be created by driver */
453 WLAN_EID_RSN,
454 WLAN_EID_QOS_CAPA,
455 WLAN_EID_RRM_ENABLED_CAPABILITIES,
456 WLAN_EID_MOBILITY_DOMAIN,
457 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
458 WLAN_EID_BSS_COEX_2040,
459 };
460
461 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
462 ARRAY_SIZE(before_extcapa), 0);
463 memcpy(buf, ies, offs);
464 /* leave a whole for extended capabilities IE */
465 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
466 ies + offs, ies_len - offs);
467 } else {
468 offs = 0;
469 }
470
471 /* place extended capabilities IE (with only driver capabilities) */
472 buf[offs] = WLAN_EID_EXT_CAPABILITY;
473 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
474 memcpy(buf + offs + 2,
475 rdev->wiphy.extended_capabilities,
476 rdev->wiphy.extended_capabilities_len);
477
478 *out_ies = buf;
479 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
480
481 return 0;
482}
483
Johannes Bergceca7b72013-05-16 00:55:45 +0200484static int cfg80211_sme_connect(struct wireless_dev *wdev,
485 struct cfg80211_connect_params *connect,
486 const u8 *prev_bssid)
487{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800488 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200489 struct cfg80211_bss *bss;
490 int err;
491
492 if (!rdev->ops->auth || !rdev->ops->assoc)
493 return -EOPNOTSUPP;
494
495 if (wdev->current_bss)
496 return -EALREADY;
497
498 if (WARN_ON(wdev->conn))
499 return -EINPROGRESS;
500
501 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
502 if (!wdev->conn)
503 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200504
505 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200506 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200507 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200508 memcpy(&wdev->conn->params, connect, sizeof(*connect));
509 if (connect->bssid) {
510 wdev->conn->params.bssid = wdev->conn->bssid;
511 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
512 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200513
Johannes Berg46b9d182015-03-31 16:09:13 +0200514 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
515 &wdev->conn->ie,
516 &wdev->conn->params.ie_len)) {
517 kfree(wdev->conn);
518 wdev->conn = NULL;
519 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200520 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200521 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200522
523 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
524 wdev->conn->auto_auth = true;
525 /* start with open system ... should mostly work */
526 wdev->conn->params.auth_type =
527 NL80211_AUTHTYPE_OPEN_SYSTEM;
528 } else {
529 wdev->conn->auto_auth = false;
530 }
531
532 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800533 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200534
535 /* see if we have the bss already */
536 bss = cfg80211_get_conn_bss(wdev);
537
538 if (prev_bssid) {
539 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
540 wdev->conn->prev_bssid_valid = true;
541 }
542
543 /* we're good if we have a matching bss struct */
544 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200545 err = cfg80211_conn_do_work(wdev);
546 cfg80211_put_bss(wdev->wiphy, bss);
547 } else {
548 /* otherwise we'll need to scan for the AP first */
549 err = cfg80211_conn_scan(wdev);
550
551 /*
552 * If we can't scan right now, then we need to scan again
553 * after the current scan finished, since the parameters
554 * changed (unless we find a good AP anyway).
555 */
556 if (err == -EBUSY) {
557 err = 0;
558 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
559 }
560 }
561
562 if (err)
563 cfg80211_sme_free(wdev);
564
565 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200566}
567
Johannes Bergceca7b72013-05-16 00:55:45 +0200568static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900569{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800570 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200571 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900572
Johannes Bergceca7b72013-05-16 00:55:45 +0200573 if (!wdev->conn)
574 return 0;
575
576 if (!rdev->ops->deauth)
577 return -EOPNOTSUPP;
578
579 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
580 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
581 err = 0;
582 goto out;
583 }
584
585 /* wdev->conn->params.bssid must be set if > SCANNING */
586 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
587 wdev->conn->params.bssid,
588 NULL, 0, reason, false);
589 out:
590 cfg80211_sme_free(wdev);
591 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900592}
593
Johannes Bergceca7b72013-05-16 00:55:45 +0200594/*
595 * code shared for in-device and software SME
596 */
597
598static bool cfg80211_is_all_idle(void)
599{
600 struct cfg80211_registered_device *rdev;
601 struct wireless_dev *wdev;
602 bool is_all_idle = true;
603
604 /*
605 * All devices must be idle as otherwise if you are actively
606 * scanning some new beacon hints could be learned and would
607 * count as new regulatory hints.
608 */
609 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
610 list_for_each_entry(wdev, &rdev->wdev_list, list) {
611 wdev_lock(wdev);
612 if (wdev->conn || wdev->current_bss)
613 is_all_idle = false;
614 wdev_unlock(wdev);
615 }
616 }
617
618 return is_all_idle;
619}
620
621static void disconnect_work(struct work_struct *work)
622{
623 rtnl_lock();
624 if (cfg80211_is_all_idle())
625 regulatory_hint_disconnect();
626 rtnl_unlock();
627}
628
629static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
630
631
632/*
633 * API calls for drivers implementing connect/disconnect and
634 * SME event handling
635 */
636
Ben Greear6f390902013-06-19 14:06:25 -0700637/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200638void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
639 const u8 *req_ie, size_t req_ie_len,
640 const u8 *resp_ie, size_t resp_ie_len,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200641 u16 status, bool wextev,
642 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200643{
644 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100645 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200646#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200647 union iwreq_data wrqu;
648#endif
649
Johannes Berg667503dd2009-07-07 03:56:11 +0200650 ASSERT_WDEV_LOCK(wdev);
651
Johannes Berg074ac8d2010-09-16 14:58:22 +0200652 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700653 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
654 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200655 return;
Ben Greear6f390902013-06-19 14:06:25 -0700656 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200657
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800658 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200659 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200660 resp_ie, resp_ie_len,
661 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200662
Johannes Berg3d23e342009-09-29 23:27:28 +0200663#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200664 if (wextev) {
665 if (req_ie && status == WLAN_STATUS_SUCCESS) {
666 memset(&wrqu, 0, sizeof(wrqu));
667 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800668 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200669 }
670
671 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
672 memset(&wrqu, 0, sizeof(wrqu));
673 wrqu.data.length = resp_ie_len;
674 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
675 }
676
677 memset(&wrqu, 0, sizeof(wrqu));
678 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200679 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200680 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200681 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
682 wdev->wext.prev_bssid_valid = true;
683 }
Johannes Berge45cd822009-07-02 09:58:04 +0200684 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
685 }
686#endif
687
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530688 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800689 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530690 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
691 wdev->ssid, wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200692 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200693 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530694 if (bss)
695 cfg80211_hold_bss(bss_from_pub(bss));
696 }
697
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200698 if (wdev->current_bss) {
699 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100700 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200701 wdev->current_bss = NULL;
702 }
703
Johannes Bergfffd0932009-07-08 14:22:54 +0200704 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300705 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200706 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200707 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200708 if (bss) {
709 cfg80211_unhold_bss(bss_from_pub(bss));
710 cfg80211_put_bss(wdev->wiphy, bss);
711 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300712 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200713 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200714 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200715
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530716 if (WARN_ON(!bss))
717 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200718
Johannes Bergfffd0932009-07-08 14:22:54 +0200719 wdev->current_bss = bss_from_pub(bss);
720
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700722
Johannes Berg9caf0362012-11-29 01:25:20 +0100723 rcu_read_lock();
724 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
725 if (!country_ie) {
726 rcu_read_unlock();
727 return;
728 }
729
730 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
731 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700732
733 if (!country_ie)
734 return;
735
736 /*
737 * ieee80211_bss_get_ie() ensures we can access:
738 * - country_ie + 2, the start of the country ie data, and
739 * - and country_ie[1] which is the IE length
740 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700741 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
742 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100743 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200744}
Johannes Bergf2129352009-07-01 21:26:56 +0200745
746void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
747 const u8 *req_ie, size_t req_ie_len,
748 const u8 *resp_ie, size_t resp_ie_len,
749 u16 status, gfp_t gfp)
750{
Johannes Berg667503dd2009-07-07 03:56:11 +0200751 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800752 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200753 struct cfg80211_event *ev;
754 unsigned long flags;
755
756 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
757 if (!ev)
758 return;
759
760 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800761 if (bssid)
762 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700763 if (req_ie_len) {
764 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
765 ev->cr.req_ie_len = req_ie_len;
766 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
767 }
768 if (resp_ie_len) {
769 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
770 ev->cr.resp_ie_len = resp_ie_len;
771 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
772 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200773 ev->cr.status = status;
774
775 spin_lock_irqsave(&wdev->event_lock, flags);
776 list_add_tail(&ev->list, &wdev->event_list);
777 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100778 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200779}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200780EXPORT_SYMBOL(cfg80211_connect_result);
781
Ben Greear0e3a39b2013-06-19 14:06:27 -0700782/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300783void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530784 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200785 const u8 *req_ie, size_t req_ie_len,
786 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200787{
Johannes Berg3d23e342009-09-29 23:27:28 +0200788#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200789 union iwreq_data wrqu;
790#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200791 ASSERT_WDEV_LOCK(wdev);
792
Johannes Berg074ac8d2010-09-16 14:58:22 +0200793 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
794 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530795 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200796
Johannes Bergceca7b72013-05-16 00:55:45 +0200797 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530798 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200799
Samuel Ortizb23aa672009-07-01 21:26:54 +0200800 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100801 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200802 wdev->current_bss = NULL;
803
Johannes Berg19957bb2009-07-02 17:20:43 +0200804 cfg80211_hold_bss(bss_from_pub(bss));
805 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200806
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800807 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
808 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200809 req_ie, req_ie_len, resp_ie, resp_ie_len,
810 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200811
Johannes Berg3d23e342009-09-29 23:27:28 +0200812#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200813 if (req_ie) {
814 memset(&wrqu, 0, sizeof(wrqu));
815 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800816 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200817 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200818 }
819
820 if (resp_ie) {
821 memset(&wrqu, 0, sizeof(wrqu));
822 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200823 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
824 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200825 }
826
827 memset(&wrqu, 0, sizeof(wrqu));
828 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530829 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
830 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200831 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200832 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200833#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530834
835 return;
836out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100837 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838}
Johannes Berg667503dd2009-07-07 03:56:11 +0200839
Jouni Malinened9d0102011-05-16 19:40:15 +0300840void cfg80211_roamed(struct net_device *dev,
841 struct ieee80211_channel *channel,
842 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200843 const u8 *req_ie, size_t req_ie_len,
844 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
845{
846 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530847 struct cfg80211_bss *bss;
848
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530849 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200850 wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200851 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530852 if (WARN_ON(!bss))
853 return;
854
855 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
856 resp_ie_len, gfp);
857}
858EXPORT_SYMBOL(cfg80211_roamed);
859
Ben Greear0e3a39b2013-06-19 14:06:27 -0700860/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530861void cfg80211_roamed_bss(struct net_device *dev,
862 struct cfg80211_bss *bss, const u8 *req_ie,
863 size_t req_ie_len, const u8 *resp_ie,
864 size_t resp_ie_len, gfp_t gfp)
865{
866 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800867 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200868 struct cfg80211_event *ev;
869 unsigned long flags;
870
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530871 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200872 return;
873
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530874 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
875 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100876 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530877 return;
878 }
879
Johannes Berg667503dd2009-07-07 03:56:11 +0200880 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200881 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
882 ev->rm.req_ie_len = req_ie_len;
883 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
884 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
885 ev->rm.resp_ie_len = resp_ie_len;
886 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530887 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200888
889 spin_lock_irqsave(&wdev->event_lock, flags);
890 list_add_tail(&ev->list, &wdev->event_list);
891 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100892 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200893}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530894EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200895
Johannes Berg667503dd2009-07-07 03:56:11 +0200896void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200897 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200898{
899 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800900 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200901 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200902#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200903 union iwreq_data wrqu;
904#endif
905
Johannes Berg667503dd2009-07-07 03:56:11 +0200906 ASSERT_WDEV_LOCK(wdev);
907
Johannes Berg074ac8d2010-09-16 14:58:22 +0200908 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
909 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200910 return;
911
Samuel Ortizb23aa672009-07-01 21:26:54 +0200912 if (wdev->current_bss) {
913 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100914 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200915 }
916
917 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200918 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200919
Johannes Bergfffd0932009-07-08 14:22:54 +0200920 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
921
Arend van Sprielb8607152016-02-15 14:35:53 +0100922 /* stop critical protocol if supported */
923 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
924 rdev->crit_proto_nlportid = 0;
925 rdev_crit_proto_stop(rdev, wdev);
926 }
927
Johannes Bergfffd0932009-07-08 14:22:54 +0200928 /*
929 * Delete all the keys ... pairwise keys can't really
930 * exist any more anyway, but default keys might.
931 */
932 if (rdev->ops->del_key)
933 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300934 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200935
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800936 rdev_set_qos_map(rdev, dev, NULL);
937
Johannes Berg3d23e342009-09-29 23:27:28 +0200938#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200939 memset(&wrqu, 0, sizeof(wrqu));
940 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
941 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800942 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200943#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500944
945 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200946}
947
948void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +0200949 const u8 *ie, size_t ie_len,
950 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200951{
Johannes Berg667503dd2009-07-07 03:56:11 +0200952 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800953 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200954 struct cfg80211_event *ev;
955 unsigned long flags;
956
957 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
958 if (!ev)
959 return;
960
961 ev->type = EVENT_DISCONNECTED;
962 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
963 ev->dc.ie_len = ie_len;
964 memcpy((void *)ev->dc.ie, ie, ie_len);
965 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +0200966 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +0200967
968 spin_lock_irqsave(&wdev->event_lock, flags);
969 list_add_tail(&ev->list, &wdev->event_list);
970 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100971 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200972}
973EXPORT_SYMBOL(cfg80211_disconnected);
974
Johannes Bergceca7b72013-05-16 00:55:45 +0200975/*
976 * API calls for nl80211/wext compatibility code
977 */
Johannes Berg83739b02013-05-15 17:44:01 +0200978int cfg80211_connect(struct cfg80211_registered_device *rdev,
979 struct net_device *dev,
980 struct cfg80211_connect_params *connect,
981 struct cfg80211_cached_keys *connkeys,
982 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200983{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200984 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200985 int err;
986
987 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200988
Johannes Bergfffd0932009-07-08 14:22:54 +0200989 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300990 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200991 wdev->connect_keys = NULL;
992 }
993
Ben Greear7e7c8922011-11-18 11:31:59 -0800994 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
995 rdev->wiphy.ht_capa_mod_mask);
996
Johannes Bergfffd0932009-07-08 14:22:54 +0200997 if (connkeys && connkeys->def >= 0) {
998 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200999 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001000
1001 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001002 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001003 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001004 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1005 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001006 connect->key_idx = idx;
1007 connect->key = connkeys->params[idx].key;
1008 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001009
1010 /*
1011 * If ciphers are not set (e.g. when going through
1012 * iwconfig), we have to set them appropriately here.
1013 */
1014 if (connect->crypto.cipher_group == 0)
1015 connect->crypto.cipher_group = cipher;
1016
1017 if (connect->crypto.n_ciphers_pairwise == 0) {
1018 connect->crypto.n_ciphers_pairwise = 1;
1019 connect->crypto.ciphers_pairwise[0] = cipher;
1020 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001021 }
1022 }
1023
Johannes Bergceca7b72013-05-16 00:55:45 +02001024 wdev->connect_keys = connkeys;
1025 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1026 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001027
Lior David34d50512016-01-28 10:58:25 +02001028 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1029 IEEE80211_BSS_TYPE_ESS;
1030
Johannes Bergceca7b72013-05-16 00:55:45 +02001031 if (!rdev->ops->connect)
1032 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1033 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001034 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001035
Johannes Bergceca7b72013-05-16 00:55:45 +02001036 if (err) {
1037 wdev->connect_keys = NULL;
1038 wdev->ssid_len = 0;
1039 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001040 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001041
1042 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001043}
1044
Johannes Berg83739b02013-05-15 17:44:01 +02001045int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1046 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001047{
Johannes Berg6829c872009-07-02 09:13:27 +02001048 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001049 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001050
Johannes Berg667503dd2009-07-07 03:56:11 +02001051 ASSERT_WDEV_LOCK(wdev);
1052
Johannes Bergb47f6102014-09-10 13:39:54 +03001053 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001054 wdev->connect_keys = NULL;
1055
Johannes Bergdee8a972013-08-13 09:23:57 +02001056 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001057 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001058 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001059 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001060 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001061 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001062
Johannes Bergceca7b72013-05-16 00:55:45 +02001063 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001064}