wireless: wext: avoid gcc -O3 warning
[linux-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>
8 * Copyright (C) 2009 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
27 * driver. This is is for implementing nl80211's connect/disconnect
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
70 ASSERT_RTNL();
667503dd 71 ASSERT_WDEV_LOCK(wdev);
6829c878 72
f9d15d16 73 if (rdev->scan_req || rdev->scan_msg)
6829c878
JB
74 return -EBUSY;
75
bdfbec2d 76 if (wdev->conn->params.channel)
6829c878 77 n_channels = 1;
bdfbec2d
IP
78 else
79 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
6829c878 80
6829c878
JB
81 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
82 sizeof(request->channels[0]) * n_channels,
83 GFP_KERNEL);
84 if (!request)
85 return -ENOMEM;
86
2a84ee86 87 if (wdev->conn->params.channel) {
57fbcce3 88 enum nl80211_band band = wdev->conn->params.channel->band;
2a84ee86
KB
89 struct ieee80211_supported_band *sband =
90 wdev->wiphy->bands[band];
91
92 if (!sband) {
93 kfree(request);
94 return -EINVAL;
95 }
6829c878 96 request->channels[0] = wdev->conn->params.channel;
2a84ee86
KB
97 request->rates[band] = (1 << sband->n_bitrates) - 1;
98 } else {
6829c878 99 int i = 0, j;
57fbcce3 100 enum nl80211_band band;
e3081501
RM
101 struct ieee80211_supported_band *bands;
102 struct ieee80211_channel *channel;
6829c878 103
57fbcce3 104 for (band = 0; band < NUM_NL80211_BANDS; band++) {
e3081501
RM
105 bands = wdev->wiphy->bands[band];
106 if (!bands)
6829c878 107 continue;
e3081501
RM
108 for (j = 0; j < bands->n_channels; j++) {
109 channel = &bands->channels[j];
110 if (channel->flags & IEEE80211_CHAN_DISABLED)
111 continue;
112 request->channels[i++] = channel;
113 }
114 request->rates[band] = (1 << bands->n_bitrates) - 1;
6829c878 115 }
e3081501 116 n_channels = i;
6829c878
JB
117 }
118 request->n_channels = n_channels;
5ba63533 119 request->ssids = (void *)&request->channels[n_channels];
6829c878
JB
120 request->n_ssids = 1;
121
122 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
123 wdev->conn->params.ssid_len);
124 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
125
818965d3
JM
126 eth_broadcast_addr(request->bssid);
127
fd014284 128 request->wdev = wdev;
79c97e97 129 request->wiphy = &rdev->wiphy;
15d6030b 130 request->scan_start = jiffies;
6829c878 131
79c97e97 132 rdev->scan_req = request;
6829c878 133
e35e4d28 134 err = rdev_scan(rdev, request);
6829c878
JB
135 if (!err) {
136 wdev->conn->state = CFG80211_CONN_SCANNING;
fd014284 137 nl80211_send_scan_start(rdev, wdev);
463d0183 138 dev_hold(wdev->netdev);
6829c878 139 } else {
79c97e97 140 rdev->scan_req = NULL;
6829c878
JB
141 kfree(request);
142 }
143 return err;
144}
145
3093ebbe
PK
146static int cfg80211_conn_do_work(struct wireless_dev *wdev,
147 enum nl80211_timeout_reason *treason)
6829c878 148{
f26cbf40 149 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19957bb3 150 struct cfg80211_connect_params *params;
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;
91bf9b26
JB
171 return cfg80211_mlme_auth(rdev, wdev->netdev,
172 params->channel, params->auth_type,
173 params->bssid,
174 params->ssid, params->ssid_len,
175 NULL, 0,
176 params->key, params->key_len,
177 params->key_idx, NULL, 0);
3093ebbe
PK
178 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
179 *treason = NL80211_TIMEOUT_AUTH;
923a0e7d 180 return -ENOTCONN;
6829c878 181 case CFG80211_CONN_ASSOCIATE_NEXT:
2fd05115
JB
182 if (WARN_ON(!rdev->ops->assoc))
183 return -EOPNOTSUPP;
19957bb3 184 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
f401a6f7 185 if (wdev->conn->prev_bssid_valid)
f62fab73
JB
186 req.prev_bssid = wdev->conn->prev_bssid;
187 req.ie = params->ie;
188 req.ie_len = params->ie_len;
189 req.use_mfp = params->mfp != NL80211_MFP_NO;
190 req.crypto = params->crypto;
191 req.flags = params->flags;
192 req.ht_capa = params->ht_capa;
193 req.ht_capa_mask = params->ht_capa_mask;
194 req.vht_capa = params->vht_capa;
195 req.vht_capa_mask = params->vht_capa_mask;
196
91bf9b26
JB
197 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
198 params->bssid, params->ssid,
199 params->ssid_len, &req);
19957bb3 200 if (err)
91bf9b26
JB
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING,
204 false);
19957bb3 205 return err;
3093ebbe
PK
206 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
207 *treason = NL80211_TIMEOUT_ASSOC;
208 /* fall through */
923a0e7d
JB
209 case CFG80211_CONN_ASSOC_FAILED:
210 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211 NULL, 0,
212 WLAN_REASON_DEAUTH_LEAVING, false);
213 return -ENOTCONN;
ceca7b71 214 case CFG80211_CONN_DEAUTH:
91bf9b26
JB
215 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
216 NULL, 0,
217 WLAN_REASON_DEAUTH_LEAVING, false);
e6f462df
JB
218 /* fall through */
219 case CFG80211_CONN_ABANDON:
923a0e7d
JB
220 /* free directly, disconnected event already sent */
221 cfg80211_sme_free(wdev);
ceca7b71 222 return 0;
6829c878
JB
223 default:
224 return 0;
225 }
226}
227
228void cfg80211_conn_work(struct work_struct *work)
229{
79c97e97 230 struct cfg80211_registered_device *rdev =
6829c878
JB
231 container_of(work, struct cfg80211_registered_device, conn_work);
232 struct wireless_dev *wdev;
7400f42e 233 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
3093ebbe 234 enum nl80211_timeout_reason treason;
6829c878
JB
235
236 rtnl_lock();
6829c878 237
53873f13 238 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
c8157976
JB
239 if (!wdev->netdev)
240 continue;
241
667503dd
JB
242 wdev_lock(wdev);
243 if (!netif_running(wdev->netdev)) {
244 wdev_unlock(wdev);
6829c878 245 continue;
667503dd 246 }
ceca7b71
JB
247 if (!wdev->conn ||
248 wdev->conn->state == CFG80211_CONN_CONNECTED) {
667503dd 249 wdev_unlock(wdev);
6829c878 250 continue;
667503dd 251 }
7400f42e
JB
252 if (wdev->conn->params.bssid) {
253 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
254 bssid = bssid_buf;
255 }
3093ebbe
PK
256 treason = NL80211_TIMEOUT_UNSPECIFIED;
257 if (cfg80211_conn_do_work(wdev, &treason)) {
5349a0f7
VK
258 struct cfg80211_connect_resp_params cr;
259
260 memset(&cr, 0, sizeof(cr));
261 cr.status = -1;
262 cr.bssid = bssid;
263 cr.timeout_reason = treason;
264 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 265 }
667503dd 266 wdev_unlock(wdev);
6829c878
JB
267 }
268
6829c878
JB
269 rtnl_unlock();
270}
271
0e3a39b5 272/* Returned bss is reference counted and must be cleaned up appropriately. */
bbac31f4 273static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
6829c878 274{
f26cbf40 275 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
6829c878 276 struct cfg80211_bss *bss;
6829c878 277
667503dd
JB
278 ASSERT_WDEV_LOCK(wdev);
279
ed9d0102
JM
280 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
281 wdev->conn->params.bssid,
6829c878
JB
282 wdev->conn->params.ssid,
283 wdev->conn->params.ssid_len,
34d50519 284 wdev->conn_bss_type,
6eb18137 285 IEEE80211_PRIVACY(wdev->conn->params.privacy));
6829c878 286 if (!bss)
bbac31f4 287 return NULL;
6829c878
JB
288
289 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
290 wdev->conn->params.bssid = wdev->conn->bssid;
291 wdev->conn->params.channel = bss->channel;
292 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
79c97e97 293 schedule_work(&rdev->conn_work);
6829c878 294
bbac31f4 295 return bss;
6829c878
JB
296}
297
667503dd 298static void __cfg80211_sme_scan_done(struct net_device *dev)
6829c878
JB
299{
300 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 301 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
bbac31f4 302 struct cfg80211_bss *bss;
6829c878 303
667503dd
JB
304 ASSERT_WDEV_LOCK(wdev);
305
d4b1a687 306 if (!wdev->conn)
6829c878
JB
307 return;
308
309 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
310 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
311 return;
312
bbac31f4 313 bss = cfg80211_get_conn_bss(wdev);
ceca7b71 314 if (bss)
5b112d3d 315 cfg80211_put_bss(&rdev->wiphy, bss);
ceca7b71
JB
316 else
317 schedule_work(&rdev->conn_work);
6829c878
JB
318}
319
667503dd
JB
320void cfg80211_sme_scan_done(struct net_device *dev)
321{
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
323
324 wdev_lock(wdev);
325 __cfg80211_sme_scan_done(dev);
326 wdev_unlock(wdev);
327}
328
ceca7b71 329void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
6829c878 330{
6829c878 331 struct wiphy *wiphy = wdev->wiphy;
f26cbf40 332 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
6829c878
JB
333 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
334 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
335
667503dd
JB
336 ASSERT_WDEV_LOCK(wdev);
337
ceca7b71 338 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
6829c878
JB
339 return;
340
341 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
342 wdev->conn->auto_auth &&
343 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
344 /* select automatically between only open, shared, leap */
345 switch (wdev->conn->params.auth_type) {
346 case NL80211_AUTHTYPE_OPEN_SYSTEM:
fffd0934
JB
347 if (wdev->connect_keys)
348 wdev->conn->params.auth_type =
349 NL80211_AUTHTYPE_SHARED_KEY;
350 else
351 wdev->conn->params.auth_type =
352 NL80211_AUTHTYPE_NETWORK_EAP;
6829c878
JB
353 break;
354 case NL80211_AUTHTYPE_SHARED_KEY:
355 wdev->conn->params.auth_type =
356 NL80211_AUTHTYPE_NETWORK_EAP;
357 break;
358 default:
359 /* huh? */
360 wdev->conn->params.auth_type =
361 NL80211_AUTHTYPE_OPEN_SYSTEM;
362 break;
363 }
364 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
365 schedule_work(&rdev->conn_work);
19957bb3 366 } else if (status_code != WLAN_STATUS_SUCCESS) {
5349a0f7
VK
367 struct cfg80211_connect_resp_params cr;
368
369 memset(&cr, 0, sizeof(cr));
370 cr.status = status_code;
371 cr.bssid = mgmt->bssid;
372 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
373 __cfg80211_connect_result(wdev->netdev, &cr, false);
ceca7b71 374 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
6829c878
JB
375 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
376 schedule_work(&rdev->conn_work);
377 }
378}
b23aa676 379
ceca7b71 380bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
f401a6f7 381{
f26cbf40 382 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
f401a6f7 383
ceca7b71 384 if (!wdev->conn)
f401a6f7
JB
385 return false;
386
ceca7b71
JB
387 if (status == WLAN_STATUS_SUCCESS) {
388 wdev->conn->state = CFG80211_CONN_CONNECTED;
f401a6f7 389 return false;
ceca7b71 390 }
f401a6f7 391
ceca7b71
JB
392 if (wdev->conn->prev_bssid_valid) {
393 /*
394 * Some stupid APs don't accept reassoc, so we
395 * need to fall back to trying regular assoc;
396 * return true so no event is sent to userspace.
397 */
398 wdev->conn->prev_bssid_valid = false;
399 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
400 schedule_work(&rdev->conn_work);
401 return true;
402 }
403
923a0e7d 404 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
f401a6f7 405 schedule_work(&rdev->conn_work);
ceca7b71
JB
406 return false;
407}
f401a6f7 408
ceca7b71
JB
409void cfg80211_sme_deauth(struct wireless_dev *wdev)
410{
411 cfg80211_sme_free(wdev);
f401a6f7
JB
412}
413
ceca7b71 414void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
7d930bc3 415{
f26cbf40 416 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
923a0e7d
JB
417
418 if (!wdev->conn)
419 return;
420
3093ebbe 421 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
923a0e7d 422 schedule_work(&rdev->conn_work);
ceca7b71 423}
7d930bc3 424
ceca7b71
JB
425void cfg80211_sme_disassoc(struct wireless_dev *wdev)
426{
f26cbf40 427 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
428
429 if (!wdev->conn)
430 return;
431
432 wdev->conn->state = CFG80211_CONN_DEAUTH;
7d930bc3
JB
433 schedule_work(&rdev->conn_work);
434}
435
ceca7b71
JB
436void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
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_ASSOC_FAILED_TIMEOUT;
923a0e7d 444 schedule_work(&rdev->conn_work);
ceca7b71
JB
445}
446
e6f462df
JB
447void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
448{
449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
450
451 if (!wdev->conn)
452 return;
453
454 wdev->conn->state = CFG80211_CONN_ABANDON;
455 schedule_work(&rdev->conn_work);
456}
457
46b9d180
JB
458static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
459 const u8 *ies, size_t ies_len,
460 const u8 **out_ies, size_t *out_ies_len)
461{
462 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
463 u8 *buf;
464 size_t offs;
465
466 if (!rdev->wiphy.extended_capabilities_len ||
467 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
468 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
469 if (!*out_ies)
470 return -ENOMEM;
471 *out_ies_len = ies_len;
472 return 0;
473 }
474
475 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
476 GFP_KERNEL);
477 if (!buf)
478 return -ENOMEM;
479
480 if (ies_len) {
481 static const u8 before_extcapa[] = {
482 /* not listing IEs expected to be created by driver */
483 WLAN_EID_RSN,
484 WLAN_EID_QOS_CAPA,
485 WLAN_EID_RRM_ENABLED_CAPABILITIES,
486 WLAN_EID_MOBILITY_DOMAIN,
487 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
488 WLAN_EID_BSS_COEX_2040,
489 };
490
491 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
492 ARRAY_SIZE(before_extcapa), 0);
493 memcpy(buf, ies, offs);
494 /* leave a whole for extended capabilities IE */
495 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
496 ies + offs, ies_len - offs);
497 } else {
498 offs = 0;
499 }
500
501 /* place extended capabilities IE (with only driver capabilities) */
502 buf[offs] = WLAN_EID_EXT_CAPABILITY;
503 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
504 memcpy(buf + offs + 2,
505 rdev->wiphy.extended_capabilities,
506 rdev->wiphy.extended_capabilities_len);
507
508 *out_ies = buf;
509 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
510
511 return 0;
512}
513
ceca7b71
JB
514static int cfg80211_sme_connect(struct wireless_dev *wdev,
515 struct cfg80211_connect_params *connect,
516 const u8 *prev_bssid)
517{
f26cbf40 518 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
519 struct cfg80211_bss *bss;
520 int err;
521
522 if (!rdev->ops->auth || !rdev->ops->assoc)
523 return -EOPNOTSUPP;
524
4ce2bd9c 525 if (wdev->current_bss) {
4ce2bd9c
JM
526 cfg80211_unhold_bss(wdev->current_bss);
527 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
528 wdev->current_bss = NULL;
529
530 cfg80211_sme_free(wdev);
531 }
ceca7b71
JB
532
533 if (WARN_ON(wdev->conn))
534 return -EINPROGRESS;
535
536 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
537 if (!wdev->conn)
538 return -ENOMEM;
539
540 /*
541 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
542 */
543 memcpy(&wdev->conn->params, connect, sizeof(*connect));
544 if (connect->bssid) {
545 wdev->conn->params.bssid = wdev->conn->bssid;
546 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
547 }
548
46b9d180
JB
549 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
550 &wdev->conn->ie,
551 &wdev->conn->params.ie_len)) {
552 kfree(wdev->conn);
553 wdev->conn = NULL;
554 return -ENOMEM;
ceca7b71 555 }
46b9d180 556 wdev->conn->params.ie = wdev->conn->ie;
ceca7b71
JB
557
558 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
559 wdev->conn->auto_auth = true;
560 /* start with open system ... should mostly work */
561 wdev->conn->params.auth_type =
562 NL80211_AUTHTYPE_OPEN_SYSTEM;
563 } else {
564 wdev->conn->auto_auth = false;
565 }
566
567 wdev->conn->params.ssid = wdev->ssid;
babd3a27 568 wdev->conn->params.ssid_len = wdev->ssid_len;
ceca7b71
JB
569
570 /* see if we have the bss already */
571 bss = cfg80211_get_conn_bss(wdev);
572
573 if (prev_bssid) {
574 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
575 wdev->conn->prev_bssid_valid = true;
576 }
577
578 /* we're good if we have a matching bss struct */
579 if (bss) {
3093ebbe
PK
580 enum nl80211_timeout_reason treason;
581
582 err = cfg80211_conn_do_work(wdev, &treason);
ceca7b71
JB
583 cfg80211_put_bss(wdev->wiphy, bss);
584 } else {
585 /* otherwise we'll need to scan for the AP first */
586 err = cfg80211_conn_scan(wdev);
587
588 /*
589 * If we can't scan right now, then we need to scan again
590 * after the current scan finished, since the parameters
591 * changed (unless we find a good AP anyway).
592 */
593 if (err == -EBUSY) {
594 err = 0;
595 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
596 }
597 }
598
599 if (err)
600 cfg80211_sme_free(wdev);
601
602 return err;
603}
604
605static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
606{
f26cbf40 607 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
ceca7b71
JB
608 int err;
609
610 if (!wdev->conn)
611 return 0;
612
613 if (!rdev->ops->deauth)
614 return -EOPNOTSUPP;
615
616 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
617 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
618 err = 0;
619 goto out;
620 }
621
622 /* wdev->conn->params.bssid must be set if > SCANNING */
623 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
624 wdev->conn->params.bssid,
625 NULL, 0, reason, false);
626 out:
627 cfg80211_sme_free(wdev);
628 return err;
629}
630
631/*
632 * code shared for in-device and software SME
633 */
634
635static bool cfg80211_is_all_idle(void)
636{
637 struct cfg80211_registered_device *rdev;
638 struct wireless_dev *wdev;
639 bool is_all_idle = true;
640
641 /*
642 * All devices must be idle as otherwise if you are actively
643 * scanning some new beacon hints could be learned and would
644 * count as new regulatory hints.
113f3aaa
S
645 * Also if there is any other active beaconing interface we
646 * need not issue a disconnect hint and reset any info such
647 * as chan dfs state, etc.
ceca7b71
JB
648 */
649 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53873f13 650 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
ceca7b71 651 wdev_lock(wdev);
113f3aaa
S
652 if (wdev->conn || wdev->current_bss ||
653 cfg80211_beaconing_iface_active(wdev))
ceca7b71
JB
654 is_all_idle = false;
655 wdev_unlock(wdev);
656 }
657 }
658
659 return is_all_idle;
660}
661
662static void disconnect_work(struct work_struct *work)
663{
664 rtnl_lock();
665 if (cfg80211_is_all_idle())
666 regulatory_hint_disconnect();
667 rtnl_unlock();
668}
669
e005bd7d 670DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
ceca7b71
JB
671
672
673/*
674 * API calls for drivers implementing connect/disconnect and
675 * SME event handling
676 */
677
6f390908 678/* This method must consume bss one way or another */
5349a0f7
VK
679void __cfg80211_connect_result(struct net_device *dev,
680 struct cfg80211_connect_resp_params *cr,
681 bool wextev)
b23aa676
SO
682{
683 struct wireless_dev *wdev = dev->ieee80211_ptr;
9caf0364 684 const u8 *country_ie;
3d23e349 685#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
686 union iwreq_data wrqu;
687#endif
688
667503dd
JB
689 ASSERT_WDEV_LOCK(wdev);
690
074ac8df 691 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6f390908 692 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
5349a0f7 693 cfg80211_put_bss(wdev->wiphy, cr->bss);
b23aa676 694 return;
6f390908 695 }
b23aa676 696
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) {
b47f610b 744 kzfree(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
f26cbf40 924 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
29ce6ecb 925 wdev->netdev, info, GFP_KERNEL);
b23aa676 926
3d23e349 927#ifdef CONFIG_CFG80211_WEXT
29ce6ecb 928 if (info->req_ie) {
b23aa676 929 memset(&wrqu, 0, sizeof(wrqu));
29ce6ecb 930 wrqu.data.length = info->req_ie_len;
3409ff77 931 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
29ce6ecb 932 &wrqu, info->req_ie);
b23aa676
SO
933 }
934
29ce6ecb 935 if (info->resp_ie) {
b23aa676 936 memset(&wrqu, 0, sizeof(wrqu));
29ce6ecb 937 wrqu.data.length = info->resp_ie_len;
667503dd 938 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
29ce6ecb 939 &wrqu, info->resp_ie);
b23aa676
SO
940 }
941
942 memset(&wrqu, 0, sizeof(wrqu));
943 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
29ce6ecb
AS
944 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
945 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
f401a6f7 946 wdev->wext.prev_bssid_valid = true;
667503dd 947 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
b23aa676 948#endif
adbde344
VT
949
950 return;
951out:
29ce6ecb 952 cfg80211_put_bss(wdev->wiphy, info->bss);
adbde344 953}
adbde344 954
29ce6ecb
AS
955/* Consumes info->bss object one way or another */
956void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
957 gfp_t gfp)
667503dd
JB
958{
959 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 960 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
961 struct cfg80211_event *ev;
962 unsigned long flags;
e841b7b1 963 u8 *next;
667503dd 964
29ce6ecb
AS
965 if (!info->bss) {
966 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
967 info->bssid, wdev->ssid,
968 wdev->ssid_len,
969 wdev->conn_bss_type,
970 IEEE80211_PRIVACY_ANY);
971 }
972
973 if (WARN_ON(!info->bss))
adbde344
VT
974 return;
975
e841b7b1
AVS
976 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
977 info->fils.kek_len + info->fils.pmk_len +
978 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
adbde344 979 if (!ev) {
29ce6ecb 980 cfg80211_put_bss(wdev->wiphy, info->bss);
667503dd 981 return;
adbde344 982 }
667503dd
JB
983
984 ev->type = EVENT_ROAMED;
e841b7b1
AVS
985 next = ((u8 *)ev) + sizeof(*ev);
986 if (info->req_ie_len) {
987 ev->rm.req_ie = next;
988 ev->rm.req_ie_len = info->req_ie_len;
989 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
990 next += info->req_ie_len;
991 }
992 if (info->resp_ie_len) {
993 ev->rm.resp_ie = next;
994 ev->rm.resp_ie_len = info->resp_ie_len;
995 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
996 info->resp_ie_len);
997 next += info->resp_ie_len;
998 }
999 if (info->fils.kek_len) {
1000 ev->rm.fils.kek = next;
1001 ev->rm.fils.kek_len = info->fils.kek_len;
1002 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1003 info->fils.kek_len);
1004 next += info->fils.kek_len;
1005 }
1006 if (info->fils.pmk_len) {
1007 ev->rm.fils.pmk = next;
1008 ev->rm.fils.pmk_len = info->fils.pmk_len;
1009 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1010 info->fils.pmk_len);
1011 next += info->fils.pmk_len;
1012 }
1013 if (info->fils.pmkid) {
1014 ev->rm.fils.pmkid = next;
1015 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1016 WLAN_PMKID_LEN);
1017 next += WLAN_PMKID_LEN;
1018 }
1019 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1020 if (info->fils.update_erp_next_seq_num)
1021 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
29ce6ecb 1022 ev->rm.bss = info->bss;
667503dd
JB
1023
1024 spin_lock_irqsave(&wdev->event_lock, flags);
1025 list_add_tail(&ev->list, &wdev->event_list);
1026 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1027 queue_work(cfg80211_wq, &rdev->event_work);
667503dd 1028}
29ce6ecb 1029EXPORT_SYMBOL(cfg80211_roamed);
b23aa676 1030
503c1fb9
AS
1031void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1032{
1033 ASSERT_WDEV_LOCK(wdev);
1034
1035 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1036 return;
1037
1038 if (WARN_ON(!wdev->current_bss) ||
1039 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1040 return;
1041
1042 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1043 bssid);
1044}
1045
1046void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1047 gfp_t gfp)
1048{
1049 struct wireless_dev *wdev = dev->ieee80211_ptr;
1050 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1051 struct cfg80211_event *ev;
1052 unsigned long flags;
1053
1054 if (WARN_ON(!bssid))
1055 return;
1056
1057 ev = kzalloc(sizeof(*ev), gfp);
1058 if (!ev)
1059 return;
1060
1061 ev->type = EVENT_PORT_AUTHORIZED;
1062 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1063
1064 /*
1065 * Use the wdev event list so that if there are pending
1066 * connected/roamed events, they will be reported first.
1067 */
1068 spin_lock_irqsave(&wdev->event_lock, flags);
1069 list_add_tail(&ev->list, &wdev->event_list);
1070 spin_unlock_irqrestore(&wdev->event_lock, flags);
1071 queue_work(cfg80211_wq, &rdev->event_work);
1072}
1073EXPORT_SYMBOL(cfg80211_port_authorized);
1074
667503dd 1075void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
6829c878 1076 size_t ie_len, u16 reason, bool from_ap)
b23aa676
SO
1077{
1078 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1079 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
fffd0934 1080 int i;
3d23e349 1081#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1082 union iwreq_data wrqu;
1083#endif
1084
667503dd
JB
1085 ASSERT_WDEV_LOCK(wdev);
1086
074ac8df
JB
1087 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1088 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
b23aa676
SO
1089 return;
1090
b23aa676
SO
1091 if (wdev->current_bss) {
1092 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 1093 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
b23aa676
SO
1094 }
1095
1096 wdev->current_bss = NULL;
8dadadb7 1097 wdev->ssid_len = 0;
bd2522b1 1098 wdev->conn_owner_nlportid = 0;
3027a8e7
AS
1099 kzfree(wdev->connect_keys);
1100 wdev->connect_keys = NULL;
b23aa676 1101
fffd0934
JB
1102 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1103
b8607152
AS
1104 /* stop critical protocol if supported */
1105 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1106 rdev->crit_proto_nlportid = 0;
1107 rdev_crit_proto_stop(rdev, wdev);
1108 }
1109
fffd0934
JB
1110 /*
1111 * Delete all the keys ... pairwise keys can't really
1112 * exist any more anyway, but default keys might.
1113 */
1114 if (rdev->ops->del_key)
1115 for (i = 0; i < 6; i++)
e35e4d28 1116 rdev_del_key(rdev, dev, i, false, NULL);
b23aa676 1117
fa9ffc74
KP
1118 rdev_set_qos_map(rdev, dev, NULL);
1119
3d23e349 1120#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
1121 memset(&wrqu, 0, sizeof(wrqu));
1122 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1123 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
5f612033 1124 wdev->wext.connect.ssid_len = 0;
b23aa676 1125#endif
09d989d1
LR
1126
1127 schedule_work(&cfg80211_disconnect_work);
b23aa676
SO
1128}
1129
1130void cfg80211_disconnected(struct net_device *dev, u16 reason,
80279fb7
JB
1131 const u8 *ie, size_t ie_len,
1132 bool locally_generated, gfp_t gfp)
b23aa676 1133{
667503dd 1134 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 1135 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
1136 struct cfg80211_event *ev;
1137 unsigned long flags;
1138
1139 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1140 if (!ev)
1141 return;
1142
1143 ev->type = EVENT_DISCONNECTED;
1144 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1145 ev->dc.ie_len = ie_len;
1146 memcpy((void *)ev->dc.ie, ie, ie_len);
1147 ev->dc.reason = reason;
80279fb7 1148 ev->dc.locally_generated = locally_generated;
667503dd
JB
1149
1150 spin_lock_irqsave(&wdev->event_lock, flags);
1151 list_add_tail(&ev->list, &wdev->event_list);
1152 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 1153 queue_work(cfg80211_wq, &rdev->event_work);
b23aa676
SO
1154}
1155EXPORT_SYMBOL(cfg80211_disconnected);
1156
ceca7b71
JB
1157/*
1158 * API calls for nl80211/wext compatibility code
1159 */
83739b03
JB
1160int cfg80211_connect(struct cfg80211_registered_device *rdev,
1161 struct net_device *dev,
1162 struct cfg80211_connect_params *connect,
1163 struct cfg80211_cached_keys *connkeys,
1164 const u8 *prev_bssid)
b23aa676 1165{
b23aa676 1166 struct wireless_dev *wdev = dev->ieee80211_ptr;
667503dd
JB
1167 int err;
1168
1169 ASSERT_WDEV_LOCK(wdev);
b23aa676 1170
51e13359
JB
1171 /*
1172 * If we have an ssid_len, we're trying to connect or are
1173 * already connected, so reject a new SSID unless it's the
1174 * same (which is the case for re-association.)
1175 */
1176 if (wdev->ssid_len &&
1177 (wdev->ssid_len != connect->ssid_len ||
1178 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1179 return -EALREADY;
1180
1181 /*
1182 * If connected, reject (re-)association unless prev_bssid
1183 * matches the current BSSID.
1184 */
1185 if (wdev->current_bss) {
1186 if (!prev_bssid)
1187 return -EALREADY;
1188 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1189 return -ENOTCONN;
fffd0934
JB
1190 }
1191
51e13359
JB
1192 /*
1193 * Reject if we're in the process of connecting with WEP,
1194 * this case isn't very interesting and trying to handle
1195 * it would make the code much more complex.
1196 */
1197 if (wdev->connect_keys)
1198 return -EINPROGRESS;
1199
7e7c8926
BG
1200 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1201 rdev->wiphy.ht_capa_mod_mask);
81c5dce2
SM
1202 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1203 rdev->wiphy.vht_capa_mod_mask);
7e7c8926 1204
fffd0934
JB
1205 if (connkeys && connkeys->def >= 0) {
1206 int idx;
bcba8eae 1207 u32 cipher;
fffd0934
JB
1208
1209 idx = connkeys->def;
bcba8eae 1210 cipher = connkeys->params[idx].cipher;
fffd0934 1211 /* If given a WEP key we may need it for shared key auth */
bcba8eae
SO
1212 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1213 cipher == WLAN_CIPHER_SUITE_WEP104) {
fffd0934
JB
1214 connect->key_idx = idx;
1215 connect->key = connkeys->params[idx].key;
1216 connect->key_len = connkeys->params[idx].key_len;
bcba8eae
SO
1217
1218 /*
1219 * If ciphers are not set (e.g. when going through
1220 * iwconfig), we have to set them appropriately here.
1221 */
1222 if (connect->crypto.cipher_group == 0)
1223 connect->crypto.cipher_group = cipher;
1224
1225 if (connect->crypto.n_ciphers_pairwise == 0) {
1226 connect->crypto.n_ciphers_pairwise = 1;
1227 connect->crypto.ciphers_pairwise[0] = cipher;
1228 }
fffd0934 1229 }
b8676221
DS
1230
1231 connect->crypto.wep_keys = connkeys->params;
1232 connect->crypto.wep_tx_key = connkeys->def;
f1c1f17a
JB
1233 } else {
1234 if (WARN_ON(connkeys))
1235 return -EINVAL;
fffd0934
JB
1236 }
1237
ceca7b71
JB
1238 wdev->connect_keys = connkeys;
1239 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1240 wdev->ssid_len = connect->ssid_len;
6829c878 1241
34d50519
LD
1242 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1243 IEEE80211_BSS_TYPE_ESS;
1244
ceca7b71
JB
1245 if (!rdev->ops->connect)
1246 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1247 else
e35e4d28 1248 err = rdev_connect(rdev, dev, connect);
b23aa676 1249
ceca7b71
JB
1250 if (err) {
1251 wdev->connect_keys = NULL;
51e13359
JB
1252 /*
1253 * This could be reassoc getting refused, don't clear
1254 * ssid_len in that case.
1255 */
1256 if (!wdev->current_bss)
1257 wdev->ssid_len = 0;
ceca7b71 1258 return err;
6829c878 1259 }
ceca7b71
JB
1260
1261 return 0;
b23aa676
SO
1262}
1263
83739b03
JB
1264int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1265 struct net_device *dev, u16 reason, bool wextev)
b23aa676 1266{
6829c878 1267 struct wireless_dev *wdev = dev->ieee80211_ptr;
dee8a973 1268 int err = 0;
b23aa676 1269
667503dd
JB
1270 ASSERT_WDEV_LOCK(wdev);
1271
b47f610b 1272 kzfree(wdev->connect_keys);
fffd0934
JB
1273 wdev->connect_keys = NULL;
1274
bd2522b1
AZ
1275 wdev->conn_owner_nlportid = 0;
1276
dee8a973 1277 if (wdev->conn)
ceca7b71 1278 err = cfg80211_sme_disconnect(wdev, reason);
dee8a973 1279 else if (!rdev->ops->disconnect)
ceca7b71 1280 cfg80211_mlme_down(rdev, dev);
0711d638 1281 else if (wdev->ssid_len)
e35e4d28 1282 err = rdev_disconnect(rdev, dev, reason);
b23aa676 1283
51e13359
JB
1284 /*
1285 * Clear ssid_len unless we actually were fully connected,
1286 * in which case cfg80211_disconnected() will take care of
1287 * this later.
1288 */
1289 if (!wdev->current_bss)
1290 wdev->ssid_len = 0;
1291
ceca7b71 1292 return err;
19957bb3 1293}
bd2522b1
AZ
1294
1295/*
1296 * Used to clean up after the connection / connection attempt owner socket
1297 * disconnects
1298 */
1299void cfg80211_autodisconnect_wk(struct work_struct *work)
1300{
1301 struct wireless_dev *wdev =
1302 container_of(work, struct wireless_dev, disconnect_wk);
1303 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1304
1305 wdev_lock(wdev);
1306
1307 if (wdev->conn_owner_nlportid) {
37b1c004
DK
1308 switch (wdev->iftype) {
1309 case NL80211_IFTYPE_ADHOC:
1310 cfg80211_leave_ibss(rdev, wdev->netdev, false);
1311 break;
1312 case NL80211_IFTYPE_AP:
1313 case NL80211_IFTYPE_P2P_GO:
1314 cfg80211_stop_ap(rdev, wdev->netdev, false);
1315 break;
1316 case NL80211_IFTYPE_MESH_POINT:
1317 cfg80211_leave_mesh(rdev, wdev->netdev);
1318 break;
1319 case NL80211_IFTYPE_STATION:
1320 case NL80211_IFTYPE_P2P_CLIENT:
1321 /*
1322 * Use disconnect_bssid if still connecting and
1323 * ops->disconnect not implemented. Otherwise we can
1324 * use cfg80211_disconnect.
1325 */
1326 if (rdev->ops->disconnect || wdev->current_bss)
1327 cfg80211_disconnect(rdev, wdev->netdev,
1328 WLAN_REASON_DEAUTH_LEAVING,
1329 true);
1330 else
1331 cfg80211_mlme_deauth(rdev, wdev->netdev,
1332 wdev->disconnect_bssid,
1333 NULL, 0,
1334 WLAN_REASON_DEAUTH_LEAVING,
1335 false);
1336 break;
1337 default:
1338 break;
1339 }
bd2522b1
AZ
1340 }
1341
1342 wdev_unlock(wdev);
1343}