]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/wireless/sme.c
Merge tag 'gpio-updates-for-v5.14-rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / net / wireless / sme.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
b23aa676 2/*
ceca7b71
JB
3 * SME code for cfg80211
4 * both driver SME event handling and the SME implementation
5 * (for nl80211's connect() and wext)
b23aa676
SO
6 *
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
0e47901d 8 * Copyright (C) 2009, 2020 Intel Corporation. All rights reserved.
29ce6ecb 9 * Copyright 2017 Intel Deutschland GmbH
b23aa676
SO
10 */
11
12#include <linux/etherdevice.h>
13#include <linux/if_arp.h>
5a0e3ad6 14#include <linux/slab.h>
b23aa676 15#include <linux/workqueue.h>
a9a11622 16#include <linux/wireless.h>
bc3b2d7f 17#include <linux/export.h>
a9a11622 18#include <net/iw_handler.h>
b23aa676
SO
19#include <net/cfg80211.h>
20#include <net/rtnetlink.h>
21#include "nl80211.h"
8b19e6ca 22#include "reg.h"
e35e4d28 23#include "rdev-ops.h"
b23aa676 24
ceca7b71
JB
25/*
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
54f65de0 27 * driver. This is for implementing nl80211's connect/disconnect
ceca7b71
JB
28 * and wireless extensions (if configured.)
29 */
30
6829c878
JB
31struct cfg80211_conn {
32 struct cfg80211_connect_params params;
33 /* these are sub-states of the _CONNECTING sme_state */
34 enum {
6829c878
JB
35 CFG80211_CONN_SCANNING,
36 CFG80211_CONN_SCAN_AGAIN,
37 CFG80211_CONN_AUTHENTICATE_NEXT,
38 CFG80211_CONN_AUTHENTICATING,
3093ebbe 39 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
6829c878
JB
40 CFG80211_CONN_ASSOCIATE_NEXT,
41 CFG80211_CONN_ASSOCIATING,
923a0e7d 42 CFG80211_CONN_ASSOC_FAILED,
3093ebbe 43 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
ceca7b71 44 CFG80211_CONN_DEAUTH,
e6f462df 45 CFG80211_CONN_ABANDON,
ceca7b71 46 CFG80211_CONN_CONNECTED,
6829c878 47 } state;
f401a6f7 48 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
46b9d180 49 const u8 *ie;
6829c878 50 size_t ie_len;
f401a6f7 51 bool auto_auth, prev_bssid_valid;
6829c878
JB
52};
53
ceca7b71 54static void cfg80211_sme_free(struct wireless_dev *wdev)
09d989d1 55{
ceca7b71
JB
56 if (!wdev->conn)
57 return;
09d989d1 58
ceca7b71
JB
59 kfree(wdev->conn->ie);
60 kfree(wdev->conn);
61 wdev->conn = NULL;
09d989d1
LR
62}
63
6829c878
JB
64static int cfg80211_conn_scan(struct wireless_dev *wdev)
65{
f26cbf40 66 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878
JB
67 struct cfg80211_scan_request *request;
68 int n_channels, err;
69
667503dd 70 ASSERT_WDEV_LOCK(wdev);
6829c878 71
f9d15d16 72 if (rdev->scan_req || rdev->scan_msg)
6829c878
JB
73 return -EBUSY;
74
bdfbec2d 75 if (wdev->conn->params.channel)
6829c878 76 n_channels = 1;
bdfbec2d
IP
77 else
78 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
6829c878 79
6829c878
JB
80 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
81 sizeof(request->channels[0]) * n_channels,
82 GFP_KERNEL);
83 if (!request)
84 return -ENOMEM;
85
2a84ee86 86 if (wdev->conn->params.channel) {
57fbcce3 87 enum nl80211_band band = wdev->conn->params.channel->band;
2a84ee86
KB
88 struct ieee80211_supported_band *sband =
89 wdev->wiphy->bands[band];
90
91 if (!sband) {
92 kfree(request);
93 return -EINVAL;
94 }
6829c878 95 request->channels[0] = wdev->conn->params.channel;
2a84ee86
KB
96 request->rates[band] = (1 << sband->n_bitrates) - 1;
97 } else {
6829c878 98 int i = 0, j;
57fbcce3 99 enum nl80211_band band;
e3081501
RM
100 struct ieee80211_supported_band *bands;
101 struct ieee80211_channel *channel;
6829c878 102
57fbcce3 103 for (band = 0; band < NUM_NL80211_BANDS; band++) {
e3081501
RM
104 bands = wdev->wiphy->bands[band];
105 if (!bands)
6829c878 106 continue;
e3081501
RM
107 for (j = 0; j < bands->n_channels; j++) {
108 channel = &bands->channels[j];
109 if (channel->flags & IEEE80211_CHAN_DISABLED)
110 continue;
111 request->channels[i++] = channel;
112 }
113 request->rates[band] = (1 << bands->n_bitrates) - 1;
6829c878 114 }
e3081501 115 n_channels = i;
6829c878
JB
116 }
117 request->n_channels = n_channels;
5ba63533 118 request->ssids = (void *)&request->channels[n_channels];
6829c878
JB
119 request->n_ssids = 1;
120
121 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
122 wdev->conn->params.ssid_len);
123 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
124
818965d3
JM
125 eth_broadcast_addr(request->bssid);
126
fd014284 127 request->wdev = wdev;
79c97e97 128 request->wiphy = &rdev->wiphy;
15d6030b 129 request->scan_start = jiffies;
6829c878 130
79c97e97 131 rdev->scan_req = request;
6829c878 132
e35e4d28 133 err = rdev_scan(rdev, request);
6829c878
JB
134 if (!err) {
135 wdev->conn->state = CFG80211_CONN_SCANNING;
fd014284 136 nl80211_send_scan_start(rdev, wdev);
463d0183 137 dev_hold(wdev->netdev);
6829c878 138 } else {
79c97e97 139 rdev->scan_req = NULL;
6829c878
JB
140 kfree(request);
141 }
142 return err;
143}
144
3093ebbe
PK
145static int cfg80211_conn_do_work(struct wireless_dev *wdev,
146 enum nl80211_timeout_reason *treason)
6829c878 147{
f26cbf40 148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19957bb3 149 struct cfg80211_connect_params *params;
f62fab73 150 struct cfg80211_assoc_request req = {};
19957bb3 151 int err;
6829c878 152
667503dd
JB
153 ASSERT_WDEV_LOCK(wdev);
154
6829c878
JB
155 if (!wdev->conn)
156 return 0;
157
19957bb3
JB
158 params = &wdev->conn->params;
159
6829c878 160 switch (wdev->conn->state) {
ceca7b71
JB
161 case CFG80211_CONN_SCANNING:
162 /* didn't find it during scan ... */
163 return -ENOENT;
6829c878
JB
164 case CFG80211_CONN_SCAN_AGAIN:
165 return cfg80211_conn_scan(wdev);
166 case CFG80211_CONN_AUTHENTICATE_NEXT:
2fd05115
JB
167 if (WARN_ON(!rdev->ops->auth))
168 return -EOPNOTSUPP;
19957bb3 169 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
91bf9b26
JB
170 return cfg80211_mlme_auth(rdev, wdev->netdev,
171 params->channel, params->auth_type,
172 params->bssid,
173 params->ssid, params->ssid_len,
174 NULL, 0,
175 params->key, params->key_len,
176 params->key_idx, NULL, 0);
3093ebbe
PK
177 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
178 *treason = NL80211_TIMEOUT_AUTH;
923a0e7d 179 return -ENOTCONN;
6829c878 180 case CFG80211_CONN_ASSOCIATE_NEXT:
2fd05115
JB
181 if (WARN_ON(!rdev->ops->assoc))
182 return -EOPNOTSUPP;
19957bb3 183 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
f401a6f7 184 if (wdev->conn->prev_bssid_valid)
f62fab73
JB
185 req.prev_bssid = wdev->conn->prev_bssid;
186 req.ie = params->ie;
187 req.ie_len = params->ie_len;
188 req.use_mfp = params->mfp != NL80211_MFP_NO;
189 req.crypto = params->crypto;
190 req.flags = params->flags;
191 req.ht_capa = params->ht_capa;
192 req.ht_capa_mask = params->ht_capa_mask;
193 req.vht_capa = params->vht_capa;
194 req.vht_capa_mask = params->vht_capa_mask;
195
91bf9b26
JB
196 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
197 params->bssid, params->ssid,
198 params->ssid_len, &req);
19957bb3 199 if (err)
91bf9b26
JB
200 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
201 NULL, 0,
202 WLAN_REASON_DEAUTH_LEAVING,
203 false);
19957bb3 204 return err;
3093ebbe
PK
205 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
206 *treason = NL80211_TIMEOUT_ASSOC;
7b506ff6 207 fallthrough;
923a0e7d
JB
208 case CFG80211_CONN_ASSOC_FAILED:
209 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
210 NULL, 0,
211 WLAN_REASON_DEAUTH_LEAVING, false);
212 return -ENOTCONN;
ceca7b71 213 case CFG80211_CONN_DEAUTH:
91bf9b26
JB
214 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
215 NULL, 0,
216 WLAN_REASON_DEAUTH_LEAVING, false);
7b506ff6 217 fallthrough;
e6f462df 218 case CFG80211_CONN_ABANDON:
923a0e7d
JB
219 /* free directly, disconnected event already sent */
220 cfg80211_sme_free(wdev);
ceca7b71 221 return 0;
6829c878
JB
222 default:
223 return 0;
224 }
225}
226
227void cfg80211_conn_work(struct work_struct *work)
228{
79c97e97 229 struct cfg80211_registered_device *rdev =
6829c878
JB
230 container_of(work, struct cfg80211_registered_device, conn_work);
231 struct wireless_dev *wdev;
7400f42e 232 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
3093ebbe 233 enum nl80211_timeout_reason treason;
6829c878 234
a05829a7 235 wiphy_lock(&rdev->wiphy);
6829c878 236
53873f13 237 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
c8157976
JB
238 if (!wdev->netdev)
239 continue;
240
667503dd
JB
241 wdev_lock(wdev);
242 if (!netif_running(wdev->netdev)) {
243 wdev_unlock(wdev);
6829c878 244 continue;
667503dd 245 }
ceca7b71
JB
246 if (!wdev->conn ||
247 wdev->conn->state == CFG80211_CONN_CONNECTED) {
667503dd 248 wdev_unlock(wdev);
6829c878 249 continue;
667503dd 250 }
7400f42e
JB
251 if (wdev->conn->params.bssid) {
252 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
253 bssid = bssid_buf;
254 }
3093ebbe
PK
255 treason = NL80211_TIMEOUT_UNSPECIFIED;
256 if (cfg80211_conn_do_work(wdev, &treason)) {
5349a0f7
VK
257 struct cfg80211_connect_resp_params cr;
258
259 memset(&cr, 0, sizeof(cr));
260 cr.status = -1;
261 cr.bssid = bssid;
262 cr.timeout_reason = treason;
263 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 264 }
667503dd 265 wdev_unlock(wdev);
6829c878
JB
266 }
267
a05829a7 268 wiphy_unlock(&rdev->wiphy);
6829c878
JB
269}
270
0e3a39b5 271/* Returned bss is reference counted and must be cleaned up appropriately. */
bbac31f4 272static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
6829c878 273{
f26cbf40 274 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878 275 struct cfg80211_bss *bss;
6829c878 276
667503dd
JB
277 ASSERT_WDEV_LOCK(wdev);
278
ed9d0102
JM
279 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
280 wdev->conn->params.bssid,
6829c878
JB
281 wdev->conn->params.ssid,
282 wdev->conn->params.ssid_len,
34d50519 283 wdev->conn_bss_type,
6eb18137 284 IEEE80211_PRIVACY(wdev->conn->params.privacy));
6829c878 285 if (!bss)
bbac31f4 286 return NULL;
6829c878
JB
287
288 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
289 wdev->conn->params.bssid = wdev->conn->bssid;
290 wdev->conn->params.channel = bss->channel;
291 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
79c97e97 292 schedule_work(&rdev->conn_work);
6829c878 293
bbac31f4 294 return bss;
6829c878
JB
295}
296
667503dd 297static void __cfg80211_sme_scan_done(struct net_device *dev)
6829c878
JB
298{
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
bbac31f4 301 struct cfg80211_bss *bss;
6829c878 302
667503dd
JB
303 ASSERT_WDEV_LOCK(wdev);
304
d4b1a687 305 if (!wdev->conn)
6829c878
JB
306 return;
307
308 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
309 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
310 return;
311
bbac31f4 312 bss = cfg80211_get_conn_bss(wdev);
ceca7b71 313 if (bss)
5b112d3d 314 cfg80211_put_bss(&rdev->wiphy, bss);
ceca7b71
JB
315 else
316 schedule_work(&rdev->conn_work);
6829c878
JB
317}
318
667503dd
JB
319void cfg80211_sme_scan_done(struct net_device *dev)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322
323 wdev_lock(wdev);
324 __cfg80211_sme_scan_done(dev);
325 wdev_unlock(wdev);
326}
327
ceca7b71 328void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
6829c878 329{
6829c878 330 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 331 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
6829c878
JB
332 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
333 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
334
667503dd
JB
335 ASSERT_WDEV_LOCK(wdev);
336
ceca7b71 337 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
6829c878
JB
338 return;
339
340 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
341 wdev->conn->auto_auth &&
342 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
343 /* select automatically between only open, shared, leap */
344 switch (wdev->conn->params.auth_type) {
345 case NL80211_AUTHTYPE_OPEN_SYSTEM:
fffd0934
JB
346 if (wdev->connect_keys)
347 wdev->conn->params.auth_type =
348 NL80211_AUTHTYPE_SHARED_KEY;
349 else
350 wdev->conn->params.auth_type =
351 NL80211_AUTHTYPE_NETWORK_EAP;
6829c878
JB
352 break;
353 case NL80211_AUTHTYPE_SHARED_KEY:
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_NETWORK_EAP;
356 break;
357 default:
358 /* huh? */
359 wdev->conn->params.auth_type =
360 NL80211_AUTHTYPE_OPEN_SYSTEM;
361 break;
362 }
363 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
364 schedule_work(&rdev->conn_work);
19957bb3 365 } else if (status_code != WLAN_STATUS_SUCCESS) {
5349a0f7
VK
366 struct cfg80211_connect_resp_params cr;
367
368 memset(&cr, 0, sizeof(cr));
369 cr.status = status_code;
370 cr.bssid = mgmt->bssid;
371 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
372 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 373 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
6829c878
JB
374 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
375 schedule_work(&rdev->conn_work);
376 }
377}
b23aa676 378
ceca7b71 379bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
f401a6f7 380{
f26cbf40 381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
f401a6f7 382
ceca7b71 383 if (!wdev->conn)
f401a6f7
JB
384 return false;
385
ceca7b71
JB
386 if (status == WLAN_STATUS_SUCCESS) {
387 wdev->conn->state = CFG80211_CONN_CONNECTED;
f401a6f7 388 return false;
ceca7b71 389 }
f401a6f7 390
ceca7b71
JB
391 if (wdev->conn->prev_bssid_valid) {
392 /*
393 * Some stupid APs don't accept reassoc, so we
394 * need to fall back to trying regular assoc;
395 * return true so no event is sent to userspace.
396 */
397 wdev->conn->prev_bssid_valid = false;
398 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
399 schedule_work(&rdev->conn_work);
400 return true;
401 }
402
923a0e7d 403 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
f401a6f7 404 schedule_work(&rdev->conn_work);
ceca7b71
JB
405 return false;
406}
f401a6f7 407
ceca7b71
JB
408void cfg80211_sme_deauth(struct wireless_dev *wdev)
409{
410 cfg80211_sme_free(wdev);
f401a6f7
JB
411}
412
ceca7b71 413void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
7d930bc3 414{
f26cbf40 415 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
923a0e7d
JB
416
417 if (!wdev->conn)
418 return;
419
3093ebbe 420 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
923a0e7d 421 schedule_work(&rdev->conn_work);
ceca7b71 422}
7d930bc3 423
ceca7b71
JB
424void cfg80211_sme_disassoc(struct wireless_dev *wdev)
425{
f26cbf40 426 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
427
428 if (!wdev->conn)
429 return;
430
431 wdev->conn->state = CFG80211_CONN_DEAUTH;
7d930bc3
JB
432 schedule_work(&rdev->conn_work);
433}
434
ceca7b71
JB
435void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
436{
f26cbf40 437 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
923a0e7d
JB
438
439 if (!wdev->conn)
440 return;
441
3093ebbe 442 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
923a0e7d 443 schedule_work(&rdev->conn_work);
ceca7b71
JB
444}
445
e6f462df
JB
446void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
447{
448 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
449
450 if (!wdev->conn)
451 return;
452
453 wdev->conn->state = CFG80211_CONN_ABANDON;
454 schedule_work(&rdev->conn_work);
455}
456
46b9d180
JB
457static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
458 const u8 *ies, size_t ies_len,
459 const u8 **out_ies, size_t *out_ies_len)
460{
461 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
462 u8 *buf;
463 size_t offs;
464
465 if (!rdev->wiphy.extended_capabilities_len ||
466 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
467 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
468 if (!*out_ies)
469 return -ENOMEM;
470 *out_ies_len = ies_len;
471 return 0;
472 }
473
474 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
475 GFP_KERNEL);
476 if (!buf)
477 return -ENOMEM;
478
479 if (ies_len) {
480 static const u8 before_extcapa[] = {
481 /* not listing IEs expected to be created by driver */
482 WLAN_EID_RSN,
483 WLAN_EID_QOS_CAPA,
484 WLAN_EID_RRM_ENABLED_CAPABILITIES,
485 WLAN_EID_MOBILITY_DOMAIN,
486 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
487 WLAN_EID_BSS_COEX_2040,
488 };
489
490 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
491 ARRAY_SIZE(before_extcapa), 0);
492 memcpy(buf, ies, offs);
493 /* leave a whole for extended capabilities IE */
494 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
495 ies + offs, ies_len - offs);
496 } else {
497 offs = 0;
498 }
499
500 /* place extended capabilities IE (with only driver capabilities) */
501 buf[offs] = WLAN_EID_EXT_CAPABILITY;
502 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
503 memcpy(buf + offs + 2,
504 rdev->wiphy.extended_capabilities,
505 rdev->wiphy.extended_capabilities_len);
506
507 *out_ies = buf;
508 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
509
510 return 0;
511}
512
ceca7b71
JB
513static int cfg80211_sme_connect(struct wireless_dev *wdev,
514 struct cfg80211_connect_params *connect,
515 const u8 *prev_bssid)
516{
f26cbf40 517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
518 struct cfg80211_bss *bss;
519 int err;
520
521 if (!rdev->ops->auth || !rdev->ops->assoc)
522 return -EOPNOTSUPP;
523
4ce2bd9c 524 if (wdev->current_bss) {
4ce2bd9c
JM
525 cfg80211_unhold_bss(wdev->current_bss);
526 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
527 wdev->current_bss = NULL;
528
529 cfg80211_sme_free(wdev);
530 }
ceca7b71 531
1b5ab825 532 if (wdev->conn)
ceca7b71
JB
533 return -EINPROGRESS;
534
535 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
536 if (!wdev->conn)
537 return -ENOMEM;
538
539 /*
540 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
541 */
542 memcpy(&wdev->conn->params, connect, sizeof(*connect));
543 if (connect->bssid) {
544 wdev->conn->params.bssid = wdev->conn->bssid;
545 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
546 }
547
46b9d180
JB
548 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
549 &wdev->conn->ie,
550 &wdev->conn->params.ie_len)) {
551 kfree(wdev->conn);
552 wdev->conn = NULL;
553 return -ENOMEM;
ceca7b71 554 }
46b9d180 555 wdev->conn->params.ie = wdev->conn->ie;
ceca7b71
JB
556
557 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
558 wdev->conn->auto_auth = true;
559 /* start with open system ... should mostly work */
560 wdev->conn->params.auth_type =
561 NL80211_AUTHTYPE_OPEN_SYSTEM;
562 } else {
563 wdev->conn->auto_auth = false;
564 }
565
566 wdev->conn->params.ssid = wdev->ssid;
babd3a27 567 wdev->conn->params.ssid_len = wdev->ssid_len;
ceca7b71
JB
568
569 /* see if we have the bss already */
570 bss = cfg80211_get_conn_bss(wdev);
571
572 if (prev_bssid) {
573 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
574 wdev->conn->prev_bssid_valid = true;
575 }
576
577 /* we're good if we have a matching bss struct */
578 if (bss) {
3093ebbe
PK
579 enum nl80211_timeout_reason treason;
580
581 err = cfg80211_conn_do_work(wdev, &treason);
ceca7b71
JB
582 cfg80211_put_bss(wdev->wiphy, bss);
583 } else {
584 /* otherwise we'll need to scan for the AP first */
585 err = cfg80211_conn_scan(wdev);
586
587 /*
588 * If we can't scan right now, then we need to scan again
589 * after the current scan finished, since the parameters
590 * changed (unless we find a good AP anyway).
591 */
592 if (err == -EBUSY) {
593 err = 0;
594 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
595 }
596 }
597
598 if (err)
599 cfg80211_sme_free(wdev);
600
601 return err;
602}
603
604static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
605{
f26cbf40 606 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
607 int err;
608
609 if (!wdev->conn)
610 return 0;
611
612 if (!rdev->ops->deauth)
613 return -EOPNOTSUPP;
614
615 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
616 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
617 err = 0;
618 goto out;
619 }
620
621 /* wdev->conn->params.bssid must be set if > SCANNING */
622 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
623 wdev->conn->params.bssid,
624 NULL, 0, reason, false);
625 out:
626 cfg80211_sme_free(wdev);
627 return err;
628}
629
630/*
631 * code shared for in-device and software SME
632 */
633
634static bool cfg80211_is_all_idle(void)
635{
636 struct cfg80211_registered_device *rdev;
637 struct wireless_dev *wdev;
638 bool is_all_idle = true;
639
640 /*
641 * All devices must be idle as otherwise if you are actively
642 * scanning some new beacon hints could be learned and would
643 * count as new regulatory hints.
113f3aaa
S
644 * Also if there is any other active beaconing interface we
645 * need not issue a disconnect hint and reset any info such
646 * as chan dfs state, etc.
ceca7b71
JB
647 */
648 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53873f13 649 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
ceca7b71 650 wdev_lock(wdev);
113f3aaa
S
651 if (wdev->conn || wdev->current_bss ||
652 cfg80211_beaconing_iface_active(wdev))
ceca7b71
JB
653 is_all_idle = false;
654 wdev_unlock(wdev);
655 }
656 }
657
658 return is_all_idle;
659}
660
661static void disconnect_work(struct work_struct *work)
662{
663 rtnl_lock();
664 if (cfg80211_is_all_idle())
665 regulatory_hint_disconnect();
666 rtnl_unlock();
667}
668
e005bd7d 669DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
ceca7b71
JB
670
671
672/*
673 * API calls for drivers implementing connect/disconnect and
674 * SME event handling
675 */
676
6f390908 677/* This method must consume bss one way or another */
5349a0f7
VK
678void __cfg80211_connect_result(struct net_device *dev,
679 struct cfg80211_connect_resp_params *cr,
680 bool wextev)
b23aa676
SO
681{
682 struct wireless_dev *wdev = dev->ieee80211_ptr;
9caf0364 683 const u8 *country_ie;
3d23e349 684#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
685 union iwreq_data wrqu;
686#endif
687
667503dd
JB
688 ASSERT_WDEV_LOCK(wdev);
689
074ac8df 690 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6f390908 691 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
5349a0f7 692 cfg80211_put_bss(wdev->wiphy, cr->bss);
b23aa676 693 return;
6f390908 694 }
b23aa676 695
4d797fce 696 wdev->unprot_beacon_reported = 0;
5349a0f7
VK
697 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
698 GFP_KERNEL);
e45cd82a 699
3d23e349 700#ifdef CONFIG_CFG80211_WEXT
e45cd82a 701 if (wextev) {
5349a0f7 702 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
e45cd82a 703 memset(&wrqu, 0, sizeof(wrqu));
5349a0f7
VK
704 wrqu.data.length = cr->req_ie_len;
705 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
706 cr->req_ie);
e45cd82a
JB
707 }
708
5349a0f7 709 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
e45cd82a 710 memset(&wrqu, 0, sizeof(wrqu));
5349a0f7
VK
711 wrqu.data.length = cr->resp_ie_len;
712 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
713 cr->resp_ie);
e45cd82a
JB
714 }
715
716 memset(&wrqu, 0, sizeof(wrqu));
717 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
5349a0f7
VK
718 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
719 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
720 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
f401a6f7
JB
721 wdev->wext.prev_bssid_valid = true;
722 }
e45cd82a
JB
723 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
724 }
725#endif
726
5349a0f7 727 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
f26cbf40 728 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
5349a0f7
VK
729 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
730 wdev->ssid, wdev->ssid_len,
731 wdev->conn_bss_type,
732 IEEE80211_PRIVACY_ANY);
733 if (cr->bss)
734 cfg80211_hold_bss(bss_from_pub(cr->bss));
4c4d684a
UR
735 }
736
df7fc0f9
JB
737 if (wdev->current_bss) {
738 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 739 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
df7fc0f9
JB
740 wdev->current_bss = NULL;
741 }
742
5349a0f7 743 if (cr->status != WLAN_STATUS_SUCCESS) {
453431a5 744 kfree_sensitive(wdev->connect_keys);
fffd0934 745 wdev->connect_keys = NULL;
8dadadb7 746 wdev->ssid_len = 0;
bd2522b1 747 wdev->conn_owner_nlportid = 0;
5349a0f7
VK
748 if (cr->bss) {
749 cfg80211_unhold_bss(bss_from_pub(cr->bss));
750 cfg80211_put_bss(wdev->wiphy, cr->bss);
f1940c57 751 }
c1fbb258 752 cfg80211_sme_free(wdev);
fffd0934 753 return;
b23aa676 754 }
fffd0934 755
5349a0f7 756 if (WARN_ON(!cr->bss))
4c4d684a 757 return;
fffd0934 758
5349a0f7 759 wdev->current_bss = bss_from_pub(cr->bss);
fffd0934 760
b8676221
DS
761 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
762 cfg80211_upload_connect_keys(wdev);
8b19e6ca 763
9caf0364 764 rcu_read_lock();
5349a0f7 765 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
9caf0364
JB
766 if (!country_ie) {
767 rcu_read_unlock();
768 return;
769 }
770
771 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
772 rcu_read_unlock();
8b19e6ca
LR
773
774 if (!country_ie)
775 return;
776
777 /*
778 * ieee80211_bss_get_ie() ensures we can access:
779 * - country_ie + 2, the start of the country ie data, and
780 * - and country_ie[1] which is the IE length
781 */
5349a0f7 782 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
789fd033 783 country_ie + 2, country_ie[1]);
9caf0364 784 kfree(country_ie);
b23aa676 785}
f2129354 786
e7054989 787/* Consumes bss object one way or another */
5349a0f7
VK
788void cfg80211_connect_done(struct net_device *dev,
789 struct cfg80211_connect_resp_params *params,
790 gfp_t gfp)
f2129354 791{
667503dd 792 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 793 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
794 struct cfg80211_event *ev;
795 unsigned long flags;
5349a0f7 796 u8 *next;
667503dd 797
5349a0f7 798 if (params->bss) {
5349a0f7 799 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
e7054989 800
a3ce17d1
CT
801 if (list_empty(&ibss->list)) {
802 struct cfg80211_bss *found = NULL, *tmp = params->bss;
803
804 found = cfg80211_get_bss(wdev->wiphy, NULL,
805 params->bss->bssid,
806 wdev->ssid, wdev->ssid_len,
807 wdev->conn_bss_type,
808 IEEE80211_PRIVACY_ANY);
809 if (found) {
810 /* The same BSS is already updated so use it
811 * instead, as it has latest info.
812 */
813 params->bss = found;
814 } else {
815 /* Update with BSS provided by driver, it will
816 * be freshly added and ref cnted, we can free
817 * the old one.
818 *
819 * signal_valid can be false, as we are not
820 * expecting the BSS to be found.
821 *
822 * keep the old timestamp to avoid confusion
823 */
824 cfg80211_bss_update(rdev, ibss, false,
825 ibss->ts);
826 }
827
828 cfg80211_put_bss(wdev->wiphy, tmp);
e7054989
KV
829 }
830 }
831
5349a0f7 832 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
a3caf744 833 params->req_ie_len + params->resp_ie_len +
76804d28
AVS
834 params->fils.kek_len + params->fils.pmk_len +
835 (params->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
e7054989 836 if (!ev) {
5349a0f7 837 cfg80211_put_bss(wdev->wiphy, params->bss);
667503dd 838 return;
e7054989 839 }
667503dd
JB
840
841 ev->type = EVENT_CONNECT_RESULT;
5349a0f7
VK
842 next = ((u8 *)ev) + sizeof(*ev);
843 if (params->bssid) {
844 ev->cr.bssid = next;
845 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
846 next += ETH_ALEN;
7834704b 847 }
5349a0f7
VK
848 if (params->req_ie_len) {
849 ev->cr.req_ie = next;
850 ev->cr.req_ie_len = params->req_ie_len;
851 memcpy((void *)ev->cr.req_ie, params->req_ie,
852 params->req_ie_len);
853 next += params->req_ie_len;
7834704b 854 }
5349a0f7
VK
855 if (params->resp_ie_len) {
856 ev->cr.resp_ie = next;
857 ev->cr.resp_ie_len = params->resp_ie_len;
858 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
859 params->resp_ie_len);
860 next += params->resp_ie_len;
861 }
76804d28
AVS
862 if (params->fils.kek_len) {
863 ev->cr.fils.kek = next;
864 ev->cr.fils.kek_len = params->fils.kek_len;
865 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
866 params->fils.kek_len);
867 next += params->fils.kek_len;
a3caf744 868 }
76804d28
AVS
869 if (params->fils.pmk_len) {
870 ev->cr.fils.pmk = next;
871 ev->cr.fils.pmk_len = params->fils.pmk_len;
872 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
873 params->fils.pmk_len);
874 next += params->fils.pmk_len;
a3caf744 875 }
76804d28
AVS
876 if (params->fils.pmkid) {
877 ev->cr.fils.pmkid = next;
878 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
879 WLAN_PMKID_LEN);
a3caf744
VK
880 next += WLAN_PMKID_LEN;
881 }
76804d28
AVS
882 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
883 if (params->fils.update_erp_next_seq_num)
884 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
5349a0f7
VK
885 if (params->bss)
886 cfg80211_hold_bss(bss_from_pub(params->bss));
887 ev->cr.bss = params->bss;
888 ev->cr.status = params->status;
889 ev->cr.timeout_reason = params->timeout_reason;
667503dd
JB
890
891 spin_lock_irqsave(&wdev->event_lock, flags);
892 list_add_tail(&ev->list, &wdev->event_list);
893 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 894 queue_work(cfg80211_wq, &rdev->event_work);
f2129354 895}
5349a0f7 896EXPORT_SYMBOL(cfg80211_connect_done);
b23aa676 897
0e3a39b5 898/* Consumes bss object one way or another */
ed9d0102 899void __cfg80211_roamed(struct wireless_dev *wdev,
29ce6ecb 900 struct cfg80211_roam_info *info)
b23aa676 901{
3d23e349 902#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
903 union iwreq_data wrqu;
904#endif
667503dd
JB
905 ASSERT_WDEV_LOCK(wdev);
906
074ac8df
JB
907 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
908 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
adbde344 909 goto out;
b23aa676 910
ceca7b71 911 if (WARN_ON(!wdev->current_bss))
adbde344 912 goto out;
b23aa676
SO
913
914 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 915 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
b23aa676
SO
916 wdev->current_bss = NULL;
917
29ce6ecb
AS
918 if (WARN_ON(!info->bss))
919 return;
920
921 cfg80211_hold_bss(bss_from_pub(info->bss));
922 wdev->current_bss = bss_from_pub(info->bss);
b23aa676 923
4d797fce 924 wdev->unprot_beacon_reported = 0;
f26cbf40 925 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
29ce6ecb 926 wdev->netdev, info, GFP_KERNEL);
b23aa676 927
3d23e349 928#ifdef CONFIG_CFG80211_WEXT
29ce6ecb 929 if (info->req_ie) {
b23aa676 930 memset(&wrqu, 0, sizeof(wrqu));
29ce6ecb 931 wrqu.data.length = info->req_ie_len;
3409ff77 932 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
29ce6ecb 933 &wrqu, info->req_ie);
b23aa676
SO
934 }
935
29ce6ecb 936 if (info->resp_ie) {
b23aa676 937 memset(&wrqu, 0, sizeof(wrqu));
29ce6ecb 938 wrqu.data.length = info->resp_ie_len;
667503dd 939 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
29ce6ecb 940 &wrqu, info->resp_ie);
b23aa676
SO
941 }
942
943 memset(&wrqu, 0, sizeof(wrqu));
944 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
29ce6ecb
AS
945 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
946 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
f401a6f7 947 wdev->wext.prev_bssid_valid = true;
667503dd 948 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
b23aa676 949#endif
adbde344
VT
950
951 return;
952out:
29ce6ecb 953 cfg80211_put_bss(wdev->wiphy, info->bss);
adbde344 954}
adbde344 955
29ce6ecb
AS
956/* Consumes info->bss object one way or another */
957void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
958 gfp_t gfp)
667503dd
JB
959{
960 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 961 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
962 struct cfg80211_event *ev;
963 unsigned long flags;
e841b7b1 964 u8 *next;
667503dd 965
29ce6ecb
AS
966 if (!info->bss) {
967 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
968 info->bssid, wdev->ssid,
969 wdev->ssid_len,
970 wdev->conn_bss_type,
971 IEEE80211_PRIVACY_ANY);
972 }
973
974 if (WARN_ON(!info->bss))
adbde344
VT
975 return;
976
e841b7b1
AVS
977 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
978 info->fils.kek_len + info->fils.pmk_len +
979 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
adbde344 980 if (!ev) {
29ce6ecb 981 cfg80211_put_bss(wdev->wiphy, info->bss);
667503dd 982 return;
adbde344 983 }
667503dd
JB
984
985 ev->type = EVENT_ROAMED;
e841b7b1
AVS
986 next = ((u8 *)ev) + sizeof(*ev);
987 if (info->req_ie_len) {
988 ev->rm.req_ie = next;
989 ev->rm.req_ie_len = info->req_ie_len;
990 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
991 next += info->req_ie_len;
992 }
993 if (info->resp_ie_len) {
994 ev->rm.resp_ie = next;
995 ev->rm.resp_ie_len = info->resp_ie_len;
996 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
997 info->resp_ie_len);
998 next += info->resp_ie_len;
999 }
1000 if (info->fils.kek_len) {
1001 ev->rm.fils.kek = next;
1002 ev->rm.fils.kek_len = info->fils.kek_len;
1003 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1004 info->fils.kek_len);
1005 next += info->fils.kek_len;
1006 }
1007 if (info->fils.pmk_len) {
1008 ev->rm.fils.pmk = next;
1009 ev->rm.fils.pmk_len = info->fils.pmk_len;
1010 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1011 info->fils.pmk_len);
1012 next += info->fils.pmk_len;
1013 }
1014 if (info->fils.pmkid) {
1015 ev->rm.fils.pmkid = next;
1016 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1017 WLAN_PMKID_LEN);
1018 next += WLAN_PMKID_LEN;
1019 }
1020 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1021 if (info->fils.update_erp_next_seq_num)
1022 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
29ce6ecb 1023 ev->rm.bss = info->bss;
667503dd
JB
1024
1025 spin_lock_irqsave(&wdev->event_lock, flags);
1026 list_add_tail(&ev->list, &wdev->event_list);
1027 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1028 queue_work(cfg80211_wq, &rdev->event_work);
667503dd 1029}
29ce6ecb 1030EXPORT_SYMBOL(cfg80211_roamed);
b23aa676 1031
503c1fb9
AS
1032void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1033{
1034 ASSERT_WDEV_LOCK(wdev);
1035
1036 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1037 return;
1038
1039 if (WARN_ON(!wdev->current_bss) ||
1040 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1041 return;
1042
1043 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1044 bssid);
1045}
1046
1047void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1048 gfp_t gfp)
1049{
1050 struct wireless_dev *wdev = dev->ieee80211_ptr;
1051 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1052 struct cfg80211_event *ev;
1053 unsigned long flags;
1054
1055 if (WARN_ON(!bssid))
1056 return;
1057
1058 ev = kzalloc(sizeof(*ev), gfp);
1059 if (!ev)
1060 return;
1061
1062 ev->type = EVENT_PORT_AUTHORIZED;
1063 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1064
1065 /*
1066 * Use the wdev event list so that if there are pending
1067 * connected/roamed events, they will be reported first.
1068 */
1069 spin_lock_irqsave(&wdev->event_lock, flags);
1070 list_add_tail(&ev->list, &wdev->event_list);
1071 spin_unlock_irqrestore(&wdev->event_lock, flags);
1072 queue_work(cfg80211_wq, &rdev->event_work);
1073}
1074EXPORT_SYMBOL(cfg80211_port_authorized);
1075
667503dd 1076void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
6829c878 1077 size_t ie_len, u16 reason, bool from_ap)
b23aa676
SO
1078{
1079 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1080 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
fffd0934 1081 int i;
3d23e349 1082#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1083 union iwreq_data wrqu;
1084#endif
1085
667503dd
JB
1086 ASSERT_WDEV_LOCK(wdev);
1087
074ac8df
JB
1088 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1089 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
b23aa676
SO
1090 return;
1091
b23aa676
SO
1092 if (wdev->current_bss) {
1093 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 1094 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
b23aa676
SO
1095 }
1096
1097 wdev->current_bss = NULL;
8dadadb7 1098 wdev->ssid_len = 0;
bd2522b1 1099 wdev->conn_owner_nlportid = 0;
453431a5 1100 kfree_sensitive(wdev->connect_keys);
3027a8e7 1101 wdev->connect_keys = NULL;
b23aa676 1102
fffd0934
JB
1103 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1104
b8607152
AS
1105 /* stop critical protocol if supported */
1106 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1107 rdev->crit_proto_nlportid = 0;
1108 rdev_crit_proto_stop(rdev, wdev);
1109 }
1110
fffd0934
JB
1111 /*
1112 * Delete all the keys ... pairwise keys can't really
1113 * exist any more anyway, but default keys might.
1114 */
56be393f
JM
1115 if (rdev->ops->del_key) {
1116 int max_key_idx = 5;
1117
1118 if (wiphy_ext_feature_isset(
1119 wdev->wiphy,
0e47901d
JB
1120 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1121 wiphy_ext_feature_isset(
1122 wdev->wiphy,
1123 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
56be393f
JM
1124 max_key_idx = 7;
1125 for (i = 0; i <= max_key_idx; i++)
e35e4d28 1126 rdev_del_key(rdev, dev, i, false, NULL);
56be393f 1127 }
b23aa676 1128
fa9ffc74
KP
1129 rdev_set_qos_map(rdev, dev, NULL);
1130
3d23e349 1131#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1132 memset(&wrqu, 0, sizeof(wrqu));
1133 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1134 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
5f612033 1135 wdev->wext.connect.ssid_len = 0;
b23aa676 1136#endif
09d989d1
LR
1137
1138 schedule_work(&cfg80211_disconnect_work);
b23aa676
SO
1139}
1140
1141void cfg80211_disconnected(struct net_device *dev, u16 reason,
80279fb7
JB
1142 const u8 *ie, size_t ie_len,
1143 bool locally_generated, gfp_t gfp)
b23aa676 1144{
667503dd 1145 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1146 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
1147 struct cfg80211_event *ev;
1148 unsigned long flags;
1149
1150 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1151 if (!ev)
1152 return;
1153
1154 ev->type = EVENT_DISCONNECTED;
1155 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1156 ev->dc.ie_len = ie_len;
1157 memcpy((void *)ev->dc.ie, ie, ie_len);
1158 ev->dc.reason = reason;
80279fb7 1159 ev->dc.locally_generated = locally_generated;
667503dd
JB
1160
1161 spin_lock_irqsave(&wdev->event_lock, flags);
1162 list_add_tail(&ev->list, &wdev->event_list);
1163 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1164 queue_work(cfg80211_wq, &rdev->event_work);
b23aa676
SO
1165}
1166EXPORT_SYMBOL(cfg80211_disconnected);
1167
ceca7b71
JB
1168/*
1169 * API calls for nl80211/wext compatibility code
1170 */
83739b03
JB
1171int cfg80211_connect(struct cfg80211_registered_device *rdev,
1172 struct net_device *dev,
1173 struct cfg80211_connect_params *connect,
1174 struct cfg80211_cached_keys *connkeys,
1175 const u8 *prev_bssid)
b23aa676 1176{
b23aa676 1177 struct wireless_dev *wdev = dev->ieee80211_ptr;
667503dd
JB
1178 int err;
1179
1180 ASSERT_WDEV_LOCK(wdev);
b23aa676 1181
51e13359
JB
1182 /*
1183 * If we have an ssid_len, we're trying to connect or are
1184 * already connected, so reject a new SSID unless it's the
1185 * same (which is the case for re-association.)
1186 */
1187 if (wdev->ssid_len &&
1188 (wdev->ssid_len != connect->ssid_len ||
1189 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1190 return -EALREADY;
1191
1192 /*
1193 * If connected, reject (re-)association unless prev_bssid
1194 * matches the current BSSID.
1195 */
1196 if (wdev->current_bss) {
1197 if (!prev_bssid)
1198 return -EALREADY;
1199 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1200 return -ENOTCONN;
fffd0934
JB
1201 }
1202
51e13359
JB
1203 /*
1204 * Reject if we're in the process of connecting with WEP,
1205 * this case isn't very interesting and trying to handle
1206 * it would make the code much more complex.
1207 */
1208 if (wdev->connect_keys)
1209 return -EINPROGRESS;
1210
7e7c8926
BG
1211 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1212 rdev->wiphy.ht_capa_mod_mask);
81c5dce2
SM
1213 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1214 rdev->wiphy.vht_capa_mod_mask);
7e7c8926 1215
fffd0934
JB
1216 if (connkeys && connkeys->def >= 0) {
1217 int idx;
bcba8eae 1218 u32 cipher;
fffd0934
JB
1219
1220 idx = connkeys->def;
bcba8eae 1221 cipher = connkeys->params[idx].cipher;
fffd0934 1222 /* If given a WEP key we may need it for shared key auth */
bcba8eae
SO
1223 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1224 cipher == WLAN_CIPHER_SUITE_WEP104) {
fffd0934
JB
1225 connect->key_idx = idx;
1226 connect->key = connkeys->params[idx].key;
1227 connect->key_len = connkeys->params[idx].key_len;
bcba8eae
SO
1228
1229 /*
1230 * If ciphers are not set (e.g. when going through
1231 * iwconfig), we have to set them appropriately here.
1232 */
1233 if (connect->crypto.cipher_group == 0)
1234 connect->crypto.cipher_group = cipher;
1235
1236 if (connect->crypto.n_ciphers_pairwise == 0) {
1237 connect->crypto.n_ciphers_pairwise = 1;
1238 connect->crypto.ciphers_pairwise[0] = cipher;
1239 }
fffd0934 1240 }
b8676221
DS
1241
1242 connect->crypto.wep_keys = connkeys->params;
1243 connect->crypto.wep_tx_key = connkeys->def;
f1c1f17a
JB
1244 } else {
1245 if (WARN_ON(connkeys))
1246 return -EINVAL;
fffd0934
JB
1247 }
1248
ceca7b71
JB
1249 wdev->connect_keys = connkeys;
1250 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1251 wdev->ssid_len = connect->ssid_len;
6829c878 1252
34d50519
LD
1253 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1254 IEEE80211_BSS_TYPE_ESS;
1255
ceca7b71
JB
1256 if (!rdev->ops->connect)
1257 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1258 else
e35e4d28 1259 err = rdev_connect(rdev, dev, connect);
b23aa676 1260
ceca7b71
JB
1261 if (err) {
1262 wdev->connect_keys = NULL;
51e13359
JB
1263 /*
1264 * This could be reassoc getting refused, don't clear
1265 * ssid_len in that case.
1266 */
1267 if (!wdev->current_bss)
1268 wdev->ssid_len = 0;
ceca7b71 1269 return err;
6829c878 1270 }
ceca7b71
JB
1271
1272 return 0;
b23aa676
SO
1273}
1274
83739b03
JB
1275int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1276 struct net_device *dev, u16 reason, bool wextev)
b23aa676 1277{
6829c878 1278 struct wireless_dev *wdev = dev->ieee80211_ptr;
dee8a973 1279 int err = 0;
b23aa676 1280
667503dd
JB
1281 ASSERT_WDEV_LOCK(wdev);
1282
453431a5 1283 kfree_sensitive(wdev->connect_keys);
fffd0934
JB
1284 wdev->connect_keys = NULL;
1285
bd2522b1
AZ
1286 wdev->conn_owner_nlportid = 0;
1287
dee8a973 1288 if (wdev->conn)
ceca7b71 1289 err = cfg80211_sme_disconnect(wdev, reason);
dee8a973 1290 else if (!rdev->ops->disconnect)
ceca7b71 1291 cfg80211_mlme_down(rdev, dev);
0711d638 1292 else if (wdev->ssid_len)
e35e4d28 1293 err = rdev_disconnect(rdev, dev, reason);
b23aa676 1294
51e13359
JB
1295 /*
1296 * Clear ssid_len unless we actually were fully connected,
1297 * in which case cfg80211_disconnected() will take care of
1298 * this later.
1299 */
1300 if (!wdev->current_bss)
1301 wdev->ssid_len = 0;
1302
ceca7b71 1303 return err;
19957bb3 1304}
bd2522b1
AZ
1305
1306/*
1307 * Used to clean up after the connection / connection attempt owner socket
1308 * disconnects
1309 */
1310void cfg80211_autodisconnect_wk(struct work_struct *work)
1311{
1312 struct wireless_dev *wdev =
1313 container_of(work, struct wireless_dev, disconnect_wk);
1314 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1315
1316 wdev_lock(wdev);
1317
1318 if (wdev->conn_owner_nlportid) {
37b1c004
DK
1319 switch (wdev->iftype) {
1320 case NL80211_IFTYPE_ADHOC:
5a128a08 1321 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
37b1c004
DK
1322 break;
1323 case NL80211_IFTYPE_AP:
1324 case NL80211_IFTYPE_P2P_GO:
5a128a08 1325 __cfg80211_stop_ap(rdev, wdev->netdev, false);
37b1c004
DK
1326 break;
1327 case NL80211_IFTYPE_MESH_POINT:
5a128a08 1328 __cfg80211_leave_mesh(rdev, wdev->netdev);
37b1c004
DK
1329 break;
1330 case NL80211_IFTYPE_STATION:
1331 case NL80211_IFTYPE_P2P_CLIENT:
1332 /*
1333 * Use disconnect_bssid if still connecting and
1334 * ops->disconnect not implemented. Otherwise we can
1335 * use cfg80211_disconnect.
1336 */
1337 if (rdev->ops->disconnect || wdev->current_bss)
1338 cfg80211_disconnect(rdev, wdev->netdev,
1339 WLAN_REASON_DEAUTH_LEAVING,
1340 true);
1341 else
1342 cfg80211_mlme_deauth(rdev, wdev->netdev,
1343 wdev->disconnect_bssid,
1344 NULL, 0,
1345 WLAN_REASON_DEAUTH_LEAVING,
1346 false);
1347 break;
1348 default:
1349 break;
1350 }
bd2522b1
AZ
1351 }
1352
1353 wdev_unlock(wdev);
1354}