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