]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/wireless/sme.c
cfg80211: remove macro ASSERT_RDEV_LOCK(rdev)
[mirror_ubuntu-artful-kernel.git] / net / wireless / sme.c
CommitLineData
b23aa676 1/*
ceca7b71
JB
2 * SME code for cfg80211
3 * both driver SME event handling and the SME implementation
4 * (for nl80211's connect() and wext)
b23aa676
SO
5 *
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 */
9
10#include <linux/etherdevice.h>
11#include <linux/if_arp.h>
5a0e3ad6 12#include <linux/slab.h>
b23aa676 13#include <linux/workqueue.h>
a9a11622 14#include <linux/wireless.h>
bc3b2d7f 15#include <linux/export.h>
a9a11622 16#include <net/iw_handler.h>
b23aa676
SO
17#include <net/cfg80211.h>
18#include <net/rtnetlink.h>
19#include "nl80211.h"
8b19e6ca 20#include "reg.h"
e35e4d28 21#include "rdev-ops.h"
b23aa676 22
ceca7b71
JB
23/*
24 * Software SME in cfg80211, using auth/assoc/deauth calls to the
25 * driver. This is is for implementing nl80211's connect/disconnect
26 * and wireless extensions (if configured.)
27 */
28
6829c878
JB
29struct cfg80211_conn {
30 struct cfg80211_connect_params params;
31 /* these are sub-states of the _CONNECTING sme_state */
32 enum {
6829c878
JB
33 CFG80211_CONN_SCANNING,
34 CFG80211_CONN_SCAN_AGAIN,
35 CFG80211_CONN_AUTHENTICATE_NEXT,
36 CFG80211_CONN_AUTHENTICATING,
923a0e7d 37 CFG80211_CONN_AUTH_FAILED,
6829c878
JB
38 CFG80211_CONN_ASSOCIATE_NEXT,
39 CFG80211_CONN_ASSOCIATING,
923a0e7d 40 CFG80211_CONN_ASSOC_FAILED,
ceca7b71
JB
41 CFG80211_CONN_DEAUTH,
42 CFG80211_CONN_CONNECTED,
6829c878 43 } state;
f401a6f7 44 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
6829c878
JB
45 u8 *ie;
46 size_t ie_len;
f401a6f7 47 bool auto_auth, prev_bssid_valid;
6829c878
JB
48};
49
ceca7b71 50static void cfg80211_sme_free(struct wireless_dev *wdev)
09d989d1 51{
ceca7b71
JB
52 if (!wdev->conn)
53 return;
09d989d1 54
ceca7b71
JB
55 kfree(wdev->conn->ie);
56 kfree(wdev->conn);
57 wdev->conn = NULL;
09d989d1
LR
58}
59
6829c878
JB
60static int cfg80211_conn_scan(struct wireless_dev *wdev)
61{
79c97e97 62 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
6829c878
JB
63 struct cfg80211_scan_request *request;
64 int n_channels, err;
65
66 ASSERT_RTNL();
667503dd 67 ASSERT_WDEV_LOCK(wdev);
6829c878 68
f9d15d16 69 if (rdev->scan_req || rdev->scan_msg)
6829c878
JB
70 return -EBUSY;
71
bdfbec2d 72 if (wdev->conn->params.channel)
6829c878 73 n_channels = 1;
bdfbec2d
IP
74 else
75 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
6829c878 76
6829c878
JB
77 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 sizeof(request->channels[0]) * n_channels,
79 GFP_KERNEL);
80 if (!request)
81 return -ENOMEM;
82
6829c878
JB
83 if (wdev->conn->params.channel)
84 request->channels[0] = wdev->conn->params.channel;
85 else {
86 int i = 0, j;
87 enum ieee80211_band band;
e3081501
RM
88 struct ieee80211_supported_band *bands;
89 struct ieee80211_channel *channel;
6829c878
JB
90
91 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
e3081501
RM
92 bands = wdev->wiphy->bands[band];
93 if (!bands)
6829c878 94 continue;
e3081501
RM
95 for (j = 0; j < bands->n_channels; j++) {
96 channel = &bands->channels[j];
97 if (channel->flags & IEEE80211_CHAN_DISABLED)
98 continue;
99 request->channels[i++] = channel;
100 }
101 request->rates[band] = (1 << bands->n_bitrates) - 1;
6829c878 102 }
e3081501 103 n_channels = i;
6829c878
JB
104 }
105 request->n_channels = n_channels;
5ba63533 106 request->ssids = (void *)&request->channels[n_channels];
6829c878
JB
107 request->n_ssids = 1;
108
109 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
110 wdev->conn->params.ssid_len);
111 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
112
fd014284 113 request->wdev = wdev;
79c97e97 114 request->wiphy = &rdev->wiphy;
15d6030b 115 request->scan_start = jiffies;
6829c878 116
79c97e97 117 rdev->scan_req = request;
6829c878 118
e35e4d28 119 err = rdev_scan(rdev, request);
6829c878
JB
120 if (!err) {
121 wdev->conn->state = CFG80211_CONN_SCANNING;
fd014284 122 nl80211_send_scan_start(rdev, wdev);
463d0183 123 dev_hold(wdev->netdev);
6829c878 124 } else {
79c97e97 125 rdev->scan_req = NULL;
6829c878
JB
126 kfree(request);
127 }
128 return err;
129}
130
131static int cfg80211_conn_do_work(struct wireless_dev *wdev)
132{
79c97e97 133 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
19957bb3 134 struct cfg80211_connect_params *params;
f62fab73 135 struct cfg80211_assoc_request req = {};
19957bb3 136 int err;
6829c878 137
667503dd
JB
138 ASSERT_WDEV_LOCK(wdev);
139
6829c878
JB
140 if (!wdev->conn)
141 return 0;
142
19957bb3
JB
143 params = &wdev->conn->params;
144
6829c878 145 switch (wdev->conn->state) {
ceca7b71
JB
146 case CFG80211_CONN_SCANNING:
147 /* didn't find it during scan ... */
148 return -ENOENT;
6829c878
JB
149 case CFG80211_CONN_SCAN_AGAIN:
150 return cfg80211_conn_scan(wdev);
151 case CFG80211_CONN_AUTHENTICATE_NEXT:
79c97e97 152 BUG_ON(!rdev->ops->auth);
19957bb3 153 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
91bf9b26
JB
154 return cfg80211_mlme_auth(rdev, wdev->netdev,
155 params->channel, params->auth_type,
156 params->bssid,
157 params->ssid, params->ssid_len,
158 NULL, 0,
159 params->key, params->key_len,
160 params->key_idx, NULL, 0);
923a0e7d
JB
161 case CFG80211_CONN_AUTH_FAILED:
162 return -ENOTCONN;
6829c878 163 case CFG80211_CONN_ASSOCIATE_NEXT:
79c97e97 164 BUG_ON(!rdev->ops->assoc);
19957bb3 165 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
f401a6f7 166 if (wdev->conn->prev_bssid_valid)
f62fab73
JB
167 req.prev_bssid = wdev->conn->prev_bssid;
168 req.ie = params->ie;
169 req.ie_len = params->ie_len;
170 req.use_mfp = params->mfp != NL80211_MFP_NO;
171 req.crypto = params->crypto;
172 req.flags = params->flags;
173 req.ht_capa = params->ht_capa;
174 req.ht_capa_mask = params->ht_capa_mask;
175 req.vht_capa = params->vht_capa;
176 req.vht_capa_mask = params->vht_capa_mask;
177
91bf9b26
JB
178 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
179 params->bssid, params->ssid,
180 params->ssid_len, &req);
19957bb3 181 if (err)
91bf9b26
JB
182 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
183 NULL, 0,
184 WLAN_REASON_DEAUTH_LEAVING,
185 false);
19957bb3 186 return err;
923a0e7d
JB
187 case CFG80211_CONN_ASSOC_FAILED:
188 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
189 NULL, 0,
190 WLAN_REASON_DEAUTH_LEAVING, false);
191 return -ENOTCONN;
ceca7b71 192 case CFG80211_CONN_DEAUTH:
91bf9b26
JB
193 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194 NULL, 0,
195 WLAN_REASON_DEAUTH_LEAVING, false);
923a0e7d
JB
196 /* free directly, disconnected event already sent */
197 cfg80211_sme_free(wdev);
ceca7b71 198 return 0;
6829c878
JB
199 default:
200 return 0;
201 }
202}
203
204void cfg80211_conn_work(struct work_struct *work)
205{
79c97e97 206 struct cfg80211_registered_device *rdev =
6829c878
JB
207 container_of(work, struct cfg80211_registered_device, conn_work);
208 struct wireless_dev *wdev;
7400f42e 209 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
6829c878
JB
210
211 rtnl_lock();
6829c878 212
89a54e48 213 list_for_each_entry(wdev, &rdev->wdev_list, list) {
c8157976
JB
214 if (!wdev->netdev)
215 continue;
216
667503dd
JB
217 wdev_lock(wdev);
218 if (!netif_running(wdev->netdev)) {
219 wdev_unlock(wdev);
6829c878 220 continue;
667503dd 221 }
ceca7b71
JB
222 if (!wdev->conn ||
223 wdev->conn->state == CFG80211_CONN_CONNECTED) {
667503dd 224 wdev_unlock(wdev);
6829c878 225 continue;
667503dd 226 }
7400f42e
JB
227 if (wdev->conn->params.bssid) {
228 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
229 bssid = bssid_buf;
230 }
ceca7b71 231 if (cfg80211_conn_do_work(wdev)) {
667503dd 232 __cfg80211_connect_result(
7d930bc3 233 wdev->netdev, bssid,
667503dd
JB
234 NULL, 0, NULL, 0,
235 WLAN_STATUS_UNSPECIFIED_FAILURE,
df7fc0f9 236 false, NULL);
ceca7b71
JB
237 cfg80211_sme_free(wdev);
238 }
667503dd 239 wdev_unlock(wdev);
6829c878
JB
240 }
241
6829c878
JB
242 rtnl_unlock();
243}
244
0e3a39b5 245/* Returned bss is reference counted and must be cleaned up appropriately. */
bbac31f4 246static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
6829c878 247{
79c97e97 248 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
6829c878
JB
249 struct cfg80211_bss *bss;
250 u16 capa = WLAN_CAPABILITY_ESS;
251
667503dd
JB
252 ASSERT_WDEV_LOCK(wdev);
253
6829c878
JB
254 if (wdev->conn->params.privacy)
255 capa |= WLAN_CAPABILITY_PRIVACY;
256
ed9d0102
JM
257 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
258 wdev->conn->params.bssid,
6829c878
JB
259 wdev->conn->params.ssid,
260 wdev->conn->params.ssid_len,
261 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
262 capa);
6829c878 263 if (!bss)
bbac31f4 264 return NULL;
6829c878
JB
265
266 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
267 wdev->conn->params.bssid = wdev->conn->bssid;
268 wdev->conn->params.channel = bss->channel;
269 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
79c97e97 270 schedule_work(&rdev->conn_work);
6829c878 271
bbac31f4 272 return bss;
6829c878
JB
273}
274
667503dd 275static void __cfg80211_sme_scan_done(struct net_device *dev)
6829c878
JB
276{
277 struct wireless_dev *wdev = dev->ieee80211_ptr;
79c97e97 278 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
bbac31f4 279 struct cfg80211_bss *bss;
6829c878 280
667503dd
JB
281 ASSERT_WDEV_LOCK(wdev);
282
d4b1a687 283 if (!wdev->conn)
6829c878
JB
284 return;
285
286 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
287 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
288 return;
289
bbac31f4 290 bss = cfg80211_get_conn_bss(wdev);
ceca7b71 291 if (bss)
5b112d3d 292 cfg80211_put_bss(&rdev->wiphy, bss);
ceca7b71
JB
293 else
294 schedule_work(&rdev->conn_work);
6829c878
JB
295}
296
667503dd
JB
297void cfg80211_sme_scan_done(struct net_device *dev)
298{
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
300
301 wdev_lock(wdev);
302 __cfg80211_sme_scan_done(dev);
303 wdev_unlock(wdev);
304}
305
ceca7b71 306void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
6829c878 307{
6829c878
JB
308 struct wiphy *wiphy = wdev->wiphy;
309 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
310 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
311 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
312
667503dd
JB
313 ASSERT_WDEV_LOCK(wdev);
314
ceca7b71 315 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
6829c878
JB
316 return;
317
318 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
319 wdev->conn->auto_auth &&
320 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
321 /* select automatically between only open, shared, leap */
322 switch (wdev->conn->params.auth_type) {
323 case NL80211_AUTHTYPE_OPEN_SYSTEM:
fffd0934
JB
324 if (wdev->connect_keys)
325 wdev->conn->params.auth_type =
326 NL80211_AUTHTYPE_SHARED_KEY;
327 else
328 wdev->conn->params.auth_type =
329 NL80211_AUTHTYPE_NETWORK_EAP;
6829c878
JB
330 break;
331 case NL80211_AUTHTYPE_SHARED_KEY:
332 wdev->conn->params.auth_type =
333 NL80211_AUTHTYPE_NETWORK_EAP;
334 break;
335 default:
336 /* huh? */
337 wdev->conn->params.auth_type =
338 NL80211_AUTHTYPE_OPEN_SYSTEM;
339 break;
340 }
341 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
342 schedule_work(&rdev->conn_work);
19957bb3 343 } else if (status_code != WLAN_STATUS_SUCCESS) {
ceca7b71
JB
344 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
345 NULL, 0, NULL, 0,
df7fc0f9 346 status_code, false, NULL);
ceca7b71 347 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
6829c878
JB
348 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
349 schedule_work(&rdev->conn_work);
350 }
351}
b23aa676 352
ceca7b71 353bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
f401a6f7 354{
ceca7b71 355 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
f401a6f7 356
ceca7b71 357 if (!wdev->conn)
f401a6f7
JB
358 return false;
359
ceca7b71
JB
360 if (status == WLAN_STATUS_SUCCESS) {
361 wdev->conn->state = CFG80211_CONN_CONNECTED;
f401a6f7 362 return false;
ceca7b71 363 }
f401a6f7 364
ceca7b71
JB
365 if (wdev->conn->prev_bssid_valid) {
366 /*
367 * Some stupid APs don't accept reassoc, so we
368 * need to fall back to trying regular assoc;
369 * return true so no event is sent to userspace.
370 */
371 wdev->conn->prev_bssid_valid = false;
372 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
373 schedule_work(&rdev->conn_work);
374 return true;
375 }
376
923a0e7d 377 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
f401a6f7 378 schedule_work(&rdev->conn_work);
ceca7b71
JB
379 return false;
380}
f401a6f7 381
ceca7b71
JB
382void cfg80211_sme_deauth(struct wireless_dev *wdev)
383{
384 cfg80211_sme_free(wdev);
f401a6f7
JB
385}
386
ceca7b71 387void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
7d930bc3 388{
923a0e7d
JB
389 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
390
391 if (!wdev->conn)
392 return;
393
394 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
395 schedule_work(&rdev->conn_work);
ceca7b71 396}
7d930bc3 397
ceca7b71
JB
398void cfg80211_sme_disassoc(struct wireless_dev *wdev)
399{
400 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
401
402 if (!wdev->conn)
403 return;
404
405 wdev->conn->state = CFG80211_CONN_DEAUTH;
7d930bc3
JB
406 schedule_work(&rdev->conn_work);
407}
408
ceca7b71
JB
409void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
410{
923a0e7d
JB
411 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
412
413 if (!wdev->conn)
414 return;
415
416 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
417 schedule_work(&rdev->conn_work);
ceca7b71
JB
418}
419
420static int cfg80211_sme_connect(struct wireless_dev *wdev,
421 struct cfg80211_connect_params *connect,
422 const u8 *prev_bssid)
423{
424 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
425 struct cfg80211_bss *bss;
426 int err;
427
428 if (!rdev->ops->auth || !rdev->ops->assoc)
429 return -EOPNOTSUPP;
430
431 if (wdev->current_bss)
432 return -EALREADY;
433
434 if (WARN_ON(wdev->conn))
435 return -EINPROGRESS;
436
437 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
438 if (!wdev->conn)
439 return -ENOMEM;
440
441 /*
442 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
443 */
444 memcpy(&wdev->conn->params, connect, sizeof(*connect));
445 if (connect->bssid) {
446 wdev->conn->params.bssid = wdev->conn->bssid;
447 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
448 }
449
450 if (connect->ie) {
451 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
452 GFP_KERNEL);
453 wdev->conn->params.ie = wdev->conn->ie;
454 if (!wdev->conn->ie) {
455 kfree(wdev->conn);
456 wdev->conn = NULL;
457 return -ENOMEM;
458 }
459 }
460
461 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
462 wdev->conn->auto_auth = true;
463 /* start with open system ... should mostly work */
464 wdev->conn->params.auth_type =
465 NL80211_AUTHTYPE_OPEN_SYSTEM;
466 } else {
467 wdev->conn->auto_auth = false;
468 }
469
470 wdev->conn->params.ssid = wdev->ssid;
471 wdev->conn->params.ssid_len = connect->ssid_len;
472
473 /* see if we have the bss already */
474 bss = cfg80211_get_conn_bss(wdev);
475
476 if (prev_bssid) {
477 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
478 wdev->conn->prev_bssid_valid = true;
479 }
480
481 /* we're good if we have a matching bss struct */
482 if (bss) {
483 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
484 err = cfg80211_conn_do_work(wdev);
485 cfg80211_put_bss(wdev->wiphy, bss);
486 } else {
487 /* otherwise we'll need to scan for the AP first */
488 err = cfg80211_conn_scan(wdev);
489
490 /*
491 * If we can't scan right now, then we need to scan again
492 * after the current scan finished, since the parameters
493 * changed (unless we find a good AP anyway).
494 */
495 if (err == -EBUSY) {
496 err = 0;
497 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
498 }
499 }
500
501 if (err)
502 cfg80211_sme_free(wdev);
503
504 return err;
505}
506
507static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
508{
509 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
510 int err;
511
512 if (!wdev->conn)
513 return 0;
514
515 if (!rdev->ops->deauth)
516 return -EOPNOTSUPP;
517
518 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
519 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
520 err = 0;
521 goto out;
522 }
523
524 /* wdev->conn->params.bssid must be set if > SCANNING */
525 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
526 wdev->conn->params.bssid,
527 NULL, 0, reason, false);
528 out:
529 cfg80211_sme_free(wdev);
530 return err;
531}
532
533/*
534 * code shared for in-device and software SME
535 */
536
537static bool cfg80211_is_all_idle(void)
538{
539 struct cfg80211_registered_device *rdev;
540 struct wireless_dev *wdev;
541 bool is_all_idle = true;
542
543 /*
544 * All devices must be idle as otherwise if you are actively
545 * scanning some new beacon hints could be learned and would
546 * count as new regulatory hints.
547 */
548 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
549 list_for_each_entry(wdev, &rdev->wdev_list, list) {
550 wdev_lock(wdev);
551 if (wdev->conn || wdev->current_bss)
552 is_all_idle = false;
553 wdev_unlock(wdev);
554 }
555 }
556
557 return is_all_idle;
558}
559
560static void disconnect_work(struct work_struct *work)
561{
562 rtnl_lock();
563 if (cfg80211_is_all_idle())
564 regulatory_hint_disconnect();
565 rtnl_unlock();
566}
567
568static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
569
570
571/*
572 * API calls for drivers implementing connect/disconnect and
573 * SME event handling
574 */
575
6f390908 576/* This method must consume bss one way or another */
667503dd
JB
577void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
578 const u8 *req_ie, size_t req_ie_len,
579 const u8 *resp_ie, size_t resp_ie_len,
df7fc0f9
JB
580 u16 status, bool wextev,
581 struct cfg80211_bss *bss)
b23aa676
SO
582{
583 struct wireless_dev *wdev = dev->ieee80211_ptr;
9caf0364 584 const u8 *country_ie;
3d23e349 585#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
586 union iwreq_data wrqu;
587#endif
588
667503dd
JB
589 ASSERT_WDEV_LOCK(wdev);
590
074ac8df 591 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6f390908
BG
592 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
593 cfg80211_put_bss(wdev->wiphy, bss);
b23aa676 594 return;
6f390908 595 }
b23aa676 596
ea416a79 597 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
e45cd82a 598 bssid, req_ie, req_ie_len,
ea416a79
JB
599 resp_ie, resp_ie_len,
600 status, GFP_KERNEL);
e45cd82a 601
3d23e349 602#ifdef CONFIG_CFG80211_WEXT
e45cd82a
JB
603 if (wextev) {
604 if (req_ie && status == WLAN_STATUS_SUCCESS) {
605 memset(&wrqu, 0, sizeof(wrqu));
606 wrqu.data.length = req_ie_len;
3409ff77 607 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
e45cd82a
JB
608 }
609
610 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
611 memset(&wrqu, 0, sizeof(wrqu));
612 wrqu.data.length = resp_ie_len;
613 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
614 }
615
616 memset(&wrqu, 0, sizeof(wrqu));
617 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
f401a6f7 618 if (bssid && status == WLAN_STATUS_SUCCESS) {
e45cd82a 619 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
f401a6f7
JB
620 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
621 wdev->wext.prev_bssid_valid = true;
622 }
e45cd82a
JB
623 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
624 }
625#endif
626
4c4d684a
UR
627 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
628 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
629 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
630 wdev->ssid, wdev->ssid_len,
631 WLAN_CAPABILITY_ESS,
632 WLAN_CAPABILITY_ESS);
633 if (bss)
634 cfg80211_hold_bss(bss_from_pub(bss));
635 }
636
df7fc0f9
JB
637 if (wdev->current_bss) {
638 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 639 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
df7fc0f9
JB
640 wdev->current_bss = NULL;
641 }
642
fffd0934 643 if (status != WLAN_STATUS_SUCCESS) {
fffd0934
JB
644 kfree(wdev->connect_keys);
645 wdev->connect_keys = NULL;
8dadadb7 646 wdev->ssid_len = 0;
f1940c57
JB
647 if (bss) {
648 cfg80211_unhold_bss(bss_from_pub(bss));
649 cfg80211_put_bss(wdev->wiphy, bss);
650 }
fffd0934 651 return;
b23aa676 652 }
fffd0934 653
4c4d684a
UR
654 if (WARN_ON(!bss))
655 return;
fffd0934 656
fffd0934
JB
657 wdev->current_bss = bss_from_pub(bss);
658
fffd0934 659 cfg80211_upload_connect_keys(wdev);
8b19e6ca 660
9caf0364
JB
661 rcu_read_lock();
662 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
663 if (!country_ie) {
664 rcu_read_unlock();
665 return;
666 }
667
668 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
669 rcu_read_unlock();
8b19e6ca
LR
670
671 if (!country_ie)
672 return;
673
674 /*
675 * ieee80211_bss_get_ie() ensures we can access:
676 * - country_ie + 2, the start of the country ie data, and
677 * - and country_ie[1] which is the IE length
678 */
789fd033
LR
679 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
680 country_ie + 2, country_ie[1]);
9caf0364 681 kfree(country_ie);
b23aa676 682}
f2129354
JB
683
684void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
685 const u8 *req_ie, size_t req_ie_len,
686 const u8 *resp_ie, size_t resp_ie_len,
687 u16 status, gfp_t gfp)
688{
667503dd
JB
689 struct wireless_dev *wdev = dev->ieee80211_ptr;
690 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
691 struct cfg80211_event *ev;
692 unsigned long flags;
693
694 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
695 if (!ev)
696 return;
697
698 ev->type = EVENT_CONNECT_RESULT;
16a832e7
ZY
699 if (bssid)
700 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
7834704b
NS
701 if (req_ie_len) {
702 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
703 ev->cr.req_ie_len = req_ie_len;
704 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
705 }
706 if (resp_ie_len) {
707 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
708 ev->cr.resp_ie_len = resp_ie_len;
709 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
710 }
667503dd
JB
711 ev->cr.status = status;
712
713 spin_lock_irqsave(&wdev->event_lock, flags);
714 list_add_tail(&ev->list, &wdev->event_list);
715 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 716 queue_work(cfg80211_wq, &rdev->event_work);
f2129354 717}
b23aa676
SO
718EXPORT_SYMBOL(cfg80211_connect_result);
719
0e3a39b5 720/* Consumes bss object one way or another */
ed9d0102 721void __cfg80211_roamed(struct wireless_dev *wdev,
adbde344 722 struct cfg80211_bss *bss,
667503dd
JB
723 const u8 *req_ie, size_t req_ie_len,
724 const u8 *resp_ie, size_t resp_ie_len)
b23aa676 725{
3d23e349 726#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
727 union iwreq_data wrqu;
728#endif
667503dd
JB
729 ASSERT_WDEV_LOCK(wdev);
730
074ac8df
JB
731 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
732 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
adbde344 733 goto out;
b23aa676 734
ceca7b71 735 if (WARN_ON(!wdev->current_bss))
adbde344 736 goto out;
b23aa676
SO
737
738 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 739 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
b23aa676
SO
740 wdev->current_bss = NULL;
741
19957bb3
JB
742 cfg80211_hold_bss(bss_from_pub(bss));
743 wdev->current_bss = bss_from_pub(bss);
b23aa676 744
adbde344 745 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
667503dd
JB
746 req_ie, req_ie_len, resp_ie, resp_ie_len,
747 GFP_KERNEL);
b23aa676 748
3d23e349 749#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
750 if (req_ie) {
751 memset(&wrqu, 0, sizeof(wrqu));
752 wrqu.data.length = req_ie_len;
3409ff77 753 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
667503dd 754 &wrqu, req_ie);
b23aa676
SO
755 }
756
757 if (resp_ie) {
758 memset(&wrqu, 0, sizeof(wrqu));
759 wrqu.data.length = resp_ie_len;
667503dd
JB
760 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
761 &wrqu, resp_ie);
b23aa676
SO
762 }
763
764 memset(&wrqu, 0, sizeof(wrqu));
765 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
adbde344
VT
766 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
767 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
f401a6f7 768 wdev->wext.prev_bssid_valid = true;
667503dd 769 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
b23aa676 770#endif
adbde344
VT
771
772 return;
773out:
5b112d3d 774 cfg80211_put_bss(wdev->wiphy, bss);
b23aa676 775}
667503dd 776
ed9d0102
JM
777void cfg80211_roamed(struct net_device *dev,
778 struct ieee80211_channel *channel,
779 const u8 *bssid,
667503dd
JB
780 const u8 *req_ie, size_t req_ie_len,
781 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
adbde344
VT
782{
783 struct wireless_dev *wdev = dev->ieee80211_ptr;
784 struct cfg80211_bss *bss;
785
adbde344
VT
786 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
787 wdev->ssid_len, WLAN_CAPABILITY_ESS,
788 WLAN_CAPABILITY_ESS);
789 if (WARN_ON(!bss))
790 return;
791
792 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
793 resp_ie_len, gfp);
794}
795EXPORT_SYMBOL(cfg80211_roamed);
796
0e3a39b5 797/* Consumes bss object one way or another */
adbde344
VT
798void cfg80211_roamed_bss(struct net_device *dev,
799 struct cfg80211_bss *bss, const u8 *req_ie,
800 size_t req_ie_len, const u8 *resp_ie,
801 size_t resp_ie_len, gfp_t gfp)
667503dd
JB
802{
803 struct wireless_dev *wdev = dev->ieee80211_ptr;
804 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
805 struct cfg80211_event *ev;
806 unsigned long flags;
807
adbde344
VT
808 if (WARN_ON(!bss))
809 return;
810
667503dd 811 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
adbde344 812 if (!ev) {
5b112d3d 813 cfg80211_put_bss(wdev->wiphy, bss);
667503dd 814 return;
adbde344 815 }
667503dd
JB
816
817 ev->type = EVENT_ROAMED;
667503dd
JB
818 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
819 ev->rm.req_ie_len = req_ie_len;
820 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
821 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
822 ev->rm.resp_ie_len = resp_ie_len;
823 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
adbde344 824 ev->rm.bss = bss;
667503dd
JB
825
826 spin_lock_irqsave(&wdev->event_lock, flags);
827 list_add_tail(&ev->list, &wdev->event_list);
828 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 829 queue_work(cfg80211_wq, &rdev->event_work);
667503dd 830}
adbde344 831EXPORT_SYMBOL(cfg80211_roamed_bss);
b23aa676 832
667503dd 833void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
6829c878 834 size_t ie_len, u16 reason, bool from_ap)
b23aa676
SO
835{
836 struct wireless_dev *wdev = dev->ieee80211_ptr;
fffd0934
JB
837 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
838 int i;
3d23e349 839#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
840 union iwreq_data wrqu;
841#endif
842
667503dd
JB
843 ASSERT_WDEV_LOCK(wdev);
844
074ac8df
JB
845 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
846 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
b23aa676
SO
847 return;
848
b23aa676
SO
849 if (wdev->current_bss) {
850 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 851 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
b23aa676
SO
852 }
853
854 wdev->current_bss = NULL;
8dadadb7 855 wdev->ssid_len = 0;
b23aa676 856
fffd0934
JB
857 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
858
859 /*
860 * Delete all the keys ... pairwise keys can't really
861 * exist any more anyway, but default keys might.
862 */
863 if (rdev->ops->del_key)
864 for (i = 0; i < 6; i++)
e35e4d28 865 rdev_del_key(rdev, dev, i, false, NULL);
b23aa676 866
fa9ffc74
KP
867 rdev_set_qos_map(rdev, dev, NULL);
868
3d23e349 869#ifdef CONFIG_CFG80211_WEXT
b23aa676
SO
870 memset(&wrqu, 0, sizeof(wrqu));
871 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
872 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
5f612033 873 wdev->wext.connect.ssid_len = 0;
b23aa676 874#endif
09d989d1
LR
875
876 schedule_work(&cfg80211_disconnect_work);
b23aa676
SO
877}
878
879void cfg80211_disconnected(struct net_device *dev, u16 reason,
880 u8 *ie, size_t ie_len, gfp_t gfp)
881{
667503dd
JB
882 struct wireless_dev *wdev = dev->ieee80211_ptr;
883 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
884 struct cfg80211_event *ev;
885 unsigned long flags;
886
887 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
888 if (!ev)
889 return;
890
891 ev->type = EVENT_DISCONNECTED;
892 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
893 ev->dc.ie_len = ie_len;
894 memcpy((void *)ev->dc.ie, ie, ie_len);
895 ev->dc.reason = reason;
896
897 spin_lock_irqsave(&wdev->event_lock, flags);
898 list_add_tail(&ev->list, &wdev->event_list);
899 spin_unlock_irqrestore(&wdev->event_lock, flags);
e60d7443 900 queue_work(cfg80211_wq, &rdev->event_work);
b23aa676
SO
901}
902EXPORT_SYMBOL(cfg80211_disconnected);
903
ceca7b71
JB
904/*
905 * API calls for nl80211/wext compatibility code
906 */
83739b03
JB
907int cfg80211_connect(struct cfg80211_registered_device *rdev,
908 struct net_device *dev,
909 struct cfg80211_connect_params *connect,
910 struct cfg80211_cached_keys *connkeys,
911 const u8 *prev_bssid)
b23aa676 912{
b23aa676 913 struct wireless_dev *wdev = dev->ieee80211_ptr;
667503dd
JB
914 int err;
915
916 ASSERT_WDEV_LOCK(wdev);
b23aa676 917
fffd0934
JB
918 if (WARN_ON(wdev->connect_keys)) {
919 kfree(wdev->connect_keys);
920 wdev->connect_keys = NULL;
921 }
922
7e7c8926
BG
923 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
924 rdev->wiphy.ht_capa_mod_mask);
925
fffd0934
JB
926 if (connkeys && connkeys->def >= 0) {
927 int idx;
bcba8eae 928 u32 cipher;
fffd0934
JB
929
930 idx = connkeys->def;
bcba8eae 931 cipher = connkeys->params[idx].cipher;
fffd0934 932 /* If given a WEP key we may need it for shared key auth */
bcba8eae
SO
933 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
934 cipher == WLAN_CIPHER_SUITE_WEP104) {
fffd0934
JB
935 connect->key_idx = idx;
936 connect->key = connkeys->params[idx].key;
937 connect->key_len = connkeys->params[idx].key_len;
bcba8eae
SO
938
939 /*
940 * If ciphers are not set (e.g. when going through
941 * iwconfig), we have to set them appropriately here.
942 */
943 if (connect->crypto.cipher_group == 0)
944 connect->crypto.cipher_group = cipher;
945
946 if (connect->crypto.n_ciphers_pairwise == 0) {
947 connect->crypto.n_ciphers_pairwise = 1;
948 connect->crypto.ciphers_pairwise[0] = cipher;
949 }
fffd0934
JB
950 }
951 }
952
ceca7b71
JB
953 wdev->connect_keys = connkeys;
954 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
955 wdev->ssid_len = connect->ssid_len;
6829c878 956
ceca7b71
JB
957 if (!rdev->ops->connect)
958 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
959 else
e35e4d28 960 err = rdev_connect(rdev, dev, connect);
b23aa676 961
ceca7b71
JB
962 if (err) {
963 wdev->connect_keys = NULL;
964 wdev->ssid_len = 0;
965 return err;
6829c878 966 }
ceca7b71
JB
967
968 return 0;
b23aa676
SO
969}
970
83739b03
JB
971int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
972 struct net_device *dev, u16 reason, bool wextev)
b23aa676 973{
6829c878 974 struct wireless_dev *wdev = dev->ieee80211_ptr;
dee8a973 975 int err = 0;
b23aa676 976
667503dd
JB
977 ASSERT_WDEV_LOCK(wdev);
978
fffd0934
JB
979 kfree(wdev->connect_keys);
980 wdev->connect_keys = NULL;
981
dee8a973 982 if (wdev->conn)
ceca7b71 983 err = cfg80211_sme_disconnect(wdev, reason);
dee8a973 984 else if (!rdev->ops->disconnect)
ceca7b71 985 cfg80211_mlme_down(rdev, dev);
dee8a973 986 else if (wdev->current_bss)
e35e4d28 987 err = rdev_disconnect(rdev, dev, reason);
b23aa676 988
ceca7b71 989 return err;
19957bb3 990}