blob: 5cf182ee42478f12b33eed713ed910d2dce944a5 [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
Sachin Ahuja4216dfb2014-04-28 15:26:53 +053050static bool cfg80211_is_all_countryie_ignore(void)
51{
52 struct cfg80211_registered_device *rdev;
53 struct wireless_dev *wdev;
54 bool is_all_countryie_ignore = true;
55
56 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
57 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
58 wdev_lock(wdev);
59 if (!(wdev->wiphy->regulatory_flags &
60 REGULATORY_COUNTRY_IE_IGNORE)) {
61 is_all_countryie_ignore = false;
62 wdev_unlock(wdev);
63 goto out;
64 }
65 wdev_unlock(wdev);
66 }
67 }
68
69out:
70 return is_all_countryie_ignore;
71}
72
Johannes Bergceca7b72013-05-16 00:55:45 +020073static void cfg80211_sme_free(struct wireless_dev *wdev)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050074{
Johannes Bergceca7b72013-05-16 00:55:45 +020075 if (!wdev->conn)
76 return;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050077
Johannes Bergceca7b72013-05-16 00:55:45 +020078 kfree(wdev->conn->ie);
79 kfree(wdev->conn);
80 wdev->conn = NULL;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050081}
82
Johannes Berg6829c872009-07-02 09:13:27 +020083static int cfg80211_conn_scan(struct wireless_dev *wdev)
84{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080085 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020086 struct cfg80211_scan_request *request;
87 int n_channels, err;
88
89 ASSERT_RTNL();
Johannes Berg667503dd2009-07-07 03:56:11 +020090 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020091
Johannes Bergf9d15d12014-01-22 11:14:19 +020092 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg6829c872009-07-02 09:13:27 +020093 return -EBUSY;
94
Ilan Peerbdfbec22014-01-09 11:37:23 +020095 if (wdev->conn->params.channel)
Johannes Berg6829c872009-07-02 09:13:27 +020096 n_channels = 1;
Ilan Peerbdfbec22014-01-09 11:37:23 +020097 else
98 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020099
Johannes Berg6829c872009-07-02 09:13:27 +0200100 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
101 sizeof(request->channels[0]) * n_channels,
102 GFP_KERNEL);
103 if (!request)
104 return -ENOMEM;
105
Karl Beldan2a84ee82014-10-07 11:42:18 +0200106 if (wdev->conn->params.channel) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200107 enum nl80211_band band = wdev->conn->params.channel->band;
Karl Beldan2a84ee82014-10-07 11:42:18 +0200108 struct ieee80211_supported_band *sband =
109 wdev->wiphy->bands[band];
110
111 if (!sband) {
112 kfree(request);
113 return -EINVAL;
114 }
Johannes Berg6829c872009-07-02 09:13:27 +0200115 request->channels[0] = wdev->conn->params.channel;
Karl Beldan2a84ee82014-10-07 11:42:18 +0200116 request->rates[band] = (1 << sband->n_bitrates) - 1;
117 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200118 int i = 0, j;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200119 enum nl80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530120 struct ieee80211_supported_band *bands;
121 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200122
Johannes Berg57fbcce2016-04-12 15:56:15 +0200123 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530124 bands = wdev->wiphy->bands[band];
125 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200126 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530127 for (j = 0; j < bands->n_channels; j++) {
128 channel = &bands->channels[j];
129 if (channel->flags & IEEE80211_CHAN_DISABLED)
130 continue;
131 request->channels[i++] = channel;
132 }
133 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200134 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530135 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200136 }
137 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200138 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200139 request->n_ssids = 1;
140
141 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
142 wdev->conn->params.ssid_len);
143 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
144
Jouni Malinen818965d2016-02-26 22:12:47 +0200145 eth_broadcast_addr(request->bssid);
146
Johannes Bergfd014282012-06-18 19:17:03 +0200147 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200148 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700149 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200150
Johannes Berg79c97e92009-07-07 03:56:12 +0200151 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200152
Hila Gonene35e4d22012-06-27 17:19:42 +0300153 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200154 if (!err) {
155 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200156 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200157 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200158 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200159 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200160 kfree(request);
161 }
162 return err;
163}
164
165static int cfg80211_conn_do_work(struct wireless_dev *wdev)
166{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800167 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200168 struct cfg80211_connect_params *params;
Johannes Bergf62fab72013-02-21 20:09:09 +0100169 struct cfg80211_assoc_request req = {};
Johannes Berg19957bb2009-07-02 17:20:43 +0200170 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200171
Johannes Berg667503dd2009-07-07 03:56:11 +0200172 ASSERT_WDEV_LOCK(wdev);
173
Johannes Berg6829c872009-07-02 09:13:27 +0200174 if (!wdev->conn)
175 return 0;
176
Johannes Berg19957bb2009-07-02 17:20:43 +0200177 params = &wdev->conn->params;
178
Johannes Berg6829c872009-07-02 09:13:27 +0200179 switch (wdev->conn->state) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200180 case CFG80211_CONN_SCANNING:
181 /* didn't find it during scan ... */
182 return -ENOENT;
Johannes Berg6829c872009-07-02 09:13:27 +0200183 case CFG80211_CONN_SCAN_AGAIN:
184 return cfg80211_conn_scan(wdev);
185 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200186 if (WARN_ON(!rdev->ops->auth))
187 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200188 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg91bf9b22013-05-15 17:44:01 +0200189 return cfg80211_mlme_auth(rdev, wdev->netdev,
190 params->channel, params->auth_type,
191 params->bssid,
192 params->ssid, params->ssid_len,
193 NULL, 0,
194 params->key, params->key_len,
195 params->key_idx, NULL, 0);
Johannes Berg923a0e72013-06-28 11:38:54 +0200196 case CFG80211_CONN_AUTH_FAILED:
197 return -ENOTCONN;
Johannes Berg6829c872009-07-02 09:13:27 +0200198 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg2fd05112014-04-29 17:52:36 +0200199 if (WARN_ON(!rdev->ops->assoc))
200 return -EOPNOTSUPP;
Johannes Berg19957bb2009-07-02 17:20:43 +0200201 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200202 if (wdev->conn->prev_bssid_valid)
Johannes Bergf62fab72013-02-21 20:09:09 +0100203 req.prev_bssid = wdev->conn->prev_bssid;
204 req.ie = params->ie;
205 req.ie_len = params->ie_len;
206 req.use_mfp = params->mfp != NL80211_MFP_NO;
207 req.crypto = params->crypto;
208 req.flags = params->flags;
209 req.ht_capa = params->ht_capa;
210 req.ht_capa_mask = params->ht_capa_mask;
211 req.vht_capa = params->vht_capa;
212 req.vht_capa_mask = params->vht_capa_mask;
213
Johannes Berg91bf9b22013-05-15 17:44:01 +0200214 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
215 params->bssid, params->ssid,
216 params->ssid_len, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200217 if (err)
Johannes Berg91bf9b22013-05-15 17:44:01 +0200218 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
219 NULL, 0,
220 WLAN_REASON_DEAUTH_LEAVING,
221 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200222 return err;
Johannes Berg923a0e72013-06-28 11:38:54 +0200223 case CFG80211_CONN_ASSOC_FAILED:
224 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
225 NULL, 0,
226 WLAN_REASON_DEAUTH_LEAVING, false);
227 return -ENOTCONN;
Johannes Bergceca7b72013-05-16 00:55:45 +0200228 case CFG80211_CONN_DEAUTH:
Johannes Berg91bf9b22013-05-15 17:44:01 +0200229 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
230 NULL, 0,
231 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg923a0e72013-06-28 11:38:54 +0200232 /* free directly, disconnected event already sent */
233 cfg80211_sme_free(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200234 return 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200235 default:
236 return 0;
237 }
238}
239
240void cfg80211_conn_work(struct work_struct *work)
241{
Johannes Berg79c97e92009-07-07 03:56:12 +0200242 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200243 container_of(work, struct cfg80211_registered_device, conn_work);
244 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100245 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200246
247 rtnl_lock();
Johannes Berg6829c872009-07-02 09:13:27 +0200248
Johannes Berg53873f12016-05-03 16:52:04 +0300249 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergc8157972013-05-23 18:10:21 +0200250 if (!wdev->netdev)
251 continue;
252
Johannes Berg667503dd2009-07-07 03:56:11 +0200253 wdev_lock(wdev);
254 if (!netif_running(wdev->netdev)) {
255 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200256 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200257 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200258 if (!wdev->conn ||
259 wdev->conn->state == CFG80211_CONN_CONNECTED) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200260 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200261 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200262 }
Johannes Berg7400f422009-10-31 07:40:37 +0100263 if (wdev->conn->params.bssid) {
264 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
265 bssid = bssid_buf;
266 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200267 if (cfg80211_conn_do_work(wdev)) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200268 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900269 wdev->netdev, bssid,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300270 NULL, 0, NULL, 0, -1, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200271 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200272 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200273 }
274
Johannes Berg6829c872009-07-02 09:13:27 +0200275 rtnl_unlock();
276}
277
Ben Greear0e3a39b2013-06-19 14:06:27 -0700278/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Bergbbac31f2009-09-16 09:04:26 -0700279static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200280{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800281 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200282 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200283
Johannes Berg667503dd2009-07-07 03:56:11 +0200284 ASSERT_WDEV_LOCK(wdev);
285
Jouni Malinened9d0102011-05-16 19:40:15 +0300286 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
287 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200288 wdev->conn->params.ssid,
289 wdev->conn->params.ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200290 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200291 IEEE80211_PRIVACY(wdev->conn->params.privacy));
Johannes Berg6829c872009-07-02 09:13:27 +0200292 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700293 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200294
295 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
296 wdev->conn->params.bssid = wdev->conn->bssid;
297 wdev->conn->params.channel = bss->channel;
298 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
Johannes Berg79c97e92009-07-07 03:56:12 +0200299 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200300
Johannes Bergbbac31f2009-09-16 09:04:26 -0700301 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200302}
303
Johannes Berg667503dd2009-07-07 03:56:11 +0200304static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200305{
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800307 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700308 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200309
Johannes Berg667503dd2009-07-07 03:56:11 +0200310 ASSERT_WDEV_LOCK(wdev);
311
Zhu Yid4b1a682009-07-16 17:34:14 +0800312 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200313 return;
314
315 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
316 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
317 return;
318
Johannes Bergbbac31f2009-09-16 09:04:26 -0700319 bss = cfg80211_get_conn_bss(wdev);
Johannes Bergceca7b72013-05-16 00:55:45 +0200320 if (bss)
Johannes Berg5b112d32013-02-01 01:49:58 +0100321 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergceca7b72013-05-16 00:55:45 +0200322 else
323 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200324}
325
Johannes Berg667503dd2009-07-07 03:56:11 +0200326void cfg80211_sme_scan_done(struct net_device *dev)
327{
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329
330 wdev_lock(wdev);
331 __cfg80211_sme_scan_done(dev);
332 wdev_unlock(wdev);
333}
334
Johannes Bergceca7b72013-05-16 00:55:45 +0200335void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200336{
Johannes Berg6829c872009-07-02 09:13:27 +0200337 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800338 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200339 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
340 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
341
Johannes Berg667503dd2009-07-07 03:56:11 +0200342 ASSERT_WDEV_LOCK(wdev);
343
Johannes Bergceca7b72013-05-16 00:55:45 +0200344 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
Johannes Berg6829c872009-07-02 09:13:27 +0200345 return;
346
347 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
348 wdev->conn->auto_auth &&
349 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
350 /* select automatically between only open, shared, leap */
351 switch (wdev->conn->params.auth_type) {
352 case NL80211_AUTHTYPE_OPEN_SYSTEM:
Johannes Bergfffd0932009-07-08 14:22:54 +0200353 if (wdev->connect_keys)
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_SHARED_KEY;
356 else
357 wdev->conn->params.auth_type =
358 NL80211_AUTHTYPE_NETWORK_EAP;
Johannes Berg6829c872009-07-02 09:13:27 +0200359 break;
360 case NL80211_AUTHTYPE_SHARED_KEY:
361 wdev->conn->params.auth_type =
362 NL80211_AUTHTYPE_NETWORK_EAP;
363 break;
364 default:
365 /* huh? */
366 wdev->conn->params.auth_type =
367 NL80211_AUTHTYPE_OPEN_SYSTEM;
368 break;
369 }
370 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
371 schedule_work(&rdev->conn_work);
Johannes Berg19957bb2009-07-02 17:20:43 +0200372 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200373 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
374 NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200375 status_code, false, NULL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200376 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
Johannes Berg6829c872009-07-02 09:13:27 +0200377 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
378 schedule_work(&rdev->conn_work);
379 }
380}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200381
Johannes Bergceca7b72013-05-16 00:55:45 +0200382bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200383{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800384 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200385
Johannes Bergceca7b72013-05-16 00:55:45 +0200386 if (!wdev->conn)
Johannes Bergf401a6f2009-08-07 14:51:05 +0200387 return false;
388
Johannes Bergceca7b72013-05-16 00:55:45 +0200389 if (status == WLAN_STATUS_SUCCESS) {
390 wdev->conn->state = CFG80211_CONN_CONNECTED;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200391 return false;
Johannes Bergceca7b72013-05-16 00:55:45 +0200392 }
393
394 if (wdev->conn->prev_bssid_valid) {
395 /*
396 * Some stupid APs don't accept reassoc, so we
397 * need to fall back to trying regular assoc;
398 * return true so no event is sent to userspace.
399 */
400 wdev->conn->prev_bssid_valid = false;
401 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
402 schedule_work(&rdev->conn_work);
403 return true;
404 }
405
Johannes Berg923a0e72013-06-28 11:38:54 +0200406 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
Johannes Bergceca7b72013-05-16 00:55:45 +0200407 schedule_work(&rdev->conn_work);
408 return false;
409}
410
411void cfg80211_sme_deauth(struct wireless_dev *wdev)
412{
413 cfg80211_sme_free(wdev);
414}
415
416void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
417{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800418 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200419
420 if (!wdev->conn)
421 return;
422
423 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
424 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200425}
426
427void cfg80211_sme_disassoc(struct wireless_dev *wdev)
428{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800429 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200430
431 if (!wdev->conn)
432 return;
433
434 wdev->conn->state = CFG80211_CONN_DEAUTH;
435 schedule_work(&rdev->conn_work);
436}
437
438void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
439{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800440 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg923a0e72013-06-28 11:38:54 +0200441
442 if (!wdev->conn)
443 return;
444
445 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
446 schedule_work(&rdev->conn_work);
Johannes Bergceca7b72013-05-16 00:55:45 +0200447}
448
Johannes Berg46b9d182015-03-31 16:09:13 +0200449static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
450 const u8 *ies, size_t ies_len,
451 const u8 **out_ies, size_t *out_ies_len)
452{
453 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
454 u8 *buf;
455 size_t offs;
456
457 if (!rdev->wiphy.extended_capabilities_len ||
458 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
459 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
460 if (!*out_ies)
461 return -ENOMEM;
462 *out_ies_len = ies_len;
463 return 0;
464 }
465
466 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
467 GFP_KERNEL);
468 if (!buf)
469 return -ENOMEM;
470
471 if (ies_len) {
472 static const u8 before_extcapa[] = {
473 /* not listing IEs expected to be created by driver */
474 WLAN_EID_RSN,
475 WLAN_EID_QOS_CAPA,
476 WLAN_EID_RRM_ENABLED_CAPABILITIES,
477 WLAN_EID_MOBILITY_DOMAIN,
478 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
479 WLAN_EID_BSS_COEX_2040,
480 };
481
482 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
483 ARRAY_SIZE(before_extcapa), 0);
484 memcpy(buf, ies, offs);
485 /* leave a whole for extended capabilities IE */
486 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
487 ies + offs, ies_len - offs);
488 } else {
489 offs = 0;
490 }
491
492 /* place extended capabilities IE (with only driver capabilities) */
493 buf[offs] = WLAN_EID_EXT_CAPABILITY;
494 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
495 memcpy(buf + offs + 2,
496 rdev->wiphy.extended_capabilities,
497 rdev->wiphy.extended_capabilities_len);
498
499 *out_ies = buf;
500 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
501
502 return 0;
503}
504
Johannes Bergceca7b72013-05-16 00:55:45 +0200505static int cfg80211_sme_connect(struct wireless_dev *wdev,
506 struct cfg80211_connect_params *connect,
507 const u8 *prev_bssid)
508{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800509 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200510 struct cfg80211_bss *bss;
511 int err;
512
513 if (!rdev->ops->auth || !rdev->ops->assoc)
514 return -EOPNOTSUPP;
515
Jouni Malinen4ce2bd92016-03-29 13:53:28 +0300516 if (wdev->current_bss) {
517 if (!prev_bssid)
518 return -EALREADY;
519 if (prev_bssid &&
520 !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
521 return -ENOTCONN;
522 cfg80211_unhold_bss(wdev->current_bss);
523 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
524 wdev->current_bss = NULL;
525
526 cfg80211_sme_free(wdev);
527 }
Johannes Bergceca7b72013-05-16 00:55:45 +0200528
529 if (WARN_ON(wdev->conn))
530 return -EINPROGRESS;
531
532 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
533 if (!wdev->conn)
534 return -ENOMEM;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200535
536 /*
Johannes Bergceca7b72013-05-16 00:55:45 +0200537 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
Johannes Bergf401a6f2009-08-07 14:51:05 +0200538 */
Johannes Bergceca7b72013-05-16 00:55:45 +0200539 memcpy(&wdev->conn->params, connect, sizeof(*connect));
540 if (connect->bssid) {
541 wdev->conn->params.bssid = wdev->conn->bssid;
542 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
543 }
Johannes Bergf401a6f2009-08-07 14:51:05 +0200544
Johannes Berg46b9d182015-03-31 16:09:13 +0200545 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
546 &wdev->conn->ie,
547 &wdev->conn->params.ie_len)) {
548 kfree(wdev->conn);
549 wdev->conn = NULL;
550 return -ENOMEM;
Johannes Bergceca7b72013-05-16 00:55:45 +0200551 }
Johannes Berg46b9d182015-03-31 16:09:13 +0200552 wdev->conn->params.ie = wdev->conn->ie;
Johannes Bergceca7b72013-05-16 00:55:45 +0200553
554 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
555 wdev->conn->auto_auth = true;
556 /* start with open system ... should mostly work */
557 wdev->conn->params.auth_type =
558 NL80211_AUTHTYPE_OPEN_SYSTEM;
559 } else {
560 wdev->conn->auto_auth = false;
561 }
562
563 wdev->conn->params.ssid = wdev->ssid;
Zhao, Gangbabd3a22014-03-19 23:02:21 +0800564 wdev->conn->params.ssid_len = wdev->ssid_len;
Johannes Bergceca7b72013-05-16 00:55:45 +0200565
566 /* see if we have the bss already */
567 bss = cfg80211_get_conn_bss(wdev);
568
569 if (prev_bssid) {
570 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
571 wdev->conn->prev_bssid_valid = true;
572 }
573
574 /* we're good if we have a matching bss struct */
575 if (bss) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200576 err = cfg80211_conn_do_work(wdev);
577 cfg80211_put_bss(wdev->wiphy, bss);
578 } else {
579 /* otherwise we'll need to scan for the AP first */
580 err = cfg80211_conn_scan(wdev);
581
582 /*
583 * If we can't scan right now, then we need to scan again
584 * after the current scan finished, since the parameters
585 * changed (unless we find a good AP anyway).
586 */
587 if (err == -EBUSY) {
588 err = 0;
589 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
590 }
591 }
592
593 if (err)
594 cfg80211_sme_free(wdev);
595
596 return err;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200597}
598
Johannes Bergceca7b72013-05-16 00:55:45 +0200599static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
Johannes Berg7d930bc2009-10-20 15:08:53 +0900600{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800601 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +0200602 int err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900603
Johannes Bergceca7b72013-05-16 00:55:45 +0200604 if (!wdev->conn)
605 return 0;
606
607 if (!rdev->ops->deauth)
608 return -EOPNOTSUPP;
609
610 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
611 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
612 err = 0;
613 goto out;
614 }
615
616 /* wdev->conn->params.bssid must be set if > SCANNING */
617 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
618 wdev->conn->params.bssid,
619 NULL, 0, reason, false);
620 out:
621 cfg80211_sme_free(wdev);
622 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900623}
624
Johannes Bergceca7b72013-05-16 00:55:45 +0200625/*
626 * code shared for in-device and software SME
627 */
628
629static bool cfg80211_is_all_idle(void)
630{
631 struct cfg80211_registered_device *rdev;
632 struct wireless_dev *wdev;
633 bool is_all_idle = true;
634
635 /*
636 * All devices must be idle as otherwise if you are actively
637 * scanning some new beacon hints could be learned and would
638 * count as new regulatory hints.
639 */
640 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Johannes Berg53873f12016-05-03 16:52:04 +0300641 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergceca7b72013-05-16 00:55:45 +0200642 wdev_lock(wdev);
643 if (wdev->conn || wdev->current_bss)
644 is_all_idle = false;
645 wdev_unlock(wdev);
646 }
647 }
648
649 return is_all_idle;
650}
651
652static void disconnect_work(struct work_struct *work)
653{
654 rtnl_lock();
Sachin Ahuja4216dfb2014-04-28 15:26:53 +0530655 if (cfg80211_is_all_idle() &&
656 !cfg80211_is_all_countryie_ignore())
Johannes Bergceca7b72013-05-16 00:55:45 +0200657 regulatory_hint_disconnect();
658 rtnl_unlock();
659}
660
661static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
662
663
664/*
665 * API calls for drivers implementing connect/disconnect and
666 * SME event handling
667 */
668
Ben Greear6f390902013-06-19 14:06:25 -0700669/* This method must consume bss one way or another */
Johannes Berg667503dd2009-07-07 03:56:11 +0200670void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
671 const u8 *req_ie, size_t req_ie_len,
672 const u8 *resp_ie, size_t resp_ie_len,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300673 int status, bool wextev,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200674 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200675{
676 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100677 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200678#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200679 union iwreq_data wrqu;
680#endif
681
Johannes Berg667503dd2009-07-07 03:56:11 +0200682 ASSERT_WDEV_LOCK(wdev);
683
Johannes Berg074ac8d2010-09-16 14:58:22 +0200684 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
Ben Greear6f390902013-06-19 14:06:25 -0700685 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
686 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200687 return;
Ben Greear6f390902013-06-19 14:06:25 -0700688 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200689
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800690 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200691 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200692 resp_ie, resp_ie_len,
693 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200694
Johannes Berg3d23e342009-09-29 23:27:28 +0200695#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200696 if (wextev) {
697 if (req_ie && status == WLAN_STATUS_SUCCESS) {
698 memset(&wrqu, 0, sizeof(wrqu));
699 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800700 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200701 }
702
703 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
704 memset(&wrqu, 0, sizeof(wrqu));
705 wrqu.data.length = resp_ie_len;
706 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
707 }
708
709 memset(&wrqu, 0, sizeof(wrqu));
710 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200711 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200712 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200713 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
714 wdev->wext.prev_bssid_valid = true;
715 }
Johannes Berge45cd822009-07-02 09:58:04 +0200716 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
717 }
718#endif
719
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530720 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800721 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530722 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
723 wdev->ssid, wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200724 wdev->conn_bss_type,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200725 IEEE80211_PRIVACY_ANY);
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530726 if (bss)
727 cfg80211_hold_bss(bss_from_pub(bss));
728 }
729
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200730 if (wdev->current_bss) {
731 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100732 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200733 wdev->current_bss = NULL;
734 }
735
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 if (status != WLAN_STATUS_SUCCESS) {
Johannes Bergb47f6102014-09-10 13:39:54 +0300737 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200738 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200739 wdev->ssid_len = 0;
Johannes Bergf1940c52013-06-19 13:21:15 +0200740 if (bss) {
741 cfg80211_unhold_bss(bss_from_pub(bss));
742 cfg80211_put_bss(wdev->wiphy, bss);
743 }
Eliad Pellerc1fbb252014-04-30 15:58:13 +0300744 cfg80211_sme_free(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200745 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200746 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200747
Ujjal Roy4c4d6842013-12-04 17:27:34 +0530748 if (WARN_ON(!bss))
749 return;
Johannes Bergfffd0932009-07-08 14:22:54 +0200750
Johannes Bergfffd0932009-07-08 14:22:54 +0200751 wdev->current_bss = bss_from_pub(bss);
752
David Spinadelb8676222016-09-22 23:16:50 +0300753 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
754 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700755
Johannes Berg9caf0362012-11-29 01:25:20 +0100756 rcu_read_lock();
757 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
758 if (!country_ie) {
759 rcu_read_unlock();
760 return;
761 }
762
763 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
764 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700765
766 if (!country_ie)
767 return;
768
769 /*
770 * ieee80211_bss_get_ie() ensures we can access:
771 * - country_ie + 2, the start of the country ie data, and
772 * - and country_ie[1] which is the IE length
773 */
Luis R. Rodriguez789fd032013-10-04 18:07:24 -0700774 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
775 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100776 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200777}
Johannes Bergf2129352009-07-01 21:26:56 +0200778
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530779/* Consumes bss object one way or another */
780void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
781 struct cfg80211_bss *bss, const u8 *req_ie,
782 size_t req_ie_len, const u8 *resp_ie,
Jouni Malinenbf1ecd22016-05-31 00:16:50 +0300783 size_t resp_ie_len, int status, gfp_t gfp)
Johannes Bergf2129352009-07-01 21:26:56 +0200784{
Johannes Berg667503dd2009-07-07 03:56:11 +0200785 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800786 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200787 struct cfg80211_event *ev;
788 unsigned long flags;
789
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530790 if (bss) {
791 /* Make sure the bss entry provided by the driver is valid. */
792 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
793
794 if (WARN_ON(list_empty(&ibss->list))) {
795 cfg80211_put_bss(wdev->wiphy, bss);
796 return;
797 }
798 }
799
Johannes Berg667503dd2009-07-07 03:56:11 +0200800 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530801 if (!ev) {
802 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg667503dd2009-07-07 03:56:11 +0200803 return;
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530804 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200805
806 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800807 if (bssid)
808 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700809 if (req_ie_len) {
810 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
811 ev->cr.req_ie_len = req_ie_len;
812 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
813 }
814 if (resp_ie_len) {
815 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
816 ev->cr.resp_ie_len = resp_ie_len;
817 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
818 }
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530819 if (bss)
820 cfg80211_hold_bss(bss_from_pub(bss));
821 ev->cr.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200822 ev->cr.status = status;
823
824 spin_lock_irqsave(&wdev->event_lock, flags);
825 list_add_tail(&ev->list, &wdev->event_list);
826 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100827 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200828}
Kanchanapally, Vidyullathae7054982016-04-11 15:16:01 +0530829EXPORT_SYMBOL(cfg80211_connect_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200830
Ben Greear0e3a39b2013-06-19 14:06:27 -0700831/* Consumes bss object one way or another */
Jouni Malinened9d0102011-05-16 19:40:15 +0300832void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530833 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200834 const u8 *req_ie, size_t req_ie_len,
835 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200836{
Johannes Berg3d23e342009-09-29 23:27:28 +0200837#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200838 union iwreq_data wrqu;
839#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200840 ASSERT_WDEV_LOCK(wdev);
841
Johannes Berg074ac8d2010-09-16 14:58:22 +0200842 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
843 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530844 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200845
Johannes Bergceca7b72013-05-16 00:55:45 +0200846 if (WARN_ON(!wdev->current_bss))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530847 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200848
Samuel Ortizb23aa672009-07-01 21:26:54 +0200849 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100850 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200851 wdev->current_bss = NULL;
852
Johannes Berg19957bb2009-07-02 17:20:43 +0200853 cfg80211_hold_bss(bss_from_pub(bss));
854 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200855
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800856 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
857 wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200858 req_ie, req_ie_len, resp_ie, resp_ie_len,
859 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200860
Johannes Berg3d23e342009-09-29 23:27:28 +0200861#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200862 if (req_ie) {
863 memset(&wrqu, 0, sizeof(wrqu));
864 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800865 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200866 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200867 }
868
869 if (resp_ie) {
870 memset(&wrqu, 0, sizeof(wrqu));
871 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200872 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
873 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200874 }
875
876 memset(&wrqu, 0, sizeof(wrqu));
877 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530878 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
879 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200880 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200881 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200882#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530883
884 return;
885out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100886 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200887}
Johannes Berg667503dd2009-07-07 03:56:11 +0200888
Jouni Malinened9d0102011-05-16 19:40:15 +0300889void cfg80211_roamed(struct net_device *dev,
890 struct ieee80211_channel *channel,
891 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200892 const u8 *req_ie, size_t req_ie_len,
893 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
894{
895 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530896 struct cfg80211_bss *bss;
897
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530898 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200899 wdev->ssid_len,
Lior David34d50512016-01-28 10:58:25 +0200900 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530901 if (WARN_ON(!bss))
902 return;
903
904 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
905 resp_ie_len, gfp);
906}
907EXPORT_SYMBOL(cfg80211_roamed);
908
Ben Greear0e3a39b2013-06-19 14:06:27 -0700909/* Consumes bss object one way or another */
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530910void cfg80211_roamed_bss(struct net_device *dev,
911 struct cfg80211_bss *bss, const u8 *req_ie,
912 size_t req_ie_len, const u8 *resp_ie,
913 size_t resp_ie_len, gfp_t gfp)
914{
915 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800916 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +0200917 struct cfg80211_event *ev;
918 unsigned long flags;
919
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530920 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200921 return;
922
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530923 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
924 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100925 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530926 return;
927 }
928
Johannes Berg667503dd2009-07-07 03:56:11 +0200929 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200930 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
931 ev->rm.req_ie_len = req_ie_len;
932 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
933 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
934 ev->rm.resp_ie_len = resp_ie_len;
935 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530936 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200937
938 spin_lock_irqsave(&wdev->event_lock, flags);
939 list_add_tail(&ev->list, &wdev->event_list);
940 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100941 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200942}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530943EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200944
Johannes Berg667503dd2009-07-07 03:56:11 +0200945void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200946 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200947{
948 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800949 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200950 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200951#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200952 union iwreq_data wrqu;
953#endif
954
Johannes Berg667503dd2009-07-07 03:56:11 +0200955 ASSERT_WDEV_LOCK(wdev);
956
Johannes Berg074ac8d2010-09-16 14:58:22 +0200957 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
958 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200959 return;
960
Samuel Ortizb23aa672009-07-01 21:26:54 +0200961 if (wdev->current_bss) {
962 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100963 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200964 }
965
966 wdev->current_bss = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200967 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200968
Johannes Bergfffd0932009-07-08 14:22:54 +0200969 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
970
Arend van Sprielb8607152016-02-15 14:35:53 +0100971 /* stop critical protocol if supported */
972 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
973 rdev->crit_proto_nlportid = 0;
974 rdev_crit_proto_stop(rdev, wdev);
975 }
976
Johannes Bergfffd0932009-07-08 14:22:54 +0200977 /*
978 * Delete all the keys ... pairwise keys can't really
979 * exist any more anyway, but default keys might.
980 */
981 if (rdev->ops->del_key)
982 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300983 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200984
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800985 rdev_set_qos_map(rdev, dev, NULL);
986
Johannes Berg3d23e342009-09-29 23:27:28 +0200987#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200988 memset(&wrqu, 0, sizeof(wrqu));
989 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
990 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800991 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200992#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500993
994 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200995}
996
997void cfg80211_disconnected(struct net_device *dev, u16 reason,
Johannes Berg80279fb2015-05-22 16:22:20 +0200998 const u8 *ie, size_t ie_len,
999 bool locally_generated, gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001000{
Johannes Berg667503dd2009-07-07 03:56:11 +02001001 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001002 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +02001003 struct cfg80211_event *ev;
1004 unsigned long flags;
1005
1006 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1007 if (!ev)
1008 return;
1009
1010 ev->type = EVENT_DISCONNECTED;
1011 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1012 ev->dc.ie_len = ie_len;
1013 memcpy((void *)ev->dc.ie, ie, ie_len);
1014 ev->dc.reason = reason;
Johannes Berg80279fb2015-05-22 16:22:20 +02001015 ev->dc.locally_generated = locally_generated;
Johannes Berg667503dd2009-07-07 03:56:11 +02001016
1017 spin_lock_irqsave(&wdev->event_lock, flags);
1018 list_add_tail(&ev->list, &wdev->event_list);
1019 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +01001020 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001021}
1022EXPORT_SYMBOL(cfg80211_disconnected);
1023
Johannes Bergceca7b72013-05-16 00:55:45 +02001024/*
1025 * API calls for nl80211/wext compatibility code
1026 */
Johannes Berg83739b02013-05-15 17:44:01 +02001027int cfg80211_connect(struct cfg80211_registered_device *rdev,
1028 struct net_device *dev,
1029 struct cfg80211_connect_params *connect,
1030 struct cfg80211_cached_keys *connkeys,
1031 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001032{
Samuel Ortizb23aa672009-07-01 21:26:54 +02001033 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +02001034 int err;
1035
1036 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001037
Johannes Bergfffd0932009-07-08 14:22:54 +02001038 if (WARN_ON(wdev->connect_keys)) {
Johannes Bergb47f6102014-09-10 13:39:54 +03001039 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001040 wdev->connect_keys = NULL;
1041 }
1042
Ben Greear7e7c8922011-11-18 11:31:59 -08001043 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1044 rdev->wiphy.ht_capa_mod_mask);
1045
Johannes Bergfffd0932009-07-08 14:22:54 +02001046 if (connkeys && connkeys->def >= 0) {
1047 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001048 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001049
1050 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001051 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +02001052 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001053 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1054 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +02001055 connect->key_idx = idx;
1056 connect->key = connkeys->params[idx].key;
1057 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +02001058
1059 /*
1060 * If ciphers are not set (e.g. when going through
1061 * iwconfig), we have to set them appropriately here.
1062 */
1063 if (connect->crypto.cipher_group == 0)
1064 connect->crypto.cipher_group = cipher;
1065
1066 if (connect->crypto.n_ciphers_pairwise == 0) {
1067 connect->crypto.n_ciphers_pairwise = 1;
1068 connect->crypto.ciphers_pairwise[0] = cipher;
1069 }
Johannes Bergfffd0932009-07-08 14:22:54 +02001070 }
David Spinadelb8676222016-09-22 23:16:50 +03001071
1072 connect->crypto.wep_keys = connkeys->params;
1073 connect->crypto.wep_tx_key = connkeys->def;
Johannes Bergf1c1f172016-09-13 17:08:23 +02001074 } else {
1075 if (WARN_ON(connkeys))
1076 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001077 }
1078
Johannes Bergceca7b72013-05-16 00:55:45 +02001079 wdev->connect_keys = connkeys;
1080 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1081 wdev->ssid_len = connect->ssid_len;
Johannes Berg6829c872009-07-02 09:13:27 +02001082
Lior David34d50512016-01-28 10:58:25 +02001083 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1084 IEEE80211_BSS_TYPE_ESS;
1085
Johannes Bergceca7b72013-05-16 00:55:45 +02001086 if (!rdev->ops->connect)
1087 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1088 else
Hila Gonene35e4d22012-06-27 17:19:42 +03001089 err = rdev_connect(rdev, dev, connect);
Johannes Berg6829c872009-07-02 09:13:27 +02001090
Johannes Bergceca7b72013-05-16 00:55:45 +02001091 if (err) {
1092 wdev->connect_keys = NULL;
1093 wdev->ssid_len = 0;
1094 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001095 }
Johannes Bergceca7b72013-05-16 00:55:45 +02001096
1097 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001098}
1099
Johannes Berg83739b02013-05-15 17:44:01 +02001100int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1101 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +02001102{
Johannes Berg6829c872009-07-02 09:13:27 +02001103 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergdee8a972013-08-13 09:23:57 +02001104 int err = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001105
Johannes Berg667503dd2009-07-07 03:56:11 +02001106 ASSERT_WDEV_LOCK(wdev);
1107
Johannes Bergb47f6102014-09-10 13:39:54 +03001108 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +02001109 wdev->connect_keys = NULL;
1110
Johannes Bergdee8a972013-08-13 09:23:57 +02001111 if (wdev->conn)
Johannes Bergceca7b72013-05-16 00:55:45 +02001112 err = cfg80211_sme_disconnect(wdev, reason);
Johannes Bergdee8a972013-08-13 09:23:57 +02001113 else if (!rdev->ops->disconnect)
Johannes Bergceca7b72013-05-16 00:55:45 +02001114 cfg80211_mlme_down(rdev, dev);
Johannes Bergdee8a972013-08-13 09:23:57 +02001115 else if (wdev->current_bss)
Hila Gonene35e4d22012-06-27 17:19:42 +03001116 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +02001117
Johannes Bergceca7b72013-05-16 00:55:45 +02001118 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +02001119}