wifi: cfg80211: sme: hold wiphy lock for wdev iteration
[linux-2.6-block.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>
e9da6df7 8 * Copyright (C) 2009, 2020, 2022-2023 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;
325839da 150 struct cfg80211_auth_request auth_req = {};
f62fab73 151 struct cfg80211_assoc_request req = {};
19957bb3 152 int err;
6829c878 153
667503dd
JB
154 ASSERT_WDEV_LOCK(wdev);
155
6829c878
JB
156 if (!wdev->conn)
157 return 0;
158
19957bb3
JB
159 params = &wdev->conn->params;
160
6829c878 161 switch (wdev->conn->state) {
ceca7b71
JB
162 case CFG80211_CONN_SCANNING:
163 /* didn't find it during scan ... */
164 return -ENOENT;
6829c878
JB
165 case CFG80211_CONN_SCAN_AGAIN:
166 return cfg80211_conn_scan(wdev);
167 case CFG80211_CONN_AUTHENTICATE_NEXT:
2fd05115
JB
168 if (WARN_ON(!rdev->ops->auth))
169 return -EOPNOTSUPP;
19957bb3 170 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
325839da
JB
171 auth_req.key = params->key;
172 auth_req.key_len = params->key_len;
173 auth_req.key_idx = params->key_idx;
174 auth_req.auth_type = params->auth_type;
175 auth_req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
176 params->bssid,
177 params->ssid, params->ssid_len,
178 IEEE80211_BSS_TYPE_ESS,
179 IEEE80211_PRIVACY_ANY);
d648c230 180 auth_req.link_id = -1;
325839da
JB
181 err = cfg80211_mlme_auth(rdev, wdev->netdev, &auth_req);
182 cfg80211_put_bss(&rdev->wiphy, auth_req.bss);
183 return err;
3093ebbe
PK
184 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
185 *treason = NL80211_TIMEOUT_AUTH;
923a0e7d 186 return -ENOTCONN;
6829c878 187 case CFG80211_CONN_ASSOCIATE_NEXT:
2fd05115
JB
188 if (WARN_ON(!rdev->ops->assoc))
189 return -EOPNOTSUPP;
19957bb3 190 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
f401a6f7 191 if (wdev->conn->prev_bssid_valid)
f62fab73
JB
192 req.prev_bssid = wdev->conn->prev_bssid;
193 req.ie = params->ie;
194 req.ie_len = params->ie_len;
195 req.use_mfp = params->mfp != NL80211_MFP_NO;
196 req.crypto = params->crypto;
197 req.flags = params->flags;
198 req.ht_capa = params->ht_capa;
199 req.ht_capa_mask = params->ht_capa_mask;
200 req.vht_capa = params->vht_capa;
201 req.vht_capa_mask = params->vht_capa_mask;
d648c230 202 req.link_id = -1;
f62fab73 203
0f759448
JB
204 req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
205 params->bssid,
206 params->ssid, params->ssid_len,
207 IEEE80211_BSS_TYPE_ESS,
208 IEEE80211_PRIVACY_ANY);
209 if (!req.bss) {
210 err = -ENOENT;
211 } else {
212 err = cfg80211_mlme_assoc(rdev, wdev->netdev, &req);
213 cfg80211_put_bss(&rdev->wiphy, req.bss);
214 }
215
19957bb3 216 if (err)
91bf9b26
JB
217 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
218 NULL, 0,
219 WLAN_REASON_DEAUTH_LEAVING,
220 false);
19957bb3 221 return err;
3093ebbe
PK
222 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
223 *treason = NL80211_TIMEOUT_ASSOC;
7b506ff6 224 fallthrough;
923a0e7d
JB
225 case CFG80211_CONN_ASSOC_FAILED:
226 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
227 NULL, 0,
228 WLAN_REASON_DEAUTH_LEAVING, false);
229 return -ENOTCONN;
ceca7b71 230 case CFG80211_CONN_DEAUTH:
91bf9b26
JB
231 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
232 NULL, 0,
233 WLAN_REASON_DEAUTH_LEAVING, false);
7b506ff6 234 fallthrough;
e6f462df 235 case CFG80211_CONN_ABANDON:
923a0e7d
JB
236 /* free directly, disconnected event already sent */
237 cfg80211_sme_free(wdev);
ceca7b71 238 return 0;
6829c878
JB
239 default:
240 return 0;
241 }
242}
243
244void cfg80211_conn_work(struct work_struct *work)
245{
79c97e97 246 struct cfg80211_registered_device *rdev =
6829c878
JB
247 container_of(work, struct cfg80211_registered_device, conn_work);
248 struct wireless_dev *wdev;
7400f42e 249 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
3093ebbe 250 enum nl80211_timeout_reason treason;
6829c878 251
a05829a7 252 wiphy_lock(&rdev->wiphy);
6829c878 253
53873f13 254 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
c8157976
JB
255 if (!wdev->netdev)
256 continue;
257
667503dd
JB
258 wdev_lock(wdev);
259 if (!netif_running(wdev->netdev)) {
260 wdev_unlock(wdev);
6829c878 261 continue;
667503dd 262 }
ceca7b71
JB
263 if (!wdev->conn ||
264 wdev->conn->state == CFG80211_CONN_CONNECTED) {
667503dd 265 wdev_unlock(wdev);
6829c878 266 continue;
667503dd 267 }
7400f42e
JB
268 if (wdev->conn->params.bssid) {
269 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
270 bssid = bssid_buf;
271 }
3093ebbe
PK
272 treason = NL80211_TIMEOUT_UNSPECIFIED;
273 if (cfg80211_conn_do_work(wdev, &treason)) {
5349a0f7
VK
274 struct cfg80211_connect_resp_params cr;
275
276 memset(&cr, 0, sizeof(cr));
277 cr.status = -1;
efbabc11 278 cr.links[0].bssid = bssid;
5349a0f7
VK
279 cr.timeout_reason = treason;
280 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 281 }
667503dd 282 wdev_unlock(wdev);
6829c878
JB
283 }
284
a05829a7 285 wiphy_unlock(&rdev->wiphy);
6829c878
JB
286}
287
015b8cc5
AW
288static void cfg80211_step_auth_next(struct cfg80211_conn *conn,
289 struct cfg80211_bss *bss)
290{
291 memcpy(conn->bssid, bss->bssid, ETH_ALEN);
292 conn->params.bssid = conn->bssid;
293 conn->params.channel = bss->channel;
294 conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
295}
296
0e3a39b5 297/* Returned bss is reference counted and must be cleaned up appropriately. */
bbac31f4 298static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
6829c878 299{
f26cbf40 300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878 301 struct cfg80211_bss *bss;
6829c878 302
667503dd
JB
303 ASSERT_WDEV_LOCK(wdev);
304
ed9d0102
JM
305 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
306 wdev->conn->params.bssid,
6829c878
JB
307 wdev->conn->params.ssid,
308 wdev->conn->params.ssid_len,
34d50519 309 wdev->conn_bss_type,
6eb18137 310 IEEE80211_PRIVACY(wdev->conn->params.privacy));
6829c878 311 if (!bss)
bbac31f4 312 return NULL;
6829c878 313
015b8cc5 314 cfg80211_step_auth_next(wdev->conn, bss);
79c97e97 315 schedule_work(&rdev->conn_work);
6829c878 316
bbac31f4 317 return bss;
6829c878
JB
318}
319
667503dd 320static void __cfg80211_sme_scan_done(struct net_device *dev)
6829c878
JB
321{
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 323 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
bbac31f4 324 struct cfg80211_bss *bss;
6829c878 325
667503dd
JB
326 ASSERT_WDEV_LOCK(wdev);
327
d4b1a687 328 if (!wdev->conn)
6829c878
JB
329 return;
330
331 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
332 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
333 return;
334
bbac31f4 335 bss = cfg80211_get_conn_bss(wdev);
ceca7b71 336 if (bss)
5b112d3d 337 cfg80211_put_bss(&rdev->wiphy, bss);
ceca7b71
JB
338 else
339 schedule_work(&rdev->conn_work);
6829c878
JB
340}
341
667503dd
JB
342void cfg80211_sme_scan_done(struct net_device *dev)
343{
344 struct wireless_dev *wdev = dev->ieee80211_ptr;
345
346 wdev_lock(wdev);
347 __cfg80211_sme_scan_done(dev);
348 wdev_unlock(wdev);
349}
350
ceca7b71 351void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
6829c878 352{
6829c878 353 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 354 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
6829c878
JB
355 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
356 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
357
667503dd
JB
358 ASSERT_WDEV_LOCK(wdev);
359
ceca7b71 360 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
6829c878
JB
361 return;
362
363 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
364 wdev->conn->auto_auth &&
365 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
366 /* select automatically between only open, shared, leap */
367 switch (wdev->conn->params.auth_type) {
368 case NL80211_AUTHTYPE_OPEN_SYSTEM:
fffd0934
JB
369 if (wdev->connect_keys)
370 wdev->conn->params.auth_type =
371 NL80211_AUTHTYPE_SHARED_KEY;
372 else
373 wdev->conn->params.auth_type =
374 NL80211_AUTHTYPE_NETWORK_EAP;
6829c878
JB
375 break;
376 case NL80211_AUTHTYPE_SHARED_KEY:
377 wdev->conn->params.auth_type =
378 NL80211_AUTHTYPE_NETWORK_EAP;
379 break;
380 default:
381 /* huh? */
382 wdev->conn->params.auth_type =
383 NL80211_AUTHTYPE_OPEN_SYSTEM;
384 break;
385 }
386 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
387 schedule_work(&rdev->conn_work);
19957bb3 388 } else if (status_code != WLAN_STATUS_SUCCESS) {
5349a0f7
VK
389 struct cfg80211_connect_resp_params cr;
390
391 memset(&cr, 0, sizeof(cr));
392 cr.status = status_code;
efbabc11 393 cr.links[0].bssid = mgmt->bssid;
5349a0f7
VK
394 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
395 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 396 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
6829c878
JB
397 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
398 schedule_work(&rdev->conn_work);
399 }
400}
b23aa676 401
ceca7b71 402bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
f401a6f7 403{
f26cbf40 404 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
f401a6f7 405
ceca7b71 406 if (!wdev->conn)
f401a6f7
JB
407 return false;
408
ceca7b71
JB
409 if (status == WLAN_STATUS_SUCCESS) {
410 wdev->conn->state = CFG80211_CONN_CONNECTED;
f401a6f7 411 return false;
ceca7b71 412 }
f401a6f7 413
ceca7b71
JB
414 if (wdev->conn->prev_bssid_valid) {
415 /*
416 * Some stupid APs don't accept reassoc, so we
417 * need to fall back to trying regular assoc;
418 * return true so no event is sent to userspace.
419 */
420 wdev->conn->prev_bssid_valid = false;
421 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
422 schedule_work(&rdev->conn_work);
423 return true;
424 }
425
923a0e7d 426 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
f401a6f7 427 schedule_work(&rdev->conn_work);
ceca7b71
JB
428 return false;
429}
f401a6f7 430
ceca7b71
JB
431void cfg80211_sme_deauth(struct wireless_dev *wdev)
432{
433 cfg80211_sme_free(wdev);
f401a6f7
JB
434}
435
ceca7b71 436void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
7d930bc3 437{
f26cbf40 438 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
923a0e7d
JB
439
440 if (!wdev->conn)
441 return;
442
3093ebbe 443 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
923a0e7d 444 schedule_work(&rdev->conn_work);
ceca7b71 445}
7d930bc3 446
ceca7b71
JB
447void cfg80211_sme_disassoc(struct wireless_dev *wdev)
448{
f26cbf40 449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
450
451 if (!wdev->conn)
452 return;
453
454 wdev->conn->state = CFG80211_CONN_DEAUTH;
7d930bc3
JB
455 schedule_work(&rdev->conn_work);
456}
457
ceca7b71
JB
458void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
459{
f26cbf40 460 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
923a0e7d
JB
461
462 if (!wdev->conn)
463 return;
464
3093ebbe 465 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
923a0e7d 466 schedule_work(&rdev->conn_work);
ceca7b71
JB
467}
468
e6f462df
JB
469void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
470{
471 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
472
473 if (!wdev->conn)
474 return;
475
476 wdev->conn->state = CFG80211_CONN_ABANDON;
477 schedule_work(&rdev->conn_work);
478}
479
7b0a0e3c
JB
480static void cfg80211_wdev_release_bsses(struct wireless_dev *wdev)
481{
482 unsigned int link;
483
484 for_each_valid_link(wdev, link) {
485 if (!wdev->links[link].client.current_bss)
486 continue;
487 cfg80211_unhold_bss(wdev->links[link].client.current_bss);
488 cfg80211_put_bss(wdev->wiphy,
489 &wdev->links[link].client.current_bss->pub);
490 wdev->links[link].client.current_bss = NULL;
491 }
492}
493
065563b2
VJ
494void cfg80211_wdev_release_link_bsses(struct wireless_dev *wdev, u16 link_mask)
495{
496 unsigned int link;
497
498 for_each_valid_link(wdev, link) {
499 if (!wdev->links[link].client.current_bss ||
500 !(link_mask & BIT(link)))
501 continue;
502 cfg80211_unhold_bss(wdev->links[link].client.current_bss);
503 cfg80211_put_bss(wdev->wiphy,
504 &wdev->links[link].client.current_bss->pub);
505 wdev->links[link].client.current_bss = NULL;
506 }
507}
508
46b9d180
JB
509static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
510 const u8 *ies, size_t ies_len,
511 const u8 **out_ies, size_t *out_ies_len)
512{
513 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
514 u8 *buf;
515 size_t offs;
516
517 if (!rdev->wiphy.extended_capabilities_len ||
518 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
519 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
520 if (!*out_ies)
521 return -ENOMEM;
522 *out_ies_len = ies_len;
523 return 0;
524 }
525
526 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
527 GFP_KERNEL);
528 if (!buf)
529 return -ENOMEM;
530
531 if (ies_len) {
532 static const u8 before_extcapa[] = {
533 /* not listing IEs expected to be created by driver */
534 WLAN_EID_RSN,
535 WLAN_EID_QOS_CAPA,
536 WLAN_EID_RRM_ENABLED_CAPABILITIES,
537 WLAN_EID_MOBILITY_DOMAIN,
538 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
539 WLAN_EID_BSS_COEX_2040,
540 };
541
542 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
543 ARRAY_SIZE(before_extcapa), 0);
544 memcpy(buf, ies, offs);
545 /* leave a whole for extended capabilities IE */
546 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
547 ies + offs, ies_len - offs);
548 } else {
549 offs = 0;
550 }
551
552 /* place extended capabilities IE (with only driver capabilities) */
553 buf[offs] = WLAN_EID_EXT_CAPABILITY;
554 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
555 memcpy(buf + offs + 2,
556 rdev->wiphy.extended_capabilities,
557 rdev->wiphy.extended_capabilities_len);
558
559 *out_ies = buf;
560 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
561
562 return 0;
563}
564
ceca7b71
JB
565static int cfg80211_sme_connect(struct wireless_dev *wdev,
566 struct cfg80211_connect_params *connect,
567 const u8 *prev_bssid)
568{
f26cbf40 569 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
570 struct cfg80211_bss *bss;
571 int err;
572
573 if (!rdev->ops->auth || !rdev->ops->assoc)
574 return -EOPNOTSUPP;
575
7b0a0e3c 576 cfg80211_wdev_release_bsses(wdev);
4ce2bd9c 577
7b0a0e3c 578 if (wdev->connected) {
4ce2bd9c 579 cfg80211_sme_free(wdev);
7b0a0e3c 580 wdev->connected = false;
4ce2bd9c 581 }
ceca7b71 582
1b5ab825 583 if (wdev->conn)
ceca7b71
JB
584 return -EINPROGRESS;
585
586 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
587 if (!wdev->conn)
588 return -ENOMEM;
589
590 /*
591 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
592 */
593 memcpy(&wdev->conn->params, connect, sizeof(*connect));
594 if (connect->bssid) {
595 wdev->conn->params.bssid = wdev->conn->bssid;
596 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
597 }
598
46b9d180
JB
599 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
600 &wdev->conn->ie,
601 &wdev->conn->params.ie_len)) {
602 kfree(wdev->conn);
603 wdev->conn = NULL;
604 return -ENOMEM;
ceca7b71 605 }
46b9d180 606 wdev->conn->params.ie = wdev->conn->ie;
ceca7b71
JB
607
608 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
609 wdev->conn->auto_auth = true;
610 /* start with open system ... should mostly work */
611 wdev->conn->params.auth_type =
612 NL80211_AUTHTYPE_OPEN_SYSTEM;
613 } else {
614 wdev->conn->auto_auth = false;
615 }
616
7b0a0e3c
JB
617 wdev->conn->params.ssid = wdev->u.client.ssid;
618 wdev->conn->params.ssid_len = wdev->u.client.ssid_len;
ceca7b71
JB
619
620 /* see if we have the bss already */
015b8cc5
AW
621 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
622 wdev->conn->params.bssid,
623 wdev->conn->params.ssid,
624 wdev->conn->params.ssid_len,
625 wdev->conn_bss_type,
626 IEEE80211_PRIVACY(wdev->conn->params.privacy));
ceca7b71
JB
627
628 if (prev_bssid) {
629 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
630 wdev->conn->prev_bssid_valid = true;
631 }
632
633 /* we're good if we have a matching bss struct */
634 if (bss) {
3093ebbe
PK
635 enum nl80211_timeout_reason treason;
636
015b8cc5 637 cfg80211_step_auth_next(wdev->conn, bss);
3093ebbe 638 err = cfg80211_conn_do_work(wdev, &treason);
ceca7b71
JB
639 cfg80211_put_bss(wdev->wiphy, bss);
640 } else {
641 /* otherwise we'll need to scan for the AP first */
642 err = cfg80211_conn_scan(wdev);
643
644 /*
645 * If we can't scan right now, then we need to scan again
646 * after the current scan finished, since the parameters
647 * changed (unless we find a good AP anyway).
648 */
649 if (err == -EBUSY) {
650 err = 0;
651 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
652 }
653 }
654
655 if (err)
656 cfg80211_sme_free(wdev);
657
658 return err;
659}
660
661static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
662{
f26cbf40 663 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
664 int err;
665
666 if (!wdev->conn)
667 return 0;
668
669 if (!rdev->ops->deauth)
670 return -EOPNOTSUPP;
671
672 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
673 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
674 err = 0;
675 goto out;
676 }
677
678 /* wdev->conn->params.bssid must be set if > SCANNING */
679 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
680 wdev->conn->params.bssid,
681 NULL, 0, reason, false);
682 out:
683 cfg80211_sme_free(wdev);
684 return err;
685}
686
687/*
688 * code shared for in-device and software SME
689 */
690
691static bool cfg80211_is_all_idle(void)
692{
693 struct cfg80211_registered_device *rdev;
694 struct wireless_dev *wdev;
695 bool is_all_idle = true;
696
697 /*
698 * All devices must be idle as otherwise if you are actively
699 * scanning some new beacon hints could be learned and would
700 * count as new regulatory hints.
113f3aaa
S
701 * Also if there is any other active beaconing interface we
702 * need not issue a disconnect hint and reset any info such
703 * as chan dfs state, etc.
ceca7b71 704 */
7483a214 705 for_each_rdev(rdev) {
beb2df47 706 wiphy_lock(&rdev->wiphy);
53873f13 707 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
ceca7b71 708 wdev_lock(wdev);
7b0a0e3c 709 if (wdev->conn || wdev->connected ||
113f3aaa 710 cfg80211_beaconing_iface_active(wdev))
ceca7b71
JB
711 is_all_idle = false;
712 wdev_unlock(wdev);
713 }
beb2df47 714 wiphy_unlock(&rdev->wiphy);
ceca7b71
JB
715 }
716
717 return is_all_idle;
718}
719
720static void disconnect_work(struct work_struct *work)
721{
722 rtnl_lock();
723 if (cfg80211_is_all_idle())
724 regulatory_hint_disconnect();
725 rtnl_unlock();
726}
727
e005bd7d 728DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
ceca7b71 729
efbabc11
VJ
730static void
731cfg80211_connect_result_release_bsses(struct wireless_dev *wdev,
732 struct cfg80211_connect_resp_params *cr)
733{
734 unsigned int link;
735
736 for_each_valid_link(cr, link) {
737 if (!cr->links[link].bss)
738 continue;
739 cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
740 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
741 }
742}
743
ceca7b71
JB
744/*
745 * API calls for drivers implementing connect/disconnect and
746 * SME event handling
747 */
748
6f390908 749/* This method must consume bss one way or another */
5349a0f7
VK
750void __cfg80211_connect_result(struct net_device *dev,
751 struct cfg80211_connect_resp_params *cr,
752 bool wextev)
b23aa676
SO
753{
754 struct wireless_dev *wdev = dev->ieee80211_ptr;
e3d331c9 755 const struct element *country_elem = NULL;
c38c7018 756 const struct element *ssid;
fb8b53ac
JB
757 const u8 *country_data;
758 u8 country_datalen;
3d23e349 759#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
760 union iwreq_data wrqu;
761#endif
efbabc11
VJ
762 unsigned int link;
763 const u8 *connected_addr;
764 bool bss_not_found = false;
b23aa676 765
667503dd
JB
766 ASSERT_WDEV_LOCK(wdev);
767
074ac8df 768 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
efbabc11
VJ
769 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
770 goto out;
771
772 if (cr->valid_links) {
773 if (WARN_ON(!cr->ap_mld_addr))
774 goto out;
775
776 for_each_valid_link(cr, link) {
777 if (WARN_ON(!cr->links[link].addr))
778 goto out;
779 }
5ec245e4
VJ
780
781 if (WARN_ON(wdev->connect_keys))
782 goto out;
6f390908 783 }
b23aa676 784
4d797fce 785 wdev->unprot_beacon_reported = 0;
5349a0f7
VK
786 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
787 GFP_KERNEL);
efbabc11 788 connected_addr = cr->valid_links ? cr->ap_mld_addr : cr->links[0].bssid;
e45cd82a 789
3d23e349 790#ifdef CONFIG_CFG80211_WEXT
efbabc11 791 if (wextev && !cr->valid_links) {
5349a0f7 792 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
e45cd82a 793 memset(&wrqu, 0, sizeof(wrqu));
5349a0f7
VK
794 wrqu.data.length = cr->req_ie_len;
795 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
796 cr->req_ie);
e45cd82a
JB
797 }
798
5349a0f7 799 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
e45cd82a 800 memset(&wrqu, 0, sizeof(wrqu));
5349a0f7
VK
801 wrqu.data.length = cr->resp_ie_len;
802 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
803 cr->resp_ie);
e45cd82a
JB
804 }
805
806 memset(&wrqu, 0, sizeof(wrqu));
807 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
efbabc11
VJ
808 if (connected_addr && cr->status == WLAN_STATUS_SUCCESS) {
809 memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
810 memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
f401a6f7
JB
811 wdev->wext.prev_bssid_valid = true;
812 }
e45cd82a
JB
813 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
814 }
815#endif
816
efbabc11 817 if (cr->status == WLAN_STATUS_SUCCESS) {
baa56dfe
VJ
818 if (!wiphy_to_rdev(wdev->wiphy)->ops->connect) {
819 for_each_valid_link(cr, link) {
820 if (WARN_ON_ONCE(!cr->links[link].bss))
821 break;
822 }
efbabc11
VJ
823 }
824
825 for_each_valid_link(cr, link) {
53ad07e9
JB
826 /* don't do extra lookups for failures */
827 if (cr->links[link].status != WLAN_STATUS_SUCCESS)
828 continue;
829
efbabc11
VJ
830 if (cr->links[link].bss)
831 continue;
832
833 cr->links[link].bss =
834 cfg80211_get_bss(wdev->wiphy, NULL,
835 cr->links[link].bssid,
836 wdev->u.client.ssid,
837 wdev->u.client.ssid_len,
838 wdev->conn_bss_type,
839 IEEE80211_PRIVACY_ANY);
840 if (!cr->links[link].bss) {
841 bss_not_found = true;
842 break;
843 }
844 cfg80211_hold_bss(bss_from_pub(cr->links[link].bss));
845 }
4c4d684a
UR
846 }
847
7b0a0e3c 848 cfg80211_wdev_release_bsses(wdev);
df7fc0f9 849
5349a0f7 850 if (cr->status != WLAN_STATUS_SUCCESS) {
453431a5 851 kfree_sensitive(wdev->connect_keys);
fffd0934 852 wdev->connect_keys = NULL;
7b0a0e3c 853 wdev->u.client.ssid_len = 0;
bd2522b1 854 wdev->conn_owner_nlportid = 0;
efbabc11 855 cfg80211_connect_result_release_bsses(wdev, cr);
c1fbb258 856 cfg80211_sme_free(wdev);
fffd0934 857 return;
b23aa676 858 }
fffd0934 859
efbabc11
VJ
860 if (WARN_ON(bss_not_found)) {
861 cfg80211_connect_result_release_bsses(wdev, cr);
4c4d684a 862 return;
efbabc11 863 }
fffd0934 864
efbabc11 865 memset(wdev->links, 0, sizeof(wdev->links));
53ad07e9
JB
866 for_each_valid_link(cr, link) {
867 if (cr->links[link].status == WLAN_STATUS_SUCCESS)
868 continue;
869 cr->valid_links &= ~BIT(link);
870 /* don't require bss pointer for failed links */
871 if (!cr->links[link].bss)
872 continue;
873 cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
874 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
875 }
efbabc11
VJ
876 wdev->valid_links = cr->valid_links;
877 for_each_valid_link(cr, link)
878 wdev->links[link].client.current_bss =
879 bss_from_pub(cr->links[link].bss);
7b0a0e3c 880 wdev->connected = true;
efbabc11
VJ
881 ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
882 if (cr->valid_links) {
883 for_each_valid_link(cr, link)
884 memcpy(wdev->links[link].addr, cr->links[link].addr,
885 ETH_ALEN);
886 }
fffd0934 887
585b6e13 888 cfg80211_upload_connect_keys(wdev);
8b19e6ca 889
9caf0364 890 rcu_read_lock();
efbabc11
VJ
891 for_each_valid_link(cr, link) {
892 country_elem =
893 ieee80211_bss_get_elem(cr->links[link].bss,
894 WLAN_EID_COUNTRY);
895 if (country_elem)
896 break;
897 }
fb8b53ac 898 if (!country_elem) {
9caf0364
JB
899 rcu_read_unlock();
900 return;
901 }
902
fb8b53ac
JB
903 country_datalen = country_elem->datalen;
904 country_data = kmemdup(country_elem->data, country_datalen, GFP_ATOMIC);
9caf0364 905 rcu_read_unlock();
8b19e6ca 906
fb8b53ac 907 if (!country_data)
8b19e6ca
LR
908 return;
909
efbabc11
VJ
910 regulatory_hint_country_ie(wdev->wiphy,
911 cr->links[link].bss->channel->band,
fb8b53ac
JB
912 country_data, country_datalen);
913 kfree(country_data);
efbabc11 914
c38c7018
MB
915 if (!wdev->u.client.ssid_len) {
916 rcu_read_lock();
917 for_each_valid_link(cr, link) {
918 ssid = ieee80211_bss_get_elem(cr->links[link].bss,
919 WLAN_EID_SSID);
920
921 if (!ssid || !ssid->datalen)
922 continue;
923
924 memcpy(wdev->u.client.ssid, ssid->data, ssid->datalen);
925 wdev->u.client.ssid_len = ssid->datalen;
926 break;
927 }
928 rcu_read_unlock();
929 }
930
efbabc11
VJ
931 return;
932out:
933 for_each_valid_link(cr, link)
934 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
b23aa676 935}
f2129354 936
efbabc11
VJ
937static void cfg80211_update_link_bss(struct wireless_dev *wdev,
938 struct cfg80211_bss **bss)
939{
940 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
941 struct cfg80211_internal_bss *ibss;
942
943 if (!*bss)
944 return;
945
946 ibss = bss_from_pub(*bss);
947 if (list_empty(&ibss->list)) {
948 struct cfg80211_bss *found = NULL, *tmp = *bss;
949
950 found = cfg80211_get_bss(wdev->wiphy, NULL,
951 (*bss)->bssid,
952 wdev->u.client.ssid,
953 wdev->u.client.ssid_len,
954 wdev->conn_bss_type,
955 IEEE80211_PRIVACY_ANY);
956 if (found) {
957 /* The same BSS is already updated so use it
958 * instead, as it has latest info.
959 */
960 *bss = found;
961 } else {
962 /* Update with BSS provided by driver, it will
963 * be freshly added and ref cnted, we can free
964 * the old one.
965 *
966 * signal_valid can be false, as we are not
967 * expecting the BSS to be found.
968 *
969 * keep the old timestamp to avoid confusion
970 */
971 cfg80211_bss_update(rdev, ibss, false,
972 ibss->ts);
973 }
974
975 cfg80211_put_bss(wdev->wiphy, tmp);
976 }
977}
978
979/* Consumes bss object(s) one way or another */
5349a0f7
VK
980void cfg80211_connect_done(struct net_device *dev,
981 struct cfg80211_connect_resp_params *params,
982 gfp_t gfp)
f2129354 983{
667503dd 984 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 985 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
986 struct cfg80211_event *ev;
987 unsigned long flags;
5349a0f7 988 u8 *next;
efbabc11
VJ
989 size_t link_info_size = 0;
990 unsigned int link;
667503dd 991
efbabc11
VJ
992 for_each_valid_link(params, link) {
993 cfg80211_update_link_bss(wdev, &params->links[link].bss);
994 link_info_size += params->links[link].bssid ? ETH_ALEN : 0;
995 link_info_size += params->links[link].addr ? ETH_ALEN : 0;
e7054989
KV
996 }
997
efbabc11 998 ev = kzalloc(sizeof(*ev) + (params->ap_mld_addr ? ETH_ALEN : 0) +
a3caf744 999 params->req_ie_len + params->resp_ie_len +
76804d28 1000 params->fils.kek_len + params->fils.pmk_len +
efbabc11
VJ
1001 (params->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
1002 gfp);
1003
e7054989 1004 if (!ev) {
efbabc11
VJ
1005 for_each_valid_link(params, link)
1006 cfg80211_put_bss(wdev->wiphy,
1007 params->links[link].bss);
667503dd 1008 return;
e7054989 1009 }
667503dd
JB
1010
1011 ev->type = EVENT_CONNECT_RESULT;
5349a0f7 1012 next = ((u8 *)ev) + sizeof(*ev);
efbabc11
VJ
1013 if (params->ap_mld_addr) {
1014 ev->cr.ap_mld_addr = next;
1015 memcpy((void *)ev->cr.ap_mld_addr, params->ap_mld_addr,
1016 ETH_ALEN);
5349a0f7 1017 next += ETH_ALEN;
7834704b 1018 }
5349a0f7
VK
1019 if (params->req_ie_len) {
1020 ev->cr.req_ie = next;
1021 ev->cr.req_ie_len = params->req_ie_len;
1022 memcpy((void *)ev->cr.req_ie, params->req_ie,
1023 params->req_ie_len);
1024 next += params->req_ie_len;
7834704b 1025 }
5349a0f7
VK
1026 if (params->resp_ie_len) {
1027 ev->cr.resp_ie = next;
1028 ev->cr.resp_ie_len = params->resp_ie_len;
1029 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
1030 params->resp_ie_len);
1031 next += params->resp_ie_len;
1032 }
76804d28
AVS
1033 if (params->fils.kek_len) {
1034 ev->cr.fils.kek = next;
1035 ev->cr.fils.kek_len = params->fils.kek_len;
1036 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
1037 params->fils.kek_len);
1038 next += params->fils.kek_len;
a3caf744 1039 }
76804d28
AVS
1040 if (params->fils.pmk_len) {
1041 ev->cr.fils.pmk = next;
1042 ev->cr.fils.pmk_len = params->fils.pmk_len;
1043 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
1044 params->fils.pmk_len);
1045 next += params->fils.pmk_len;
a3caf744 1046 }
76804d28
AVS
1047 if (params->fils.pmkid) {
1048 ev->cr.fils.pmkid = next;
1049 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
1050 WLAN_PMKID_LEN);
a3caf744
VK
1051 next += WLAN_PMKID_LEN;
1052 }
76804d28
AVS
1053 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
1054 if (params->fils.update_erp_next_seq_num)
1055 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
efbabc11
VJ
1056 ev->cr.valid_links = params->valid_links;
1057 for_each_valid_link(params, link) {
1058 if (params->links[link].bss)
1059 cfg80211_hold_bss(
1060 bss_from_pub(params->links[link].bss));
1061 ev->cr.links[link].bss = params->links[link].bss;
1062
1063 if (params->links[link].addr) {
1064 ev->cr.links[link].addr = next;
1065 memcpy((void *)ev->cr.links[link].addr,
1066 params->links[link].addr,
1067 ETH_ALEN);
1068 next += ETH_ALEN;
1069 }
1070 if (params->links[link].bssid) {
1071 ev->cr.links[link].bssid = next;
1072 memcpy((void *)ev->cr.links[link].bssid,
1073 params->links[link].bssid,
1074 ETH_ALEN);
1075 next += ETH_ALEN;
1076 }
1077 }
5349a0f7
VK
1078 ev->cr.status = params->status;
1079 ev->cr.timeout_reason = params->timeout_reason;
667503dd
JB
1080
1081 spin_lock_irqsave(&wdev->event_lock, flags);
1082 list_add_tail(&ev->list, &wdev->event_list);
1083 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1084 queue_work(cfg80211_wq, &rdev->event_work);
f2129354 1085}
5349a0f7 1086EXPORT_SYMBOL(cfg80211_connect_done);
b23aa676 1087
0e3a39b5 1088/* Consumes bss object one way or another */
ed9d0102 1089void __cfg80211_roamed(struct wireless_dev *wdev,
29ce6ecb 1090 struct cfg80211_roam_info *info)
b23aa676 1091{
3d23e349 1092#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1093 union iwreq_data wrqu;
1094#endif
efbabc11
VJ
1095 unsigned int link;
1096 const u8 *connected_addr;
1097
667503dd
JB
1098 ASSERT_WDEV_LOCK(wdev);
1099
074ac8df
JB
1100 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1101 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
adbde344 1102 goto out;
b23aa676 1103
7b0a0e3c 1104 if (WARN_ON(!wdev->connected))
adbde344 1105 goto out;
b23aa676 1106
efbabc11
VJ
1107 if (info->valid_links) {
1108 if (WARN_ON(!info->ap_mld_addr))
1109 goto out;
1110
1111 for_each_valid_link(info, link) {
1112 if (WARN_ON(!info->links[link].addr))
1113 goto out;
1114 }
1115 }
1116
7b0a0e3c 1117 cfg80211_wdev_release_bsses(wdev);
b23aa676 1118
efbabc11
VJ
1119 for_each_valid_link(info, link) {
1120 if (WARN_ON(!info->links[link].bss))
1121 goto out;
1122 }
29ce6ecb 1123
efbabc11
VJ
1124 memset(wdev->links, 0, sizeof(wdev->links));
1125 wdev->valid_links = info->valid_links;
1126 for_each_valid_link(info, link) {
1127 cfg80211_hold_bss(bss_from_pub(info->links[link].bss));
1128 wdev->links[link].client.current_bss =
1129 bss_from_pub(info->links[link].bss);
1130 }
b23aa676 1131
efbabc11
VJ
1132 connected_addr = info->valid_links ?
1133 info->ap_mld_addr :
1134 info->links[0].bss->bssid;
1135 ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
1136 if (info->valid_links) {
1137 for_each_valid_link(info, link)
1138 memcpy(wdev->links[link].addr, info->links[link].addr,
1139 ETH_ALEN);
1140 }
4d797fce 1141 wdev->unprot_beacon_reported = 0;
f26cbf40 1142 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
29ce6ecb 1143 wdev->netdev, info, GFP_KERNEL);
b23aa676 1144
3d23e349 1145#ifdef CONFIG_CFG80211_WEXT
efbabc11
VJ
1146 if (!info->valid_links) {
1147 if (info->req_ie) {
1148 memset(&wrqu, 0, sizeof(wrqu));
1149 wrqu.data.length = info->req_ie_len;
1150 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
1151 &wrqu, info->req_ie);
1152 }
1153
1154 if (info->resp_ie) {
1155 memset(&wrqu, 0, sizeof(wrqu));
1156 wrqu.data.length = info->resp_ie_len;
1157 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
1158 &wrqu, info->resp_ie);
1159 }
b23aa676 1160
b23aa676 1161 memset(&wrqu, 0, sizeof(wrqu));
efbabc11
VJ
1162 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1163 memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
1164 memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
1165 wdev->wext.prev_bssid_valid = true;
1166 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
b23aa676 1167 }
b23aa676 1168#endif
adbde344
VT
1169
1170 return;
1171out:
efbabc11
VJ
1172 for_each_valid_link(info, link)
1173 cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
adbde344 1174}
adbde344 1175
efbabc11 1176/* Consumes info->links.bss object(s) one way or another */
29ce6ecb
AS
1177void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
1178 gfp_t gfp)
667503dd
JB
1179{
1180 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1181 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
1182 struct cfg80211_event *ev;
1183 unsigned long flags;
e841b7b1 1184 u8 *next;
efbabc11
VJ
1185 unsigned int link;
1186 size_t link_info_size = 0;
1187 bool bss_not_found = false;
1188
1189 for_each_valid_link(info, link) {
1190 link_info_size += info->links[link].addr ? ETH_ALEN : 0;
1191 link_info_size += info->links[link].bssid ? ETH_ALEN : 0;
1192
1193 if (info->links[link].bss)
1194 continue;
667503dd 1195
efbabc11
VJ
1196 info->links[link].bss =
1197 cfg80211_get_bss(wdev->wiphy,
1198 info->links[link].channel,
1199 info->links[link].bssid,
1200 wdev->u.client.ssid,
1201 wdev->u.client.ssid_len,
1202 wdev->conn_bss_type,
1203 IEEE80211_PRIVACY_ANY);
1204
1205 if (!info->links[link].bss) {
1206 bss_not_found = true;
1207 break;
1208 }
29ce6ecb
AS
1209 }
1210
efbabc11
VJ
1211 if (WARN_ON(bss_not_found))
1212 goto out;
adbde344 1213
e841b7b1
AVS
1214 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
1215 info->fils.kek_len + info->fils.pmk_len +
efbabc11
VJ
1216 (info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
1217 (info->ap_mld_addr ? ETH_ALEN : 0) + link_info_size, gfp);
1218 if (!ev)
1219 goto out;
667503dd
JB
1220
1221 ev->type = EVENT_ROAMED;
e841b7b1
AVS
1222 next = ((u8 *)ev) + sizeof(*ev);
1223 if (info->req_ie_len) {
1224 ev->rm.req_ie = next;
1225 ev->rm.req_ie_len = info->req_ie_len;
1226 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
1227 next += info->req_ie_len;
1228 }
1229 if (info->resp_ie_len) {
1230 ev->rm.resp_ie = next;
1231 ev->rm.resp_ie_len = info->resp_ie_len;
1232 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
1233 info->resp_ie_len);
1234 next += info->resp_ie_len;
1235 }
1236 if (info->fils.kek_len) {
1237 ev->rm.fils.kek = next;
1238 ev->rm.fils.kek_len = info->fils.kek_len;
1239 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1240 info->fils.kek_len);
1241 next += info->fils.kek_len;
1242 }
1243 if (info->fils.pmk_len) {
1244 ev->rm.fils.pmk = next;
1245 ev->rm.fils.pmk_len = info->fils.pmk_len;
1246 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1247 info->fils.pmk_len);
1248 next += info->fils.pmk_len;
1249 }
1250 if (info->fils.pmkid) {
1251 ev->rm.fils.pmkid = next;
1252 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1253 WLAN_PMKID_LEN);
1254 next += WLAN_PMKID_LEN;
1255 }
1256 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1257 if (info->fils.update_erp_next_seq_num)
1258 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
efbabc11
VJ
1259 if (info->ap_mld_addr) {
1260 ev->rm.ap_mld_addr = next;
1261 memcpy((void *)ev->rm.ap_mld_addr, info->ap_mld_addr,
1262 ETH_ALEN);
1263 next += ETH_ALEN;
1264 }
1265 ev->rm.valid_links = info->valid_links;
1266 for_each_valid_link(info, link) {
1267 ev->rm.links[link].bss = info->links[link].bss;
1268
1269 if (info->links[link].addr) {
1270 ev->rm.links[link].addr = next;
1271 memcpy((void *)ev->rm.links[link].addr,
1272 info->links[link].addr,
1273 ETH_ALEN);
1274 next += ETH_ALEN;
1275 }
1276
1277 if (info->links[link].bssid) {
1278 ev->rm.links[link].bssid = next;
1279 memcpy((void *)ev->rm.links[link].bssid,
1280 info->links[link].bssid,
1281 ETH_ALEN);
1282 next += ETH_ALEN;
1283 }
1284 }
667503dd
JB
1285
1286 spin_lock_irqsave(&wdev->event_lock, flags);
1287 list_add_tail(&ev->list, &wdev->event_list);
1288 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1289 queue_work(cfg80211_wq, &rdev->event_work);
efbabc11
VJ
1290
1291 return;
1292out:
1293 for_each_valid_link(info, link)
1294 cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1295
667503dd 1296}
29ce6ecb 1297EXPORT_SYMBOL(cfg80211_roamed);
b23aa676 1298
0ff57171
VY
1299void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid,
1300 const u8 *td_bitmap, u8 td_bitmap_len)
503c1fb9
AS
1301{
1302 ASSERT_WDEV_LOCK(wdev);
1303
8d70f33e
VY
1304 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1305 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
503c1fb9
AS
1306 return;
1307
7b0a0e3c
JB
1308 if (WARN_ON(!wdev->connected) ||
1309 WARN_ON(!ether_addr_equal(wdev->u.client.connected_addr, bssid)))
503c1fb9
AS
1310 return;
1311
1312 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
0ff57171 1313 bssid, td_bitmap, td_bitmap_len);
503c1fb9
AS
1314}
1315
1316void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
0ff57171 1317 const u8 *td_bitmap, u8 td_bitmap_len, gfp_t gfp)
503c1fb9
AS
1318{
1319 struct wireless_dev *wdev = dev->ieee80211_ptr;
1320 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1321 struct cfg80211_event *ev;
1322 unsigned long flags;
1323
1324 if (WARN_ON(!bssid))
1325 return;
1326
0ff57171 1327 ev = kzalloc(sizeof(*ev) + td_bitmap_len, gfp);
503c1fb9
AS
1328 if (!ev)
1329 return;
1330
1331 ev->type = EVENT_PORT_AUTHORIZED;
1332 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
0ff57171
VY
1333 ev->pa.td_bitmap = ((u8 *)ev) + sizeof(*ev);
1334 ev->pa.td_bitmap_len = td_bitmap_len;
1335 memcpy((void *)ev->pa.td_bitmap, td_bitmap, td_bitmap_len);
503c1fb9
AS
1336
1337 /*
1338 * Use the wdev event list so that if there are pending
1339 * connected/roamed events, they will be reported first.
1340 */
1341 spin_lock_irqsave(&wdev->event_lock, flags);
1342 list_add_tail(&ev->list, &wdev->event_list);
1343 spin_unlock_irqrestore(&wdev->event_lock, flags);
1344 queue_work(cfg80211_wq, &rdev->event_work);
1345}
1346EXPORT_SYMBOL(cfg80211_port_authorized);
1347
667503dd 1348void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
6829c878 1349 size_t ie_len, u16 reason, bool from_ap)
b23aa676
SO
1350{
1351 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1352 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
fffd0934 1353 int i;
3d23e349 1354#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1355 union iwreq_data wrqu;
1356#endif
1357
667503dd
JB
1358 ASSERT_WDEV_LOCK(wdev);
1359
074ac8df
JB
1360 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1361 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
b23aa676
SO
1362 return;
1363
7b0a0e3c
JB
1364 cfg80211_wdev_release_bsses(wdev);
1365 wdev->connected = false;
1366 wdev->u.client.ssid_len = 0;
bd2522b1 1367 wdev->conn_owner_nlportid = 0;
453431a5 1368 kfree_sensitive(wdev->connect_keys);
3027a8e7 1369 wdev->connect_keys = NULL;
b23aa676 1370
fffd0934
JB
1371 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1372
b8607152
AS
1373 /* stop critical protocol if supported */
1374 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1375 rdev->crit_proto_nlportid = 0;
1376 rdev_crit_proto_stop(rdev, wdev);
1377 }
1378
fffd0934
JB
1379 /*
1380 * Delete all the keys ... pairwise keys can't really
1381 * exist any more anyway, but default keys might.
1382 */
56be393f
JM
1383 if (rdev->ops->del_key) {
1384 int max_key_idx = 5;
1385
1386 if (wiphy_ext_feature_isset(
1387 wdev->wiphy,
0e47901d
JB
1388 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1389 wiphy_ext_feature_isset(
1390 wdev->wiphy,
1391 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
56be393f
JM
1392 max_key_idx = 7;
1393 for (i = 0; i <= max_key_idx; i++)
e7a7b84e 1394 rdev_del_key(rdev, dev, -1, i, false, NULL);
56be393f 1395 }
b23aa676 1396
fa9ffc74
KP
1397 rdev_set_qos_map(rdev, dev, NULL);
1398
3d23e349 1399#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1400 memset(&wrqu, 0, sizeof(wrqu));
1401 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1402 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
5f612033 1403 wdev->wext.connect.ssid_len = 0;
b23aa676 1404#endif
09d989d1
LR
1405
1406 schedule_work(&cfg80211_disconnect_work);
b23aa676
SO
1407}
1408
1409void cfg80211_disconnected(struct net_device *dev, u16 reason,
80279fb7
JB
1410 const u8 *ie, size_t ie_len,
1411 bool locally_generated, gfp_t gfp)
b23aa676 1412{
667503dd 1413 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1414 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
1415 struct cfg80211_event *ev;
1416 unsigned long flags;
1417
1418 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1419 if (!ev)
1420 return;
1421
1422 ev->type = EVENT_DISCONNECTED;
1423 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1424 ev->dc.ie_len = ie_len;
1425 memcpy((void *)ev->dc.ie, ie, ie_len);
1426 ev->dc.reason = reason;
80279fb7 1427 ev->dc.locally_generated = locally_generated;
667503dd
JB
1428
1429 spin_lock_irqsave(&wdev->event_lock, flags);
1430 list_add_tail(&ev->list, &wdev->event_list);
1431 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1432 queue_work(cfg80211_wq, &rdev->event_work);
b23aa676
SO
1433}
1434EXPORT_SYMBOL(cfg80211_disconnected);
1435
ceca7b71
JB
1436/*
1437 * API calls for nl80211/wext compatibility code
1438 */
83739b03
JB
1439int cfg80211_connect(struct cfg80211_registered_device *rdev,
1440 struct net_device *dev,
1441 struct cfg80211_connect_params *connect,
1442 struct cfg80211_cached_keys *connkeys,
1443 const u8 *prev_bssid)
b23aa676 1444{
b23aa676 1445 struct wireless_dev *wdev = dev->ieee80211_ptr;
667503dd
JB
1446 int err;
1447
1448 ASSERT_WDEV_LOCK(wdev);
b23aa676 1449
51e13359
JB
1450 /*
1451 * If we have an ssid_len, we're trying to connect or are
1452 * already connected, so reject a new SSID unless it's the
1453 * same (which is the case for re-association.)
1454 */
7b0a0e3c
JB
1455 if (wdev->u.client.ssid_len &&
1456 (wdev->u.client.ssid_len != connect->ssid_len ||
1457 memcmp(wdev->u.client.ssid, connect->ssid, wdev->u.client.ssid_len)))
51e13359
JB
1458 return -EALREADY;
1459
1460 /*
1461 * If connected, reject (re-)association unless prev_bssid
1462 * matches the current BSSID.
1463 */
7b0a0e3c 1464 if (wdev->connected) {
51e13359
JB
1465 if (!prev_bssid)
1466 return -EALREADY;
7b0a0e3c
JB
1467 if (!ether_addr_equal(prev_bssid,
1468 wdev->u.client.connected_addr))
51e13359 1469 return -ENOTCONN;
fffd0934
JB
1470 }
1471
51e13359
JB
1472 /*
1473 * Reject if we're in the process of connecting with WEP,
1474 * this case isn't very interesting and trying to handle
1475 * it would make the code much more complex.
1476 */
1477 if (wdev->connect_keys)
1478 return -EINPROGRESS;
1479
7e7c8926
BG
1480 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1481 rdev->wiphy.ht_capa_mod_mask);
81c5dce2
SM
1482 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1483 rdev->wiphy.vht_capa_mod_mask);
7e7c8926 1484
fffd0934
JB
1485 if (connkeys && connkeys->def >= 0) {
1486 int idx;
bcba8eae 1487 u32 cipher;
fffd0934
JB
1488
1489 idx = connkeys->def;
bcba8eae 1490 cipher = connkeys->params[idx].cipher;
fffd0934 1491 /* If given a WEP key we may need it for shared key auth */
bcba8eae
SO
1492 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1493 cipher == WLAN_CIPHER_SUITE_WEP104) {
fffd0934
JB
1494 connect->key_idx = idx;
1495 connect->key = connkeys->params[idx].key;
1496 connect->key_len = connkeys->params[idx].key_len;
bcba8eae
SO
1497
1498 /*
1499 * If ciphers are not set (e.g. when going through
1500 * iwconfig), we have to set them appropriately here.
1501 */
1502 if (connect->crypto.cipher_group == 0)
1503 connect->crypto.cipher_group = cipher;
1504
1505 if (connect->crypto.n_ciphers_pairwise == 0) {
1506 connect->crypto.n_ciphers_pairwise = 1;
1507 connect->crypto.ciphers_pairwise[0] = cipher;
1508 }
fffd0934 1509 }
f1c1f17a
JB
1510 } else {
1511 if (WARN_ON(connkeys))
1512 return -EINVAL;
015b8cc5
AW
1513
1514 /* connect can point to wdev->wext.connect which
1515 * can hold key data from a previous connection
1516 */
1517 connect->key = NULL;
1518 connect->key_len = 0;
1519 connect->key_idx = 0;
fffd0934
JB
1520 }
1521
ceca7b71 1522 wdev->connect_keys = connkeys;
7b0a0e3c
JB
1523 memcpy(wdev->u.client.ssid, connect->ssid, connect->ssid_len);
1524 wdev->u.client.ssid_len = connect->ssid_len;
6829c878 1525
34d50519
LD
1526 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1527 IEEE80211_BSS_TYPE_ESS;
1528
ceca7b71
JB
1529 if (!rdev->ops->connect)
1530 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1531 else
e35e4d28 1532 err = rdev_connect(rdev, dev, connect);
b23aa676 1533
ceca7b71
JB
1534 if (err) {
1535 wdev->connect_keys = NULL;
51e13359
JB
1536 /*
1537 * This could be reassoc getting refused, don't clear
1538 * ssid_len in that case.
1539 */
7b0a0e3c
JB
1540 if (!wdev->connected)
1541 wdev->u.client.ssid_len = 0;
ceca7b71 1542 return err;
6829c878 1543 }
ceca7b71
JB
1544
1545 return 0;
b23aa676
SO
1546}
1547
83739b03
JB
1548int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1549 struct net_device *dev, u16 reason, bool wextev)
b23aa676 1550{
6829c878 1551 struct wireless_dev *wdev = dev->ieee80211_ptr;
dee8a973 1552 int err = 0;
b23aa676 1553
667503dd
JB
1554 ASSERT_WDEV_LOCK(wdev);
1555
453431a5 1556 kfree_sensitive(wdev->connect_keys);
fffd0934
JB
1557 wdev->connect_keys = NULL;
1558
bd2522b1
AZ
1559 wdev->conn_owner_nlportid = 0;
1560
dee8a973 1561 if (wdev->conn)
ceca7b71 1562 err = cfg80211_sme_disconnect(wdev, reason);
dee8a973 1563 else if (!rdev->ops->disconnect)
ceca7b71 1564 cfg80211_mlme_down(rdev, dev);
7b0a0e3c 1565 else if (wdev->u.client.ssid_len)
e35e4d28 1566 err = rdev_disconnect(rdev, dev, reason);
b23aa676 1567
51e13359
JB
1568 /*
1569 * Clear ssid_len unless we actually were fully connected,
1570 * in which case cfg80211_disconnected() will take care of
1571 * this later.
1572 */
7b0a0e3c
JB
1573 if (!wdev->connected)
1574 wdev->u.client.ssid_len = 0;
51e13359 1575
ceca7b71 1576 return err;
19957bb3 1577}
bd2522b1
AZ
1578
1579/*
1580 * Used to clean up after the connection / connection attempt owner socket
1581 * disconnects
1582 */
1583void cfg80211_autodisconnect_wk(struct work_struct *work)
1584{
1585 struct wireless_dev *wdev =
1586 container_of(work, struct wireless_dev, disconnect_wk);
1587 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1588
e9da6df7 1589 wiphy_lock(wdev->wiphy);
bd2522b1
AZ
1590 wdev_lock(wdev);
1591
1592 if (wdev->conn_owner_nlportid) {
37b1c004
DK
1593 switch (wdev->iftype) {
1594 case NL80211_IFTYPE_ADHOC:
5a128a08 1595 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
37b1c004
DK
1596 break;
1597 case NL80211_IFTYPE_AP:
1598 case NL80211_IFTYPE_P2P_GO:
7b0a0e3c 1599 __cfg80211_stop_ap(rdev, wdev->netdev, -1, false);
37b1c004
DK
1600 break;
1601 case NL80211_IFTYPE_MESH_POINT:
5a128a08 1602 __cfg80211_leave_mesh(rdev, wdev->netdev);
37b1c004
DK
1603 break;
1604 case NL80211_IFTYPE_STATION:
1605 case NL80211_IFTYPE_P2P_CLIENT:
1606 /*
1607 * Use disconnect_bssid if still connecting and
1608 * ops->disconnect not implemented. Otherwise we can
1609 * use cfg80211_disconnect.
1610 */
7b0a0e3c 1611 if (rdev->ops->disconnect || wdev->connected)
37b1c004
DK
1612 cfg80211_disconnect(rdev, wdev->netdev,
1613 WLAN_REASON_DEAUTH_LEAVING,
1614 true);
1615 else
1616 cfg80211_mlme_deauth(rdev, wdev->netdev,
1617 wdev->disconnect_bssid,
1618 NULL, 0,
1619 WLAN_REASON_DEAUTH_LEAVING,
1620 false);
1621 break;
1622 default:
1623 break;
1624 }
bd2522b1
AZ
1625 }
1626
1627 wdev_unlock(wdev);
e9da6df7 1628 wiphy_unlock(wdev->wiphy);
bd2522b1 1629}